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

Submit StopLoss after multiple position exit

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

    Submit StopLoss after multiple position exit

    Hello,

    I got a problem with submitting a stoploss for a new position after exiting a position with multiple entries.
    However, this only happens, if the position(s) of one direction get closed at the same time the position of the other direction gets opened. Also, if I limit the numbers of entries per direction to 1, everything works fine.
    I think the problem is, because as long as the direction is - for example - short, a ShortStopLimit order is pending at the same level of the StopLoss set for the next position after changing the direction. As soon as the direction changes, this ShortStopLimit order gets cancelled, thus also cancelling the StopLoss order.
    Is there a possibility to avoid the cancellation of the StopLoss order?

    If requested, I can provide you the code by e-mail for further hints.

    Regards,
    Sepp

    #2
    Hello Sepp,

    Thank you for your post.

    When this occurs is there an error listed in the Log tab of the NinjaTrader Control Center regarding the Internal Order Handling Rules? In most cases the Internal Order Handling Rules have caught one of the following for methods that generate orders to enter a position and has subsequently ignored the entry:
    • A position is open and an order submitted by an exit method (ExitLongLimit() for example) is active and the order is used to open a position in the opposite direction
    • A position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction
    • The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
    • The entry signal name is not unique

    For information on the Internal Order Handling Rules please visit the following link: http://www.ninjatrader.com/support/h...d_approach.htm

    If one of the rules above was caught the entry order would be cancelled. You can use the Unmanaged Order Methods if you wish to avoid this item. For information on the Unmanaged Order Methods please visit the following link: http://www.ninjatrader.com/support/h...d_approach.htm

    Please let me know if you have any questions.

    Comment


      #3
      There is no particular error message displayed for a specific trade. However, some of the events you listed do occur. For example, as soon as an order is executed, another StopLimit order in the same direction is opened. This one stays active until the whole position gets closed - which happens to be at the same time the new position in the opposite direction is opened. Could it be because of that?
      Another - somehow related - cause could be, that there is a LongLimit order as well as a StopLoss order active at the same time. Are they cancelling out each other? How is the order handling if you have a StopLoss order as well as a LongLimit/Stop/StopLimit order active at the same time?

      Regards,
      Sepp

      Comment


        #4
        Hello Sepp,

        Thank you for your response.

        I cannot say what is occurring, however can you send your script to support[at]ninjatrader[dot]com with 'ATTN: Patrick - 910936' in the subject line?

        Please also advise what instrument, interval and period type you are running this strategy on that generates this item.

        Comment


          #5
          Thank you very much for your help, I have sent you an e-mail!

          Comment


            #6
            Hello MasterSepp,

            Thank you for your patience.

            While I was able to run the strategy in the Strategy Analyzer with your settings I do not see the same Stop Loss on August 13th (probably a difference in historical data form one provider to the next). I did however receive the following errors in the Output window after running the backtest:
            • **NT** A SellShort stop order placed at '3/6/2013 9:30:00 AM' has been ignored since the stop price is greater than or equal to close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.
            • **NT** A Set() method to submit an exit order at '6/13/2013 11:00:00 AM' has been ignored. Please search on the term 'Internal Order Handling Rules' in the Help Guide for detailed explanation.

            For information on the Internal Order Handling Rules please visit the following link: http://www.ninjatrader.com/support/h...d_approach.htm

            Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :

            First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.

            For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders

            It may also help to add drawing objects to your chart for signal and condition confirmation - Drawing Objects.

            If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings - Click here for a list of certified NinjaScript Consultants

            Please let me know if I may be of further assistance.

            Comment


              #7
              Hello Patrick,

              Thank you very much for your help so far!
              I will try to debug the strategy on my own.

              However, I have one final question with which I'm struggling: Is it possible to maintain a SetStopLoss() order as well as a ExitShortStop() order?
              I know that with the managed approach, maintaining several orders on one position isn't possible. However, I'm not totally sure, if the SetStopLoss() command is seen as a regular order by the system. This would clarify quite a few questions for me.

              Best regards,
              Sepp

              Comment


                #8
                Hello Sepp,

                Thank you for your response.

                The SetStopLoss() would be seen as a Stop Limit order with a 20 tick offset.

                Please let me know if I may be of further assistance.

                Comment


                  #9
                  Hi Patrick,

                  I am sorry to bother you again, but I still struggle with managing my StopLosses. Perhaps you can give me some insight on the order handling, since this seems to be the problem. Looking up in the help section of NinjaTrader didn't solve the problem unfortunately.
                  The problem I have is, that after I am into one direction with a position, there is another Stop order generated in the same direction (for stacking up the position) as well as a SetStopLoss() order. This order is active until the position gets closed (or the order gets executed). Now, if the Stop Loss order gets executed, everything works fine afterwards. But when the position gets closed through a change of the direction (e.g. changing from short to long - Strategy Analyzer notes there "Close Position") through a Market order, the SetStopLoss() order for the new position isn't generated properly. I strongly assume, that this has something to do with the fact, that at the moment the direction is being changed, the Stop order from the old direction is still active and thus conflicting with the new SetStopLoss() order.
                  Could this be the case? And if yes: How can I solve this problem? I'm thinking of cancelling all active orders (although this should be done automatically when the "Close Position" (i.e. Market Order for closing the position) order is executed, but CancelOrder() didn't work so far. Is there another possibility?
                  Thank you very much in advance!

                  Regards,
                  Sepp

                  Comment


                    #10
                    Hello Sepp,

                    Thank you for your response.

                    Is the SetStopLoss() method used in the Initialize() method and/or the OnBarUpdate() method?

                    I look forward to your response.

                    Comment


                      #11
                      The Stop order is tested in the OnBarUpdate() method and the SetStopLoss() method is used in the OnExecution() method.
                      However, I am using the exact same command for setting the StopLoss for the initial position as well as for the positions stacked up later. After stacking up for the first time, the stoploss works perfectly fine, but for the initial position it doesn't seem to work.
                      Following the code snippet:

                      if (execution.Order != null && execution.Order.OrderAction == OrderAction.Buy)
                      {
                      SetStopLoss(CalculationMode.Price, execution.Price - 2 * ATR(14)[0]);
                      }

                      Best regards,
                      Sepp

                      Comment


                        #12
                        Hello Sepp,

                        Thank you for your response.

                        When Flat are you resetting the SetStopLoss()? Reset the stop loss price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position.

                        If you are not resetting the SetStopLoss() to a new level when flat please do so and advise if this corrects the matter.

                        For a reference sample on modifying SetStopLoss() that shows how to reset the SetStopLoss() please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3222

                        Comment


                          #13
                          Hi Patrick,

                          Thank you very much for your help. Indeed, this could solve the problem. But since there is only a direct change from e.g. short to long (through the command EnterLong() ), I am not sure, if my strategy is at any time flat, so I could implement this option. Is this possible to do so? Or is there another way to reset the StopLoss, when the positions only get directly changed, without flattening first?

                          Comment


                            #14
                            You can reset the stop loss at any time you wish. The method Patrick mentioned is a common approach as a lot of strategies will go flat for a time at which point you could calibrate different settings.

                            If you have an always in strategy, you would need to determine when a good time would be for your stop loss to be reset. For example, you can use the "OnExecution" event and reset your stop loss once you detect that either your long or short entry was executed.

                            We also have a reference sample on related methods, which is a bit more advanced, but should give you more control:

                            The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
                            MatthewNinjaTrader Product Management

                            Comment


                              #15
                              Dear Matthew,

                              Thank you for your contribution. Following the code snippet I am posting below, I "outsourced" the SetStopLoss() method in my always in strategy indeed to the OnExecution() method. As stated before, the change with the StopLoss doesn't work, although it gets changed on each Execution event. Adding the command to set back the StopLoss order didn't work either.
                              Following the code snippet, which is responsible for generating the StopLoss order. Maybe you can see an obvious error, which causes these problems.

                              Old code snippet:
                              Code:
                              if (execution.Order != null && execution.Order.OrderAction == OrderAction.SellShort)
                              {
                              SetStopLoss(CalculationMode.Price, execution.Price + 2 * ATR(14)[0]);
                              }
                              if (execution.Order != null && execution.Order.OrderAction == OrderAction.Buy)
                              {
                              SetStopLoss(CalculationMode.Price, execution.Price + 2 * ATR(14)[0]);
                              }

                              New code snippet:
                              Code:
                              if (execution.Order != null && execution.Order.OrderAction == OrderAction.SellShort)
                              {
                              stopOrder = null;
                              stopOrder = ExitShortStop(0, true, Position.Quantity, execution.Price + 2 * ATR(14)[0], "", "");
                              }
                              if (execution.Order != null && execution.Order.OrderAction == OrderAction.Buy)
                              {
                              stopOrder = null;
                              stopOrder = ExitLongStop(0, true, Position.Quantity, execution.Price - 2 * ATR(14)[0], "", "");
                              }

                              EDIT:
                              Another thing, which is actually the most annoying part of this whole problem, is that sometimes, within the Order tab in the Strategy Analyzer, you can clearly see that the StopLoss was activated after a direct change of direction, whereas the next time it wasn't, although the condiitions were exactly the same...
                              Last edited by MasterSepp; 09-06-2013, 10:47 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by RubenCazorla, 08-30-2022, 06:36 AM
                              3 responses
                              77 views
                              0 likes
                              Last Post PaulMohn  
                              Started by f.saeidi, Yesterday, 12:14 PM
                              9 responses
                              23 views
                              0 likes
                              Last Post f.saeidi  
                              Started by Tim-c, Today, 03:54 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Tim-c
                              by Tim-c
                               
                              Started by FrancisMorro, Today, 03:24 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post FrancisMorro  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              10 responses
                              1,772 views
                              0 likes
                              Last Post Leafcutter  
                              Working...
                              X