Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Place Trailing Stop

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

    Place Trailing Stop

    Hi, I'm trying to place a Traling Stop on my strategy. And I was wondering on how can I implement it.

    Currently I have my OrderExecutionUpdate function this way:

    Code:
            
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                if (entryOrder != null && entryOrder == execution.Order)
                {
                    // Check if the entry order is filled
                    if (execution.Order.OrderState == OrderState.Filled)
                    {
                        double takeProfitPrice = 0;
                        // Submit stop loss and take profit orders
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
                            takeProfitPrice = Position.AveragePrice + (Position.AveragePrice - SL)*takeProfitFactor; // 1:1 ratio
                            stopOrder = ExitLongStopMarket(0, true, execution.Quantity, SL, "StopLoss", "EntryOrder");
                            targetOrder = ExitLongLimit(0, true, execution.Quantity, takeProfitPrice, "TakeProfit", "EntryOrder");
                        }
                        else if (Position.MarketPosition == MarketPosition.Short)
                        {
                            takeProfitPrice = Position.AveragePrice - (SL - Position.AveragePrice)*takeProfitFactor; // 1:1 ratio
                            stopOrder = ExitShortStopMarket(0, true, execution.Quantity, SL, "StopLoss", "EntryOrder");
                            targetOrder = ExitShortLimit(0, true, execution.Quantity, takeProfitPrice, "TakeProfit", "EntryOrder");
                        }
    
                        entryOrder = null;
                    }
                }
            }​
    Any guess on how can I do it?

    #2
    Hello speedytrade02,

    Thanks for your post.

    You would need to call the Exit order methods again with a new price to change the orders to the new price.

    You could view the TrailBuilderExample_NT8 sample strategy on the forum thread linked below as a good starting point to see how to implement a trailing stop using Exit methods. Similar logic could be implemented into your script to include a trailing stop.
    https://ninjatrader.com/support/foru...der#post806596

    There is a trailing stop exit orders sample in the following link that shows the correct way to submit the targets from OnExecutionUpdate and then later manage them from OnBarUpdate.
    https://forum.ninjatrader.com/forum/ninjatrader-8/platform-technical-support-aa/97419-priceos-cs-expecting?p=802269#post802269​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    43 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    30 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    47 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    38 views
    0 likes
    Last Post CarlTrading  
    Working...
    X