Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Timing issues for bracket orders submission

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

    Timing issues for bracket orders submission

    Hello! I have some issues implementing my bracket orders strategy. When I submit an market order (Buy or Sell) the bracket orders (target profit and stop loss) should be send too according to this piece of code :

    Code:
    string[] instruments = {"ES"}
    
    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled,
        double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
        ...
        else if (order.Name == "EnterLong_" + instruments[0] || order.Name == "EnterShort_" + instruments[0])
        {
            if (orderState == OrderState.Filled)
            {
                SendBracket_Orders(order.Name);
            }
        }
        ...
      
    }
    private void SendBracket_Orders(string orderName)
    {
        Print("sending bracket orders...");
        string ocoString = string.Format("unmanagedexitlongoco{0}-{1}", DateTime.Now.ToString("hhmmssffff"), instruments[0]);
        if (orderName == "EnterLong_" + instruments[0])
        {
            double profitTargetPrice = PositionAccount.AveragePrice + tpInTicks * TickSize;
            double stopLossPrice = PositionAccount.AveragePrice - slInTicks * TickSize;
            SubmitOrderUnmanaged(BarsInProgress, OrderAction.Sell, OrderType.MIT, quantities[0], 0, profitTargetPrice, ocoString, "profit_target_long");
            SubmitOrderUnmanaged(BarsInProgress, OrderAction.Sell, OrderType.StopMarket, quantities[0], 0, stopLossPrice, ocoString, "stop_loss_long");
        }
        else if (orderName == "EnterShort_" + instruments[0])
        {
            double profitTargetPrice = PositionAccount.AveragePrice - tpInTicks * TickSize;
            double stopLossPrice = PositionAccount.AveragePrice + slInTicks * TickSize;
            SubmitOrderUnmanaged(BarsInProgress, OrderAction.Buy, OrderType.MIT, quantities[0], 0, profitTargetPrice, ocoString, "profit_target_short");
            SubmitOrderUnmanaged(BarsInProgress, OrderAction.Buy, OrderType.StopMarket, quantities[0], 0, stopLossPrice, ocoString, "stop_loss_short");
        }
        else
        {
            Print("Can't submit bracket orders for flat position");
            return;
        }
    }​
    However, that is not always the case in reality. Sometimes the market order is filled but the position is not yet open and therefore the PositionAccount.AveragePrice variable seems empty which leads to an incorrect calculation of the stop loss price and target profit price values ​​when entering the "SendBracket_Orders" method. Maybe I could implement a while loop in that method that would wait for the position to be open or maybe there is a better solution to this problem ?
    Last edited by Tessan; 03-14-2025, 12:30 PM.

    #2
    Hello Tessan,

    Thank you for your post.

    We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate() which ensures your strategy has received the execution which is used for internal signal tracking. By doing so, you can avoid having your strategy submit stops/targets before the entry is working.

    The post linked below has a strategy (UnmanagedTemplate_December2020.zip) that demonstrates how to trigger stop/target order through OnExecution() while using the unmanaged approach for orders:



    Please let me know if I may be of any further assistance.

    Comment


      #3
      Tanks Eduardo, it actually help me! I wasn't aware of OnExecution() method.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      87 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      132 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      65 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      118 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      67 views
      0 likes
      Last Post PaulMohn  
      Working...
      X