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 NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 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