
This strategy combines Bollinger Bands and the 5-day Exponential Moving Average (EMA) to generate trading signals. When the price breaks above the upper Bollinger Band and closes below the 5-day EMA, a short position is opened. Conversely, when the price breaks below the lower Bollinger Band and closes above the 5-day EMA, a long position is opened. Additionally, when a reverse signal appears, the strategy closes the current position and opens a new position in the opposite direction. The strategy aims to capture market volatility and trend changes by using Bollinger Bands to gauge relative price levels and the EMA as a trend filter to generate trading signals.
By combining Bollinger Bands and EMA, this strategy can effectively capture trending and volatility opportunities, suitable for medium to long-term trading strategies. However, attention should be paid to parameter optimization, position control, and risk management. It should also be combined with other technical indicators and fundamental analysis for better performance. The strategy’s performance may be influenced by market conditions and require adjustments and optimizations based on actual situations.
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Bollinger Bands and EMA Strategy", overlay=true)
// Define the Bollinger Bands
length = input.int(20, title="BB Length")
src = input(close, title="BB Source")
mult = input.float(2.0, title="BB Multiplier")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
// Plot Bollinger Bands
plot(upper, "Upper Band", color=color.red)
plot(lower, "Lower Band", color=color.green)
plot(basis, "Middle Band", color=color.blue) // Use plot instead of hline for basis
// Define the 5-period EMA
ema5 = ta.ema(close, 5)
// Plot the 5 EMA
plot(ema5, "5 EMA", color=color.orange)
// Generate signals
var float entry_price = na
var string trade_direction = "none"
if (na(close[1]))
trade_direction := "none"
// Condition for entering a short trade
if (open > upper and close < ema5)
if (trade_direction != "short")
strategy.entry("Short", strategy.short)
entry_price := close
trade_direction := "short"
// Condition for entering a long trade
if (open < lower and close > ema5)
if (trade_direction != "long")
strategy.entry("Long", strategy.long)
entry_price := close
trade_direction := "long"
// Close short trade on a long signal
if (trade_direction == "short" and open < lower and close > ema5)
strategy.close("Short")
strategy.entry("Long", strategy.long)
entry_price := close
trade_direction := "long"
// Close long trade on a short signal
if (trade_direction == "long" and open > upper and close < ema5)
strategy.close("Long")
strategy.entry("Short", strategy.short)
entry_price := close
trade_direction := "short"
// Close trades when opposite signal is generated
if (trade_direction == "long" and open > upper and close < ema5)
strategy.close("Long")
trade_direction := "none"
if (trade_direction == "short" and open < lower and close > ema5)
strategy.close("Short")
trade_direction := "none"