Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss after order is missing

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

    SetStopLoss after order is missing

    Hello,

    If I have a strategy that for some reason (internet glitch, broker rejection) is missing a stop loss, and submit a SetStopLoss again (after it had one), the position is left without a stop. Shouldn't SetStopLoss guarantee that the position always has the proper stop?

    Regards
    Ulises
    ulisesguerrero
    NinjaTrader Ecosystem Vendor - Thrifty Coders

    #2
    Hello Ulises,

    This would indicate likely improper code. For example using an invalid price, trying to call a set method after an entry method and not before, etc.

    TraceOrders and prints would tell you why the order is or is not submitted.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      This happens a lot when there are order rejections, or if the user accidentally closes the stop order from the trading window. When SetStopLoss is called again, it won't set a stop , and leave the position naked.

      Any way to fix this?

      Regards
      Ulises
      ulisesguerrero
      NinjaTrader Ecosystem Vendor - Thrifty Coders

      Comment


        #4
        Hello Ulises,

        Use the RealtimeErrorHandling.StopCancelCloseIgnoreRejects , detect the order is cancelled or rejected in OnOrderUpdate(), submit a new order. Be careful doing this, that you do not cause an endless loop.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          Yes, that was I was trying to do, but the SetStopLoss does nothing, doesn't submit a new order. Leaves the position naked, or with the wrong number of contracts.

          Regards
          Ulises
          ulisesguerrero
          NinjaTrader Ecosystem Vendor - Thrifty Coders

          Comment


            #6
            Hello Ulises,

            TraceOrders and prints would tell you why the order is or is not submitted.

            Enable TraceOrders, print the conditions where the order is being submitted, print the order object in OnOrderUpdate().

            Provide the output saved to a text file.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              Here's a screenshot that shows the result of trace and popup message of order being rejected.

              SetStopLoss is called on every bar close, but once the order is rejected, the SetStopLoss does nothing, and the position remains naked without a stop.
              (If IgnoreRejects is set).

              So, what should I do in order for SetStopLoss guarantees it always protects the position properly?

              I can post the example code if you prefer.

              Click image for larger version

Name:	Order Rejected.png
Views:	145
Size:	1.58 MB
ID:	1213536
              Regards
              Ulises
              ulisesguerrero
              NinjaTrader Ecosystem Vendor - Thrifty Coders

              Comment


                #8
                Hello Ulises,

                Then the order is being submitted.

                I would recommend you correct the code and stop placing orders that are getting rejected.

                Submitting orders over and over that are all getting rejected is not the answer. This is just an endless loop.

                Submit the order below the current bid price so the order is not rejected.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chelsea,

                  I always set it below and check the Bid/Ask... Unfortunately the market moves fast, and it always ends up getting rejections. Specially with Rithmic brokerage, in which there are continuous glitches in which orders hang and fills take longer than normal.

                  So, customers end up with unwanted / stopless positions which are very detrimental to the trading account.

                  So, if calling SetStopLoss won't make sure a stop loss is set, the only solution is unmanaged orders?

                  Regards,
                  Ulises
                  ulisesguerrero
                  NinjaTrader Ecosystem Vendor - Thrifty Coders

                  Comment


                    #10
                    Hello Ulises,

                    Move the order further below the bid or use a market order instead to immediately exit.

                    If you pick up the phone and call your broker and tell your broker to place a sell stop order above the current bid, they won't be able to do it. The exchange will not accept a sell stop above the bid.
                    It doesn't matter if you use unmanaged. If you place a sell stop above the bid using the unmanaged approach, your order will be rejected and the strategy will be disabled. If you force it to keep running and resubmit another invalid order, you've just created an endless loop.

                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Chelsea,

                      That is not the case, I always send orders below the bid for a buy stop. The customer is not looking to exit, but simply a trailing stop. The new stop price sometimes is rejected, but that doesn't mean the position must be exited.

                      Even at "conservative" stop prices, a single news event will almost always end up with unwanted naked positions due to the nature of the market (if StopCancelIgnoreRejections is used), or strategy always end up being closed (undesirable).

                      Is there a way to check / guarantee that there is a stop placed? As of now, SetStopLoss is useless once a naked position is left after a news event. I always end up with unwanted naked positions. SetStopLoss is certainly not working in those cases, as there is no stop and the position ends up with unlimited risk.

                      Regards
                      ​​​​​​​Ulises
                      ulisesguerrero
                      NinjaTrader Ecosystem Vendor - Thrifty Coders

                      Comment


                        #12
                        Hello Ulises,

                        If the order is rejected then it is on the wrong side of the market. Meaning the price is already hit. It wouldn't trail anyway if it wasn't rejected, it would fill. So just submit a market order.

                        Otherwise move the order further away.

                        A way to 'guarantee that there is a stop placed'? Not really. Put the stop really really really far away like 20 points (not ticks) and it would be extremely unlikely the market will dip that quick. (There are protections in place that will halt the exchange if the market moves that fast)

                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello Chelsea,

                          There's several other instances where this causes an issue. For example:

                          1) If in a live Long position, with a stop loss 20 ticks away.
                          2) Send a signal to exit, with ExitLong()
                          3) Market news event comes, and while the broker is confirming the ExitLong(), the stop order is triggered, created an unwanted position
                          4) No stop is set for the unwanted position.

                          So, I'm trying to find a way to at least ensure that even in those situations with overfills, etc, at least there is a stop set for the position and not have an infinite risk situation.
                          ulisesguerrero
                          NinjaTrader Ecosystem Vendor - Thrifty Coders

                          Comment


                            #14
                            Hello ulisesguerrero,

                            Welcome to the reality of automated trading programming (or what have you). I have run into that or similar scenarios several times. It is VERY frustrating. I wish there was an order type that would automatically convert to a market order if past the limit, but, that doesn't exist.

                            So, I manage my own entries, stop losses and target prices locally. If the price is >= or <= as appropriate, then I submit a market order, guaranteeing a fill. Frankly, I don't think there's any other way around that if you're placing orders that are nearby current price.

                            There are two clear drawbacks to this approach. First is slippage, though sometimes, you might get a more favorable entry or exit price. The other drawback is there is no order with the broker. For stop losses, I suppose you could submit a fail safe StopLoss that is significantly farther away and manage that appropriately. The advantage is that you're guaranteed to get in or out of the market. In most cases, there's a enough liquidity that this approach is viable.

                            To Chelsea's point, though, if an order is rejected because it has already moved beyond your price, then I would think you would want to exit immediately via market order rather than risk another rejected StopLoss order attempt and, thusly, potentially losing more value to the market.

                            Not sure it does, but I hope this helps! I wish you well!
                            Kind regards,
                            Matt

                            Comment


                              #15
                              Hello ulisesguerrero,

                              1) If in a live Long position, with a stop loss 20 ticks away.
                              2) Send a signal to exit, with ExitLong()
                              3) Market news event comes, and while the broker is confirming the ExitLong(), the stop order is triggered, created an unwanted position
                              4) No stop is set for the unwanted position.
                              This is known as an inflight execution.
                              https://ninjatrader.com/support/help...ightExecutions

                              To prevent this, cancel the first order, wait until it is cancelled in OnOrderUpdate() and then submit the replacement order. This prevents any in-flight-executions.
                              (Your order may fill before it can be cancelled, but then that would just be what handles the position)

                              A heads up, Set methods (like SetStopLoss()) cannot be cancelled. So you would need to use ExitLongStopMarket()/ExitShortStopMarket() or the unmanaged approach with SubmitOrderUnmanaged().
                              Last edited by NinjaTrader_ChelseaB; 08-30-2022, 07:32 AM.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              52 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              70 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              48 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X