I have years of MT4 programming experience but am brand-new to NinjaTrader.
When does the OnBarUpdate() strategy event fire? I would expect it to fire whenever the current bar is updated, similiar to MT4's start() function, but when I load my test strategy it immediately fires for every historical bar in the chart. I added some debug lines to display Time for each call to OnBarUpdate() and it's clearly being called hundreds of times as there are hundreds of bars on my 1-Min chart. This all happens within 1-2 seconds of enabling the strategy.
Am I loading the strategy incorrectly? It's behaving as if I'm doing a backtest, not a forward test. Again, for those familiar with MT4, I'm trying to duplicate MT4's start() functionality, which fires on each new incoming tick.
You need to set the CalculateOnBarClose property to TRUE in the Initialize() method of the indicator/strategy for OnBarUpdate() to fire once your tick/range/minute/renko/etc. bar closes otherwise this event will fire for each incoming tick of data...
Thank you for the reply. I am already setting CalculateOnBarClose to TRUE in the Initialize method as you recommended.
The problem is: when I first load the strategy, OnBarUpdate() is getting called for every pre-existing historical bar in the chart. In other words: not just for new incoming ticks, but also for every bar already on the chart.
Again, it's acting like it's in "backtest" mode. I want it to process only future ticks, not past ticks. I must be missing something very elementary.
Check out the help in NT for the keyword HISTORICAL
Indicates if the NinjaScript object is running on historical data. With a strategy, when running a backtest, this property will always return true. When running a live strategy, this property will be true until the strategy starts processing real-time data. The property can be of value for many different applications.
1. Filtering out historical data for real-time strategies that does not require any historical data to initialize.
This property returns true if the strategy is running on historical data; otherwise, false.
Custom Indicator, Custom Strategy
protected override void OnBarUpdate()
// Only run on real-time data
The following user says Thank You to gulabv for this post: