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

Strangely getting different results when they should be the same with Dataseries

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

    Strangely getting different results when they should be the same with Dataseries

    I was trying to modify the DoubleStochastics indicator to show the predicted high and low values on a range bar chart in real time (COBC = false).I'm not an expert programmer but I make do. Let me explain what I have, what I think should happen, and my problem. The NT version is 7.0.1000.7

    I'm using just the first iteration of the DoubleStochastic indicator for testing. In the code, nbhi and nblo are the predicted high and low closing prices for the current range bar.
    I have data series (one for high price, and one for low price) to correspond with each data series (p1, p2, p3) of the original code.
    To test this code I've added some print statements to compare results in the Output Window. Now what should happen is that when the last price is equal to nbhi,
    EMA(p1, 3)[0] should equal to EMA(nHIp1Forecast, 3)[0]
    or when last price equal to nblo,
    EMA(p1, 3)[0] should equal EMA(nLOp1Forecast, 3)[0].
    If you compile this, add the indicator to a range bar chart and open the Output window you will see the values equal when the conditions are met.
    Ok. Now my problem. When I take an EMA value of the high or low data series and send this data to another data series like this,
    nHIp2Forecast.Set(EMA(nHIp1Forecast, 3)[0]);,
    the values of EMA(p1, 3)[0] and EMA(nHIp1Forecast, 3)[0] no longer equal to each other. Even if I assign EMA(nHIp1Forecast, 3)[0] to a variable like this,
    double nHtest = EMA(nHIp1Forecast, 3)[0], the values of the two ema calculations no longer would equal each other when conditions are met.
    To try it uncomment in the code //nHIp2Forecast.Set(EMA(nHIp1Forecast, 3)[0]); .
    I can't figure out what is wrong or what to do that the values would remain the same when conditions are met. Can anyone help?
    Thanks

    Code:
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// Double stochastics
        /// </summary>
        [Description("Double stochastics Test")]
        [Gui.Design.DisplayName("DStest")]
        public class DStest : Indicator
        {
            #region Variables
            private int period = 10;
    		private DataSeries p1;
    		private DataSeries p2;
    		private DataSeries p3;
    
    		
    		private double						nbhi;
    		private double						nblo;			
    		private DataSeries					nHIp1Forecast;
    		private DataSeries					nHIp2Forecast;
    		private DataSeries					nHIp3Forecast;		
    		private DataSeries					nLOp1Forecast;
    		private DataSeries					nLOp2Forecast;
    		private DataSeries					nLOp3Forecast;		
    
    
    				//double nHODstokForecast;
    				//double nLODstokForecast;	
    		
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Add(new Plot(Color.Red, PlotStyle.Line, "K"));
    			Add(new Plot(new Pen(Color.Green, 1),PlotStyle.Dot, "DSHigh"));
    			Add(new Plot(new Pen(Color.Red, 1),PlotStyle.Dot, "DSLow"));			
    
                Add(new Line(Color.Blue, 90, "Upper"));
                Add(new Line(Color.Blue, 10, "Lower"));
    			
    
    			
    			Lines[0].Pen.DashStyle = DashStyle.Dash;
    			Lines[1].Pen.DashStyle = DashStyle.Dash;
    
    			p1 = new DataSeries(this);
    			p2 = new DataSeries(this);
    			p3 = new DataSeries(this);
    
    			nHIp1Forecast = new DataSeries(this);
    			nHIp2Forecast = new DataSeries(this);
    			nHIp3Forecast = new DataSeries(this);			
    			
    			nLOp1Forecast = new DataSeries(this);
    			nLOp2Forecast = new DataSeries(this);
    			nLOp3Forecast = new DataSeries(this);
    
    
    			
    			//PriceTypeSupported	= true;
    			CalculateOnBarClose = false;			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			
    			if (CurrentBar < 3) // 
    			{
    				return;				
    			}			
    			
    			//------------ Range bar projected high and low price -----------
    			if (Bars.Period.Id == PeriodType.Range)
    			{
                	nbhi = Low[0] + (Bars.Period.Value * TickSize);
    				nblo = High[0] - (Bars.Period.Value * TickSize);
    			}			
    		    //---------------------------
    			
    			double r = MAX(High, Period)[0] - MIN(Low, Period)[0]; //Orig DoubleStochastics code
                r = r.Compare(0, 0.000000000001) == 0 ? 0 : r;         //Orig  DoubleStochastics code
    			
                if (r == 0) //Orig DoubleStochastics code
    			{
                    p1.Set(CurrentBar == 0 ? 50 : p1[1]);  //Orig DoubleStochastics code
    				
    				nHIp1Forecast.Set(CurrentBar == 0 ? 50 : nHIp1Forecast[1]);
    				nLOp1Forecast.Set(CurrentBar == 0 ? 50 : nLOp1Forecast[1]);
    			}
                else
    			{
                    p1.Set(Math.Min(100, Math.Max(0, 100 * (Close[0] - MIN(Low, Period)[0]) / r)));  //Orig DoubleStochastics code
    				
    
    				nHIp1Forecast.Set(Math.Min(100, Math.Max(0, 100 * (nbhi - MIN(Low, Period)[0]) / r)));  // value based on range bar high
    				nLOp1Forecast.Set(Math.Min(100, Math.Max(0, 100 * (nblo - MIN(Low, Period)[0]) / r))); //value based on range bar low
    				
    			
    			}
    			
    			nHIp1Forecast.Set(1,p1[1]);  //
    			nLOp1Forecast.Set(1,p1[1]);  //
    			
    			
    			p2.Set(EMA(p1, 3)[0]);  //Orig DoubleStochastics code
    			
    			//nHIp2Forecast.Set(EMA(nHIp1Forecast, 3)[0]);
    			//nLOp2Forecast.Set(EMA(nLOp1Forecast, 3)[0]);
    
    			
                // --------- Start Debug of Code ------------------
    			if(p1[0] == nHIp1Forecast[0])  //When these two values are the same (based on when last price = range bar high), print the values
    			{
    			Print("-------Start HIGH Part-----------");
    			Print(p1[0]);
    			Print(p1[1]);
    			Print(p1[2]);
    			Print(p1[3]);
    			//Print(p1[4]);
    			Print("--high----------");
    			Print(nHIp1Forecast[0]);
    			Print(nHIp1Forecast[1]);
    			Print(nHIp1Forecast[2]);
    			Print(nHIp1Forecast[3]);
    			
    
    			Print(" ");
    			Print(EMA(p1, 3)[0]);              //<--- This value should be the same as
    			Print(EMA(nHIp1Forecast, 3)[0]);   //<----this value once the if statement is true
    			
    			//Print(nHIp2Forecast[0]);
    			//Print(EMA(nLOp3Forecast, 3)[0]);
    			Print("  ");	            // test
    			}
    
    			if(p1[0] == nLOp1Forecast[0])
    			{
    				Print("-------Start LOW part-----------");
    				Print(p1[0]);
    				Print(p1[1]);
    				Print(p1[2]);
    				Print(p1[3]);
    				
    				Print("--low----------");
    				Print(nLOp1Forecast[0]);
    				Print(nLOp1Forecast[1]);
    				Print(nLOp1Forecast[2]);
    				Print(nLOp1Forecast[3]);
    				//Print(emaseries1H[4]);
    
    				Print(" ");
    				Print(EMA(p1, 3)[0]);                //<--- This value should be the same as
    				Print(EMA(nLOp1Forecast, 3)[0]);     //<----this value once the if statement is true
    				//Print(nHOp2Forecast[0]);
    				//Print(EMA(nLOp3Forecast, 3)[0]);
    				Print("  ");	            
    			}	
    			// ---------- End Debug -----------------
    			
    			//nHIp2Forecast.Set(EMA(nHIp1Forecast, 3)[0]); //But if I store the results of the EMA in another data series or even assign to a variable the 
    			                                              //EMA(p1, 3)[0] and EMA(nHIp1Forecast, 3)[0] are no longer have the same values. The later somehow gets a
    			                                              //different value. I can't figure out why?
    			K.Set(p2[0]); //this is K				
    
    			
            }
    
            #region Properties
            [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries K  
            {
                get { return Values[0]; }
            }
    		
            [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries DSHigh  //
            {
                get { return Values[1]; }
            }
    		
            [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries DSLow  //
            {
                get { return Values[2]; }
            }		
    		
            [Description("")]
            [GridCategory("Parameters")]
            public int Period
            {
                get { return period; }
                set { period = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    jabeztrading,

    Could you please send your indicator to [email protected] with ATTN : Adam in the message? NinjaTrader generates a lot of extra required code for indicators that appears to be missing in your example. Mainly, it would help me test it on my end more efficiently as well as modify it. Typically things like this are unsupported however I would be happy to take a look and see if I can see anything obvious.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Adam. I've emailed the indicator.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by PaulMohn, Today, 03:49 AM
      0 responses
      4 views
      0 likes
      Last Post PaulMohn  
      Started by inanazsocial, Today, 01:15 AM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by rocketman7, Today, 02:12 AM
      0 responses
      10 views
      0 likes
      Last Post rocketman7  
      Started by dustydbayer, Today, 01:59 AM
      0 responses
      2 views
      0 likes
      Last Post dustydbayer  
      Started by trilliantrader, 04-18-2024, 08:16 AM
      5 responses
      23 views
      0 likes
      Last Post trilliantrader  
      Working...
      X