Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Strange behavior of protective orders on replay data - programming error, NT bug?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Strange behavior of protective orders on replay data - programming error, NT bug?

    Dear NinjaTrader Community,
    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
    Attached Files

    #2
    Hello pgm1980,

    Thank you for your post.

    This looks to be most likely a situation in which a historical overfill occurs and the strategy ceases processing as the historical overfill basically "breaks" the simulator. I especially think this may be the case as you've mentioned that increasing the stop loss ticks resolves the issue.

    Here's an example case. In this case, the order of events is as follows:

    1) Enter Long is triggered on Bar x

    2) Enter Long fills immediately in the fill engine and throws the OnExecutionUpdate event for OrderState.Filled.

    3) This triggers the strategy logic to submit the ExitLongLimit and ExitLongStopMarket (still on Bar x).

    4) The Limit Price is below the High of Bar x + 1 and the Stop Price is above the Low of Bar x + 1 → Therefore these orders also fill immediately.

    5) This results in a historical overfill which managed NinjaScript is not designed to handle and anything occurring from this point forward is likely going to cause issues.

    6) Strategy calls EnterLong on Bar x + 1 → Order reaches State.Working and nothing else happens.

    In this case, one solution may be to modify our code to not enter on the same bar we might have had an overfill on (from the previous position) and to not allow for an overfill on the same bar we entered on (by not submitting our stop/target until the next bar). Another would simply to be to increase the distance of the stop loss or profit target.

    This sounds a lot like your scenario here. When you look at the Orders display after one of these runs where it stops partway through, do you see any orders noted as being in a Working state once the backtest completes?

    If so, something to try would be using Order Fill Resolution - High in the Backtest settings. This is because the root of this issue is really that in a backtest, only the OHLC of a bar are known and the simulator doesn't know how the bar was built.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello NinjaTrader_Kate,

      thank you for your reply.

      I understood the example description and also assume that the described scenario is exactly the problem.

      Unfortunately I am still very inexperienced with NinjaScript API and could not develop a "workaround" today. As far as I understood, the necessary information is in the OrderFillResolution enum/object. However, I did not manage to extract the information to prevent the so called "history overfill" on backtest data.

      Do you or anyone else know if this problem has been solved before in this forum? Are there any code examples for the general use of OrderFillRosulution with illustrative examples?

      Would it be possible for the NinjaTrader Developer Team to take a short look at this?

      Best regards

      Patrick

      Comment


        #4
        Hello pgm1980,

        Thank you for your reply.

        You can set the order fill resolution to High in the Strategy parameters when applying a strategy to a chart:



        Click image for larger version

Name:	2021-05-07_08-47-59.png
Views:	131
Size:	111.8 KB
ID:	1155127

        The information I've previously provided is from our development team. Any fixes would need to be from the user side - meaning you would need to modify the script as noted in my previous reply or try running it using Order Fill Resolution set to High as seen in the above screenshot.

        ​​​​​​​Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Waxavi, Today, 02:10 AM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        13 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Waxavi, Today, 02:00 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by alifarahani, Today, 09:40 AM
        5 responses
        23 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by gentlebenthebear, Today, 01:30 AM
        3 responses
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X