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

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​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by JoMoon2024, Today, 06:56 AM
    0 responses
    6 views
    0 likes
    Last Post JoMoon2024  
    Started by Haiasi, 04-25-2024, 06:53 PM
    2 responses
    17 views
    0 likes
    Last Post Massinisa  
    Started by Creamers, Today, 05:32 AM
    0 responses
    5 views
    0 likes
    Last Post Creamers  
    Started by Segwin, 05-07-2018, 02:15 PM
    12 responses
    1,786 views
    0 likes
    Last Post Leafcutter  
    Started by poplagelu, Today, 05:00 AM
    0 responses
    3 views
    0 likes
    Last Post poplagelu  
    Working...
    X