Dear NinjaTrader Developer Team,
I recently bought a license for NinjaTrader and after some training I have developed a daytrading and scalping strategy which is suitable for my purposes.
Since the manual execution of this strategy every five to ten minutes is very time-consuming, I would now like to automate this strategy. And since I am a C# programmer, I am currently working my way into the NinjaScript API and would like to develop a basic framework for my strategies.
Currently I am dealing with protective orders (stop loss, profit target, break even, trailing) and testing my code with the Playback Connection on downloaded historic data (market replay, historic). Unfortunately, my code leads to completely strange behavior on this data and I can't find a solution despite a lot of effort. I would be very grateful if you could help me.
Attached are three versions of my code. The first version uses the Built-In functions SetProfitTarget and SetStopLoss in the OnStateChange method. The second version uses these Built-In functions in the OnExecutionUpdate method. The third and my preferred version uses the fully configurable ExitLongStopMarket and ExitLongLimit methods in the OnExecutionUpdate method. In all versions the stop loss ticks are intitially 12 ticks and the profit target ticks are 20 ticks, but can be configured as desired via properties.
My second and in particular my third version are based on an example published by NinjaTrader Customer Support in this forum. (https://ninjatrader.com/support/foru...tective-orders). This example causes the same strange behavior. However, I only noticed this after my code was alread showing this strange beahvior.
I am currently using NinjaTrader version 8.0.23.2, however the problem also occurs in the latest NinjaTrader version 8.0.24.2. I used the historical data from February 15, 2021 to February 19, 2021 from MES 03-21 as the data set to reproduce the strange behavior (attached PDF, page 1).
The first version works fine and runs from the beginning to the end of the candlestick chart (attached PDF, page 2). The second version works fine too. The third version simply stops in the middle of the candlestick chart (attached PDF, page 3) and does not continue. If you now increase the stop loss ticks from 12 to 14, the third version also works perfectly, which is strange and illogical behavior from my point of view (attached PDF, page 4 + 5).
At least until now I thought that version 1 and version 2 worked fine and version 3 contained a programming error somewhere. For completeness, I used different test dates for a second test, in this case April 19, 2021 to April 23, 2021 from MES 06-21. Now version 1 and version 2 as well as version 3 do not work either and stop in the middle of the candlestick chart (attached PDF, page 6 + 7). When increasing the stop loss ticks from 12 to 25 (14 is not enough here), all versions seems to work and run through the whole candlestick chart from beginning to the end.
I am at my wit's end. Is this a programming mistake in my source code, a bug in NinjaTrader or a problem with the historical data or replay data?
If there is a defect in my program code, I would be very grateful to you for a fix. If there is indeed a bug in NinjaTrader or a problem with the historical/replay data, I would be very grateful for a workaround.
Thanks for your help and support in advance.
Best regards
Patrick
Comment