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
// 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 } }
Comment