I read a thread that said CurrentBar could not be relied on to be in sync within the OnMarketData method. So I am trying to store my custom bar data - indexed by barID - by getting the bar ID based on the time in the MarketDataEventArgs.
The problem is: absolutely every time I invoke GetBar it returns 0.
Here is the test case:
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; namespace NinjaTrader.Indicator { public class MarketDataTest : Indicator { protected override void Initialize() { Overlay = true; CalculateOnBarClose = false; } protected override void OnBarUpdate() { Print(""); Print("OnBarUpdate()"); Print(" DateTime.Now=" + DateTime.Now); Print(" CurrentBar=" + CurrentBar); // ALWAYS 0! Print(" GetBar(DateTime.Now)=" + GetBar(DateTime.Now)); // Assert: if (GetBar(DateTime.Now) != 0) throw new InvalidOperationException("Never thrown."); Print(" Time[0]=" + Time[0]); Print(" GetBar(Time[0])=" + GetBar(Time[0])); // Assert: if (GetBar(Time[0]) != 0) throw new InvalidOperationException("Never thrown."); } protected override void OnMarketData(MarketDataEventArgs evt) { Print(""); Print("\tOnMarketData()"); Print("\t evt=" + evt); Print("\t CurrentBar=" + CurrentBar); // I read a thread that said CurrentBar could not be relied on to be in sync // within the OnMarketData method. // So I am trying to store my custom bar data - indexed by barID - by getting // the bar ID based on the time in the MarketDataEventArgs. // NinjaScript > Language Reference > Data > Bars > // GetBar() // Definition // Returns the bar index of the 1st bar from oldest to newest // that matches the time stamp based on the DateTime parameter passed in. // If the time stamp passed in is older than the 1st bar, // a bar index of 0 is returned. // If the time stamp is newer than the last bar, the last bar index is returned. // ALWAYS 0! Print("\t GetBar(evt.Time)=" + GetBar(evt.Time)); // Assert: if (GetBar(evt.Time) != 0) throw new InvalidOperationException("Never thrown."); } } }
Comment