Skip to content

Instantly share code, notes, and snippets.

@elja
Last active December 17, 2021 15:36
Show Gist options
  • Save elja/7a0672134005282a3f8fe166b8091740 to your computer and use it in GitHub Desktop.
Save elja/7a0672134005282a3f8fe166b8091740 to your computer and use it in GitHub Desktop.
//@version=5
indicator('Bot ELB Buy Sell', overlay=true, format=format.price, precision=2)
Periods = input(title='ATR Period', defval=1)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
ConfirmInterval = input.int(title='Signal Confirmaion (Milliseconds)', step=1000, defval=5000)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
highlighting = input(title='Highlighter On/Off ?', defval=true)
SMA200 = ta.sma(close, 200)
SMA150 = ta.sma(close, 150)
atr2 = ta.rma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.blue, 0))
sellSignal = trend == -1 and trend[1] == 1
varip confBarIndex = int(na)
varip buySignalStarted = false
varip sellSignalStarted = false
varip buySignalConfirmed = false
varip sellSignalConfirmed = false
varip buySignalLastTimestamp = int(na)
varip sellSignalLastTimestamp = int(na)
// ON next tick
if barstate.isrealtime
if not sellSignal
sellSignalStarted := false
sellSignalConfirmed := false
sellSignalLastTimestamp := int(na)
if not buySignal
buySignalStarted := false
buySignalConfirmed := false
buySignalLastTimestamp := int(na)
if not sellSignalConfirmed
if sellSignal and sellSignalStarted and (timenow - sellSignalLastTimestamp) >= ConfirmInterval
sellSignalConfirmed := true
sellSignalStarted := false
sellSignalLastTimestamp := int(na)
if sellSignal and not sellSignalStarted
sellSignalStarted := true
sellSignalConfirmed := false
sellSignalLastTimestamp := timenow
if not buySignalConfirmed
if buySignal and buySignalStarted and (timenow - buySignalLastTimestamp) >= ConfirmInterval
buySignalConfirmed := true
buySignalStarted := false
buySignalLastTimestamp := int(na)
if buySignal and not buySignalStarted
buySignalStarted := true
buySignalConfirmed := false
buySignalLastTimestamp := timenow
alertcondition(buySignalConfirmed, title='Confirmed Buy', message='Buy!')
alertcondition(sellSignalConfirmed, title='Confirmed Sell', message='Sell!')
tableColumn = 1
tableRow = 4
var table panel = table.new(position.top_right, tableColumn, tableRow)
if barstate.islast
table.cell(panel, 0, 0, str.format("Buy Detected?: {0}", buySignal), bgcolor=color.black, text_color=color.white)
table.cell(panel, 0, 1, str.format("Sell Detected?: {0}", sellSignal), bgcolor=color.black, text_color=color.white)
table.cell(panel, 0, 2, str.format("Trying Confirm Buy?: {0}, Passed: {1}", buySignalStarted, buySignalStarted ? (timenow - buySignalLastTimestamp) : 0), bgcolor=color.black, text_color=color.white)
table.cell(panel, 0, 3, str.format("Trying Confirm Sell?: {0}, Passed: {1}", sellSignalStarted, sellSignalStarted ? (timenow - sellSignalLastTimestamp) : 0), bgcolor=color.black, text_color=color.white)
if barstate.isnew
sellSignalStarted := false
sellSignalConfirmed := false
sellSignalLastTimestamp := int(na)
buySignalStarted := false
buySignalConfirmed := false
buySignalLastTimestamp := int(na)
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.auto, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.small, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.auto, color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.auto, color=color.new(color.blue, 0), textcolor=color.new(color.white, 0))
// strategy.entry("buy", strategy.long, 10, when=buySignal and strategy.position_size <= 0)
// strategy.entry("sell", strategy.short, 10, when=sellSignal and strategy.position_size > 0)
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
plot(SMA200, title='SMA200', color=color.new(#9bf4f1, 0), linewidth=2)
plot(SMA150, title='SMA150', color=color.new(#9bf4f1, 0), linewidth=2)
p1 = plot(open)
p2 = plot(close)
fill(p1, p2, color=color.new(color.green, 90))
longFillColor = highlighting ? trend == 1 ? color.green : color.white : color.white
shortFillColor = highlighting ? trend == -1 ? color.red : color.white : color.white
fill(mPlot, upPlot, title='UpTrend Highligter', color=longFillColor, transp=90)
fill(mPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor, transp=90)
alertcondition(buySignal, title='SuperTrend Buy', message='Buy!')
alertcondition(sellSignal, title='SuperTrend Sell', message='Sell!')
changeCond = trend != trend[1]
alertcondition(changeCond, title='SuperTrend Direction Change', message='SuperTrend has changed direction!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment