Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

2 step stop price modification

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

    2 step stop price modification

    Hi,
    I've been working with the sample price modification (break-even) strategy. I'm trying to add a second stop price mod step/stage as price advances, but if/when price regresses below the second step's price threshold, the stop, instead of staying at the second price, goes back to the original break even price. Can anyone show me how to add a second step to the sample price mod. strategy, where the strategy ignores the break-even statement after we've achieved the second stage? Am I making sense?

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 120);
    }

    // If a long position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
    if (Close[0] > Position.AveragePrice + 120 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }

    //Here's the second stage I'm trying to add

    //if (Close[0] > Position.AveragePrice + 240 * TickSize)
    //{
    // SetStopLoss(CalculationMode.Price, Position.AveragePrice + 120 * TickSize);
    //}


    }


    // Set 1
    if (

    (Slope(SMA(21), 10, 0) > 0)
    && (Times[0][0].TimeOfDay > new TimeSpan(9, 30, 0))
    && (Times[0][0].TimeOfDay < new TimeSpan(12, 30, 0))
    )
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    }
    }
    }
    thanks,
    David
    Last edited by trader3000a; 12-15-2021, 04:21 AM.

    #2
    Hello trader3000a,

    Thanks for your post.

    You could consider using a bool to control your strategy's logic for placing or changing the price of an order.

    A bool (initially set to false) would be created in your script. You would check if you are in a Flat position and set the bool to false. This way the bool is reset to false when you are in a flat position. You would check if the bool is false in the first condition where you move the price to breakeven, call SetStopLoss() to move the price to breakeven, and set the bool to true. Then, in your second condition to move the stop loss, you would check if the bool is true and call SetStopLoss() to move the price of the stop loss. Once you are in a flat position, the bool will be reset to flat.

    Since you are checking if the bool is false in your first breakeven condition and then setting the bool to true once the condition becomes true and the stop is moved, the first breakeven condition would not become true again until you are in a flat position and the bool is reset to false. The second condition will become true after your first condition occurs, however, since the bool would be true and you are checking if the bool is true in your second condition to move the stop.

    Let us know if we may assist further.
    <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
      Hi Brandon,
      I've added the bool and now the second stop works, but not the first. Would you mind having a look to see if you can spot the issue?
      thanks,
      David
      Attached Files

      Comment


        #4
        Hello trader3000a,

        Thanks for your note.

        Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

        That said, I do not see anything specific in your code that would prevent the first breakeven condition from occurring.

        To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects 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. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

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

        Please let me know if I may further assist
        <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
        39 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        64 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