I am trying to program a strategy to buy on a 20 bar high and sell on a 20 bar low.
However I can't really get the strategy to execute according to plan... it doesn't seem like it looks at the highest high of the last 20 bars and instead just enters quite randomly...
Could you please maybe advise on where I am going wrong with this code?
protected override void OnBarUpdate()
{
// Condition set 1
if (EMA(20)[0] > EMA(60)[0]
&& EMA(60)[0] > EMA(100)[0]
&& High[0] > MAX(High,20)[1])
{
EnterLong(DefaultQuantity, "Long");
}
// Condition set 2
if (Close[0] < MAX(Low,20)[1])
{
ExitLong("Close Short", "Long");
}
// Condition set 3
if (EMA(20)[0] < EMA(60)[0]
&& EMA(60)[0] < EMA(100)[0]
&& Close[0] < MAX(Low,20)[1])
{
EnterShort(DefaultQuantity, "Short");
}
// Condition set 4
if (Close[0] > MAX(High,20)[1])
{
ExitShort("Exit Short", "Short");
}
}

Comment