Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SL and TP Orders Not Executed at the Same Time as Entry

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

    SL and TP Orders Not Executed at the Same Time as Entry

    Hi everyone,

    I have a issue with strategy.

    When the trade is placed (in this example, a Long trade), only the entry order appears initially, but the SL and TP orders do not. After the 5-minute bar closes, the SL and TP orders are finally placed. However, I cannot afford to be without a stop loss for those 5 minutes.
    So what I did next was place the SL and TP orders together with entryOrder = EnterLong. They do appear immediately, but after the 5-minute bar closes, the SL and TP orders disappear, leaving only the entry order​

    What is wrong? How can I resolve it? Is it okay if I use 2 times the order SL and TP, one with entryorder (MarketPosition Flat) and other in MarketPosition.Long?

    Note 1: The strategy must be used with Calculate.OnBarClose and for this example we are gonna use timeframe 5 minutes, Long trade

    Note 2: When I take the SAME trade at Playback, it takes SL and TP order at the same time Entry order, even when I use Calculate.OnBarClose. Exacly same trade, same instrument, same time. But in "Real" SL and TP doesnt match with entry order


    Thanks for 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:57 AM.

    #2
    Hello FaaZer,

    Submit the exit orders from OnExecutionUpdate() when the entry fills.

    Assign orders to variables from the order parameter of OnOrderUpdate() only.

    Below is a link to an example.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Oh I see where is the problem

      Thank you!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      81 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      64 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      66 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X