permanent regression line and spread
Forums › ProRealTime English forum › ProBuilder support › permanent regression line and spread
- This topic has 6 replies, 3 voices, and was last updated 1 week ago by
renzocarioca.
-
-
05/23/2025 at 5:29 PM #247567
Hi,
This is the DJI index since 1928 with a regression line (red) till today.
I would like to make this line “permanent” and chart the spread between the index price and the regression line.
thank you
05/26/2025 at 9:22 AM #247598Hi. You can calculate de linear regression and the spread like this:
12345678910111213// --- PARAMETERS ---Lookback = 500 // Number of bars for regression// --- Linear Regression calculation ---slope = LinearRegressionSlope[Lookback](close)Intercept = average[Lookback](close) - Slope * average[Lookback](barindex)// Regression LineregressionLine = slope * barindex + intercept// --- SPREAD between price and Regression Line ---spread = close - regressionLineRETURN spread AS "Spread Price-Regression"If you want print the regression line in the last bar you could use this code:
12345678910111213141516171819defparam drawonlastbaronly=trueLookback = 500 // Number of bars for regressionSlope = LinearRegressionSlope[Lookback](close)Intercept = average[Lookback](close) - Slope * average[Lookback](barindex)// Get Regression Line at Start and End of Lookback PeriodStartBar = barindex - LookbackEndBar = barindexRegStart = Intercept + Slope * StartBarRegEnd = Intercept + Slope * EndBarif islastbarupdate thendrawsegment(StartBar, RegStart, EndBar, RegEnd) coloured("blue") style(dottedline) // Regression Lineendifreturn1 user thanked author for this post.
05/26/2025 at 8:59 PM #247635thanks but I need to have a fixed starting date. Ex 19281031
The regression line shown in the chart (illustration) basically is like a “money market” with an average compounded return..
since I can know the coordinates of two points ex: date and value, because I can measure them in the chart, it can become a “permanent line” and once in a while… I can change change the second point.
Then I can calculate the spread between the closing price and such line..
makes sense to you?
I tried like this to start with but… it says x1,y1 is undefined..
DRAWLINE(x1,y1,x2,y2)
xl=date=19283110
yl=252
x2=date=1980831
y2=2781.78
DRAWLINE(x1,y1,x2,y2)
return05/27/2025 at 9:28 AM #247643For example, if you have 5k sails loaded, there will actually be 5499 sails. You can put a lookback=5499
Your code doesn’t work because x can’t be a date, it’s a barindex.
for example you could locate the candle that meets opendate=20241208 and then store the X as a barindex.
1234if opendate=20241208 and opendate<>opendate[1] thenx=barindexy=closeendif05/27/2025 at 6:18 PM #247659thanks for your help but sorry, getting confusing to me here.
it seems mission impossible to translate a regression line drawn in the chart (see illustration) into a script, formula which i can use to calculate such spread
there are many things great about prorealtime but others which seems hard to figure out.
05/27/2025 at 7:01 PM #247660Just a few comments on earlier example with DRAWLINE
Your example uses DRAWLINE twice, the first time appears being used before defining x1, this will triggered the ‘undefinrd’ error. (x1 doesn’t = a value, it still = UNDEFINED)
You cannot enter a date directly for x1 and x2 in DRAW commands however, take a look at the DATETOBARINDEX( YYYYMMDDhhmmss ) command.
This command retrieves the barindex value that relates to the date/time set in the brackets.
The value received is not available to use as a variable, and the (date/time) doesn’t need all the date/time but all options start from the YYYY side.
ex.
x1 = 20241201
y1 = 252
x2 = 20250501
y2 = 2781.78
DRAWLINE(DATETOBARINDEX(x1), y1, DATETOBARINDEX(x2), y2)
One note, the dates need to be covered by the historical bars of the chart.
Not sure if this is helpful in you bigger problem, but just thought I’d point it out as a reference.
1 user thanked author for this post.
06/02/2025 at 3:13 PM #247811thank you, I’ll give a try
-
AuthorPosts
Find exclusive trading pro-tools on