Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study("SMT Divergence", overlay=true, max_bars_back = 4000)
- lb = input(5, title="Left Bars", minval=1)
- rb = input(5, title="Right Bars", minval=1)
- shownum = input(true, title="Show Divergence Number")
- showindis = input(false, title="Show Indicator Names")
- calcrsi = input(true, title="RSI")
- calcsmt = input(true, title="SMT")
- thickness = input(2, title = "Line Thickness (1-4)", minval = 1, maxval = 4)
- isFractal(mode) =>
- ret = mode == 1 ? high[4] < high[2] and high[3] <= high[2] and high[2] >= high[1] and high[2] > high[0] :
- mode == -1 ? low[4] > low[2] and low[3] >= low[2] and low[2] <= low[1] and low[2] < low[0] : false
- topFractal = isFractal(1)
- bottomFractal = isFractal(-1)
- // RSI
- rsi = rsi(close, 14)
- // SMT
- symbol = syminfo.ticker
- second_symbol =
- (symbol == "AUDCAD") ? "NZDCAD"
- : (symbol == "AUDCHF") ? "NZDCHF"
- : (symbol == "AUDJPY") ? "NZDJPY"
- : (symbol == "AUDUSD") ? "NZDUSD"
- : (symbol == "CADCHF") ? "USDCHF"
- : (symbol == "CADJPY") ? "USDJPY"
- : (symbol == "EURAUD") ? "GBPAUD"
- : (symbol == "EURCAD") ? "GBPCAD"
- : (symbol == "EURCHF") ? "GBPCHF"
- : (symbol == "EURJPY") ? "GBPJPY"
- : (symbol == "EURNZD") ? "GBPNZD"
- : (symbol == "EURUSD") ? "GBPUSD"
- : (symbol == "GBPAUD") ? "EURAUD"
- : (symbol == "GBPCAD") ? "EURCAD"
- : (symbol == "GBPCHF") ? "EURCHF"
- : (symbol == "GBPJPY") ? "EURJPY"
- : (symbol == "GBPNZD") ? "EURNZD"
- : (symbol == "GBPUSD") ? "EURUSD"
- : (symbol == "NZDCAD") ? "AUDCAD"
- : (symbol == "NZDCHF") ? "AUDCHF"
- : (symbol == "NZDJPY") ? "AUDJPY"
- : (symbol == "NZDUSD") ? "AUDUSD"
- : (symbol == "USDCHF") ? "CADCHF"
- : (symbol == "USDJPY") ? "CADJPY"
- : (symbol == "USOIL") ? "UKOIL"
- : (symbol == "UKOIL") ? "USOIL"
- : (symbol == "XAUUSD") ? "XAGUSD"
- : (symbol == "XAGUSD") ? "XAUUSD"
- : "DXY"
- smt = security(second_symbol, timeframe.period, close)
- float top = na
- float bottom = na
- top := pivothigh(lb, rb)
- bottom := pivotlow(lb, rb)
- previoustop = 0, previousbottom = 0
- previoustop := top ? lb : nz(previoustop[1]) + 1
- previousbottom := bottom ? lb : nz(previousbottom[1]) + 1
- // Positive Divergence (checking possible Lower lows(lb=0))
- newbottom = pivotlow(lb, 0) // check only left side
- emptyl = true
- if not na(newbottom) and newbottom < low[previousbottom] // there must not close price lower than the line between last PL and current low
- diff = (newbottom - low[previousbottom]) / previousbottom
- lline = newbottom - diff // virtual line to check there is no close price lower than it
- for x = 1 to previousbottom -1
- if close[x] < lline
- emptyl := false
- break
- lline := lline - diff
- else
- emptyl := false
- posdivergence = 0
- posdivtxt = ""
- if emptyl and not na(newbottom)
- if calcrsi and rsi[previousbottom] < rsi
- posdivergence := 1
- posdivtxt := "RSI\n"
- if calcsmt and smt[previousbottom] < smt
- posdivergence := 1
- posdivtxt := "SMT\n"
- newareal = false
- newareal := bottom ? false : nz(newareal[1], false)
- if posdivergence > 0
- var line divl = na
- var label lab = na
- if newareal // we remove old line until It reaches new pivot point (like animation ;)
- line.delete(divl)
- label.delete(lab)
- divl := line.new(bar_index - previousbottom, low[previousbottom], bar_index, low, color = color.lime, width = thickness)
- if shownum or showindis
- txt = showindis ? posdivtxt : ""
- txt := txt + (shownum ? tostring(posdivergence) : "")
- lab := label.new(bar_index, na, text=txt, color= color.lime, textcolor = color.black, style=label.style_labelup, yloc=yloc.belowbar)
- newareal := true
- // Negative Divergence (checking possible higher highs(lb=0))
- //newtop = pivothigh(lb, 0) // check only left side
- newtop = pivothigh(lb, 0) // check only left side
- emptyh = true
- if not na(newtop) and newtop > high[previoustop] // there must not close price higher than the line between last PH and current high
- diff = (newtop - high[previoustop]) / previoustop
- hline = newtop - diff // virtual line to check there is no close price higher than it
- for x = 1 to previoustop -1
- if close[x] > hline
- emptyh := false
- break
- hline := hline - diff
- else
- emptyh := false
- negdivergence = 0
- negdivtxt = ""
- if emptyh and not na(newtop)
- if calcrsi and rsi[previoustop] > rsi
- negdivergence := negdivergence + 1
- negdivtxt := "RSI\n"
- if calcsmt and smt[previoustop] > smt
- negdivergence := negdivergence + 1
- negdivtxt := "SMT\n"
- newareah = false
- newareah := top ? false : nz(newareah[1], false)
- if negdivergence > 0
- var line divlh = na
- var label labh = na
- if newareah // we remove old line until It reaches new pivot point (like animation ;)
- line.delete(divlh)
- label.delete(labh)
- divlh := line.new(bar_index - previoustop, high[previoustop], bar_index, high, color = color.red, width = thickness)
- if shownum or showindis
- txt = showindis ? negdivtxt : ""
- txt := txt + (shownum ? tostring(negdivergence) : "")
- labh := label.new(bar_index, na, text=txt, color= color.red, textcolor = color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- newareah := true
- alertcondition(posdivergence > 0, title='Positive Divergence', message='Positive Divergence')
- alertcondition(negdivergence > 0, title='Negative Divergence', message='Negative Divergence')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement