It all works fine, except for the very last bar. The only thing I can think of is that "r" must not be getting CalculateOnBarClose set properly, in spite of the fact that it is set in its Initialize() method.
The organization of the code is:
- "aaaTest " is the indicator.
- GetVal is a static function. That means it has no class object associated with it.
- REF is a dummy class that exists only to give GetVal() access to functions like Print and EMA -- the are not static, and so need to be accessed through a class instance.
- "r" is an instance of class REF -- GetVal() needs to make use of it.
- I have configured CalculateOnBarClose false for aaaTest in the Indicators dialog
I have included the last few lines of the printed output. You can see that everything is all peachy-keen up until that final bar. At that point:
- Input[0] has a real value
- EMA. called either of two ways in the indicator's OnBarUpdate() returns a good value
- r.EMA, called from the static function returns 0. My best guess is that, in spite of its Initialize() method setting CalculateOnBarClose=false, r is behaving as if that were set true.
So how do I make r.EMA return the last bar calculation properly? Do you see any possibility other than CalculateOnBarClose being set wrong? If that is the answer, then how do I get it set right?
--EV
Here is some output -- note that all is perfect up until the final bar:
GetVal: EMA=600.787 Input series[0]=616.440 EMA=600.787 EMA=600.787 GetVal=600.787 Input[0]=616.440 CurrentBar=1609 GetVal: EMA=602.008 Input series[0]=614.210 EMA=602.008 EMA=602.008 GetVal=602.008 Input[0]=614.210 CurrentBar=1610 GetVal: EMA=603.281 Input series[0]=616.010 EMA=603.281 EMA=603.281 GetVal=603.281 Input[0]=616.010 CurrentBar=1611 GetVal: EMA=604.516 Input series[0]=616.870 EMA=604.516 EMA=604.516 GetVal=604.516 Input[0]=616.870 CurrentBar=1612 GetVal: EMA=605.623 Input series[0]=616.690 EMA=605.623 EMA=605.623 GetVal=605.623 Input[0]=616.690 CurrentBar=1613 GetVal: EMA=0.000 Input series[0]=624.180 EMA=607.310 EMA=607.310 GetVal=0.000 Input[0]=624.180 CurrentBar=1614
#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Gui.Chart; #endregion // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { // Dummy class whose sole purpose is access to its base class' methods public class REF : Indicator { // Override abstract base members, even though we have nothing to do protected override void Initialize() { CalculateOnBarClose=false; return; } protected override void OnBarUpdate() { return; } } [Description("Test class")] public class aaaTest : Indicator { protected override void Initialize() { CalculateOnBarClose=false; return; } protected override void OnBarUpdate() { if (CurrentBar < 21) return; Print(String.Format("EMA={0:F3} EMA={1:F3} GetVal={2:F3} Input[0]={3:F3} CurrentBar={4}", EMA(21)[0], EMA(Input,21)[0], GetVal(Input), Input[0], CurrentBar)); return; } // Gives static methods access to Indicator methods, such as moving averages static Indicator r = new REF(); // Simple static class that just returns the current value of a moving average public static double GetVal(IDataSeries series) { double d = r.EMA(series, 21)[0]; r.Print( String.Format("\r\nGetVal: EMA={0:F3} Input series[0]={1:F3}", d, series[0]) ); return d; } } }
Comment