I think the problem is in the structure of my code. But as it is my first strategy, I don't know where to start...
protected override void OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
// If a long position is open, allow for stop loss modification
if (Position.MarketPosition == MarketPosition.Long)
{
// Set stop values for new higher bar
if (Open[0] > StopBarHigh || Close[0] > StopBarHigh) // higher bar
StopBarLowOld = StopBarLow;
StopBarLow = Low[0];
StopBarHighOld = StopBarHigh;
StopBarHigh = High[0];
// Once the low is greater than entry price set new stop loss below the low
if (Low[0] > Position.AvgPrice)
{
SetStopLoss(CalculationMode.Price, StopBarLow - 2 * TickSize);
}
// Set values for stop bar if new bar = inside bar
if (Open[0] > StopBarLow && Open[0] < StopBarHigh && Close[0] > StopBarLow && Close[0] < StopBarHigh) // inside bar
{
// Once the low is greater than entry price set new stop loss below low
if (Low[0] > Position.AvgPrice)
{
SetStopLoss(CalculationMode.Price, StopBarLowOld - 2 * TickSize);
}
}
}

Comment