algo indicator
algo indicator
// INDICATOR SETTINGS
swing_length=input.int(10, 'Swing High/Low Length', group='Settings', minval=1,
maxval=50)
history_of_demand_to_keep=input.int(20, 'History To Keep', minval=5, maxval=50)
box_width=input.float(2.5, 'Supply/Demand Box Width', group='Settings', minval=1,
maxval=10, step=0.5)
// SUPPORT/RESISTANCE SETTINGS
enableSR=input(false, "SR On/Off", group="SR")
colorSup=input(#00000000, "Support Color", group="SR")
colorRes=input(#00000000, "Resistance Color", group="SR")
strengthSR=input.int(2, "S/R Strength", 1, group="SR")
lineStyle=input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"],
group="SR")
lineWidth=input.int(2, "S/R Line Width", 1, group="SR")
useZones=input(true, "Zones On/Off", group="SR")
useHLZones=input(true, "High Low Zones On/Off", group="SR")
zoneWidth=input.int(2, "Zone Width %", 0, tooltip="it's calculated using % of the
distance between highest/lowest in last 300 bars", group="SR")
expandSR=input(true, "Expand SR")
// DISPLAY SETTINGS
i_alertOn=input.bool(true, 'Alert Labels On/Off', group='Display')
i_barColOn=input.bool(true, 'Bar Color On/Off', group='Display')
// Array management
f_array_add_pop(array, new_value_to_add)=>
array.unshift(array, new_value_to_add)
array.pop(array)
// Non-repainting security
rp_security(_symbol, _res, _src)=> request.security(_symbol, _res,
_src[barstate.isrealtime ? 1 : 0])
// Smooth range
smoothrng(x, t, m)=>
wper=t*2-1
avrng=ta.ema(math.abs(x-x[1]), t)
smoothrng=ta.ema(avrng, wper)*m
// Range filter
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
// Percentage width
percWidth(len, perc)=> (ta.highest(len)-ta.lowest(len))*perc/100
// Security no repaint
securityNoRep(sym, res, src)=> request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on)
// Swing points
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]
// Keltner Channel
f_kc(src, len, sensitivity)=>
basis=ta.sma(src, len)
span=ta.atr(len)
[basis+span*sensitivity, basis-span*sensitivity]
// Wave trend
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]
// Fractal functions
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)=> top_fractal=f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1
: 0
// Find divergences
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]
// MA variant
variant(type, src, len, offSig, offALMA)=>
v1=ta.sma(src, len)
v2=ta.ema(src, len)
v3=2*v2-ta.ema(v2, len)
v4=3*(v2-ta.ema(v2, len))+ta.ema(ta.ema(v2, len), len)
v5=ta.wma(src, len)
v6=ta.vwma(src, len)
v7=0.0
sma_1=ta.sma(src, len)
v7:=na(v7[1]) ? sma_1 : (v7[1]*(len-1)+src)/len
v8=ta.wma(2*ta.wma(src, len/2)-ta.wma(src, len), math.round(math.sqrt(len)))
v9=ta.linreg(src, len, offSig)
v10=ta.alma(src, len, offALMA, offSig)
v11=ta.sma(v1, len)
a1=math.exp(-1.414*3.14159/len)
b1=2*a1*math.cos(1.414*3.14159/len)
c2=b1
c3=-a1*a1
c1=1-c2-c3
v12=0.0
v12:=c1*(src+nz(src[1]))/2+c2*nz(v12[1])+c3*nz(v12[2])
type=='EMA' ? v2 : type=='DEMA' ? v3 : type=='TEMA' ? v4 : type=='WMA' ? v5 :
type=='VWMA' ? v6 : type=='SMMA' ? v7 : type=='HullMA' ? v8 : type=='LSMA' ? v9 :
type=='ALMA' ? v10 : type=='TMA' ? v11 : type=='SSMA' ? v12 : v1
// Security wrapper
reso(exp, use, res)=> use ? request.security(syminfo.tickerid, res, exp,
gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on) : exp
// Cross detection
f_cross(_scr1, _scr2, _over)=> _over ? _scr1>_scr2 and _scr1[1]<_scr2[1] :
_scr1<_scr2 and _scr1[1]>_scr2[1]
// Source selection
src=h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
close, lookahead=barmerge.lookahead_off) : close
// ATR
atrValue=ta.atr(50)
// Swing highs/lows
swing_high=ta.pivothigh(high, swing_length, swing_length)
swing_low=ta.pivotlow(low, swing_length, swing_length)
// Arrays
var swing_high_values=array.new_float(5, 0.00)
var swing_low_values=array.new_float(5, 0.00)
var swing_high_bns=array.new_int(5, 0)
var swing_low_bns=array.new_int(5, 0)
var current_supply_box=array.new_box(history_of_demand_to_keep, na)
var current_demand_box=array.new_box(history_of_demand_to_keep, na)
var current_supply_poi=array.new_box(history_of_demand_to_keep, na)
var current_demand_poi=array.new_box(history_of_demand_to_keep, na)
var supply_bos=array.new_box(5, na)
var demand_bos=array.new_box(5, na)
// Support/Resistance calculations
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)
// Plot S/R
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, hi_col)
linefill.new(lowest_fill1, lowest_fill2, lo_col)
// RSI
rsi=ta.rsi(close, 28)
rsiOb=rsi>65 and rsi>ta.ema(rsi, 10)
rsiOs=rsi<35 and rsi<ta.ema(rsi, 10)
// Daily data
dHigh=securityNoRep(syminfo.tickerid, "D", high[1])
dLow=securityNoRep(syminfo.tickerid, "D", low[1])
dClose=securityNoRep(syminfo.tickerid, "D", close[1])
// EMA
ema=ta.ema(close, 144)
emaBull=close>ema
// Series setup
stratRes=timeframe.ismonthly ? str.tostring(timeframe.multiplier*intRes, '###M') :
timeframe.isweekly ? str.tostring(timeframe.multiplier*intRes, '###W') :
timeframe.isdaily ? str.tostring(timeframe.multiplier*intRes, '###D') :
timeframe.isintraday ? str.tostring(timeframe.multiplier*intRes, '####') : '60'
closeSeries=variant(basisType, close[delayOffset], basisLen, offsetSigma,
offsetALMA)
openSeries=variant(basisType, open[delayOffset], basisLen, offsetSigma, offsetALMA)
closeSeriesAlt=reso(closeSeries, useRes, stratRes)
openSeriesAlt=reso(openSeries, useRes, stratRes)
// Triggers
lxTrigger=false
sxTrigger=false
leTrigger=ta.crossover(closeSeriesAlt, openSeriesAlt)
seTrigger=ta.crossunder(closeSeriesAlt, openSeriesAlt)
// Initial values
var float condition=0.0
var float slLine=0.0
var float entryLine=0.0
// Stop Loss
slTopLvl=close+(close*(i_lxLvlSL/100))
slBotLvl=close-(close*(i_sxLvlSL/100))
slLine:=condition[1]<=0.0 and leTrigger ? slBotLvl : condition[1]>=0.0 and
seTrigger ? slTopLvl : nz(slLine[1])
slLong=f_cross(low, slLine, false)
slShort=f_cross(high, slLine, true)
// Take Profits
[tp3Line]=f_tp(condition, 1.2, leTrigger, seTrigger, close, i_lxLvlTP3, i_sxLvlTP3)
[tp2Line]=f_tp(condition, 1.1, leTrigger, seTrigger, close, i_lxLvlTP2, i_sxLvlTP2)
[tp1Line]=f_tp(condition, 1.0, leTrigger, seTrigger, close, i_lxLvlTP1, i_sxLvlTP1)
tp3Long=f_cross(high, tp3Line, true)
tp3Short=f_cross(low, tp3Line, false)
tp2Long=f_cross(high, tp2Line, true)
tp2Short=f_cross(low, tp2Line, false)
tp1Long=f_cross(high, tp1Line, true)
tp1Short=f_cross(low, tp1Line, false)
// Trade signals
longE=leTrigger and condition[1]<=0.0 and condition==1.0 and (tradeType=='LONG' or
tradeType=='BOTH')
shortE=seTrigger and condition[1]>=0.0 and condition==-1.0 and (tradeType=='SHORT'
or tradeType=='BOTH')
longX=lxTrigger and condition[1]>=1.0 and condition==0.0
shortX=sxTrigger and condition[1]<=-1.0 and condition==0.0
longSL=slLong and condition[1]>=1.0 and condition==0.0
shortSL=slShort and condition[1]<=-1.0 and condition==0.0
longTP3=tp3Long and condition[1]==1.2 and condition==1.3
shortTP3=tp3Short and condition[1]==-1.2 and condition==-1.3
longTP2=tp2Long and condition[1]==1.1 and condition==1.2
shortTP2=tp2Short and condition[1]==-1.1 and condition==-1.2
longTP1=tp1Long and condition[1]==1.0 and condition==1.1
shortTP1=tp1Short and condition[1]==-1.0 and condition==-1.1
// Plot lines
c_tp=leTrigger or seTrigger ? na : condition==0.0 ? na : color.green
c_entry=leTrigger or seTrigger ? na : condition==0.0 ? na : color.blue
c_sl=leTrigger or seTrigger ? na : condition==0.0 ? na : color.red
// Fill plots
fill(p_entryLine, p_tp1Line, color=color.new(color.green, 80), title="Profit Zone")
fill(p_entryLine, p_slLine, color=color.new(color.red, 80), title="Loss Zone")
// Bar coloring
barcolor(i_barColOn and scolor ? condition>0 ? color.green : condition<0 ?
color.red : na : na)
// Plot shapes
plotshape(i_alertOn and longE, title="Long Entry", style=shape.triangleup,
location=location.belowbar, color=color.green, size=size.tiny, text="Long")
plotshape(i_alertOn and shortE, title="Short Entry", style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.tiny, text="Short")
plotshape(i_alertOn and longTP1, title="Long TP1", style=shape.circle,
location=location.abovebar, color=color.green, size=size.tiny, text="TP1")
plotshape(i_alertOn and longTP2, title="Long TP2", style=shape.circle,
location=location.abovebar, color=color.green, size=size.tiny, text="TP2")
plotshape(i_alertOn and longTP3, title="Long TP3", style=shape.circle,
location=location.abovebar, color=color.green, size=size.tiny, text="TP3")
plotshape(i_alertOn and shortTP1, title="Short TP1", style=shape.circle,
location=location.belowbar, color=color.red, size=size.tiny, text="TP1")
plotshape(i_alertOn and shortTP2, title="Short TP2", style=shape.circle,
location=location.belowbar, color=color.red, size=size.tiny, text="TP2")
plotshape(i_alertOn and shortTP3, title="Short TP3", style=shape.circle,
location=location.belowbar, color=color.red, size=size.tiny, text="TP3")
plotshape(i_alertOn and longSL, title="Long SL", style=shape.xcross,
location=location.belowbar, color=color.red, size=size.tiny, text="SL")
plotshape(i_alertOn and shortSL, title="Short SL", style=shape.xcross,
location=location.abovebar, color=color.red, size=size.tiny, text="SL")
plotshape(i_alertOn and longX, title="Long Exit", style=shape.square,
location=location.belowbar, color=color.blue, size=size.tiny, text="Close")
plotshape(i_alertOn and shortX, title="Short Exit", style=shape.square,
location=location.abovebar, color=color.blue, size=size.tiny, text="Close")