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

Trail Stop

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

    Trail Stop

    Hi

    I'm trying to set-up a trial stop to the low of the last two bars minus 5 ticks (if long) or plus 5 ticks (if short). What would be the best way to achieve this?

    Many thanks

    Mark

    #2
    Hello Mark,

    Thank you for your post.

    For the low of the last two bars, you would first need to calculate this and store it in a double to call when placing the order.
    For example:
    Code:
    if(Low[1] < Low[2])
    double twoLow = Low[1];
    else
    double twoLow = Low[2];
    
    // or you can use the following
    
    double twoLow = Low[1] < Low[2] ? Low[1] : Low[2];
    Once you have this value stored you can place your SetTrailStop() in the OnBarUpdate() method so that it can be dynamically updated. You will also need to check the MarketPosition to see if it is Long or Short.
    For example:
    Code:
    protected override void OnBarUpdate()
    {
    if(Position.MarketPosition == MarketPosition.Long)
    {
        SetTrailStop(CalculationMode.Ticks, twoLow - (5*TickSize));
    }
    if(Position.MarketPosition == MarketPosition.Short)
    {
        SetTrailStop(CalculationMode.Ticks, twoLow + (5*TickSize));
    }
    Note: You should always reset the trail stop price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your trail stop order on your next open position

    For information on Position.MarketPosition please visit the following link: http://www.ninjatrader.com/support/h...etposition.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks Patrick

      Where does the twolow calculation go? As it will change with each bar does it go in the onbarupdate part as well?

      Comment


        #4
        Hello Mark,

        Thank you for your response.

        That is correct, the calculation for twoLow will go in the OnBarUpdate() method as well.

        Please let me know if I may be of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by andrewtrades, Today, 04:57 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        4 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        7 views
        0 likes
        Last Post FAQtrader  
        Started by rocketman7, Today, 09:41 AM
        5 responses
        19 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X