Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order from strategy executing at incorrect price

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

    Order from strategy executing at incorrect price

    Hello, for some reason my stop loss order is always being put somewhere where it shouldn't be. I have used calculation mode ticks and price and both seem to make the stop loss randomly appear close to the current price or just not close enough to the actual stop loss.

    When I print out the stop loss values, they are CORRECT, which is what makes this weird. Both for tick amount and when I use calculation mode price. Also, in market replay IT WORKS. The orders are correct in market replay and targets / stop loss are placed correctly.

    For some reason in normal simulation the order is wrong, I have attached a screenshot of where I execute my trade in my code. I know the actual numbers are valid because I send myself a text message with the target and stoploss but it never actually uses those. Any help would be appreciated

    #2
    Hello maramello,

    Thank you for your post.

    When you say "normal simulation" - are you seeing this happen when the strategy is processing in real-time?

    Or is this happening when it is processing in historical or testing the Strategy Analyzer? If so, you should enable Tick Replay which will ensure that the market data (bid/ask/last) that went into building a bar is loaded in the exact sequence of market data events. This guarantees that your indicators and strategies are historically calculated tick-per-tick exactly as they would have been if the indicator/strategy was running live during a period.

    Tick Replay: https://ninjatrader.com/support/help...ick_replay.htm

    ​When in historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

    Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data.​

    I look forward to your reply.​​

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello maramello,

      Thank you for your post.

      When you say "normal simulation" - are you seeing this happen when the strategy is processing in real-time?
      Hello thanks for the response, by normal simulation I mean real-time processing that is correct. When it runs on the live market it places my stop order at the wrong price, but prints out the correct price when I send myself a text when the trade enters.

      Just confused, I read that sometimes using calculation mode price can cause issues but even using tick calculation mode for setting the stop loss isn't setting it correctly today. It is very weird how it works in market replay though and not live simulation, any ideas?

      Comment


        #4
        Hello maramello,

        Thank you for your response.

        Your screenshot shows a custom method where you are calling the Set methods, where is this custom method being called from? Keep in mind when using Set methods dynamically, set methods cannot be unset. This means it is important to call the Set method with CalculationMode.Ticks to reset this before calling an entry method (not after).​

        If you want to dynamically change/modify the price of the stop loss and profit target, the method should be called from within OnBarUpdate() and the price should always be reset when the strategy becomes flat again. This information is noted in the "Tips" section on the following help guide pages:
        The reference sample script SamplePriceModification demonstrates how to change the price of stop loss and profit target orders:

        https://ninjatrader.com/support/helpGuides/nt8/modifying\_the\_price\_of\_stop\_lo.htm

        If you're not using the Set methods dynamically, then you can just call them from State.Configure.

        Additionally, I recommend debugging with prints and TraceOrders.

        Add a print that prints the time of the bar, the stop variable, and the GetCurrentBid()/GetCurrentAsk().

        Below is a support article that provides further details on debugging using prints and TraceOrders.



        I am happy to assist in analyzing the output.

        Comment


          #5
          Hello, I will try to enable TraceOrders next time my strategy executes in the coming week and see what the output is.

          The function I am calling is called from OnBarUpdate, I call SetProfitTarget and SetStopLoss before calling EnterShort or EnterLong so I assumed it would work.
          I do have a different call to SetStopLoss in OnBarUpdate though to reset the stop loss as you mentioned when market position is flat.

          In the below screenshot, HandleEntryTimeframe() is where I call the ExecuteOrderBlockEntry() function from, so that calls the Set methods. BUT, I also call SetStopLoss as long as market position is flat. I am going to try commenting out the SetStopLoss for flat market position condition and see what happens.

          Do you think that:
          1) I should move the SetProfitTarget and SetStopLoss from ExecuteOrderBlockEntry into HandleEntryTimeframe() or OnBarUpdate to make a difference?
          2) That the SetStopLoss in the flat market position condition could be the issue?

          I will reply again once I have tested this in simulation on the live market in the coming week.


          Click image for larger version

Name:	testimage.png
Views:	133
Size:	98.0 KB
ID:	1312770 , maybe that is causing the issue?

          Comment


            #6
            Hello maramello,

            It's difficult to make any suggestions without the output from debugging and TraceOrders.

            Once you've debugged the script, please post the output for review and we can provide feedback.

            Comment


              #7
              Hello,

              I have managed to resolve the issue by removing the SetStopLoss in my Flat market position condition in the second screenshot I sent in OnBarUpdate.

              Looks like my calls to SetProfitTarget and SetStopLoss were working but the stop loss was overwritten when it entered that block before the trade had actually gone through and market position was not flat.

              I assume it doesn’t change your market position from flat straight away, which is what caused the issue

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              62 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              134 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              75 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              45 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              50 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X