Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting error when I place a sell order with auto trail loss

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

    Getting error when I place a sell order with auto trail loss

    Hello,
    i am creating a strategy that receives a signal from HalfTrind indicator to buy or sell
    when i receive a signal, i check if there is any other position is open, then i close it and open the new one.

    currently, when i try to open a new order, i got this error message
    Buy stop or buy stop limit orders can't be placed below the market. affected order: BuyToCover 1 stopMarket @1234.00

    i debug the code, and this was the value when i get the erro
    Short High[0]: 4138.25 currentATR: 3.0280371924987 newStop: 4141.2780371925 _stop: 4141.2780371925

    Code:
                                    if (Position.MarketPosition == MarketPosition.Flat)
                                    {
                                        EnterShort("ShortOrder");
                                        var newStop = High[0] + currentATR;
                                        _shortStop = _shortStop == 0 ? newStop : _shortStop;
                                        _shortStop = Math.Min(_shortStop, newStop);
                                        Print("3 " + ShortOrder + " High[0]: " + High[0] + "  currentATR: " + currentATR + "   newStop: " + newStop + "   _stop: " + _shortStop);
                                        ExitShortStopMarket(_shortStop, "Trail Short", ShortOrder);
                                        //SetStopLoss(CalculationMode.Price, _shortStop);
                                    }
                                    else if (Position.MarketPosition == MarketPosition.Long)
                                    {
                                        ExitLong("LongOrder");
                                        if (Low[0] < Position.AveragePrice)
                                        {
                                            EnterShort("ShortOrder");
                                            var newStop = High[0] + currentATR;
                                            _shortStop = _shortStop == 0 ? newStop : _shortStop;
                                            _shortStop = Math.Min(_shortStop, newStop);
                                            Print("4 " + ShortOrder + " High[0]: " + High[0] + "  currentATR: " + currentATR + "   newStop: " + newStop + "   _stop: " + _shortStop);
                                            ExitShortStopMarket(_shortStop, "Trail Short", ShortOrder);
                                            //SetStopLoss(CalculationMode.Price, _shortStop);
                                        }
                                    }​
    does anyone know what is the problem in my code?

    Thanks

    #2
    If your stop market order is across the market, you should use a ExitLongMarket or ExitShortMarket instead, so the stop does not reject.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello Algo Trading,

      Thanks for your post.

      This error message "Buy stop or buy stop limit orders can't be placed below the market" indicates that your strategy is submitting an order to the wrong side of the market.

      Buy stop orders must be placed above the current ask price. Sell stop orders must be placed below the current bid price.

      You should add prints to the strategy to determine why the stop order is being ignored. One line above where the stop order is placed, print the GetCurrentAsk()/GetCurrentBid() and print the price being submitted to the order method.

      Note if this is due to market volatility then there isn't really a way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

      Something you could consider is using GetCurrentBid() and GetCurrentAsk() to offset orders so that they are more likely to land on the correct side of the market.

      See these help guide pages for more information.
      GetCurrentBid(): https://ninjatrader.com/support/help...currentbid.htm
      GetCurrentAsk(): https://ninjatrader.com/support/help...currentask.htm

      You could use RealtimeErrorHandling.IgnoreAllErrors to determine the behavior of a strategy when the strategy places an order that is returned "Rejected" or "UnableToChangeOrder". The default behavior is to stop the strategy, cancel any remaining working orders, and then close any open positions.

      Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order within OnOrderUpdate() followed by defining your own order rejection handling behavior for the rejected order.

      Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping a rejected order in OnOrderUpdate().

      RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm
      Last edited by NinjaTrader_BrandonH; 05-21-2023, 05:15 PM.
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X