Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Addint Trailing Stop Example onOrderUpdate

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

    Addint Trailing Stop Example onOrderUpdate

    Hello, do we have an example of how to add trailing stop to onOrderUpdate?
    Ii ahve seen examples such as trailstopbuilderexample and others but they all have their trailing stop defined differently. Below is how my order gets manged and i was looking to add trailing stop option...

    Code:
    #region OnOrderUpdate
    
            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 (orderState == OrderState.Filled && order.Name == "BT")
                {
                    double ask = GetCurrentAsk();
                    double bid = GetCurrentBid();
    
                    string oco = Guid.NewGuid().ToString();
    
                    if (order.IsLong)
                    {
                        double tp = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice + ProfitTarget * TickSize);
    
                        if (ProfitTarget > 0 && tp.ApproxCompare(ask) > 0)
                        {
                            targetOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, filled, tp, 0, oco, "PT");
                        }
    
                        double sl = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice - StopLoss * TickSize);
    
                        if (StopLoss > 0 && sl.ApproxCompare(bid) < 0)
                        {
                            stopOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, filled, 0, sl, oco, "SL");
                        }
                    }
    
                    if (order.IsShort)
                    {
                        double tp = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice - ProfitTarget * TickSize);
    
                        if (ProfitTarget > 0 && tp.ApproxCompare(bid) < 0)
                        {
                            targetOrder = SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, filled, tp, 0, oco, "PT");
                        }
    
                        double sl = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice + StopLoss * TickSize);
    
                        if (StopLoss > 0 && sl.ApproxCompare(ask) > 0)
                        {
                            stopOrder = SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, filled, 0, sl, oco, "SL");
                        }
                    }
                }
              }
    
            #endregion​
    Attached Files

    #2
    Hello tkaboris,

    The trailbuilder sample would be a good starting point for the trailing logic, you would need to update the order from OnBarUpdate to make it trail at the frequency you wanted. The OnOrderUpdate event is really only useful to know the order was submitted or its status is changed, to submit a target you generally do that based on the orders Fill from OnExecutionUpdate.

    There is a sample unmanaged trailing sample in the following link that shows the correct way to submit the targets from OnExecutionUpdate and then later manage them from OnBarUpdate.




    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    88 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X