1

I have an indicator working the way I want, but I'm trying to convert it to a strategy for backtesting. I have bollinger bands plotted on the 15 minute timeframe, and on the one hour. This works in the indicator, but it does not work properly in the strategy.

The error I get is: The strategy function does not have an argument with the name timeframe or timeframe_gaps

I tried removing that in the strategy, but now the 1 hr bollinger bands are incorrect:

strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)

Here is the full code of the strategy:

//@version=5
strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)
entryPrice = close
// Set input parameters
length = input.int(20, minval=1)
mult = input.float(2.5, minval=0.001, maxval=50)
offset = input.int(0, "Offset", minval = -500, maxval = 500)

// Calculate Bollinger Bands using 15 minute data
src = close
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev

// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h

// Enter long position when 15 minute chart Bollinger Band is touched and 1hr band is touched (or previous 1 hr candle)
longCondition = ta.crossover(close, lower) and (ta.crossover(low, lower1h) or ta.crossover(low[1], lower1h))
if longCondition
    strategy.entry("BB Long", strategy.long)
    entryPrice = close

// Enter short position when 15 minute chart Bollinger Band is touched and 1hr band is touched (or previous 1 hr candle)
shortCondition = ta.crossunder(close, upper)  and (ta.crossover(high, upper1h) or ta.crossover(high[1], upper1h))
if shortCondition
    strategy.entry("BB Short", strategy.short)
    entryPrice = close

// Calculate profit target
profitTarget = 1 + 0.1  // 10% profit target

// Exit long position when profit target is reached
exitLongCondition = close > entryPrice * profitTarget
if exitLongCondition
    strategy.close("BB Long")

// Exit short position when profit target is reached
exitShortCondition = close < entryPrice / profitTarget
if exitShortCondition
    strategy.close("BB Short")

// Plot Bollinger Bands
plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)
plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)

Here is the full code of the indicator (Which plots the bands correctly)

// Function to plot Bollinger Bands on a 1 hour timeframe
// Define function and set input parameters
//@version=5
indicator(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true, timeframe="", timeframe_gaps=true)
//study("Bollinger Bands - 1 Hour", overlay=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.5, minval=0.001, maxval=50, title="StdDev")
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)

// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h
// Plot Bollinger Bands

plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)


plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)

How can I use multi timeframe correctly in a strategy? The tutorials I found all use the indicator function, but I really need to backtest this!

Thanks!

I tried removing timeframe and timeframe_gaps from the strategy function, but the result is that the 1 hour Bollinger bands are incorrect.

Dan J
  • 33
  • 5

1 Answers1

0

Try this:

Change

src1h = request.security(syminfo.tickerid, '60', close)

To

src1h = request.security(syminfo.tickerid, '60', close, lookahead=barmerge.lookahead_on, gaps=barmerge.gaps_on)

I haven't tested this but have experienced something vaguely similar before, let us know how it goes.

  • Bingo, that did it! Thank you so much for answering my question. I really wasn't expecting an answer so quickly!!! – Dan J Jan 03 '23 at 01:36