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

Trailing Stop Not Fixed

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

    Trailing Stop Not Fixed

    Hi,

    Please find the trailing stop code I've been testing below. The code works fine expect that the trailing stop doesn't stay fixed. I can see that as the price changes the code will adjust the trailing stoploss but I'm not sure how to fix the stoploss so that as a limit is hit the stop moves and stays fixed until the next target is hit.

    I'm not a programmer, I know the issue just not sure how to fix it so the code behaves this way.

    Any help would be appreciated.

    // Resets stoploss to original value when all positions closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 10);
    }
    // If a short position is open all for stoploss modification
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    //Prices hit 5 ticks setstop loss to breakeven
    if (Low[0] <= Position.AveragePrice + -10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -2 * TickSize);
    }
    //Prices hit 8 ticks setstop loss to 2 ticks
    if (Low[0] <= Position.AveragePrice + -16 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -4 * TickSize);
    }


    Thanks.

    #2
    Hello Frankriso,

    Thanks for your post.

    What you would need to do is use a bool variable that can be set either true or false to help control.

    For example, if you create a bool named DoItOnce and set it to true initially, in your first 5 tick condition you would check if the bool DoItOnce is true as well as the existing price check. In the action section you would then set the bool DoItOnce false. This would prevent the condition from being used again until the bool is once again set true.

    You can reset the bool to true in the actions of the condition where you are checking for a flat position.

    For example:

    // Resets stoploss to original value when all positions closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 10);
    DoItOnce = true; // reset bool when flat
    }
    // If a short position is open all for stoploss modification
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    //Prices hit 5 ticks setstop loss to breakeven
    if (Low[0] <= Position.AveragePrice + -10 * TickSize && DoItOnce)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -2 * TickSize);
    DoItOnce = false; // prevent setting more than once
    }
    //Prices hit 8 ticks setstop loss to 2 ticks
    if (Low[0] <= Position.AveragePrice + -16 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -4 * TickSize);
    }


    Note: you will want to create the bool above the OnStateChange() method, for example:

    private bool DoItOnce = true; // create and initialize
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul has saved me hours trying to figure that one out.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Brevo, Today, 01:45 AM
      0 responses
      3 views
      0 likes
      Last Post Brevo
      by Brevo
       
      Started by aussugardefender, Today, 01:07 AM
      0 responses
      3 views
      0 likes
      Last Post aussugardefender  
      Started by pvincent, 06-23-2022, 12:53 PM
      14 responses
      239 views
      0 likes
      Last Post Nyman
      by Nyman
       
      Started by TraderG23, 12-08-2023, 07:56 AM
      9 responses
      384 views
      1 like
      Last Post Gavini
      by Gavini
       
      Started by oviejo, Today, 12:28 AM
      0 responses
      6 views
      0 likes
      Last Post oviejo
      by oviejo
       
      Working...
      X