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

Draw.RegressionChannel upper and lower channel values.

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

    Draw.RegressionChannel upper and lower channel values.

    HI,

    I am using Draw.RegressionChannel to draw a regression channel from bars A to B. I would like to get the value of the regression channel upper and lower bounds. If I use the indicator, I can get these values (RegressionChannel(A,B)[0].upper, etc. Can I get the same information when I use the RegressionChannel drawing tool? I would like to draw multiple RCs on the screen and so using the indicator is not my solution.

    I see I can return the number of SDevs to the upper or lower channel bounds. But I don't see how to then return what the standard dev is at that point.

    Thanks for any help.

    JeffCO

    #2
    Hello JeffCO,

    I don't think the values you are wanting are available with the Draw.RegressionChannel() drawing tool.

    Below is a link to the help guide with the properties available.


    The standard deviation to the high and low are available. The start and end anchors are available.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      Thank you. I went ahead and wrote my own method to do the calculation. It uses start and end Bar# instead of BarsAgo which suits my use case. I will still use the draw.RegressionChannel to do the drawings. This method will give me the standard deviations. I left some debug Print statements in the code for others to use if needed. I am also creating an update method to add additional bars to the end of the LinReg calc without redoing the entire calc. It is a simple modification of the init method. That is the reason the sum?? variables are global variables and not defined in the method.

      I checked this method against the RegressionChannel to verify the results.
      Last edited by JeffCO7; 05-24-2022, 10:24 AM.

      Comment


        #4
        Code:
        		public double mInitLinRegChannel(	int i_start,  // start bar #
        											int i_end )   // end bar #   (should already include Lag)
        		{
        			double start = i_start;
        			double end	 = i_end;
        			double mPrice;
        			int Idx;
        
        			// NOTE: these parameters are defined in the global variables section of the intidicator.
        			nBars	= 0;
        			sumX	= 0;
        			sumY	= 0;
        			sumXY	= 0;
        			sumXSqr	= 0;
        			sumYSqr	= 0;
        
        			for (int i = i_start; i <= i_end; i++)
        			{
        				Idx 	= CurrentBar - i;
        				nBars	+= 1;
        				mPrice	= Input[Idx];
        				sumX	+= i;
        				sumY	+= mPrice;
        				sumXY	+= i*mPrice;
        				sumXSqr	+= i*i;
        				sumYSqr	+= mPrice*mPrice;
        
        				Print(string.Format("CalcLR C1  Bar:{0}  i:{1}  Idx:{2}   Price:{3}  sumX:{4}   sumY:{5}   sumXY:{6}   sumXSqr:{7}   xumYSqr:{8}   nBars:{9}",
        									CurrentBar, i, Idx, Input[Idx], sumX, sumY, sumXY, sumXSqr, sumYSqr, nBars));
        
        			}
        
        			divisor		= ( nBars * sumXSqr	- sumX * sumX );
        			slope		= ( nBars * sumXY	- sumX * sumY ) / divisor;
        			intercept	= ( sumY * sumXSqr	- sumX * sumXY ) / divisor;
        
        			slopeSeries[0]		= slope;
        			interceptSeries[0]	= intercept;
        
        			// Next we calculate the standard deviation of the
        			// residuals (vertical distances to the regression line).
        
        			double sumResiduals = 0;
        			for (int i = i_start; i <= i_end; i++)
        			{
        				Idx 	= CurrentBar - i;
        				double regressionValue = intercept + slope * i;
        				double residual = Math.Abs(Input[Idx] - regressionValue);
        				sumResiduals += residual;
        
        //				Print(string.Format("LRC SR1 - Bar:{0}  slope:{1}  intercept:{2}  RegVal:{3}  Y:{4}  Residual:{5}   Sum:{6}",
        //									CurrentBar, slope, intercept, regressionValue, Input[Idx], residual, sumResiduals));
        			}
        
        			double avgResiduals = sumResiduals / Math.Min(CurrentBar - 1, nBars);
        
        //			Print(string.Format("LRC SR2 - Bar:{0}  AvgResidual:{1}",CurrentBar, avgResiduals));
        
        			sumResiduals = 0;
        			for (int i = i_start; i <= i_end; i++)
        			{
        				Idx 	= CurrentBar - i;
        				double regressionValue = intercept + slope * i;
        				double residual = Math.Abs(Input[Idx] - regressionValue);
        				sumResiduals += (residual - avgResiduals) * (residual - avgResiduals);
        //				Print(string.Format("LRC SR3 - Bar:{0}  slope:{1}  intercept:{2}  RegVal:{3}  Y:{4}  Residual:{5}   Sum:{6}",
        //									CurrentBar, slope, intercept, regressionValue, Input[Idx], residual, sumResiduals));
        			}
        
        			double stdDeviation = Math.Sqrt(sumResiduals / Math.Min(CurrentBar + 1, nBars));
        			stdDeviationSeries[0] = stdDeviation;
        //			Print(string.Format("LRC SR2 - Bar:{0}  sDevWidth:{1}  StdDev:{2}  2SDev:{3}   3SDev:{4}   4SDev:{5}",
        //				CurrentBar, sDevWidth, stdDeviation, stdDeviation*2, stdDeviation*3, stdDeviation*4));
        
        			double middle = intercept + slope * i_end;
        			Middle[0] = CurrentBar == 0 ? Input[0] : middle;
        			Upper[0] = stdDeviation.ApproxCompare(0) == 0 || Double.IsInfinity(stdDeviation) ? Input[0] : middle + stdDeviation * sDevWidth;
        			Lower[0] = stdDeviation.ApproxCompare(0) == 0 || Double.IsInfinity(stdDeviation) ? Input[0] : middle - stdDeviation * sDevWidth;
        
        			double temp = intercept + slope * i_start;
        //			Print(string.Format("LRC - Bar:{0}  Divisor:{1}   Slope:{2}   Intercept:{3}   sumX:{4}  sumY{5}, start-Bar:{6}  val:{7}  end-Bar:{8}  val{9}  Upper{10}   Lower{11}",
        //								CurrentBar, divisor, slope, intercept, sumX, sumY, i_start, temp, i_end, middle, Upper[0], Lower[0]));
        
        			return (Upper[0] - Lower[0]);
        		}
        Last edited by JeffCO7; 05-24-2022, 10:18 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by TraderBCL, Today, 04:38 AM
        3 responses
        23 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        11 responses
        1,423 views
        0 likes
        Last Post jculp
        by jculp
         
        Started by RubenCazorla, Today, 09:07 AM
        0 responses
        4 views
        0 likes
        Last Post RubenCazorla  
        Started by BarzTrading, Today, 07:25 AM
        2 responses
        29 views
        1 like
        Last Post BarzTrading  
        Started by devatechnologies, 04-14-2024, 02:58 PM
        3 responses
        21 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X