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

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

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by lightsun47, Today, 03:51 PM
        0 responses
        4 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        8 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        44 views
        0 likes
        Last Post futtrader  
        Started by Option Whisperer, Today, 09:55 AM
        1 response
        13 views
        0 likes
        Last Post bltdavid  
        Started by port119, Today, 02:43 PM
        0 responses
        8 views
        0 likes
        Last Post port119
        by port119
         
        Working...
        X