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

Stop rejected because target filled, how to detect

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

    Stop rejected because target filled, how to detect

    I am seeing a situation in my custom strategy in fast moving markets where the following occurs:
    1. Enter the market using a stop market entry (eg place a long stop market above current price and wait for it to trigger).
    2. There is a spike up and the entry triggers long
    3. The strategy then sets a stoploss and profit target as OCO orders. These are submitted at the same time.
    3. The market moves fast so the profit target is hit.
    4. The stoploss is rejected because "it is not in a state where it can be used in an OCO". So, the strategy shuts down and closes all orders.

    The problem is this: suppose I have 3 such entries with different profit targets (say 8 ticks, 15 ticks and 30 ticks), As soon as target 1 is hit, stoploss 1 order is rejected for the above reason and my strategy then closes all positions and orders. So I miss out on targets 2 and 3 because the strategy has shut down.

    Is there any way to detect a stoploss that is rejected because its corresponding target has been filled, and simply ignore that stop? Is there any danger I am not seeing to doing so?

    #2
    Hello westofpluto,

    Thanks for your question.

    RealtimeErrorHandling can be set to StopCancelCloseIgnoreRejects or IgnoreAllErrors for the strategy to stay running. You can then check for rejected orders in OnOrderUpdate.

    I would maybe suggest starting a timer when an order rejection is seen for a short amount of time, where you can then check if the target had filled, and that the strategy position is protected by your remaining orders. If everything checks out, don't do anything else, just let the strategy run. If the position is not fully protected, you could submit an order or orders to ensure the position is fully protected, or call CloseStrategy to close positions and disable the strategy.

    RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

    OnOrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

    CloseStrategy() - https://ninjatrader.com/support/help...sestrategy.htm

    I have also attached an example showing how you can start a timer for a "delayed event."

    We look forward to assisting.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim, I was reading your response and trying to figure out how to implement the timer in the strategy. So the error I have encountered is especially happening when the stop tries to move to Breakeven after my first target was reached. The error message " Playback 101 Stop Price can't be changed below the market affected Order. BuyToCover 1 StopMarket "pops up and will not close. I have to use task manager to kill NT. I have IgnoreAllErrors turned on. I need some code to handle the error. I tried StopCancelCloseIgnoreRejects and the error still occurs Or creating a handler for the breakeven code?
      Example:
      if ( Position.MarketPosition == MarketPosition.Long && Position.Quantity < (ContractsL + ContractsL2) )
      {
      if (Close[0] > Position.AveragePrice + BreakEvenLong * TickSize)
      {
      newStopLossTicks = Position.AveragePrice ;
      SetStopLoss(CalculationMode.Price, newStopLossTicks);
      }
      }
      /// Short Breakeven
      if ( Position.MarketPosition == MarketPosition.Short && Position.Quantity < (ContractsL + ContractsL2) )
      {
      if (Close[0] < Position.AveragePrice + BreakEvenLong * TickSize)
      {
      newStopLossTicks = Position.AveragePrice ;
      SetStopLoss(CalculationMode.Price, newStopLossTicks);
      }
      }
      Last edited by set2win; 08-15-2021, 06:45 AM.

      Comment


        #4
        Hello set2win,

        StopCancelCloseIgnoreRejects will keep the strategy running for order rejects but will still disable the strategy on order errors.

        You can use IgnoreAllErrors and check for error == ErrorCode.UnableToChangeOrder in OnOrderUpdate to handle that kind of order error. Other ErrorCodes are noted in the Help Guide page for OnOrderUpdate.

        Are you able to comment/uncomment the code what specifically is causing the freeze in playback? If so, could you implement the part that is causing the freeze in a separate strategy and share it here with some instruction we can follow to reproduce the freeze in Playback? Please be sure to note specific dates and your timezone, as well as the instrument to use for testing. Please also have the strategy defaults set so the issue can be easily hit.

        Exporting as source code - https://ninjatrader.com/support/help...tAsSourceFiles

        JimNinjaTrader Customer Service

        Comment


          #5
          Using the following code does kill the stoploss order and error but it leaves an open position and strategy disabled, that would be dangerous if it happens without knowing about it.
          I did share the code that was causing the issue with moving stop to breakeven in the original post. I think the fast moving price action may result in slow response to moving to breakeven and thus resulting in error message.

          protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
          int quantity, int filled, double averageFillPrice,
          Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
          {
          if (order.Name == "Stop loss")
          {
          stopLoss = order;
          if (error == ErrorCode.UnableToChangeOrder)
          {
          Print("Order change failed, submitting hard exit.");
          CancelOrder(stopLoss);
          stopLoss = null;
          SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "", "Exit");
          }
          }
          }

          protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
          Cbi.MarketPosition marketPosition, string orderId, DateTime time)
          {
          if (execution.Order.Name == "Entry")
          SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, price - 10 * TickSize, "", "Stop loss");
          }

          Comment


            #6
            Hello set2win,

            I will need some specific detail for testing, and we can both save ourselves time reporting a freezing issue when we have a compilable example that is set up to reproduce the freeze, following some steps in Playback (specific days to test, specific instrument to test, timezone you are using.)

            As for the second question involving trapping an unable to change order error with the Unmanaged Approach, I do not get this with my example, which it looks like your code is derived from. I have it attached here so you can test and compare to troubleshoot your script.


            Attached Files
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ageeholdings, Today, 07:43 AM
            0 responses
            5 views
            0 likes
            Last Post ageeholdings  
            Started by pibrew, Today, 06:37 AM
            0 responses
            4 views
            0 likes
            Last Post pibrew
            by pibrew
             
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            14 views
            0 likes
            Last Post bltdavid  
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            12 views
            0 likes
            Last Post burtoninlondon  
            Working...
            X