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

Checks to eliminate cancellation rejected error

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

    Checks to eliminate cancellation rejected error

    Hello,

    I have a strategy that is designed to close a trade and immediately enter the opposite trade if the opposite signal is detected. The issue I am having is that the strategy is also using an exit criteria to get out of a trade instead of a stop loss, so when an opposite trade is detected, the system will 1. close the original position correctly, 2. enter the opposite direction correctly but also 3. try to exit the position which no longer exists incorrectly, causing a doubling up of contracts in the opposite direction since there is no offset for the exit. This will also generate the error "Cancellation rejected by the Order Management System - Order is complete. Further cancellations on this order may lead to logoff/disable" which will sometimes turn the strategy off completely.

    I have tried to put checks in place to check for the below logic to avoid the strategy submitting the exit execution since the close position already accomplishes that. Below is an example of the strategy going from a long position to a short position:

    Once the long is closed and the opposite short is entered:
    1. Check if the strategy is already in a short and if so, set checkshort to true.

    Click image for larger version

Name:	image.png
Views:	34
Size:	9.7 KB
ID:	1297235

    2. For the exit long criteria, checkshort must = false to execute which I was hoping would not allow this set to place a sell order if the strategy has already closed the long position with a sell order.

    Click image for larger version

Name:	image.png
Views:	20
Size:	18.5 KB
ID:	1297236

    This should only allow 2 orders to be submitted - the close of the long position and the opening of the short entry. However looking at the log this doesn't work as it seems that the strategy first submits all 3 orders (close long, enter short, exit long) from the strategy before the executions actually get submitted, so my checks are bypassed because the strategy does not detect being in a position when all 3 orders are initialized. The position executions happen after all 3 orders are submitted.

    So I assume this leaves me with needing to create a check that detects if the "close position" order has already been submitted. If so, then the strategy needs to not submit the "exit long" order. I don't see any option for this in the strategy builder and was wondering if this can be accomplished within the script editor?

    I have the log with the order of events for your reference but this post will not let me upload a .xlsx file so please advise which format I can upload the log in for you to review.

    Thank you,
    Jim ​

    #2
    Hello Jim,

    Thanks for your post.

    The Strategy Builder uses the Managed Approach for order handling.

    To have a strategy reverse from a long position to a short position when using the Managed Approach, you could simply submit an Entry order in the opposite direction. For example, if you are in a long position, you could call EnterShort() to have the strategy reverse positions. This will close the long position and then submit an order to enter a short position.

    From the Managed Approach help guide documentation linked below:

    "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."

    Managed Approach: https://ninjatrader.com/support/help...d_approach.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Brandon,

      I do have an enter short order that executes a short when the opposite signal is detected. Not every short order happens while in a long. Are you advising not to use an exit long order at all? If so, my issue is that I need this exit order as not every opposing signal is automatically a short either. For example, the strategy can enter a long on a long signal, and exit this long on an opposing signal which may not meet all of the conditions to execute a short. But when all of the conditions are met to execute the short, the long exit conditions are included. Perhaps I am not following what you are saying.

      Thanks,
      Jim

      Comment


        #4
        Hello Jim,

        Thanks for your notes.

        Correct, you would not need to call the Exit order method and instead could just call EnterShort() to reverse from the long position to the short position when the condition to do so is true.

        Calling ExitLong() and EnterShort() in the same OnBarUpdate() could cause the overfill behavior you reported.

        The most common reason an overfill occurs is because an exit and entry are both submitted on the same bar update.

        When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

        If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. It takes time for an order to be submitted, processed, transmitted, accepted, working, filled, and then for NinjaTrader to process the fill and update the position. This will not happen in the same bar update, it will take time.

        The result will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

        The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

        Otherwise, the Exit order would need to be processed and filled before you try placing a short entry order. In this case, you could consider calling the ExitLong() method to exit the long position and then in a separate Set, check if the strategy position is Flat (Position.MarketPosition == MarketPosition.Flat) and call your EnterShort() method to submit that short entry order. By doing so, the long position is exited which puts you in a flat position and then the EnterShort() order is placed to put you in a short position.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thanks Brandon,

          The check flat condition works well for my purposes. It's what I was missing.

          Thanks again,
          Jim

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,404 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          95 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          8 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          159 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X