• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Trailing Stop weird behaviour in Unmanaged mode

  • Filter
  • Time
  • Show
Clear All
new posts

    Trailing Stop weird behaviour in Unmanaged mode

    Hi NT8 gurus,

    I'm really stuck with a weird issue I can't explain.
    I built a strategy based on the samples I found on this forum to bracket the market and tried to enhance it with trailing stops.
    It works fine in most cases, but I found a case that literally is driving me nuts.
    In most cases, the strategy works correctly, as shown below:

    But in this one case, the trailing stop works correctly up to a point, and then it suddenly reverts back to its initial price, when the order was first submitted.
    It looks something like that:

    The crazy thing is that, as can be seen on the chart above, the market price is never returning to that low level, but somehow my stopLoss is reverting to its initial value.
    I ran that case through the VS Debugger and just couldn't understand what happened. It all looks good until all of a sudden, my stopLoss goes out of whack.
    I tried to figure this out until my eyes hurt, but still no luck.

    I attached the NT8 logs that show all the orders and executions, it clearly shows the stopLoss executed at that crazy price, but it doesn't explain why that happened.
    I also attached my strategy in the .cs file.

    The test case to reproduce the issue is as follow:
    - Replay the market data for the GC 02-20 contract as of Friday Jan 10th 2020
    - Launch the strategy at 8.29:58 am (yes, it has to be this precise)
    - By 8.30:02 am, the problem from the screenshot above should be apparent

    I spent so much time on the debugger I have no more idea how to tackle this.
    Really appreciate is someone has some pointers to help me!
    Last edited by g49nep1f; 01-12-2020, 07:48 PM.

    Here are the logs and the .cs file
    Attached Files


      Hello g49nep1f,

      Use prints to understand the behavior.

      Below is a link to forum post that demonstrates.

      When the script stops behaving as expected, what are the values used by the order on that bar?

      May we see the output from the output window?

      You have mentioned this is based on a sample. Does the sample also experience the same behavior?
      Chelsea B.NinjaTrader Customer Service


        Hi Chelsea,

        The debugger stops step by step execution when the problem happens, so I can't tell the value of the bar.
        However, I'm starting to suspect where the problem is.
        All is fine till the price goes up, the trailing stop is moving upward, but the issue happens when the price starts to go down.
        I suspect a problem in the code, but I don't know how else to code it.
        I couldn't find any sample for Unmanaged mode trailing stop, so I suppose I have an issue there.
        I have attached the output of the strategy. Towards the end, you can see the trailing stop price is 1552.1, and suddenly the stopLoss order gets executed and filled at 1549.5, which makes no sense since the price never dropped that much during the execution of the strategy.

        Would you be able to provide a sample or point me to some forum link for unmanaged trailing stop ?
        Appreciate your help!

        Attached Files


          Hello g49nep1f,

          Below is a link to an example of an unmanaged trailing stop named ProfitChaseStopTrailUnmanagedExample.

          Use prints and TraceOrders to understand the behavior of the script.
          Where there is a condition for an entry print all values used in the conditions for the entry along with the time of the bar. In the output look for the time of the bar where you are expecting a new entry. Check that all values would allow the condition to evaluate as true. Check TraceOrders to see if orders are being ignored.
          Chelsea B.NinjaTrader Customer Service


            Thanks Chelsea,

            I will have a look tonight and revert back!


              Hi Chelsea,

              I have tried the strategy you provided above in the same context as the one I built, to make sure I'm comparing apple to apple.
              I see the exact same behavior happen. The trailing stop loss and the chasing profit target are working fine up to a point where all of the sudden, the Stop Less gets triggered at a price that's not the current market price.
              I have used the TraceOrder parameter to generate the logs, they are attached in the log file below.
              I just annotated the logs in the screenshot below to highlight the behavior:

              Click image for larger version

Name:	NT8_TraceLog.png
Views:	40
Size:	119.3 KB
ID:	1083850

              In the screenshot above, if you scroll right and check the price of the profit target and stop loss, you can see they are bracketing the current market price, which at that stage is 1552.6.
              I can't understand how the Market Replay tool is giving me an execution on the stop loss @1549.7 when the market price is 1552.6.
              Even if the price went down sharply - which didn't happen, you can check the chart on my initial post - my stop loss should have triggered around 1552.

              I really don't know what is going wrong here. The simulated feed works fine. I'm suspecting a problem with Market Replay. I have already re-imported my Market Data for the 10th of Jan, but the issue still persists.

              I will try again tonight with another date and see if the same behavior happens again.
              Attached Files


                Hi Chelsea,

                So after trying the exact same strategy on a number of different days, with the same market patterns, I can't reproduce the issue, so that would confirm my initial problem comes from Market Replay, not from the strategy itself.
                I've tried both Long and Short trailing stops, and the behaviour is what I would expect:

                Click image for larger version

Name:	Long-Trailing-Stop.png
Views:	46
Size:	32.8 KB
ID:	1083905Click image for larger version

Name:	Short-Trailing-Stop.png
Views:	38
Size:	25.3 KB
ID:	1083906

                I don't think there is much more I can do from my end. I'd be interested in knowing what exactly is causing this issue during Market Replay, but for now I guess we can close this issue.


                  Hello g49nep1f,

                  To confirm, the example script I have provided you is not reverting the stop or limit price of a working order to the original starting price, but instead, you are experiencing order fills at a different price than the stop or limit price.

                  Compare your PC Clock with https://www.time.gov/

                  Is your PC Clock in sync with internet time to the second?
                  Chelsea B.NinjaTrader Customer Service


                  Latest Posts


                  Topics Statistics Last Post
                  Started by random9570, Today, 09:01 PM
                  1 response
                  Last Post Fulcanelli  
                  Started by natngk, Today, 08:28 PM
                  0 responses
                  Last Post natngk
                  by natngk
                  Started by Borsz, Yesterday, 10:37 AM
                  2 responses
                  Last Post aligator  
                  Started by Tagliareni, Today, 08:10 PM
                  0 responses
                  Last Post Tagliareni  
                  Started by alanbb8989, Today, 06:10 PM
                  1 response
                  Last Post NinjaTrader_EricB