Potentially profitable strategy but I need assistance with an error (please)
Posted: Tue Sep 13, 2022 8:22 pm
Hi, I have seen what appeared to be a sound strategy using 3 indicators (ADX/DI, EMA Trend Bars and Braid Filter). After putting the conditions together, I am not achieving any signals on the chart (on strategy it doesn't give any trades). I've gone through it and can't see where I have gone wrong. Any assistance would be greatly appreciated. Thank you, Alistair. Script below:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AliNotty
//Thanks to Robert Hill for Braid Filter
//@version=5
//indicator('Braid Filter')
strategy('Braid Filter')
//-- Inputs
maType = input.string('EMA', 'MA Type', options=['EMA', 'DEMA', 'TEMA', 'WMA', 'VWMA', 'SMA', 'SMMA', 'HMA', 'LSMA', 'Kijun', 'McGinley', 'RMA'])
Period1 = input(3, 'Period 1')
Period2 = input(7, 'Period 2')
Period3 = input(14, 'Period 3')
atrValue = input(14, 'ATR')
PipsMinSepPercent = input(40)
//-- Moving Average
ma(type, src, len) =>
float result = 0
if type == 'SMA' // Simple
result := ta.sma(src, len)
result
if type == 'EMA' // Exponential
result := ta.ema(src, len)
result
if type == 'DEMA' // Double Exponential
e = ta.ema(src, len)
result := 2 * e - ta.ema(e, len)
result
if type == 'TEMA' // Triple Exponential
e = ta.ema(src, len)
result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
result
if type == 'WMA' // Weighted
result := ta.wma(src, len)
result
if type == 'VWMA' // Volume Weighted
result := ta.vwma(src, len)
result
if type == 'SMMA' // Smoothed
w = ta.wma(src, len)
smma_sma = ta.sma(src, len)
result := na(w[1]) ? smma_sma : (w[1] * (len - 1) + src) / len
result
if type == 'RMA'
result := ta.rma(src, len)
result
if type == 'HMA' // Hull
result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
result
if type == 'LSMA' // Least Squares
result := ta.linreg(src, len, 0)
result
if type == 'Kijun' //Kijun-sen
kijun = math.avg(ta.lowest(len), ta.highest(len))
result := kijun
result
if type == 'McGinley'
mg = 0.0
mg_ema = ta.ema(src, len)
mg := na(mg[1]) ? mg_ema : mg[1] + (src - mg[1]) / (len * math.pow(src / mg[1], 4))
result := mg
result
result
//-- Braid Filter
ma01 = ma(maType, close, Period1)
ma02 = ma(maType, open, Period2)
ma03 = ma(maType, close, Period3)
max = math.max(math.max(ma01, ma02), ma03)
min = math.min(math.min(ma01, ma02), ma03)
dif = max - min
filter = ta.atr(atrValue) * PipsMinSepPercent / 100
//-- Plots
BraidColor = ma01 > ma02 and dif > filter ? color.green : ma02 > ma01 and dif > filter ? color.red : color.gray
plot(dif, 'Braid', BraidColor, 5, plot.style_columns)
plot(filter, 'Filter', color.new(color.blue, 0), 2, plot.style_line)
bgcolor(BraidColor, transp=90)
///////////////////////////////////////////////////////
/////////////////cm ema trendbars////////////////////
///////////////////////////////////////////////////////
//Thanks to ChrisMoody
ema1 = input.int(20, minval=1, maxval=300, title='EMA 1 Length')
ema2 = input.int(50, minval=1, maxval=300, title='EMA 2 Length')
ema3 = input.int(100, minval=1, maxval=300, title='EMA 3 Length')
MTema1 = input.int(100, minval=1, maxval=300, title='MT EMA Length')
src1 = input(title='EMA 1 Source', defval=close)
src2 = input(title='EMA 2 Source', defval=close)
src3 = input(title='EMA 3 Source', defval=close)
MTsrc1 = input(title='MT EMA 1 Source', defval=close)
MTshema = input(true, title='Show Multi Timeframe EMA?')
useCurrentRes = input(false, title='Use Current Chart Resolution?')
resCustom = input.timeframe(title='Use Different Timeframe? Uncheck Box Above', defval='D')
res = useCurrentRes ? timeframe.period : resCustom
usedEma1 = ta.ema(src1, ema1)
usedEma2 = ta.ema(src2, ema2)
usedEma3 = ta.ema(src3, ema3)
fnc1 = ta.ema(MTsrc1, MTema1)
MTusedEma1 = request.security(syminfo.tickerid, res, fnc1)
col1 = hlc3 >= usedEma1 ? #59AC8B : hlc3 < usedEma1 ? #D26DFF : color.white
col2 = hlc3 >= usedEma2 ? #59AC72 : hlc3 < usedEma2 ? #FE6DFF : color.white
col3 = hlc3 >= usedEma3 ? #008000 : hlc3 < usedEma3 ? #FF6DD4 : color.white
plot(usedEma1 ? usedEma1 : na, title='EMA1', style=plot.style_line, linewidth=1, color=col1)
plot(usedEma2 ? usedEma2 : na, title='EMA2', style=plot.style_line, linewidth=2, color=col2)
plot(usedEma3 ? usedEma3 : na, title='EMA3', style=plot.style_line, linewidth=3, color=col3)
plot(MTshema and MTusedEma1 ? MTusedEma1 : na, title='MTEMA1', style=plot.style_line, linewidth=3, color=color.new(color.yellow, 0))
///////////////////////////////////////////////////////////////////
////////////////////////////////////////
////////////ADX and DI//////////////////
////////////////////////////////////////
//Thanks to BeikabuOyaji
len = input(14)
th = input(20)
TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0
SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / len + TrueRange
SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / len + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / len + DirectionalMovementMinus
DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)
plot(DIPlus, color=color.new(color.green, 0), title='DI+')
plot(DIMinus, color=color.new(color.red, 0), title='DI-')
plot(ADX, color=color.new(color.navy, 0), title='ADX')
hline(th, color=color.black)
///////////////////////////////////////////////////
//BUY CONDITIONS
//1. Buy signal from Braid (red or grey to green and above blue line)
//2. ADX above 20 and pointing up
//3. Green trend bar (close above ema)
BuyCondition1 = ma01>ma02 and dif>filter and not (ma01>ma02 and dif>filter)[1]
BuyCondition2 = ADX>20 and ADX>ADX[1]
BuyCondition3 = hlc3 >= ema3
BuyCondition = BuyCondition1 and BuyCondition2 and BuyCondition3
////////////////////////////////
//SELL CONDITIONS
//1. Stop loss at recent low
//2. RRR
//Stop loss and RRR inputs
RRR = input.float(1.5, title='Risk Reward Ratio')
LookbackForLowest = input.int(20, title='Lowest Low LookBack')
SinceLastBought = nz(ta.barssince(BuyCondition))
BuyClosePrice = nz(ta.valuewhen(BuyCondition == 1, close, 1))
LookbackFromNow = (SinceLastBought + LookbackForLowest)
LowestLow = nz(ta.lowest(low, math.max(1,LookbackFromNow))[1])
StopLoss = close<LowestLow
Exit = (BuyClosePrice - LowestLow) * RRR
//1. Stop loss at recent low
SellCondition1 = StopLoss
//2. RRR
SellCondition2 = close>Exit
SellCondition = SellCondition1 or SellCondition2
/////////////////////////////////
var pos = 0
if BuyCondition and pos <= 0
pos := 1
if SellCondition and pos >= 0
pos := -1
longsignal = pos == 1 and (pos != 1)[1]
shortsignal = pos == -1 and (pos != -1)[1]
plotshape(longsignal, 'Buy', shape.labelup, location.belowbar, color.new(color.green, 0), text='B', textcolor=color.new(color.black, 0))
plotshape(shortsignal, 'Sell', shape.labeldown, location.abovebar, color.new(color.red, 0), text='S', textcolor=color.new(color.black, 0))
//alertcondition(longsignal, title='Buy Signal', message='Buy')
//alertcondition(shortsignal, title='Sell Signal', message='Sell')
strategy.entry("BUY", strategy.long, when = longsignal)
strategy.close("BUY", when = shortsignal)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AliNotty
//Thanks to Robert Hill for Braid Filter
//@version=5
//indicator('Braid Filter')
strategy('Braid Filter')
//-- Inputs
maType = input.string('EMA', 'MA Type', options=['EMA', 'DEMA', 'TEMA', 'WMA', 'VWMA', 'SMA', 'SMMA', 'HMA', 'LSMA', 'Kijun', 'McGinley', 'RMA'])
Period1 = input(3, 'Period 1')
Period2 = input(7, 'Period 2')
Period3 = input(14, 'Period 3')
atrValue = input(14, 'ATR')
PipsMinSepPercent = input(40)
//-- Moving Average
ma(type, src, len) =>
float result = 0
if type == 'SMA' // Simple
result := ta.sma(src, len)
result
if type == 'EMA' // Exponential
result := ta.ema(src, len)
result
if type == 'DEMA' // Double Exponential
e = ta.ema(src, len)
result := 2 * e - ta.ema(e, len)
result
if type == 'TEMA' // Triple Exponential
e = ta.ema(src, len)
result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
result
if type == 'WMA' // Weighted
result := ta.wma(src, len)
result
if type == 'VWMA' // Volume Weighted
result := ta.vwma(src, len)
result
if type == 'SMMA' // Smoothed
w = ta.wma(src, len)
smma_sma = ta.sma(src, len)
result := na(w[1]) ? smma_sma : (w[1] * (len - 1) + src) / len
result
if type == 'RMA'
result := ta.rma(src, len)
result
if type == 'HMA' // Hull
result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
result
if type == 'LSMA' // Least Squares
result := ta.linreg(src, len, 0)
result
if type == 'Kijun' //Kijun-sen
kijun = math.avg(ta.lowest(len), ta.highest(len))
result := kijun
result
if type == 'McGinley'
mg = 0.0
mg_ema = ta.ema(src, len)
mg := na(mg[1]) ? mg_ema : mg[1] + (src - mg[1]) / (len * math.pow(src / mg[1], 4))
result := mg
result
result
//-- Braid Filter
ma01 = ma(maType, close, Period1)
ma02 = ma(maType, open, Period2)
ma03 = ma(maType, close, Period3)
max = math.max(math.max(ma01, ma02), ma03)
min = math.min(math.min(ma01, ma02), ma03)
dif = max - min
filter = ta.atr(atrValue) * PipsMinSepPercent / 100
//-- Plots
BraidColor = ma01 > ma02 and dif > filter ? color.green : ma02 > ma01 and dif > filter ? color.red : color.gray
plot(dif, 'Braid', BraidColor, 5, plot.style_columns)
plot(filter, 'Filter', color.new(color.blue, 0), 2, plot.style_line)
bgcolor(BraidColor, transp=90)
///////////////////////////////////////////////////////
/////////////////cm ema trendbars////////////////////
///////////////////////////////////////////////////////
//Thanks to ChrisMoody
ema1 = input.int(20, minval=1, maxval=300, title='EMA 1 Length')
ema2 = input.int(50, minval=1, maxval=300, title='EMA 2 Length')
ema3 = input.int(100, minval=1, maxval=300, title='EMA 3 Length')
MTema1 = input.int(100, minval=1, maxval=300, title='MT EMA Length')
src1 = input(title='EMA 1 Source', defval=close)
src2 = input(title='EMA 2 Source', defval=close)
src3 = input(title='EMA 3 Source', defval=close)
MTsrc1 = input(title='MT EMA 1 Source', defval=close)
MTshema = input(true, title='Show Multi Timeframe EMA?')
useCurrentRes = input(false, title='Use Current Chart Resolution?')
resCustom = input.timeframe(title='Use Different Timeframe? Uncheck Box Above', defval='D')
res = useCurrentRes ? timeframe.period : resCustom
usedEma1 = ta.ema(src1, ema1)
usedEma2 = ta.ema(src2, ema2)
usedEma3 = ta.ema(src3, ema3)
fnc1 = ta.ema(MTsrc1, MTema1)
MTusedEma1 = request.security(syminfo.tickerid, res, fnc1)
col1 = hlc3 >= usedEma1 ? #59AC8B : hlc3 < usedEma1 ? #D26DFF : color.white
col2 = hlc3 >= usedEma2 ? #59AC72 : hlc3 < usedEma2 ? #FE6DFF : color.white
col3 = hlc3 >= usedEma3 ? #008000 : hlc3 < usedEma3 ? #FF6DD4 : color.white
plot(usedEma1 ? usedEma1 : na, title='EMA1', style=plot.style_line, linewidth=1, color=col1)
plot(usedEma2 ? usedEma2 : na, title='EMA2', style=plot.style_line, linewidth=2, color=col2)
plot(usedEma3 ? usedEma3 : na, title='EMA3', style=plot.style_line, linewidth=3, color=col3)
plot(MTshema and MTusedEma1 ? MTusedEma1 : na, title='MTEMA1', style=plot.style_line, linewidth=3, color=color.new(color.yellow, 0))
///////////////////////////////////////////////////////////////////
////////////////////////////////////////
////////////ADX and DI//////////////////
////////////////////////////////////////
//Thanks to BeikabuOyaji
len = input(14)
th = input(20)
TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0
SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / len + TrueRange
SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / len + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / len + DirectionalMovementMinus
DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)
plot(DIPlus, color=color.new(color.green, 0), title='DI+')
plot(DIMinus, color=color.new(color.red, 0), title='DI-')
plot(ADX, color=color.new(color.navy, 0), title='ADX')
hline(th, color=color.black)
///////////////////////////////////////////////////
//BUY CONDITIONS
//1. Buy signal from Braid (red or grey to green and above blue line)
//2. ADX above 20 and pointing up
//3. Green trend bar (close above ema)
BuyCondition1 = ma01>ma02 and dif>filter and not (ma01>ma02 and dif>filter)[1]
BuyCondition2 = ADX>20 and ADX>ADX[1]
BuyCondition3 = hlc3 >= ema3
BuyCondition = BuyCondition1 and BuyCondition2 and BuyCondition3
////////////////////////////////
//SELL CONDITIONS
//1. Stop loss at recent low
//2. RRR
//Stop loss and RRR inputs
RRR = input.float(1.5, title='Risk Reward Ratio')
LookbackForLowest = input.int(20, title='Lowest Low LookBack')
SinceLastBought = nz(ta.barssince(BuyCondition))
BuyClosePrice = nz(ta.valuewhen(BuyCondition == 1, close, 1))
LookbackFromNow = (SinceLastBought + LookbackForLowest)
LowestLow = nz(ta.lowest(low, math.max(1,LookbackFromNow))[1])
StopLoss = close<LowestLow
Exit = (BuyClosePrice - LowestLow) * RRR
//1. Stop loss at recent low
SellCondition1 = StopLoss
//2. RRR
SellCondition2 = close>Exit
SellCondition = SellCondition1 or SellCondition2
/////////////////////////////////
var pos = 0
if BuyCondition and pos <= 0
pos := 1
if SellCondition and pos >= 0
pos := -1
longsignal = pos == 1 and (pos != 1)[1]
shortsignal = pos == -1 and (pos != -1)[1]
plotshape(longsignal, 'Buy', shape.labelup, location.belowbar, color.new(color.green, 0), text='B', textcolor=color.new(color.black, 0))
plotshape(shortsignal, 'Sell', shape.labeldown, location.abovebar, color.new(color.red, 0), text='S', textcolor=color.new(color.black, 0))
//alertcondition(longsignal, title='Buy Signal', message='Buy')
//alertcondition(shortsignal, title='Sell Signal', message='Sell')
strategy.entry("BUY", strategy.long, when = longsignal)
strategy.close("BUY", when = shortsignal)