Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.




    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by burtoninlondon, Today, 12:38 AM
    0 responses
    10 views
    0 likes
    Last Post burtoninlondon  
    Started by AaronKoRn, Yesterday, 09:49 PM
    0 responses
    14 views
    0 likes
    Last Post AaronKoRn  
    Started by carnitron, Yesterday, 08:42 PM
    0 responses
    11 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Yesterday, 07:51 PM
    0 responses
    14 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,983 views
    3 likes
    Last Post jhudas88  
    Working...
    X