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

Delay Cancelling orders

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

    Delay Cancelling orders

    Hi support,

    I am trading with TDAmeritrade, and it seems like there is a delay in cancelling stop orders.

    Here is how the trade is made

    1. submit a buy order
    2. buy order filled -> submit a stop loss order
    3. when exit signal is hit, cancel the stop order
    4. submits a sell order.


    When I look at the order status in TDAmeritrade, I see that the sell order executed before I cancel the stop order, so it rejected the sell order. However, the sell order was eventually executed somehow.

    So here is what I see in TDAmeritrade

    1. buy order filled
    2. submitted a stop loss order
    3. reject a sell order due to not enough position to sell
    4. cancel the stop loss order
    5. execute the sell order

    As you can see, it seems like the sell order was made prior to the cancel stoploss order, thus causing an error, and causing the strategy to exit.

    How can I deal with this? I was thinking about cancelling the stoploss order first, and only submit a sell order after I receive the confirm of the cancel. However, is that the only way to deal with this?


    #2
    Hello op1031,

    Thanks for your post.

    How can I deal with this? I was thinking about cancelling the stoploss order first, and only submit a sell order after I receive the confirm of the cancel. However, is that the only way to deal with this?
    Yes, this would be the recommended path to take to avoid an overbought/oversold rejection with TD Ameritrade. We would suggest cancelling the stop loss, checking for its cancelation in OnOrderUpdate, and then to exit the position. I posted a small example in the thread below.

    Hi, I have multiple open positions and at the end of the day if they don't hit their respective Target/Stop, I would like cancel the linked OCO's and close the positions. I know TD Ameritrade makes things difficult, what would be the best way to do this? I've tried various ways but cant seem to get it to work. Any help would be


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

    Please let us know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      Thank you for the response. In your example, you used Account.CancelOrder, I tried it, and in my backtest, the stop order does not get cancelled for some reason, so eventually I reverted the code back to just CancelOrder (without the Account class), and it works fine in backtesting. Could you explain the difference?

      Thanks!

      Comment


        #4
        Hello op1031,

        CancelOrder would be used to cancel orders at the strategy level, and the orders specifically need to use IsLiveUntilCancelled = true. Set methods and market orders would not apply but Account.Cancel can be used to cancel these orders. (Account.Cancel would cancel orders at the account level.)

        When I have tested on my end, I was not able to cancel the Set orders in the Strategy Analyzer with the either approach. It is a bit ambiguous if that would be expected with my example because the Set methods do not use IsLiveUntilCancelled and the Strategy Analyzer does not have a real account.

        I'm discussing this particular matter with my colleagues and will follow up here with more definitive information when it is available.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim,

          I am using ExitLongStopMarket to set stoploss, and using CancelOrder (without the Account class) does cancel the order in both backtest and live trading (It canceled the order in live trading today, but it's now complaining that I am submitting sell order twice. I will debug that on my own)

          I am still curious on the when to use Account.CancelOrder and CancelOrder.

          Thanks!

          Comment


            #6
            Hello op1031,

            CancelOrder should be used at strategy level, and there wouldn't be a need to use Account.Cancel (account level) in a strategy unless we wanted to cancel an order that was submitted from a Managed Approach order method that do not specify IsLiveUntilCancelled. I.E. if you needed to cancel a market order before it fills or if you needed to cancel an order submitted using a Set method that does not have a parameter for IsLiveUntilCancelled.

            Account.Cancel is not working in my example for the case of backtesting, so I am checking into how cancels on Set methods should be treated for this case.

            Ok, I got some clarity here.

            For anyone that is using an approach described post #2 to cancel stop and target orderd on TDA, this approach would not be backtestable and would require branching your logic so instead of attempting to cancel the order with Account.Cancel, you can branch your logic when State == State.Historical to call ExitLong/ExitShort, instead of first cancelling the order.

            It is also possible to use the Unmanaged Approach where you would not have to have a separate logical branch for backtesting, because you can cancel Unmanaged OCO orders with CancelOrder.
            Last edited by NinjaTrader_Jim; 08-30-2021, 01:29 PM.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by geddyisodin, 04-25-2024, 05:20 AM
            8 responses
            61 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by jxs_xrj, 01-12-2020, 09:49 AM
            4 responses
            3,288 views
            1 like
            Last Post jgualdronc  
            Started by Option Whisperer, Today, 09:55 AM
            0 responses
            5 views
            0 likes
            Last Post Option Whisperer  
            Started by halgo_boulder, 04-20-2024, 08:44 AM
            2 responses
            22 views
            0 likes
            Last Post halgo_boulder  
            Started by mishhh, 05-25-2010, 08:54 AM
            19 responses
            6,189 views
            0 likes
            Last Post rene69851  
            Working...
            X