Recently I developed my own indicator. I also developed a strategy based on it. To finalize the process I decided to polish the code and to optimize its performance.
But I found something very strange.
My indicator uses Calculate.OnBarClose. As I know this setting is pinpointing how often the OnBarUpdate() method will be invoked. Meanwhile it gets real-time data by OnMarketData().
My goal is all the calculations to be executed once at the bar close when all the data for the current bar is acquired. I use Plot() as expected. The plot values are set when the bar is closing, before the next bar is started (as I understand from the NinjaTrader API documentation). The plot is displayed on the currently processed bar.
The indicator is working as expected and perfectly.
I also created a Strategy, that also works perfectly. I want this strategy to open position ON THE NEXT bar. I mean on the first tick when the next bar is opened. This way my strategy is using the Plot values from the previous bar to enter a position in tthe currently opened new bar. My strategy however is Calculate.OnEachTick, because I use managed approach and I need to refresh and to update the StopLoss and TakeProfit on every tick so that the orders are executed precisly. However, I check if IsFirstTickOfBar to check the conditions based on the previous bar if the strategy is going to open a position.
And here is the thing:
When I update the Plot values in the indicator in OnBarUpdate() I also have an IF (IsFirstTickOfBar) clause, and the strategy is working fine. However, if I remove IF (IsFirstTickOfBar) clause from the indicator, the strategy is no longer working and do not open any position.
On the chart when testing the indicator there is no difference in the Plot() values (checked by the middle mouse button click and databox). The plot values are situated just the same place and values as when IF (IsFirstTickOfBar) clause is available and not available. From an indicator point of view, there is no difference in the end result.
Any advices or explanations are welcome.
Could someone give me a hint why this is working in this way?
Comment