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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    599 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    344 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    103 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    558 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    557 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X