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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X