Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move stop to break even and beyond

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

    Move stop to break even and beyond

    Hi

    I'm trying to roll my initial stop to a small profit, then when price moves further in my favour, roll it again to secure a higher profit.

    Somehow it all seems to work fine on the first trade of the period I backtest, then the rolling of the stop does not seem to work. It's like I'm not "resetting" it correctly after I've exited the trade. I think I'm doing this "reset" under the marketposition= flat section of the code below, but I must not have it right

    Any help would be great!

    // Return Stop to flat & Set ATR based Stop & ATR based Profit
    if (Position.MarketPosition == MarketPosition.Flat)

    {
    int stopLong= Convert.ToInt32(Math.Round((ATR(14)[0]/TickSize)));
    int stopShort= Convert.ToInt32(Math.Round((ATR(14)[0]/TickSize)));
    int Profit= Convert.ToInt32(Math.Round((ATR(14)[0]/TickSize)));
    SetStopLoss("GoLong", CalculationMode.Ticks,(s1*stopLong), false);
    SetStopLoss("GoShort", CalculationMode.Ticks,(s1*stopShort), false);
    SetProfitTarget("GoShort", CalculationMode.Ticks, (p1*Profit));
    SetProfitTarget("GoLong", CalculationMode.Ticks, (p1*Profit));
    rolA= true;
    rolB= false;
    rolC= false;
    rolD= false;
    rolE= false;


    // OPEN: Enter a long position

    {
    EnterLong(DefaultQuantity, "GoLong");
    }


    // ROLL 1: Secure Profit (Long)
    if (Position.MarketPosition == MarketPosition.Long
    && Close[0] >= Position.AvgPrice +s1*0.4*(Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize))))*TickSize
    && rolA)
    {
    int stopLong= Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize)));
    SetStopLoss("GoLong", CalculationMode.Price,Position.AvgPrice+0.25*stopL ong*TickSize, false);
    rolA=false;
    rolB=true;
    rolC=false;
    rolD=false;
    rolE=false;
    DrawTriangleUp("tag" + CurrentBar, true, 0, Low[0]-2*TickSize, Color.Red);

    }
    // ROLL 2: Reduce stop loss (Long)
    if (Position.MarketPosition == MarketPosition.Long
    && Close[0] >= Position.AvgPrice +(s1*x1*1.75*(Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize)))))*TickSize
    &&rolB)

    {
    int stopLong= Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize)));
    SetStopLoss("GoLong", CalculationMode.Price,Position.AvgPrice-((s1*(1-(1.75*x1))*stopLong))*TickSize, false);
    rolA=false;
    rolB=false;
    rolC=true;
    rolD=false;
    DrawTriangleUp("tag2" + CurrentBar, true, 0, Low[0]-2*TickSize, Color.Orange);
    }

    #2
    Hello lancasterstephen,

    If you test in market replay, or with the simulated data feed, do you experience the same behavior?

    Have you added print statements to your script to check the values and that if your conditions are becoming true? I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:


    I’ve provided a link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    52 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    142 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    160 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    96 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    276 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Working...
    X