Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error: Sell order stop price must be below last trade price

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

    Error: Sell order stop price must be below last trade price

    Hi,

    I have another issue with the strategy

    When the strategy take a trade, these two messages show me at the same time. I dont understand why. This never happen in Backtesting, Trade Performance or even at Playback (thats wear)

    "Sell order stop price must be below last trade price [Order parameters are invalid] affected Order: Sell 1 StopMarket @ 21668,5​"
    "Strategy/348278449' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself."
    How can I resolve it? is there anything wrong in the code or just adding a extra and simple condition should work properly?

    I was thinking something like this in Long Trades

    Code:
    if (stopLoss < Close[0])
    stopOrder = ExitLongStopMarket(0, true, entryOrder.Quantity, stopLoss, "StopLoss", "LongEntry");
    
    if (takeProfit > Close[0])
    takeOrder = ExitLongLimit(0, true, 1, takeProfit, "TakeProfit", "LongEntry");​
    I am not sure if this is gonna work, or what is another better option


    Note: When I take the SAME trade at Playback, it takes how it suppose to be taking, but in "Real" I got thats errors

    Thanks for your help

    This is the code, I will show you the code in a simple way

    Variables
    Code:
    private double stopLoss;
    private double entryPrice;
    private double takeProfitPrice;
    
    private Order stopOrder = null;
    private Order entryOrder = null;
    private Order takeOrder = null;​
    OnBarUpdate
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    stopLoss = indicator.TriggerUp - (Value / tickValue) * TickSize;
    }
    
    if (Position.MarketPosition == MarketPosition.Flat && BullishTrigger)
    {
    entryOrder = EnterLong(Convert.ToInt32(1), "LongEntry");
    }
    
    if (Position.MarketPosition == MarketPosition.Long)
    {
    stopOrder = ExitLongStopMarket(0, true, entryOrder.Quantity, stopLoss, "StopLoss", "LongEntry");
    takeOrder = ExitLongLimit(0, true, 1, takeProfitPrice, "TakeProfit", "LongEntry");
    }​​
    OnOrderUpdate
    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (order.Name.Contains("Entry"))
    {
    entryOrder = order;
    
    if (order.OrderState == OrderState.Cancelled)
    {
    entryOrder = null;
    }
    }
    
    if (order.Name == "StopLoss")
    {
    stopOrder = order;
    
    if (order.OrderState == OrderState.Cancelled)
    {
    stopOrder = null;
    }
    }
    
    if (order.Name == "TakeProfit")
    {
    takeOrder = order;
    
    if (order.OrderState == OrderState.Cancelled)
    {
    takeOrder = null;
    }
    
    if (order.OrderState == OrderState.Filled)
    {
    targetFilled = true;
    }
    }
    }
    Last edited by FaaZer; 02-01-2025, 10:58 AM.

    #2
    Hello. I don't see it anywhere, unless I missed it, but how you are setting your stopLoss variable? Perhaps adding some Print statements with the Close[0], as well as for the stopLoss level would help clear up what is happening. I'm assuming, because you are mentioning that this only happens in realtime, vs backtesting, that what is happening is when trying to take a trade, your stoploss is higher than the entry point of the trade. This was common with my strategy when I was trying to move up my stoploss, but I coded it wrong. That might not even be the case with you.

    If you could perhaps add some Print statements, perhaps even turning TraceOrders on and showing the output when you get this error, it might help.

    Comment


      #3
      Hello FaaZer,

      This error may appear when submitting a stop order if the stop price is ahead of the market.

      For a buy stop order, such as when exiting a short position or entering a long position, the stop price must be above the current ask price.
      For a sell stop order, such as when exiting a long position or entering a short position, the stop price must be below the current bid price.

      Ensure the stop price is a valid price before submitting the order.

      Below is a link to a forum post with sample code.


      Also, assign order objects to variables from the order parameter of OnOrderUpdate() only.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      39 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      64 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X