Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop and Reverse Problem

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

    Stop and Reverse Problem

    Hi guys

    My strategy currently implements a stop and reverse in the following manner, if my stop loss is hit then the order automatically turns around and goes short for the same number of contracts. I have tried to achieve this with the following code but I seem to be running into some NT internal order handling rules as my S&R is implemented as a short stop at the time the long stop is triggered. Is there some other way to do this?

    Attached code:

    //////////////////////////

    protected override void OnExecution(IExecution execution)
    {


    //LongOrder Execution
    if (LongOrder != null && LongOrder.Token == execution.Order.Token)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {

    LongOrderStop = ExitLongStop(0, true, execution.Order.Filled, LongStopPrice, "LOSS", "BR2");

    LongOrderTarget = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 45 * TickSize, "PROFIT", "BR2");

    if (LSAR == 'Y')
    {
    LongOrderSAR = EnterShortStop(0, true, NOC, LongStopPrice, "BRSAR1");
    }

    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    LongOrder = null;
    }
    }
    }


    /////////////////////////

    Thanks for any help

    Regards
    Ross

    #2
    You cannot have simultaneous working orders for entry and exit in opposite directions. When your stop is hit you will need to cancel the exit orders you have still working.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh,

      I understand the limitations with the order handling rules, I was wondering if you knew of a way to get round this as a stop and reverse is not an uncommon element in a strategy?

      I have reset the exit orders to null if the stop is executed, i just didn't copy and paste it as i didn't feel it was relevant.

      Just to clarify am I right in thinking that it is the profit target on my original long order and the sell stop that I'm attempting to use to stop and reverse that are conflicting? This doesn't make a great deal of sense to me because the profit target order would render my position flat rather than net long.

      Is it possible to change my Exit stop to be double what the original long was so that it will set the position net short after the stop is hit. Not sure if this will work on an Exitstop.

      Any help appreciated.

      Thanks
      Ross

      Comment


        #4
        The workaround is you know exactly when you are trying to reverse so cancel your exit orders first and then do the reversal.

        The conflict is with ExitLongStop/Limit() and EnterShortStop(). You cannot have both working at the same time.

        No, you cannot use double the quantity on an exit order. Orders need to be explicitly marked as Sell and SellShort.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I see, sorry, so that's what you meant by cancelling the exit orders.

          Problem being that I wanted to go short at the exact point that my original stop was set and I want this preplaced with the broker at the point that my original long is triggered.

          Am I right in thinking that what you are suggesting is monitoring for the execution of my ExitStop, cancel my ExitLongLimit and then execute a market order to go short?

          Cheers for the help
          Ross

          Comment


            #6
            No. There is no problem having ExitLongStop and ExitLongLimit simultaneously. The problem is having these WHILE also having EnterShort. You can have the exits together. You cannot pair them with an EnterShort.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              "The conflict is with ExitLongStop/Limit() and EnterShortStop(). You cannot have both working at the same time."

              Yes I understand this, that is why I'm saying that what you are suggesting seems to be only possible through a market order.

              Maybe if I give an example it will be better. So if my position is such.

              Exit Long Limit 126.80
              Entry 126.50
              Exit Long Stop 126.20

              So lets say my entry is hit at 126.50, my execution event immediately runs and places my Exit long limit at 126.80 and Exit Long Stop at 126.20. It was at this point that I wanted to add a further Short Stop at 126.20 so that if 126.20 is hit rather than going flat I would go net short for 1 contract therefore reversing my position.

              You are saying that I should monitor for the stop to be hit and then cancel the Exit Orders. All good, except for the problem that price is now at 126.20, I can cancel my Exit Long Limit obviously since it wasn't hit, Exit Long stop is filled so I just set the instance to null but the problem is that price is already at 1.20, I wanted it to enter a short stop here but we are already here so the only way I can see to achieve this is to now immediately place a market order to go short at the current price.

              Have I missed something? Sorry if I'm misunderstanding.

              Thanks
              Ross

              Comment


                #8
                Ross,

                Not possible. You cannot have Exit()s and Enter()s in opposite direction simultaneously. You will have to cancel one or the other. If you just want the order already sitting there, cancel the prior earlier. You know what the price is. If its closer to the exit long limit, leave that in place. If its closer to the 126.20, cancel the limit and place the EnterShortStop.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Ok no problem, I will work around this, it seems these internal rules are causing me more of a headache than anything else.

                  Thanks you for your help, I must say in my experience so far Ninja support does a sterling job, always answering queries really quickly and making a real effort to resolve issues. Thanks.

                  Cheers
                  Ross

                  Comment


                    #10
                    Ross,

                    Some info: The rules are in place to prevent overfills. As you may have noted that if you were long and you called EnterShort() it in fact closes your long position first and then gets you your reversal into a short position. This is why you cannot have ExitLongLimit/Stop() working in conjunction with EnterShortLimit/Stop(). If for whatever reason both got filled at the same time. The EnterShortLimit/Stop() would try and close a long position that the ExitLongLimit/Stop() was trying to close too. You essentially would end up twice in the short.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      I'm still not sure I understand, I imagine that the EnterShortLimit/Stop() does a check to see if we are currently long before flattening the position and going short, if the ExitLong has already executed then the position is already flat and so EnterShortLimit/Stop() should just go short rather than flatten first.

                      I'm pretty new to this so maybe in months to come I will be able to fully appreciate the complexity of this problem and why you guys have put in the rules, I'm sure there is good reason, in the mean time I will look to use market orders where I can.

                      Ross

                      Comment


                        #12
                        No. When you have both in the market it is very possible to get caught up with inflight executions where both get filled before your brokerage is able to notify you. This is why you cannot have Exits and Entries working in opposite directions.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Just for interests sake I've had a go this morning at resolving this with the market order method and this workaround is not something that is feasible. I imagine in live it will run correctly but you cannot do any backward analysis, primarily because when running back test over historical data there is no tick data to deal with, so by default the market order is placed at the following bars open rather than the stop price you wanted, I'm guessing on historical OnBarClose is always set to "TRUE" due to there being no tick data, this effectively renders your strategy analysis over historical data null and void.

                          I will have a go at trying to use limits and toggle back and fourth between the two based on which one the price action is closest to.

                          Comment


                            #14
                            Having just thought about the second idea it seems this to will fall foul of the tick data problem during historical analysis because onBarClose is True on backtest it means that I would be analysing the price action after the event which again would mean the limit order toggle calculation would be completely incorrect, price may have already breached my stop by the time the the onBarUpdate logic is fired to actually check where the price action currently is.

                            Comment


                              #15
                              You are correct in your assumption. Without intrabar granularity you cannot trade on your signal bar because the signal you generated was generated at a point in time when you already have the close of a bar. Having the close of a bar means there is no tradeable location on that bar and any order you place is placed at the open of the next bar.

                              If you wish to try to add some granularity into backtesting please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=6652
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              670 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              379 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              582 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X