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

Trying to determine Slope of Regression Channel

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

    Trying to determine Slope of Regression Channel

    I am using regression channel, with a period of 128, and width of 2.5

    I simply trying to calculate if the slope of the channel is either up or down.

    In the screenshot, even though the slope is up, but the code below is producing a negative value of -0.08384 which is indicating the slope is negative/down:
    double mySlope = ( Slope(RegressionChannel(128,2.5), 127, 0) );

    Also, the following code should be grabbing the value of the regression channel middle line @ 127 bars ago:
    RegressionChannel(128,2.5).Middle[127];

    I visualized the "slope" by drawing as a magenta colored line. Instead of duplicating the middle orange line, it draws a completely different line with a contradictory slope.

    What am I doing wrong? How can I get the value for the middle line @ 127 bars ago?

    Click image for larger version

Name:	SlopeHelp.png
Views:	92
Size:	42.1 KB
ID:	1290775


    Code:
            //Add your custom strategy logic here.
            if (CurrentBars[0] < 128) return;
    
    
            // --------------------------------------------------
            // Draw Regression Channel on Chart
            // --------------------------------------------------
    
            // https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?regressionchannel.htm
            // https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?draw_regressionchannel.htm
    
            NinjaTrader.NinjaScript.DrawingTools.RegressionChannel myRegChan = Draw.RegressionChannel(this, "myOuterChannel", false
                ,127, 0 // startBarsAgo = 127 (this matches 128), endBarsAgo = 0
                ,Brushes.Orange, DashStyleHelper.Solid , 1
                ,Brushes.Orange, DashStyleHelper.Solid , 1
                ,Brushes.Orange, DashStyleHelper.Solid , 1
                );
    
            // Define Channel Width
            myRegChan.StandardDeviationUpperDistance = 2.5;
            myRegChan.StandardDeviationLowerDistance = 2.5;
    
    
            // --------------------------------------------------
            // Regression Channels
            // --------------------------------------------------
    
            double PriceUpper = RegressionChannel(128,2.5).Upper[0];
            double PriceMiddle = RegressionChannel(128,2.5).Middle[0];
            double PriceBottom = RegressionChannel(128,2.5).Lower[0];
    
    
            // --------------------------------------------------
            // Determine Slope
            // --------------------------------------------------
    
            // https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?slope.htm
            // The "startBarsAgo" parameter MUST be greater than the "endBarsAgo" parameter
            double mySlope = ( Slope(RegressionChannel(128,2.5), 127, 0) );
    
    
            // --------------------------------------------------
            // Drawing the "Slope"
            // --------------------------------------------------
    
            // grabbing the value of Middle line @ 127 bars ago:
            double PriceMiddleRear = RegressionChannel(128,2.5).Middle[127];
    
            // drawing the slope as magenta colored line
            Draw.Line( this, "tag2", false, 127, PriceMiddleRear, 0, PriceMiddle, Brushes.Magenta, DashStyleHelper.Solid, 1);
    
    
            // --------------------------------------------------
            // Displaying Variables on Chart
            // --------------------------------------------------
    
            Draw.TextFixed(this, "myText"
                ,"\nPriceUpper:  " + PriceUpper
                + "\nPriceBottom: " + PriceBottom
                + "\n\nPriceMiddle:  " + PriceMiddle
                + "\nPriceMiddleRear: " + PriceMiddleRear
                + "\n\nmySlope: " + mySlope
                , TextPosition.TopRight
                );

    #2
    Hello squarewave,

    Thanks for your post.

    I see that you are calling double mySlope = ( Slope(RegressionChannel(128,2.5), 127, 0) ); and you are calling double PriceMiddle = RegressionChannel(128,2.5).Middle[0]; in your script. To clarify, are you using the Regression Channel indicator or are you using the Regression Channel drawing tool?

    To get the midline value from the Regression Channel indicator, the syntax below would be used and this syntax could be found in the RegressionChannel() help guide documentation.

    Returns default midline value

    RegressionChannel(int period, double width)[int barsAgo]
    RegressionChannel(ISeries<double> input, int period, double width)[int barsAgo]

    See this help guide page about RegressionChannel(): https://ninjatrader.com/support/help...on_channel.htm

    It seems you are comparing a drawing tool on the chart to an indicator value being accessed in the strategy.

    To confirm the RegressionChannel indicator value in the print matches the indicator value on the chart, you should be using AddChartIndicator() in the strategy script to plot the Regression Channel indicator on the chart for comparison.

    Then, add prints to the script that print out the RegressionChannel() indicator value and compare the prints in the NinjaScript Output window to the indicator on the chart.

    AddChartIndicator(): https://ninjatrader.com/support/help...indicator.htm?

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      I was attempting to calculate if regression channel slope is up or down.

      I tried two different methods, both are not working.

      1st method:
      double mySlope = ( Slope(RegressionChannel(128,2.5), 127, 0) );​ // this does not always accurately reflect the correct slope 100% of the time

      2nd method:
      would be comparing two values against each other using: RegressionChannel(intperiod,doublewidth)[intbarsAgo]​
      RegressionChannel(128,2.5)[0]​
      RegressionChannel(128,2.5)[127]​​

      The problem is that RegressionChannel(128,2.5)[127]​​​ does not provide the correct value (as demonstrated by the magenta line)


      Comment


        #4
        Hello squarewave,

        Thanks for your notes.

        I have called AddChartIndicator() in a strategy to plot the RegressionChannel indicator on the chart window and added prints to the script that print out the RegressionChannel indicator values along with the CurrentBar value.

        Then, I opened a Data Box window and compared the prints in the NinjaScript Output window to the RegressionChannel indicator's Middle plot and see the values are matching.

        See the attached images demonstrating this.

        To compare these values in your script, call AddChartIndicator() to add the RegressionChannel indicator to the chart from the strategy. Then, add prints to the script that prints out the RegressionChannel indicator values along with the CurrentBar. Compare the NinjaScript Output prints to the RegressionChannel indicator's Middle plot in the Data Box window.

        AddChartIndicator(): https://ninjatrader.com/support/help...tindicator.htm
        RegressionChannel(): https://ninjatrader.com/support/help...on_channel.htm
        Data Box: https://ninjatrader.com/support/help...8/data_box.htm

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121
        Attached Files
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          AddChartIndicator() is irrelevant to my question, but I switched over to it so you will stop focusing on it.
          Code:
          AddChartIndicator( RegressionChannel(128,2.5) );
          You are missing the actual issue I am having. I will try to be clarify my issue with better accuracy:

          The value for Middle[127] is never accurate (see the attached photo "pink" dot)

          The code I am using:
          Code:
          double Middle_0 = RegressionChannel(128,2.5).Middle[0]; // always outputs the correct value
          double Middle_127 = RegressionChannel(128,2.5).Middle[127]; // always outputs the WRONG value!
          
          // visualizing these values on the chart:
          Draw.Dot( this, "middle_0", true, 0, Middle_0 , Brushes.Green );
          Draw.Dot( this, "middle_127", true, 127, Middle_127 , Brushes.Magenta );
          ​
          Click image for larger version

Name:	PinkDot.png
Views:	66
Size:	37.7 KB
ID:	1290964

          Comment


            #6
            Hello squarewave,

            Thanks for your notes.

            The dot is actually being drawn correctly for this indicator. It is drawing based on the plot value, not the rendered lines that the indicator draws on the chart.

            If you click the indicator so the plot gripper shows up, you can see that the dot is being drawn at the correct value.

            See this demonstration video: https://brandonh-ninjatrader.tinytak...OF8yMjgxMTMzNQ

            To be able to draw on the rendered line you would need to use the OnRender logic that it already has to find the Y value for the plot value and then convert that Y to a price which could be used with the dot.

            To view the RegressionChannel indicator code, open a New > NinjaScript Editor window, open the Indicators folder, and double-click on the RegressionChannel file.

            That may be difficult though since it uses two points for the line so you would have to do something similar to the YAxisProjectionOfLine example on the forum thread linked below where you calculate the intercept

            YAxisProjectionOfLine: https://forum.ninjatrader.com/forum/...63#post1180063
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Thank you for help so far.

              This is aggravating and does not make any sense.

              The indicator I am calling is "Regression Channel". So why does it not return the channel value?

              If I wanted linear regression, then I'd use the "lin. reg." indicator.

              Click image for larger version

Name:	Redundant.png
Views:	67
Size:	71.0 KB
ID:	1291022


              Comment


                #8
                Hello squarewave,

                Thanks for your notes.

                This is how the RegressionChannel indicator was created to function. The indicator has three indicator plots for Middle, Upper, and Lower.

                If we look at the code of the RegressionChannel indicator script, we can see that in OnBarUpdate() it calculates the linear regression parameters, then calculates the standard deviation of the residuals (vertical distances to the regression line), and assigns the calculated values to the plots in the script.

                Then, the script uses OnRender() and SharpDX to custom render the three channel lines that you see on the chart window.

                The channel lines on the chart are custom rendered objects on the chart and these are not the actual plots of the indicator.

                To view the RegressionChannel indicator code, open a New > NinjaScript Editor window, open the Indicators folder, and double-click on the RegressionChannel file.​
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Option Whisperer, Today, 09:55 AM
                0 responses
                3 views
                0 likes
                Last Post Option Whisperer  
                Started by geddyisodin, 04-25-2024, 05:20 AM
                8 responses
                58 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by halgo_boulder, 04-20-2024, 08:44 AM
                2 responses
                21 views
                0 likes
                Last Post halgo_boulder  
                Started by mishhh, 05-25-2010, 08:54 AM
                19 responses
                6,189 views
                0 likes
                Last Post rene69851  
                Started by gwenael, Today, 09:29 AM
                0 responses
                5 views
                0 likes
                Last Post gwenael
                by gwenael
                 
                Working...
                X