permanent regression line and spread

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #247567 quote
    renzocarioca
    Participant
    Junior

    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

    #247598 quote
    Iván González
    Moderator
    Master

    Hi. You can calculate de linear regression and the spread like this:

    // --- PARAMETERS ---
    Lookback = 500 // Number of bars for regression
    
    // --- Linear Regression calculation ---
    slope = LinearRegressionSlope[Lookback](close)
    Intercept = average[Lookback](close) - Slope * average[Lookback](barindex)
    // Regression Line
    regressionLine = slope * barindex + intercept
    
    // --- SPREAD between price and Regression Line ---
    spread = close - regressionLine
    
    RETURN spread AS "Spread Price-Regression"

    If you want print the regression line in the last bar you could use this code:

    defparam drawonlastbaronly=true
    
    Lookback = 500  // Number of bars for regression
    
    Slope = LinearRegressionSlope[Lookback](close)
    Intercept = average[Lookback](close) - Slope * average[Lookback](barindex)
    
    // Get Regression Line at Start and End of Lookback Period
    StartBar = barindex - Lookback
    EndBar = barindex
    
    RegStart = Intercept + Slope * StartBar
    RegEnd = Intercept + Slope * EndBar
    
    if islastbarupdate then
    drawsegment(StartBar, RegStart, EndBar, RegEnd) coloured("blue") style(dottedline)  // Regression Line
    endif
    
    return 
    Zigo thanked this post
    #247635 quote
    renzocarioca
    Participant
    Junior

    thanks 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)
    return

    #247643 quote
    Iván González
    Moderator
    Master

    For 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.

    if opendate=20241208 and opendate<>opendate[1] then
    x=barindex
    y=close
    endif
    #247659 quote
    renzocarioca
    Participant
    Junior

    thanks 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.

    #247660 quote
    druby
    Participant
    New

    Just 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.

    robertogozzi thanked this post
    #247811 quote
    renzocarioca
    Participant
    Junior

    thank you,  I’ll give a try

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

permanent regression line and spread


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by renzocarioca
8 months, 1 week ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 05/23/2025
Status: Active
Attachments: 1 files
Logo Logo
Loading...