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

Stop loss not working

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

    Stop loss not working

    I’m working on a strategy where you enter on the breakage of a trend line. I have a stop limit order waiting for entry, plus stop loss and profit taking limit orders for exit. I use the functions EnterLongStop, ExitLongStop, and ExitLongLimit (and their short equivalents). The code paints a black dot for the stop level and yellow for the profit taking limit order. The attached 12/7/2011 chart is an example of where it is working.

    Where it’s failing is when during the same bar my entry order fills, then the stop loss level is hit, but the stop loss order never fires. See the attached chart for 10/23/2011.

    During the 8:59 bar, after entry, the stop loss should have filled for a relatively minor loss. Instead, it went all the way to the close, turning what should have been a 31 tick loss into a 109 tick loss.

    Any ideas what I’m doing wrong? Thanks!
    Attached Files

    #2
    calculateonbarclose = ?

    Comment


      #3
      CalculateOnBarClose = true;

      Comment


        #4
        Style,

        Could you post your strategy so we may comment further? Its hard to judge what should or shouldn't be occurring based on your description alone.

        We look forward to helping you resolve your issue.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Adam,

          See the attached.

          Thanks,
          Chris
          Attached Files

          Comment


            #6
            Style,

            Thanks for posting that.

            I don't see where you are using SetStopLoss() or SetProfitTarget(). It is advised to use these methods for stops/targets as otherwise you may run into issues related to order management.

            From our help guide : http://www.ninjatrader.com/support/h...d_approach.htm


            Methods that generate orders to enter a position will be ignored if:

            • 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

            Methods that generate orders to exit a position will be ignored if:

            • A position is open 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

            • A position is open and an order submitted by a set method (SetStopLoss() for example) is active


            Set() methods that generate orders to exit a position will be ignored if:

            • A position is open 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

            • A position is open and an order submitted by an exit method (ExitLongLimit() for example) is active
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Adam,

              This is very helpful, and explains the problems I’m having with stop and reverse (SAR) orders. I actually started using SetStopLoss and SetProfitTarget, but couldn’t get it to work for SAR orders, so I switch to using ExitLongStop and ExitLongLimit instead.

              I have two scenarios that I do not see a good solution to, based on what you posted from the help guide. Take a look and let me know how I should do handle them:

              Scenario 1
              *********
              I’m long, and am using SetStopLoss and SetProfitTarget. If the stop loss gets taken out, I want to immediately go short. How do I do this? I could not use SetStopLoss and instead use EnterShortStop, BUT it sounds like the entry order would still be ignored because of SetProfitTarget.

              Scenario 2
              *********
              I’m flat, and a triangle has formed on the chart. I want to have a stop limit order waiting to go long just above the top trend line and another waiting to go short just below the bottom trend line. How do I do this? Based on what you posted from the help guide, if I try to use EnterLongLimit and EnterShortLimit, the second order will be ignored. Please advise.

              Thanks!

              Comment


                #8
                Style,

                Scenario 1
                *********
                I’m long, and am using SetStopLoss and SetProfitTarget. If the stop loss gets taken out, I want to immediately go short. How do I do this? I could not use SetStopLoss and instead use EnterShortStop, BUT it sounds like the entry order would still be ignored because of SetProfitTarget.
                You could detect that a stop loss was filled in OnExecution, then immediately submit a short order. You would need to track that this stop loss was from a long position however, possibly using a flag or something.

                Possibly helpful references :

                Monitoring stop-loss / take-profit orders : http://www.ninjatrader.com/support/f...ead.php?t=5790

                Using OnOrderUpdate() and OnExecution() : http://www.ninjatrader.com/support/f...ead.php?t=7499

                Scenario 2
                *********
                I’m flat, and a triangle has formed on the chart. I want to have a stop limit order waiting to go long just above the top trend line and another waiting to go short just below the bottom trend line. How do I do this? Based on what you posted from the help guide, if I try to use EnterLongLimit and EnterShortLimit, the second order will be ignored. Please advise.
                This reference sample shows how to trade breakouts : http://www.ninjatrader.com/support/f...ead.php?t=3223

                Please let me know if I may assist further.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_AdamP View Post
                  This reference sample shows how to trade breakouts : http://www.ninjatrader.com/support/f...ead.php?t=3223
                  Adam,

                  I looked through the strategy SampleBreakoutStrategy, but it is a long only strategy. It uses EnterLongStop to have a stop order waiting to go long on a new high for the day. But it doesn't simultaneously have a stop order waiting to go short on a new low for the day. As such, it doesn't tell me how to resolve the problem presented by scenario 2.

                  It does not appear that I can use both EnterLongStop and EnterShortStop at the same time to enter on a breakout in either directions. From the help guide:

                  "Methods that generate orders to enter a position will be ignored if:
                  ...
                  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"

                  With that in mind, how can I play both sides of a break out?

                  Thanks!

                  Comment


                    #10
                    Style, there would be 2 ways to work this :

                    a) monitor price action / the breakout and then place only one order as dictated by it, before placing the new order needed CancelOrder the old one resting and wait for an orderstate change confirmation.

                    b) use the more flexible Unmanaged order submission approach in NT7, it would not be protected by any internal order handling rules, but just allow the direct submission of orders as you would code it in, including OCO'ed orders for entry / exit.

                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Bertrand,

                      This is very useful. Thank you!

                      Are there any sample strategies displaying the unmanaged approach?

                      I’m curious, how does the managed approach handle things like when an order doesn’t go through at the exchange, there is a loss of connectivity, etc? Where can I read about that?

                      Thanks again!

                      Comment


                        #12
                        Style,

                        Thanks for your note.

                        The main support items we have for this is this support forum, as well as the help guide. I am not aware of any reference samples but a quick search through our support forum will bring up threads on this topic.

                        Here are a few :

                        Support for the development of custom automated trading strategies using NinjaScript.






                        A more thorough search, as well as asking questions to the forum would be the best bet for finding out how to accomplish what you need.

                        Please let me know if I may assist further.
                        Adam P.NinjaTrader Customer Service

                        Comment


                          #13
                          I'm trying to make a go of this via the managed approach. I've went through the code, making sure that I don't have orders that will be ignored because of the order management rules detailed by Adam in the 6th post of this thread. However, I'm still seeing some cases where my stop is being ignored.

                          So far I've been using the functions ExitLongStop and ExitShortStop for stops. I listen for their execution via OnExecution, and when they are executed I submit my stop and reverse orders, both entry and exit:

                          protected override void OnExecution(IExecution execution)
                          {
                          if(longStopOrder != null && longStopOrder == execution.Order && shortOnReversal)
                          {
                          CancelOrder(longTargetOrder);
                          EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
                          shortStopOrder = ExitShortStop(shortStop, "short_entry");
                          shortTargetOrder = ExitShortLimit(shortPT, "short_entry");
                          }

                          if(shortStopOrder != null && shortStopOrder == execution.Order && longOnReversal)
                          {
                          CancelOrder(shortTargetOrder);
                          EnterLongStopLimit(longEntry, longEntry, "long_entry");
                          longStopOrder = ExitLongStop(longStop, "long_entry");
                          longTargetOrder = ExitLongLimit(longPT, "long_entry");
                          }

                          }
                          But the stop is some times ignored.

                          I could try using SetStopLoss and SetProfitTarget, but I'm not sure how to listen for their execution via OnExecution, since the set commands do nor return an iorder object.

                          Any ideas what I could try next? Thanks!
                          Attached Files

                          Comment


                            #14
                            Hello Style, you're likely seeing this as you just issue the CancelOrder() command but not waiting then for an orderstate change confirmation to acutally .Cancelled before placing the next order - thus you run into a race condition here in the event handling.

                            Correct, the Set()'s don't offer IOrder returns per default, but you could work with the contents of the sample below to capture them in OnOrderUpdate() -

                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              you're likely seeing this as you just issue the CancelOrder() command but not waiting then for an orderstate change confirmation to acutally .Cancelled before placing the next order - thus you run into a race condition here in the event handling.
                              Thanks Bertrand! What code would I use to wait for confirmation that the cancel had gone through?

                              Thanks again!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DanielSanMartin, Yesterday, 02:37 PM
                              2 responses
                              13 views
                              0 likes
                              Last Post DanielSanMartin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              4 responses
                              12 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by terofs, Today, 04:18 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by nandhumca, Today, 03:41 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post nandhumca  
                              Started by The_Sec, Today, 03:37 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Working...
                              X