Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Calculation of RgressionChannels

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Calculation of RgressionChannels

    Hi,

    I have a question.

    I use a regression channel with 304 periods and a width of 4. That means I have a standard deviation of 2 by each side (95,45%).

    With 304 periods I would expect to see ~13 bars outside the channels (304 - (304 * 0.9545) = 13.83). But I only observe 1 single bar outside.


    Thank you,
    Lukas
    Attached Files

    #2
    Hello Lukas,

    Thanks for your post.

    Width represents the number of standard deviations the upper and lower channels are drawn from the middle regression line.

    Below is the relevant code which shows how the line is drawn from the calculated standard deviation, multiplied by width.

    Code:
    int [B]stdDevPixels [/B]= (int) Math.Round((([B]stdDev[/B]*[B]Width[/B])/(chartScale.MaxValue - chartScale.MinValue))*panel.H, 0);
    
    ...
    // Middle
    RenderTarget.DrawLine(startVector, endVector, Plots[0].BrushDX, Plots[0].Width, Plots[0].StrokeStyle);
    
    // Upper
    RenderTarget.DrawLine(new Vector2(startVector.X, startVector.Y - [B]stdDevPixels[/B]), new Vector2(endVector.X, endVector.Y - [B]stdDevPixels[/B]), Plots[1].BrushDX, Plots[1].Width, Plots[1].StrokeStyle);
    
    // Lower
    RenderTarget.DrawLine(new Vector2(startVector.X, startVector.Y + [B]stdDevPixels[/B]), new Vector2(endVector.X, endVector.Y + [B]stdDevPixels[/B]), Plots[2].BrushDX, Plots[2].Width, Plots[2].StrokeStyle);
    The standard deviation is calculated in OnBarUpdate:

    Code:
    double stdDeviation = Math.Sqrt(sumResiduals / Math.Min(CurrentBar + 1, Period));
    stdDeviationSeries[0] = stdDeviation;
    You may check the source code for the Regression Channel indicator for a further look into how it's calculations are made. Below is a link to our Help Guide documentation which also contains an brief explanation of the indicator.



    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      thanks for your quick reply and the explanation. But I still don't understand the calculation.

      The chart attached shows a regression over 100 bars and a std.dev (Width) of 2.

      Considering your explanation above, I would assume to observe 4.55 Bars with close price outside the upper deviation line. 2 std. dev = 95,45%. 100 bars - 95.45 bars = 4.55 bars.

      But I don't observe 4 or 5 bars outside but 8, that is twice as many.
      Attached Files

      Comment


        #4
        Hello lakman184,

        I apologize for a delayed response as I wanted to test this further and I had some questions myself.

        Width determines the standard deviation in one direction, so Width would be need to be doubled and set to 4 to simulate a regular standard deviation of 2.

        The distribution also is not technically a normal distribution. A Residual Distribution is used instead.

        A publicly available resource can be found here - https://www.investopedia.com/terms/r...-deviation.asp

        We look forward to assisting.
        Last edited by NinjaTrader_Jim; 04-23-2020, 07:14 AM.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Jonafare, 12-06-2012, 03:48 PM
        5 responses
        3,986 views
        0 likes
        Last Post rene69851  
        Started by Fitspressorest, Today, 01:38 PM
        0 responses
        2 views
        0 likes
        Last Post Fitspressorest  
        Started by Jonker, Today, 01:19 PM
        0 responses
        2 views
        0 likes
        Last Post Jonker
        by Jonker
         
        Started by futtrader, Today, 01:16 PM
        0 responses
        8 views
        0 likes
        Last Post futtrader  
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,792 views
        0 likes
        Last Post aligator  
        Working...
        X