Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Autotrail in strategy

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

    Autotrail in strategy

    I have a strategy which must work in mode: calculate on each tick, I put an auto trail in this strategy so that once the price reaches +7 ticks in long for example, there is a trigger which is activated and the stop loss comes to a new position to protect the trade in the event of a reversal, the problem I have is that it works, but the new stop loss does not go to its new position as soon as the trigger is enabled but oscillating between old position and new position, I think it changes with each tick.

    Is there a possibility for the stoploss to remain fixed in its new position once the trigger is activated? although the strategy is on each tick? Thank you for your help it would help me a lot

    private void AdjustTrailingStop()
    {
    if (Position.MarketPosition != MarketPosition.Flat && !isStopLossAdjusted)
    {
    double entryPrice = Position.AveragePrice;
    double currentPrice = Close[0];
    double tickSize = TickSize;

    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (currentPrice >= entryPrice + TriggerTicks * tickSize)
    {
    double newStopPrice = entryPrice + TrailingTicks * tickSize; // TrailingTicks ticks above entry
    SetStopLoss(CalculationMode.Price, newStopPrice);
    isStopLossAdjusted = true;
    }
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    if (currentPrice <= entryPrice - TriggerTicks * tickSize)
    {
    double newStopPrice = entryPrice - TrailingTicks * tickSize; // TrailingTicks ticks below entry
    SetStopLoss(CalculationMode.Price, newStopPrice);
    isStopLossAdjusted = true;
    }

    }
    }​​

    #2
    Hello snoussi,

    Thanks for your post.

    You should make sure to reset the stop loss price / offset value when you are in a flat position if you are dynamically calling the SetStopLoss() method in OnBarUpdate() as noted in the SetStopLoss() help guide page.

    From the help guide: "Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position"

    SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm

    Here is a reference sample demonstrating resetting the stop loss price / offset when you are flat and dynamically calling Set methods in OnBarUpdate(): https://ninjatrader.com/support/help..._lo.htm​
    <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


      #3
      Hello,

      Thank you again for your response and your help, I tried what you suggested and I still have the same problem, I provided you with a video to see the problem live.
      The simple, quick and secure way to send your files around the world without an account. Share your files, photos, and videos today for free.


      In my case, I set a Trigger at +4 ticks of gain, as soon as this trigger is reached the stop loss is supposed to go to a new position, but as you see in the video, the stop loss continues to move and does not does not remain fixed in its new position, here is the new code that I put in place, thank you for your help

      if (BarsInProgress == 0 && CurrentBars[BarsInProgress] > 0)
      {
      AdjustTrailingStop();
      ResetStopLossOnFlat();
      }
      }

      private void AdjustTrailingStop()
      {
      if (Position.MarketPosition != MarketPosition.Flat && !isStopLossAdjusted)
      {
      double entryPrice = Position.AveragePrice;
      double currentPrice = Close[0];
      double tickSize = TickSize;

      if (Position.MarketPosition == MarketPosition.Long)
      {
      if (currentPrice >= entryPrice + TriggerTicks * tickSize)
      {
      double newStopPrice = entryPrice + TrailingTicks * tickSize; // TrailingTicks ticks above entry
      SetStopLoss(CalculationMode.Price, newStopPrice);
      isStopLossAdjusted = true;
      }
      }
      else if (Position.MarketPosition == MarketPosition.Short)
      {
      if (currentPrice <= entryPrice - TriggerTicks * tickSize)
      {
      double newStopPrice = entryPrice - TrailingTicks * tickSize; // TrailingTicks ticks below entry
      SetStopLoss(CalculationMode.Price, newStopPrice);
      isStopLossAdjusted = true;
      }

      }
      }
      }

      private void ResetStopLossOnFlat()
      {
      if (Position.MarketPosition == MarketPosition.Flat && isStopLossAdjusted)
      {
      SetStopLoss(CalculationMode.Ticks, StopLossTicks); // Réinitialisation du stop loss à la valeur par défaut
      isStopLossAdjusted = false;
      }
      }
      }
      }​​

      Comment


        #4
        Hello snoussi,

        Thanks for your notes.

        It could be that SetStopLoss() is being called with the initial price that you submitted the order to.

        To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Also, one line above where you are submitting the stop loss order, print the price you are submitting the stop loss to. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121

        Further, my colleague Chelsea has created educational examples of strategy builder breakeven and trailing stop in the strategy builder here that you could view:
        https://ninjatrader.com/support/forum/forum/suggestions-and-feedback/suggestions-and-feedback-
        aa/103992-request-breakeven-functions-in-strategy-builder#post806596​​​
        <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 NullPointStrategies, Today, 05:17 AM
        0 responses
        25 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        120 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        63 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X