I tried to incorrectly use the following code to generate an entry signal when the closing price of a bar was greater than the highest high for the previous 20 bars. Instead of triggering a signal when a new 20 bar high was seen, the signal generated occurred when the close of a bar was greater than the high of the 20th bar back. This signal did generate 76 trades though over a 6 year period.
protected override void OnBarUpdate()
{
int lowestLowBar = LowestBar(Low,10);
int highestHighBar = HighestBar (High,20);
// Setting stop loss to lowest low of last 10 bars
SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
// Condition set 1
if (Close[0] > High[20])
{
EnterLong(1, "");
}
}
When I changed the code to the following, no trades were taken when backtesting over the same period in the same instrument. Looking back at the chart over the given time, there were plenty of instances when a bar closed higher than the highs of the previous 20 bars. The only change I made in the code was in the if statement where I changed the value "20" to the variable "highestHighBar". Am I using the HighestBar method correctly? I tried to utilize it in a similar manner as the LowestBar method was used within the same code.
protected override void OnBarUpdate()
{
int lowestLowBar = LowestBar(Low,10);
int highestHighBar = HighestBar (High,20);
// Setting stop loss to lowest low of last 10 bars
SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
// Condition set 1
if (Close[0] > High[highestHighBar])
{
EnterLong(1, "");
}
}
Thanks in advance.
joe

Comment