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

How to detect that a stop was rejected because the target was filled

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

    How to detect that a stop was rejected because the target was filled

    I have a strategy that submits a stoploss order and a target order at the same time as an OCO pair once a trade has been entered. When an instrument moves really fast, it is possible for the target to get filled before the stoploss is accepted. As a result the stoploss order is rejected with a message that is SOMETHING LIKE: "order is not in a state where it can be used in an OCO". This wording is approximate. My broker is Dorman/Ninjatrader.

    So how can I detect exactly this error. Yes I know that I can set RealtimeErrorHandling.StopCancelCloseIgnoreRejects and catch this error in OnOrderUpdate. But when I have that order, what is the best way to check to see the the rejection was for that reason?

    I see that there is a comment field in OnOrderUpdate, and that comment will contain the error message from the broker. However, this message is from the broker, so it is not standardized, and I don't know the exact wording. Is there any list anywhere of the exact wording of error messages from Ninjatrader/Dorman broker?

    If I can't use the error message, then I thought maybe I could save all the orders in Order variables in the strategy and I could check to see if the corresponding target order has been filled. But is it possible in a fast moving market for the target to get filled and the corresponding stop gets rejected before the platform calls OnOrderUpdate for the target? In other words, is this sequence possible in a fast market:

    1. Stop and target orders submitted
    2. OnOrderUpdate called for target because order accepted
    3. target order gets filled but OnOrderUpdate is not called yet.
    4. OnOrderUpdate gets called because stoploss gets rejected.

    I also know that I can use a timer and delayed event to wait a second before checking to see if OnOrderUpdate has been called with the target being filled. But this is not very satisfying, it gives an extra second for the strategy to have no stoploss if there is a strong adverse move.

    So ideally, I want to know at the point where OnOrderUpdate is called for a rejected stop, what exactly the reason is at that moment in a way that I can act on it.

    How can I do this?


    #2
    Hello westofpluto,

    Thanks for your post.

    Commonly, the error comment seen is "Order is in a state that prohibits using it as a compound leg." You can check the log tab of the Control Center and check the native error of the rejected order to see what the comment may be.

    However, the following should be considered:

    Something we have to keep in mind is that if we get an error for an OCO id that cannot be used (other order is terminal) we may not know if the other order filled or had gotten rejected itself. The most secure thing to do would be to see what happens with the other order. Although, for the case of a target/limit order, it is likely to be filled because a limit order will fill at the limit price or better.

    Because of the above, simply checking for the OCO related rejection may not be the most sound decision to continue trading.

    We do not have to think of the delayed event of "pause and wait a second to check other order," the behavior could be "pause trading actions until we see what happens with the other order, or close the strategy after X time if we do not receive sound information that everything is ok"

    So essentially:

    1. Order rejection is seen in OnOrderUpdate, and a bool is set "WaitingForTarget" or "WaitingForStop" etc.
    2. A timer is also set for a delayed event.
    3. No additional trades take place when bools are set.
    4. OnOrderUpdate has condition that checks this bool and listens for the other order. If we see the other order fill, position would be safely closed, and we can reset the bool to continue normal trading
    5. Timer event checks the bool, and if the we reach this point, strategy should disable and take actions to make sure positions are closed

    Since listening for the other order in OnOrderUpdate and flipping the "WaitingForTarget" or "WaitingForStop" bool back would allow the strategy to resume as soon as the other order feedback is seen, this would be the fastest and safest way to continue in the event of an order rejection of this sort.

    For the thread's reference we had some previous discussion in the thread here: https://ninjatrader.com/support/foru...95#post1167395

    Let us know if you have any questions.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by lightsun47, Today, 03:51 PM
    0 responses
    5 views
    0 likes
    Last Post lightsun47  
    Started by 00nevest, Today, 02:27 PM
    1 response
    11 views
    0 likes
    Last Post 00nevest  
    Started by futtrader, 04-21-2024, 01:50 AM
    4 responses
    46 views
    0 likes
    Last Post futtrader  
    Started by Option Whisperer, Today, 09:55 AM
    1 response
    14 views
    0 likes
    Last Post bltdavid  
    Started by port119, Today, 02:43 PM
    0 responses
    10 views
    0 likes
    Last Post port119
    by port119
     
    Working...
    X