I am writing some code to short upon breaking the upper Bollinger band on the 5 minute chart, but want the entry to execute as soon as it happens (rather than waiting for the 5 minute bar to close). I understand to backtest this sort of thing, you need to program a multitimeframe strategy (rather than using CalculateOnBarClose = false).
The code backtests OK without the multi-frame strategy, although misses a number of entries. But when I add the tick time frame to the strategy to get it to do it on a tick by tick basis, it doesn't even come close to working like it should. It completely misses the breaks of the upper Bollinger band, and ends up entering at a completely unexplained time well within the bands.
I keep re-examining the code but can't figure out why it's not working. I'm using BarsArray[0] to access the Bollinger band value for the primary 5 min time frame, and checking the close of the current tick against that value.
Here's the relevant code:
protected override void Initialize()
{
Add(PeriodType.Tick, 1);
CalculateOnBarClose = true;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
}
protected override void OnBarUpdate()
{
if (BarsInProgress == 0)
{
return;
}
if (BarsInProgress == 1)
{
if( Close[0] > Bollinger(BarsArray[0], 4, 20).Upper[0] && Position.MarketPosition == MarketPosition.Flat)
{
EnterShortLimit(positionSize, Close[0] - TickSize, "ShortEntry");
}
}
}
Comment