Crypto Slaya

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 12

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © BrookstarNZ

//@version=5
indicator("CRYPTO SLAYA", overlay=true, max_labels_count=500)

// SIGNAL MEANINGS
//"Buy Label" = Strong Buy
//"Sell Label" = Strong Sell
//"Don't Buy" = Green Cross
//"Don't Sell" = Red Cross
//"Weak Buy " = Green Circle
//"Weak Sell" = Red Circle
//"Reversal Buy" = Green Diamond
//"Reversal Sell" = Red Diamond

// Get user settings


res = input.timeframe(title='Timeframe', defval='D', group = "Non Repaint")
showBuySell = input(false, "Show Buy & Sell", group="BUY & SELL SIGNALS")
sensitivity = input.float(1.7, "Sensitivity (1-6)", 0.1, 6, group="BUY & SELL
SIGNALS")
percentStop = input.float(2, "Stop Loss % (0 to Disable)", 0, group="BUY &
SELL SIGNALS")
offsetSignal = input.float(7, "Signals Offset", 0, group="BUY & SELL SIGNALS")
length = input.int(20, 'Volume Pivot Length', minval = 1, group = "ORDER
BLOCKS")
bull_ext_last = input.int(1, 'Bullish OB', minval = 1, inline = 'bull', group =
"ORDER BLOCKS")
bull_avg_css = input.color(color.new(#00ff0a, 1), '', inline = 'bull', group =
"ORDER BLOCKS")
bull_css = input.color(color.new(#00000000, 100), '', inline = 'bull',
group = "ORDER BLOCKS")
bg_bull_css = input.color(color.new(#00ff0a, 90), '', inline = 'bull', group
= "ORDER BLOCKS")
bear_ext_last = input.int(1, 'Bearish OB', minval = 1 , inline = 'bear', group
= "ORDER BLOCKS")
bear_avg_css = input.color(color.new(#ff0015, 1), '', inline = 'bear', group =
"ORDER BLOCKS")
bear_css = input.color(color.new(#00000000, 100), '', inline = 'bear',
group = "ORDER BLOCKS")
bg_bear_css = input.color(color.new(#ff0015, 90), '', inline = 'bear', group
= "ORDER BLOCKS")
line_style = input.string('⎯⎯⎯', 'Average Line Style', options = ['⎯⎯⎯',
'----', '····'], group = "ORDER BLOCKS")
line_width = input.int(2, 'Average Line Width', minval = 1, group = "ORDER
BLOCKS")
mitigation = input.string('Wick', 'Mitigation Methods', options = ['Wick',
'Close'], group = "ORDER BLOCKS")
enableSR = input(true, "S&D On/Off", group="SUPPORT & RESISTANCE")
colorSup = input(#00dbff, "Support Color", group="SUPPORT & RESISTANCE")
colorRes = input(#00dbff, "Resistance Color", group="SUPPORT &
RESISTANCE")
strengthSR = input.int(6, "S/R Strength", 1, group="SUPPORT & RESISTANCE")
lineStyle1 = input.string("Solid", "Line Style", ["Solid", "Dotted",
"Dashed"], group="SUPPORT & RESISTANCE")
lineWidth1 = input.int(4, "S/R Line Width", 1, group="SUPPORT & RESISTANCE")
useZones = input(true, "Zones On/Off", group="SUPPORT & RESISTANCE")
useHLZones = input(true, "High Low Zones On/Off", group="SUPPORT &
RESISTANCE")
zoneWidth = input.int(4, "Zone Width %", 0, tooltip="it's calculated using
% of the distance between highest/lowest in last 300 bars", group="SUPPORT &
RESISTANCE")
expandSR = input(true, "Expand S/R Lines Left?", group = "SUPPORT &
RESISTANCE")
showDashboard = input(false, "Show Dashboard", group="TREND DASHBOARD")
locationDashboard = input.string("Middle Right", "Table Location", ["Top Right",
"Middle Right", "Bottom Right", "Top Center", "Middle Center", "Bottom Center",
"Top Left", "Middle Left", "Bottom Left"], group="TREND DASHBOARD")
tableTextColor = input(color.white, "Table Text Color", group="TREND DASHBOARD")
tableBgColor = input(color.black, "Table Background Color", group="TREND
DASHBOARD")
sizeDashboard = input.string("Normal", "Table Size", ["Large", "Normal",
"Small", "Tiny"], group="TREND DASHBOARD")
showPdHlc = input(false, "Previous Day H/L/C", group="PREVIOUS DAY HIGH LOW
CLOSE")
lineColor = input.color(#00ff0a, "Line Colors", group="PREVIOUS DAY HIGH
LOW CLOSE")
lineWidth = input.int(1, "Width Lines", group="PREVIOUS DAY HIGH LOW
CLOSE")
lineStyle = input.string("Solid", "Line Style", ["Solid", "Dashed",
"Dotted"])
labelSize = input.string("normal", "Label Text Size", ["small", "normal",
"large"])
labelColor = input.color(#00ff0a, "Label Text Colors")
showReversal = input(true, "Show Reversals", group="REVERSAL SIGNALS")

// Create non-repainting security function


rp_security(_symbol, _res, _src) =>
request.security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0])

// Get HTF price data


htfHigh = rp_security(syminfo.tickerid, res, high)
htfLow = rp_security(syminfo.tickerid, res, low)

// Plot data to chart


//plot(htfHigh, color=color.new(color.red, 0), title='HTF High')
//plot(htfLow, color=color.new(color.blue, 0), title='HTF Low')

// Functions
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
: x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100
securityNoRep(sym, res, src) => request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on)
swingPoints(prd) =>
pivHi = ta.pivothigh(prd, prd)
pivLo = ta.pivotlow (prd, prd)
last_pivHi = ta.valuewhen(pivHi, pivHi, 1)
last_pivLo = ta.valuewhen(pivLo, pivLo, 1)
hh = pivHi and pivHi > last_pivHi ? pivHi : na
lh = pivHi and pivHi < last_pivHi ? pivHi : na
hl = pivLo and pivLo > last_pivLo ? pivLo : na
ll = pivLo and pivLo < last_pivLo ? pivLo : na
[hh, lh, hl, ll]
f_chartTfInMinutes() =>
float _resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
f_kc(src, len, sensitivity) =>
basis = ta.sma(src, len)
span = ta.atr(len)
[basis + span * sensitivity, basis - span * sensitivity]
wavetrend(src, chlLen, avgLen) =>
esa = ta.ema(src, chlLen)
d = ta.ema(math.abs(src - esa), chlLen)
ci = (src - esa) / (0.015 * d)
wt1 = ta.ema(ci, avgLen)
wt2 = ta.sma(wt1, 3)
[wt1, wt2]
f_top_fractal(src) => src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and
src[2] > src[0]
f_bot_fractal(src) => src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and
src[2] < src[0]
f_fractalize (src) => f_top_fractal(src) ? 1 : f_bot_fractal(src) ? -1 : 0
f_findDivs(src, topLimit, botLimit) =>
fractalTop = f_fractalize(src) > 0 and src[2] >= topLimit ? src[2] : na
fractalBot = f_fractalize(src) < 0 and src[2] <= botLimit ? src[2] : na
highPrev = ta.valuewhen(fractalTop, src[2], 0)[2]
highPrice = ta.valuewhen(fractalTop, high[2], 0)[2]
lowPrev = ta.valuewhen(fractalBot, src[2], 0)[2]
lowPrice = ta.valuewhen(fractalBot, low[2], 0)[2]
bearSignal = fractalTop and high[1] > highPrice and src[1] < highPrev
bullSignal = fractalBot and low[1] < lowPrice and src[1] > lowPrev
[bearSignal, bullSignal]

// Get components
source = close
smrng1 = smoothrng(source, 27, 1.5)
smrng2 = smoothrng(source, 55, sensitivity)
smrng = (smrng1 + smrng2) / 2
filt = rngfilt(source, smrng)
up = 0.0, up := filt > filt[1] ? nz(up[1]) + 1 : filt < filt[1] ? 0 :
nz(up[1])
dn = 0.0, dn := filt < filt[1] ? nz(dn[1]) + 1 : filt > filt[1] ? 0 :
nz(dn[1])
bullCond = bool(na), bullCond := source > filt and source > source[1] and up > 0
or source > filt and source < source[1] and up > 0
bearCond = bool(na), bearCond := source < filt and source < source[1] and dn > 0
or source < filt and source > source[1] and dn > 0
lastCond = 0, lastCond := bullCond ? 1 : bearCond ? -1 : lastCond[1]
bull = bullCond and lastCond[1] == -1
bear = bearCond and lastCond[1] == 1
countBull = ta.barssince(bull)
countBear = ta.barssince(bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
rsi = ta.rsi(close, 28)
rsiOb = rsi > 78 and rsi > ta.ema(rsi, 10)
rsiOs = rsi < 27 and rsi < ta.ema(rsi, 10)
dHigh = securityNoRep(syminfo.tickerid, "D", high [1])
dLow = securityNoRep(syminfo.tickerid, "D", low [1])
dClose = securityNoRep(syminfo.tickerid, "D", close[1])
ema = ta.ema(close, 144)
emaBull = close > ema
equal_tf(res) => str.tonumber(res) == f_chartTfInMinutes() and not
timeframe.isseconds
higher_tf(res) => str.tonumber(res) > f_chartTfInMinutes() or timeframe.isseconds
too_small_tf(res) => (timeframe.isweekly and res=="1") or (timeframe.ismonthly and
str.tonumber(res) < 10)
securityNoRep1(sym, res, src) =>
bool bull_ = na
bull_ := equal_tf(res) ? src : bull_
bull_ := higher_tf(res) ? request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on) : bull_
bull_array = request.security_lower_tf(syminfo.tickerid, higher_tf(res) ?
str.tostring(f_chartTfInMinutes()) + (timeframe.isseconds ? "S" : "") :
too_small_tf(res) ? (timeframe.isweekly ? "3" : "10") : res, src)
if array.size(bull_array) > 1 and not equal_tf(res) and not higher_tf(res)
bull_ := array.pop(bull_array)
array.clear(bull_array)
bull_
TF1Bull = securityNoRep1(syminfo.tickerid, "1" , emaBull)
TF3Bull = securityNoRep1(syminfo.tickerid, "3" , emaBull)
TF5Bull = securityNoRep1(syminfo.tickerid, "5" , emaBull)
TF15Bull = securityNoRep1(syminfo.tickerid, "15" , emaBull)
TF30Bull = securityNoRep1(syminfo.tickerid, "30" , emaBull)
TF60Bull = securityNoRep1(syminfo.tickerid, "60" , emaBull)
TF120Bull = securityNoRep1(syminfo.tickerid, "120" , emaBull)
TF240Bull = securityNoRep1(syminfo.tickerid, "240" , emaBull)
TF480Bull = securityNoRep1(syminfo.tickerid, "480" , emaBull)
TFDBull = securityNoRep1(syminfo.tickerid, "1440", emaBull)
[wt1, wt2] = wavetrend(close, 5, 10)
[wtDivBear1, wtDivBull1] = f_findDivs(wt2, 15, -40)
[wtDivBear2, wtDivBull2] = f_findDivs(wt2, 45, -65)
wtDivBull = wtDivBull1 or wtDivBull2
wtDivBear = wtDivBear1 or wtDivBear2

// Colors
white = #ffffff, white30 = color.new(white, 100)
blue = #2962ff, blue30 = color.new(blue, 100)
whiteish = #ffffff, whiteish30 = color.new(whiteish , 100)

// Plot
off = percWidth(300, offsetSignal)
plotshape(showBuySell and bull ? low - off : na, "Buy Label" , shape.labelup ,
location.absolute, color(#00ff0a), 0, "Buy" , color.white, size=size.tiny)
plotshape(showBuySell and bear ? high + off : na, "Sell Label", shape.labeldown,
location.absolute, color(#ff0015), 0, "Sell", color.white, size=size.tiny)
plotshape(ta.crossover(wt1, wt2) and wt2 <= -53, "Don't Sell/Bottom" ,
shape.xcross, location.belowbar, color(#ff0015), size=size.tiny)
plotshape(ta.crossunder(wt1, wt2) and wt2 >= 53, "Don't Buy/Top", shape.xcross,
location.abovebar, color(#00ff0a), size=size.tiny)
plotshape(wtDivBull, "Weak Buy ", shape.circle , location.belowbar,
color(#00ff0a), size=size.auto)
plotshape(wtDivBear, "Weak Sell", shape.circle, location.abovebar, color(#ff0015),
size=size.auto)
barcolor(up > dn ? white : blue)
plotshape(showReversal and rsiOs, "Reversal Buy" , shape.diamond,
location.belowbar, color(#00ff0a), size=size.tiny)
plotshape(showReversal and rsiOb, "Reversal Sell", shape.diamond,
location.abovebar, color(#ff0015), size=size.tiny)
lStyle = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ?
line.style_dotted : line.style_dashed
lSize = labelSize == "small" ? size.small : labelSize == "normal" ?
size.normal : size.large
dHighLine = showPdHlc ? line.new(bar_index, dHigh, bar_index + 1, dHigh ,
xloc.bar_index, extend.both, lineColor, lStyle, lineWidth) : na,
line.delete(dHighLine[1])
dLowLine = showPdHlc ? line.new(bar_index, dLow , bar_index + 1, dLow ,
xloc.bar_index, extend.both, lineColor, lStyle, lineWidth) : na,
line.delete(dLowLine[1])
dCloseLine = showPdHlc ? line.new(bar_index, dClose, bar_index + 1, dClose,
xloc.bar_index, extend.both, lineColor, lStyle, lineWidth) : na,
line.delete(dCloseLine[1])
dHighLabel = showPdHlc ? label.new(bar_index + 100, dHigh , "P.D.H",
xloc.bar_index, yloc.price, #000000, label.style_none, labelColor, lSize) : na,
label.delete(dHighLabel[1])
dLowLabel = showPdHlc ? label.new(bar_index + 100, dLow , "P.D.L",
xloc.bar_index, yloc.price, #000000, label.style_none, labelColor, lSize) : na,
label.delete(dLowLabel[1])
dCloseLabel = showPdHlc ? label.new(bar_index + 100, dClose, "P.D.C",
xloc.bar_index, yloc.price, #000000, label.style_none, labelColor, lSize) : na,
label.delete(dCloseLabel[1])
srcStop = close
atrBand = srcStop * (percentStop / 100)
atrStop = trigger ? srcStop - atrBand : srcStop + atrBand
lastTrade(src) => ta.valuewhen(bull or bear, src, 0)
entry_y = lastTrade(srcStop)
stop_y = lastTrade(atrStop)
tp1_y = (entry_y - lastTrade(atrStop)) * 1 + entry_y
tp2_y = (entry_y - lastTrade(atrStop)) * 2 + entry_y
tp3_y = (entry_y - lastTrade(atrStop)) * 3 + entry_y
lineTpSl(y, color) =>
line lineTpSl = percentStop != 0 ? line.new(bar_index - (trigger ? countBull :
countBear) + 4, y, bar_index + 1, y, xloc.bar_index, extend.none, color,
line.style_solid) : na
line.delete(lineTpSl[1])
lineTpSl(entry_y, color.gray)
lineTpSl(stop_y, #ff0015)
lineTpSl(tp1_y, #00ff0a)
lineTpSl(tp2_y, #00ff0a)
lineTpSl(tp3_y, #00ff0a)
var dashboard_loc = locationDashboard == "Top Right" ? position.top_right :
locationDashboard == "Middle Right" ? position.middle_right : locationDashboard ==
"Bottom Right" ? position.bottom_right : locationDashboard == "Top Center" ?
position.top_center : locationDashboard == "Middle Center" ? position.middle_center
: locationDashboard == "Bottom Center" ? position.bottom_center : locationDashboard
== "Top Left" ? position.top_left : locationDashboard == "Middle Left" ?
position.middle_left : position.bottom_left
var dashboard_size = sizeDashboard == "Large" ? size.large : sizeDashboard ==
"Normal" ? size.normal : sizeDashboard == "Small" ? size.small : size.normal
var dashboard = showDashboard ? table.new(dashboard_loc, 2, 15, tableBgColor,
#000000, 2, tableBgColor, 1) : na
dashboard_cell(column, row, txt, signal=false) => table.cell(dashboard, column,
row, txt, 0, 0, signal ? #000000 : tableTextColor, text_size=dashboard_size)
dashboard_cell_bg(column, row, col) => table.cell_set_bgcolor(dashboard, column,
row, col)
if barstate.islast and showDashboard
dashboard_cell(0, 0 , "CRYPTO SLAYA")
dashboard_cell(0, 1 , "Current Position")
dashboard_cell(0, 2 , "Current Trend")
dashboard_cell(0, 3 , "Volume")
dashboard_cell(0, 4 , "Timeframe")
dashboard_cell(0, 5 , "1 min:")
dashboard_cell(0, 6 , "3 min:")
dashboard_cell(0, 7 , "5 min:")
dashboard_cell(0, 8 , "15 min:")
dashboard_cell(0, 9 , "30 min:")
dashboard_cell(0, 10, "1 H:")
dashboard_cell(0, 11, "2 H:")
dashboard_cell(0, 12, "4 H:")
dashboard_cell(0, 13, "8 H:")
dashboard_cell(0, 14, "Daily:")
dashboard_cell(1, 0 , "")
dashboard_cell(1, 1 , trigger ? "Buy" : "Sell", true), dashboard_cell_bg(1, 1,
trigger ? #00ff0a : #ff0015)
dashboard_cell(1, 2 , emaBull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 2, emaBull ? #00ff0a : #ff0015)
dashboard_cell(1, 3 , str.tostring(volume))
dashboard_cell(1, 4 , "Trends")
dashboard_cell(1, 5 , TF1Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 5 , TF1Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 6 , TF3Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 6 , TF3Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 7 , TF5Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 7 , TF5Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 8 , TF15Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 8 , TF15Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 9 , TF30Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 9 , TF30Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 10, TF60Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 10, TF60Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 11, TF120Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 11, TF120Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 12, TF240Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 12, TF240Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 13, TF480Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 13, TF480Bull ? #00ff0a : #ff0015)
dashboard_cell(1, 14, TFDBull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 14, TFDBull ? #00ff0a : #ff0015)

// Functions
percWidth1(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100

// Get components
rb = 10
prd = 284
ChannelW = 10
label_loc = 55
style = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ?
line.style_dotted : line.style_dashed
ph = ta.pivothigh(rb, rb)
pl = ta.pivotlow (rb, rb)
sr_levels = array.new_float(21, na)
prdhighest = ta.highest(prd)
prdlowest = ta.lowest(prd)
cwidth = percWidth(prd, ChannelW)
zonePerc = percWidth(300, zoneWidth)
aas = array.new_bool(41, true)
u1 = 0.0, u1 := nz(u1[1])
d1 = 0.0, d1 := nz(d1[1])
highestph = 0.0, highestph := highestph[1]
lowestpl = 0.0, lowestpl := lowestpl[1]
var sr_levs = array.new_float(21, na)
label hlabel = na, label.delete(hlabel[1])
label llabel = na, label.delete(llabel[1])
var sr_lines = array.new_line(21, na)
var sr_linesH = array.new_line(21, na)
var sr_linesL = array.new_line(21, na)
var sr_linesF = array.new_linefill(21, na)
var sr_labels = array.new_label(21, na)
if ph or pl
for x = 0 to array.size(sr_levels) - 1
array.set(sr_levels, x, na)
highestph := prdlowest
lowestpl := prdhighest
countpp = 0
for x = 0 to prd
if na(close[x])
break
if not na(ph[x]) or not na(pl[x])
highestph := math.max(highestph, nz(ph[x], prdlowest), nz(pl[x],
prdlowest))
lowestpl := math.min(lowestpl, nz(ph[x], prdhighest), nz(pl[x],
prdhighest))
countpp += 1
if countpp > 40
break
if array.get(aas, countpp)
upl = (ph[x] ? high[x + rb] : low[x + rb]) + cwidth
dnl = (ph[x] ? high[x + rb] : low[x + rb]) - cwidth
u1 := countpp == 1 ? upl : u1
d1 := countpp == 1 ? dnl : d1
tmp = array.new_bool(41, true)
cnt = 0
tpoint = 0
for xx = 0 to prd
if na(close[xx])
break
if not na(ph[xx]) or not na(pl[xx])
chg = false
cnt += 1
if cnt > 40
break
if array.get(aas, cnt)
if not na(ph[xx])
if high[xx + rb] <= upl and high[xx + rb] >= dnl
tpoint += 1
chg := true
if not na(pl[xx])
if low[xx + rb] <= upl and low[xx + rb] >= dnl
tpoint += 1
chg := true
if chg and cnt < 41
array.set(tmp, cnt, false)
if tpoint >= strengthSR
for g = 0 to 40 by 1
if not array.get(tmp, g)
array.set(aas, g, false)
if ph[x] and countpp < 21
array.set(sr_levels, countpp, high[x + rb])
if pl[x] and countpp < 21
array.set(sr_levels, countpp, low[x + rb])

// Plot
var line highest_ = na, line.delete(highest_)
var line lowest_ = na, line.delete(lowest_)
var line highest_fill1 = na, line.delete(highest_fill1)
var line highest_fill2 = na, line.delete(highest_fill2)
var line lowest_fill1 = na, line.delete(lowest_fill1)
var line lowest_fill2 = na, line.delete(lowest_fill2)
hi_col = close >= highestph ? colorSup : colorRes
lo_col = close >= lowestpl ? colorSup : colorRes
if enableSR
highest_ := line.new(bar_index - 311, highestph, bar_index, highestph,
xloc.bar_index, expandSR ? extend.both : extend.right, hi_col, style, lineWidth)
lowest_ := line.new(bar_index - 311, lowestpl , bar_index, lowestpl ,
xloc.bar_index, expandSR ? extend.both : extend.right, lo_col, style, lineWidth)
if useHLZones
highest_fill1 := line.new(bar_index - 311, highestph + zonePerc, bar_index,
highestph + zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)
highest_fill2 := line.new(bar_index - 311, highestph - zonePerc, bar_index,
highestph - zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)
lowest_fill1 := line.new(bar_index - 311, lowestpl + zonePerc , bar_index,
lowestpl + zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na)
lowest_fill2 := line.new(bar_index - 311, lowestpl - zonePerc , bar_index,
lowestpl - zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na)
linefill.new(highest_fill1, highest_fill2, color.new(hi_col, 90))
linefill.new(lowest_fill1 , lowest_fill2 , color.new(lo_col, 90))
if ph or pl
for x = 0 to array.size(sr_lines) - 1
array.set(sr_levs, x, array.get(sr_levels, x))
for x = 0 to array.size(sr_lines) - 1
line.delete(array.get(sr_lines, x))
line.delete(array.get(sr_linesH, x))
line.delete(array.get(sr_linesL, x))
linefill.delete(array.get(sr_linesF, x))
if array.get(sr_levs, x) and enableSR
line_col = close >= array.get(sr_levs, x) ? colorSup : colorRes
array.set(sr_lines, x, line.new(bar_index - 355, array.get(sr_levs, x),
bar_index, array.get(sr_levs, x), xloc.bar_index, expandSR ? extend.both :
extend.right, line_col, style, lineWidth))
if useZones
array.set(sr_linesH, x, line.new(bar_index - 355, array.get(sr_levs, x)
+ zonePerc, bar_index, array.get(sr_levs, x) + zonePerc, xloc.bar_index, expandSR ?
extend.both : extend.right, na))
array.set(sr_linesL, x, line.new(bar_index - 355, array.get(sr_levs, x)
- zonePerc, bar_index, array.get(sr_levs, x) - zonePerc, xloc.bar_index, expandSR ?
extend.both : extend.right, na))
array.set(sr_linesF, x, linefill.new(array.get(sr_linesH, x),
array.get(sr_linesL, x), color.new(line_col, 90)))
for x = 0 to array.size(sr_labels) - 1
label.delete(array.get(sr_labels, x))
if array.get(sr_levs, x) and enableSR
lab_loc = close >= array.get(sr_levs, x) ? label.style_label_up :
label.style_label_down
lab_col = close >= array.get(sr_levs, x) ? colorSup : colorRes
//array.set(sr_labels, x, label.new(bar_index + label_loc,
array.get(sr_levs, x), str.tostring(math.round_to_mintick(array.get(sr_levs, x))),
color=lab_col , textcolor=#000000, style=lab_loc))
//hlabel := enableSR ? label.new(bar_index + label_loc +
math.round(math.sign(label_loc)) * 20, highestph, "High Level : " +
str.tostring(highestph), color=hi_col, textcolor=#000000,
style=label.style_label_down) : na
//llabel := enableSR ? label.new(bar_index + label_loc +
math.round(math.sign(label_loc)) * 20, lowestpl , "Low Level : " +
str.tostring(lowestpl) , color=lo_col, textcolor=#000000,
style=label.style_label_up ) : na

//Functions
//Line Style function
get_line_style(style) =>
out = switch style
'⎯⎯⎯' => line.style_solid
'----' => line.style_dashed
'····' => line.style_dotted

//Function to get order block coordinates


get_coordinates(condition, top, btm, ob_val)=>
var ob_top = array.new_float(0)
var ob_btm = array.new_float(0)
var ob_avg = array.new_float(0)
var ob_left = array.new_int(0)

float ob = na

//Append coordinates to arrays


if condition
avg = math.avg(top, btm)

array.unshift(ob_top, top)
array.unshift(ob_btm, btm)
array.unshift(ob_avg, avg)
array.unshift(ob_left, time[length])

ob := ob_val

[ob_top, ob_btm, ob_avg, ob_left, ob]

//Function to remove mitigated order blocks from coordinate arrays


remove_mitigated(ob_top, ob_btm, ob_left, ob_avg, target, bull)=>
mitigated = false
target_array = bull ? ob_btm : ob_top

for element in target_array


idx = array.indexof(target_array, element)

if (bull ? target < element : target > element)


mitigated := true

array.remove(ob_top, idx)
array.remove(ob_btm, idx)
array.remove(ob_avg, idx)
array.remove(ob_left, idx)

mitigated

//Function to set order blocks


set_order_blocks(ob_top, ob_btm, ob_left, ob_avg, ext_last, bg_css, border_css,
lvl_css)=>
var ob_box = array.new_box(0)
var ob_lvl = array.new_line(0)

//Fill arrays with boxes/lines


if barstate.isfirst
for i = 0 to ext_last-1
array.unshift(ob_box, box.new(na,na,na,na
, xloc = xloc.bar_time
, extend= extend.right
, bgcolor = bg_css
, border_color = color.new(border_css, 70)))

array.unshift(ob_lvl, line.new(na,na,na,na
, xloc = xloc.bar_time
, extend = extend.right
, color = lvl_css
, style = get_line_style(line_style)
, width = line_width))

//Set order blocks


if barstate.islast
if array.size(ob_top) > 0
for i = 0 to math.min(ext_last-1, array.size(ob_top)-1)
get_box = array.get(ob_box, i)
get_lvl = array.get(ob_lvl, i)

box.set_lefttop(get_box, array.get(ob_left, i), array.get(ob_top,


i))
box.set_rightbottom(get_box, array.get(ob_left, i),
array.get(ob_btm, i))

line.set_xy1(get_lvl, array.get(ob_left, i), array.get(ob_avg, i))


line.set_xy2(get_lvl, array.get(ob_left, i)+1, array.get(ob_avg,
i))

//Global elements
var os = 0
var target_bull = 0.
var target_bear = 0.

n = bar_index
upper = ta.highest(length)
lower = ta.lowest(length)

if mitigation == 'Close'
target_bull := ta.lowest(close, length)
target_bear := ta.highest(close, length)
else
target_bull := lower
target_bear := upper
os := high[length] > upper ? 0 : low[length] < lower ? 1 : os[1]

phv = ta.pivothigh(volume, length, length)

//Get bullish/bearish order blocks coordinates


[bull_top
, bull_btm
, bull_avg
, bull_left
, bull_ob] = get_coordinates(phv and os == 1, hl2[length], low[length],
low[length])

[bear_top
, bear_btm
, bear_avg
, bear_left
, bear_ob] = get_coordinates(phv and os == 0, high[length], hl2[length],
high[length])

//Remove mitigated order blocks


mitigated_bull = remove_mitigated(bull_top
, bull_btm
, bull_left
, bull_avg
, target_bull
, true)

mitigated_bear = remove_mitigated(bear_top
, bear_btm
, bear_left
, bear_avg
, target_bear
, false)

//Set bullish order blocks


set_order_blocks(bull_top
, bull_btm
, bull_left
, bull_avg
, bull_ext_last
, bg_bull_css
, bull_css
, bull_avg_css)

//Set bearish order blocks


set_order_blocks(bear_top
, bear_btm
, bear_left
, bear_avg
, bear_ext_last
, bg_bear_css
, bear_css
, bear_avg_css)

//Show detected order blocks


plot(bull_ob, 'Bull OB', bull_css, 2, plot.style_linebr
, offset = -length
, display = display.none)
plot(bear_ob, 'Bear OB', bear_css, 2, plot.style_linebr
, offset = -length
, display = display.none)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy