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);
}
Comment