//@version=4
strategy(shorttitle="BB Breakout Strategy", title="Bollinger Bands with Multiple
MAs Filter and EMA Multi-Timeframe", overlay=true,
pyramiding=1, currency=currency.NONE ,
initial_capital = 100000, default_qty_type =
strategy.percent_of_equity,
default_qty_value=100)
// Inputs for Bollinger Bands
src = input(close, title = "Source")
length = input(55, minval=1, title = "SMA length") // 20 for classic Bollinger
Bands SMA line (basis)
mult = input(1., minval=0.236, maxval=2, title="Standard Deviation") //
Maxval = 2 as higher the deviation, higher the risk
// Bollinger Bands calculations
basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
// Function for KAMA calculation
kama(src, length) =>
var float kama_value = na
efficiency_ratio = abs(src - src[length]) / sum(abs(src - src[1]), length)
smoothing_constant = pow(efficiency_ratio * (2 / (length + 1) - 2 / (length *
10 + 1)) + 2 / (length * 10 + 1), 2)
kama_value := na(kama_value) ? src : kama_value + smoothing_constant * (src -
kama_value)
kama_value
// Moving Averages selection and calculation
enableMA = input(true, title="Enable Moving Average")
maType = input("EMA", options=["EMA", "SMA", "WMA", "SMMA", "CA", "EWMA",
"TEMA", "HMA", "LWMA", "KAMA", "ZLEMA", "VWAP", "GMMA", "McGinley", "FRAMA", "FMA",
"DEMA"], title="MA Type")
maLength = input(21, minval=1, title="MA Length")
define_ma(src, length, type) =>
if (type == "EMA")
ema(src, length)
else if (type == "SMA")
sma(src, length)
else if (type == "WMA")
wma(src, length)
else if (type == "SMMA")
rma(src, length)
else if (type == "CA")
(ema(src, length) + sma(src, length)) / 2
else if (type == "EWMA")
ema(src, length)
else if (type == "TEMA")
3 * ema(src, length) - 3 * ema(ema(src, length), length) + ema(ema(ema(src,
length), length), length)
else if (type == "HMA")
wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)))
else if (type == "LWMA")
wma(src, length)
else if (type == "KAMA")
kama(src, length)
else if (type == "ZLEMA")
2 * ema(src, length) - ema(src[1], length)
else if (type == "VWAP")
vwap
else if (type == "DEMA")
2 * ema(src, length) - ema(ema(src, length), length)
else
na
maLine = enableMA ? define_ma(src, maLength, maType) : na
// EMA Multi-Timeframe Calculation
enableEMA_MTF = input(true, title="Enable EMA Multi-Timeframe")
emaLength_MTF = input(50, minval=1, title="EMA Length (Multi-Timeframe)")
emaTF = input("D", title="EMA Timeframe", options=["1", "5", "15", "30",
"60", "240", "D", "W", "M"])
emaMTF = enableEMA_MTF ? security(syminfo.tickerid, emaTF, ema(src,
emaLength_MTF)) : na
// Ichimoku Base Line Multi-Timeframe Calculation
enableIchimoku_MTF = input(true, title="Enable Ichimoku Base Line Multi-Timeframe")
ichimokuTF = input("D", title="Ichimoku Timeframe", options=["1", "5",
"15", "30", "60", "240", "D", "W", "M"])
ichiBaseLineLength = input(26, title="Ichimoku Base Line Length")
ichimokuBaseLine = enableIchimoku_MTF ? security(syminfo.tickerid, ichimokuTF,
(highest(high, ichiBaseLineLength) + lowest(low, ichiBaseLineLength)) / 2) : na
// Conversion Line Multi-Timeframe Calculation
enableConversionLine_MTF = input(true, title="Enable Conversion Line Multi-
Timeframe")
conversionLineTF = input("D", title="Conversion Line Timeframe",
options=["1", "5", "15", "30", "60", "240", "D", "W", "M"])
conversionLineLength = input(9, title="Conversion Line Length (Multi-
Timeframe)")
highestHighConversion = security(syminfo.tickerid, conversionLineTF,
highest(high, conversionLineLength))
lowestLowConversion = security(syminfo.tickerid, conversionLineTF, lowest(low,
conversionLineLength))
conversionLine_MTF = enableConversionLine_MTF ? (highestHighConversion +
lowestLowConversion) / 2 : na
// Standard Candle Filter
enableCandleFilter = input(true, title="Enable Standard Candle Filter")
bodyMinPct = input(30, title="Min Body Percentage")
upperShadowMaxPct = input(40, title="Max Upper Shadow Percentage")
lowerShadowMaxPct = input(40, title="Max Lower Shadow Percentage")
candleBodyPct = abs(open - close) / (high - low) * 100
upperShadowPct = (high - max(open, close)) / (high - low) * 100
lowerShadowPct = (min(open, close) - low) / (high - low) * 100
isStandardCandle = enableCandleFilter ? (candleBodyPct >= bodyMinPct and
upperShadowPct <= upperShadowMaxPct and lowerShadowPct <= lowerShadowMaxPct) : true
// Trade type filter
Show = input("Both", options = ["Longs Only", "Shorts Only", "Both"], title
= "Trade Type")
CC = input(true, "Color Bars")
// ADX Calculation
enableADX = input(true, title="Enable ADX Filter")
adxLength = input(14, title="ADX Length")
plusDI = sma(max(high - high[1], 0), adxLength)
minusDI = sma(max(low[1] - low, 0), adxLength)
dx = 100 * abs(plusDI - minusDI) / (plusDI + minusDI)
adxValue = sma(dx, adxLength)
// Signal definitions
short = enableMA ? (src < lower and src < maLine) : (src < lower) // Short
condition: below lower BB and below selected MA if enabled
long = enableMA ? (src > upper and src > maLine) : (src > upper) // Long
condition: above upper BB and above selected MA if enabled
// Incorporating EMA Multi-Timeframe into signals
short := enableEMA_MTF ? (short and src < emaMTF) : short
long := enableEMA_MTF ? (long and src > emaMTF) : long
// Incorporating Ichimoku Base Line Multi-Timeframe into signals
short := enableIchimoku_MTF ? (short and src < ichimokuBaseLine) : short
long := enableIchimoku_MTF ? (long and src > ichimokuBaseLine) : long
// Incorporating Conversion Line Multi-Timeframe into signals
short := enableConversionLine_MTF ? (short and src < conversionLine_MTF) : short
long := enableConversionLine_MTF ? (long and src > conversionLine_MTF) : long
short := short and isStandardCandle
long := long and isStandardCandle
L1 = barssince(long)
S1 = barssince(short)
longSignal = L1 < S1 and not (L1 < S1)[1]
shortSignal = S1 < L1 and not (S1 < L1)[1]
// Time filter for trading session with dynamic start/end times and enable/disable
enableSessionFilter = input(true, title="Enable Session Time Filter")
sessionStartHour = input(6, title="Session Start Hour")
sessionStartMinute = input(0, title="Session Start Minute")
sessionEndHour = input(21, title="Session End Hour")
sessionEndMinute = input(0, title="Session End Minute")
// Day of week filter
monday = input(true, "Enable Monday")
tuesday = input(true, "Enable Tuesday")
wednesday = input(true, "Enable Wednesday")
thursday = input(true, "Enable Thursday")
friday = input(true, "Enable Friday")
saturday = input(true, "Enable Saturday")
sunday = input(true, "Enable Sunday")
// Determine if today is a selected trading day
isTradingDay = (monday and dayofweek == dayofweek.monday) or
(tuesday and dayofweek == dayofweek.tuesday) or
(wednesday and dayofweek == dayofweek.wednesday) or
(thursday and dayofweek == dayofweek.thursday) or
(friday and dayofweek == dayofweek.friday) or
(saturday and dayofweek == dayofweek.saturday) or
(sunday and dayofweek == dayofweek.sunday)
// Combining all filters
startTime = timestamp("UTC+03:30", year, month, dayofmonth, sessionStartHour,
sessionStartMinute) // Start of session
endTime = timestamp("UTC+03:30", year, month, dayofmonth, sessionEndHour,
sessionEndMinute) // End of session
inSession = enableSessionFilter ? (time >= startTime and time <= endTime) : true
// Entry conditions based on MA, EMA Multi-Timeframe, Ichimoku, Day Filter, and
session filter
if (inSession and isTradingDay and (Show == "Longs Only" or Show == "Both") and
longSignal and (not enableADX or adxValue > 20))
strategy.entry("Long", long=true)
if (inSession and isTradingDay and (Show == "Shorts Only" or Show == "Both") and
shortSignal and (not enableADX or adxValue > 20))
strategy.entry("Short", long=false)
// Close positions at the end of session
if (enableSessionFilter and time > endTime)
strategy.close("Long", comment="Close Long at end of session")
strategy.close("Short", comment="Close Short at end of session")
// Plot MA, Bollinger Bands, EMA Multi-Timeframe, Ichimoku, Conversion Line
plot_maLine = enableMA ? maLine : na
plot(plot_maLine, color=color.orange, title="Selected MA")
plot(upper, color=color.green, title="Upper Bollinger Band")
plot(lower, color=color.red, title="Lower Bollinger Band")
plot(basis, color=color.blue, title="Bollinger Basis")
plot(enableEMA_MTF ? emaMTF : na, color=color.purple, title="EMA Multi-Timeframe")
plot(enableIchimoku_MTF ? ichimokuBaseLine : na, color=color.fuchsia,
title="Ichimoku Base Line Multi-Timeframe")
plot(enableConversionLine_MTF ? conversionLine_MTF : na, color=color.teal,
title="Conversion Line Multi-Timeframe")