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 argusthome, 03-08-2026, 10:06 AM
      0 responses
      111 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      59 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      38 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      42 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      78 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X