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

Does exiting a position after certain conditions are met automatically cancel exits?

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

    Does exiting a position after certain conditions are met automatically cancel exits?

    ​I am working on a strategy that has multiple exits, each its own Set in the the Strategy Builder;
    • 1 sets a stop loss at order entry, then moves the stop loss up to break even once the price has a specific unrealized gain.
    • 1 exits the position if two moving averages cross, and
    • 1 exits the position after a specified profit target has been reached.
    Once one of the above actions/exits occurs under specific conditions, do the other sets with exits need to be cancelled? Or are they automatically cancelled?

    If they need to be cancelled, can this be done within Strategy Builder, or must it be unlocked and added to the code?

    Click image for larger version

Name:	Screenshot (146).png
Views:	103
Size:	66.7 KB
ID:	1278065

    Attached is an example of entering a long position according to the strategy, then exiting when stop loss has been met (1st bullet from above), but then it immediately identifies the moving average crossover exit condition (2nd bullet from above) and enters into a short position, even though short positions are no where in the strategy.

    Thanks for any insight.
    Last edited by tpearson00; 11-15-2023, 08:55 AM.

    #2
    Hello tpearson00,

    Thank you for your post.

    Are these working limit or stop exit orders, or are these market exit orders?

    When using the managed approach, NinjaTrader will try and cancel any exit orders using the fromEntrySignal of the entry order's signal name if the position is closed.

    All Exit order methods have a fromEntrySignal argument, but as an example look at the syntax for SetStopLoss():

    SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool isSimulatedStop)

    SetStopLoss() - https://ninjatrader.com/support/help...etstoploss.htm


    You can supply the fromEntrySignal in the Builder, so it is not necessary to unlock the script.

    Please let me know if you have any other questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3

      All 3 Exit conditions are working exit orders- if that means that the are managed in the Conditions/Actions section and not the Stops and Targets section, for example the action to be taken when moving averages cross is the following: ExitLong(Quantity,@"Crossover",@"Long");

      I have the Stop loss as a variable, so it will exit the position as described here: ExitLongStopMarket(Quantity,(Position .AveragePrice+(StopLoss),@"StopLoss",@"Long")'

      In the screenshot above, it looks like both actions happened at the same time, so the long was closed by ExitLongStopMarket, but then when it executed ExitLong(Quantity,@"Crossover",@"Long" it created a short position.......

      Thanks for any clarification.
      Last edited by tpearson00; 11-15-2023, 09:54 AM.

      Comment


        #4
        Hello,

        Since these are working orders, we generally do not advise using multiple exits for a single entry. This is because both exits could fill and reverse the position.

        You could use ExitLongStopLimit() to exit your position instead, while still keeping track of your stop and target:

        https://ninjatrader.com/support/help...gstoplimit.htm

        Please let me know if you have any other questions.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thanks for your help. To make sure I understand what you are saying, If I use ExitLongStopLimit() to exit the position, but still have an Exit set to occur if 1 moving average crosses another (labeled "Cross"), I am still at risk of the "Cross" exit reversing the position to a short if both the crossover and the ExitLongStopLimit() occur at the same time.

          So it would seem the only way to keep both types of exits in the Strategy would be to unlock the code and add a cancel order for the other exit if the other should occur....

          Comment


            #6
            Hello,

            Yes, if you use ExitLongStopLimit() and still keep your exit for if there is a cross, you are still at risk of the exits reversing your position or an overfill.

            If you would like to unlock the script and use CancelOrder() for if a cross occurs, please keep in mind that this method does not guarantee that an order is completely cancelled.

            From the Help Guide:

            "An order can be completely filled or part filled in the time that you send the cancel request and the time the exchange receives the request. Check the OnOrderUpdate() method for the state of an order you attempted to cancelled."

            CancelOrder() - https://ninjatrader.com/support/help...ancelorder.htm

            If you do use CancelOrder(), to ensure that both orders do not fill we suggest instead cancelling the working order, then in OnOrderUpdate() detecting the order is cancelled before submitting the replacement exit order.

            Please let me know if you have any other questions.
            Gaby V.NinjaTrader Customer Service

            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
            160 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Yesterday, 09:29 PM
            0 responses
            9 views
            0 likes
            Last Post Belfortbucks  
            Working...
            X