Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Delayed Exit() problem (over fill)

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

    Delayed Exit() problem (over fill)

    Hi,
    I have a problem with sequence of actions in NinjaTrader. In my sample strategy I'm opening a Long position. If it reaches a max daily loss, position is forcefully closed.

    But if the market moves very quickly, ExitLong isn't handled properly. On Output I'm getting couple of messages "MaxDailyLoss" even though ExitLong should be triggered after the first message. Then market hits a StopLoss and position is closed.

    After StopLoss closes the position, ExitLong is executed and strategy gets over filled.

    Why in strategy with CalculateOnBarClose = false the ExitLong doesn't close position before it hits StopLoss, when market moves very quickly (about 20 ticks in 1 second)?

    I've attached my sample strategy code (part of my bigger strategy).
    Attached Files

    #2
    Hello InteRadek,

    Thank you for your note.

    You are setting your StopLoss and ProfitTarget orders to be live until cancelled. You will want to cancel these orders are you are exiting the position using ExitLong().

    You will need to assign IOrder objects to the StopLoss and ProfitTarget.
    Code:
    			if (dailyPnL + Position.GetProfitLoss(Close[0],PerformanceUnit.Currency) <= -500)
    			{
    				if (Position.MarketPosition == MarketPosition.Long)
    				{
    					Print("MaxDailyLoss");
    					allowTrade = false;
                                            CancelOrder(stopOrder);
                                            CancelOrder(targetOrder);
    					ExitLong();
    				}
    			}
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hello,
      thank you for your reply. I've implemented CancelOrder (StopLoss and ProfitTarget) to my strategy. Unfortunatelly on live market this still isn't working the way it should.
      I've attached my modified sample strategy. On Output I get:
      Position price: 10740
      StopLoss: 10732,5; TargetProfit: 10743
      MaxDailyLoss
      MaxDailyLoss
      MaxDailyLoss
      Daily P&L: -75
      Strategy should close position, when PnL is -50 and MaxDailyLoss should be printed only once. How can I achieve that? Is there any method other than Exit(), that will flatten my strategy immediately?
      Attached Files

      Comment


        #4
        Originally posted by InteRadek View Post
        Hello,
        thank you for your reply. I've implemented CancelOrder (StopLoss and ProfitTarget) to my strategy. Unfortunatelly on live market this still isn't working the way it should.
        I've attached my modified sample strategy. On Output I get:
        Strategy should close position, when PnL is -50 and MaxDailyLoss should be printed only once. How can I achieve that? Is there any method other than Exit(), that will flatten my strategy immediately?
        CalculateOnBarClose is false. "MaxDailyLoss" will print as long as the condition is true for it to Print(). In this case, it took 3 ticks before your order got executed to take you out, so you got 3 prints.

        It is not a matter of using a different order to Exit(): it is a matter of physics. "Order Execution" is not always instantaneous: actually, it rarely is. Tempus fugit.

        Comment


          #5
          InteRadek,

          You're code is not an effective test for this.

          The reason is that the Stop and Profit are being placed at the prices from 1 bar ago, low and high respectively.
          In this case we don't know what the current or next bar will start at and thus the orders can be exited out before we know that our PnL is -50 and submits the ExitLong()

          I added some room for movement around these by adding 4 ticks to each of the prices and was able to test it. I did not run into any issues with the orders getting cancelled and the ExitLong() flattening the position when the PnL was -50 or more.

          Please try this on your end and let me know of your results
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            Thank you for your input, it's very helpful. If Order Execution isn't instantaneous, than both Exit() and CancelOrder() will be executed with delay and at some point they can run into conflict with each other generating Over fill.

            I've modified my strategy (it's attached), so that Stop Loss and Profit Target price are based on Position price. I've launched my strategy 35 times on FDAX 03-15 1 Min, before I came across Over fill.

            Output:
            **NT** Enabling NinjaScript strategy 'Test/8e36e6bfcb614907abca2c4961f81cd0' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
            Tick size: 0,5
            Long buy: 10754
            StopLoss: 10751,5; TargetProfit: 10756,5
            Profit Target: 10756,5
            Daily P&L: 62,5
            Long buy: 10757
            StopLoss: 10754,5; TargetProfit: 10759,5
            Stop Loss: 10754
            Daily P&L: -12,5
            Long buy: 10756
            StopLoss: 10753,5; TargetProfit: 10758,5
            MaxDailyLoss
            Stop Loss: 10752,5
            Daily P&L: -100
            **NT** An over fill was detected on order 'Order='e9adf9f32f754d0996349bf3faddc85b/Sim101' Name='Sell' State=Filled Instrument='FDAX 03-15' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='Test' Type=Market Tif=Gtc Oco='' Filled=1 Fill price=10752,5 Token='e9adf9f32f754d0996349bf3faddc85b' Gtd='2099-12-01 00:00:00'' generated by strategy 'Test/8e36e6bfcb614907abca2c4961f81cd0' : This strategy will be disabled and NinjaTrader will attempt to cancel/close any strategy generated orders and positions. Please check your account orders and positions and take any appropriate action.
            Sell: 10752,5
            Close: 10757
            Daily P&L: -212,5
            **NT** Disabling NinjaScript strategy 'Test/8e36e6bfcb614907abca2c4961f81cd0'
            Could that Over fill be avoided in any way?
            Attached Files

            Comment


              #7
              Originally posted by InteRadek View Post
              Thank you for your input, it's very helpful. If Order Execution isn't instantaneous, than both Exit() and CancelOrder() will be executed with delay and at some point they can run into conflict with each other generating Over fill.

              I've modified my strategy (it's attached), so that Stop Loss and Profit Target price are based on Position price. I've launched my strategy 35 times on FDAX 03-15 1 Min, before I came across Over fill.

              Output:
              Could that Over fill be avoided in any way?
              Yes, but nothing is free. There is a price to pay. All my strategies do it, and so far every optimization run shows that the effect is not as onerous as I thought that it should be. On the other hand, all my strategies also use CalculateOnBarClose = true, so your milage will almost certainly vary.

              My method consists of NOT placing a hard stop, and simulating a mental stop. IOW, I mark the Stop Price and exit only after it is violated.

              Comment


                #8
                Yes, it is a way to avoid over fill, but generates a risk of leaving the position open without stop loss, if strategy loses connection with the market.
                It is then a matter of priorities. Thank you very much for your help.

                Comment


                  #9
                  Originally posted by InteRadek View Post
                  Yes, it is a way to avoid over fill, but generates a risk of leaving the position open without stop loss, if strategy loses connection with the market.
                  It is then a matter of priorities. Thank you very much for your help.
                  Which is why my brokers' telephone numbers are taped to my monitor.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  637 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  366 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  107 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  569 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  571 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X