Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.3 kB
5
Indexable
Never
// ATR Envelope Indicator - Credits to QuantNomad, HPotter (a legend)
//@version=5
sess1txt = 'Timeframe to monitor for signals'
sess2txt = 'Timeframe to use for displaying market signals of importance'

indicator(title='ATR Envelope', overlay=true)
session_timeframe   = input.session(defval="0700-1600",title="Signals And Alerts Session",tooltip = sess1txt,group='Sessions')
primary_session     = input.string(defval="0932-1600",title='Primary Signals Session',tooltip = sess2txt,group='Sessions')

sensitivity         = input(1.3, title='Sensitivity',group = 'General Settings')
atr_period          = input(10, title='ATR Period')
use_confirm         = input(defval=false,title='Use Confirmation')
show_labels         = input.bool(defval=true,title='Show Buy/Sell Labels')
show_tbl_popup      = input.bool(defval = true,title='Show Signal Table Popup')
color_bars          = input.bool(defval=false,title='Color Candles')
//src                 = input.source(defval=close,title='Source Type')
timeframe = input.timeframe(defval='5',title='Timeframe')

src = request.security(syminfo.tickerid, timeframe, close)//ta.atr(14))

InSession(sessionTimes) =>
    not na(time(timeframe.period, sessionTimes))

In_The_Session      = InSession(session_timeframe)
In_Primary_Session  = InSession(primary_session)

ATRTrail = 0.0
xATR = request.security(syminfo.tickerid, timeframe, ta.atr(atr_period))//ta.atr(atr_period)
nLoss = sensitivity * xATR

cond1 = src > nz(ATRTrail[1], 0) ? src - nLoss : src + nLoss
cond2 = src < nz(ATRTrail[1], 0) and src[1] < nz(ATRTrail[1], 0) ? math.min(nz(ATRTrail[1]), src + nLoss) : cond1
ATRTrail := src > nz(ATRTrail[1], 0) and src[1] > nz(ATRTrail[1], 0) ? math.max(nz(ATRTrail[1]), src - nLoss) : cond2

pos = 0
colorref = src[1] > nz(ATRTrail[1], 0) and src < nz(ATRTrail[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(ATRTrail[1], 0) and src > nz(ATRTrail[1], 0) ? 1 : colorref

xcolor = pos == -1 ? color.new(#e2940e, 0) : pos == 1 ? color.new(color.lime,0) : color.blue

plot(ATRTrail, title='ATR Trail', color=xcolor, linewidth=3, style=plot.style_line)

ab1 = ta.crossover(src,ATRTrail)
be1 = ta.crossunder(src,ATRTrail)
ab2 = ta.crossover(src,ATRTrail) and barstate.isconfirmed
be2 = ta.crossunder(src,ATRTrail) and barstate.isconfirmed
above = use_confirm ? ab2 : ab1
below = use_confirm ? be2 : be1

buy = src > ATRTrail and above and In_The_Session
sell = src < ATRTrail and below and In_The_Session
var float bprice = na
var float sprice = na
var int bbarindex = na 
var int sbarindex = na 
var line L = na 

if buy
    bprice := close
    bbarindex := bar_index
    L := line.new(sbarindex,sprice,bar_index,bprice,color=color.yellow,style=line.style_dotted,width=2)
if sell
    sbarindex := bar_index
    sprice := close
    L := line.new(bbarindex,bprice,bar_index,sprice,color=color.red,style=line.style_dotted,width=2)



barbuy = src > ATRTrail
barsell = src <= ATRTrail

plotshape(buy and show_labels, title='Buy', text='L', style=shape.labelup, location=location.belowbar, color= In_Primary_Session ?color.new(color.yellow, 0) : color.new(color.blue,0), textcolor=In_Primary_Session ?color.new(color.black,0) : color.new(color.white, 0), size=size.tiny)
plotshape(sell and show_labels, title='Sell', text='S', style=shape.labeldown, location=location.abovebar, color=In_Primary_Session ? color.new(color.yellow, 0) : color.new(color.blue,0), textcolor=In_Primary_Session ?color.new(color.black,0) : color.new(color.white, 0), size=size.tiny)

barcolor(color_bars and barbuy ? color.new(color.rgb(39, 103, 41),0) : na)
barcolor(color_bars and barsell ? color.rgb(181, 8, 8) : na)

if buy
    alert('ATR Envelope Long   ATR Envelope Long', alert.freq_once_per_bar)
if sell
    alert('ATR Envelope   ATR Envelope', alert.freq_once_per_bar)

var testTable1 = table.new(position = position.bottom_right, columns = 1, rows = 2, bgcolor = color.yellow, border_width = 1)
if buy and In_Primary_Session and show_tbl_popup
    table.cell(table_id = testTable1, column = 0, row = 0, text = 'ATR Envelope LONG CROSS  ATR Envelope LONG CROSS')
    table.set_bgcolor(table_id=testTable1,bgcolor=color.new(#329e6a, 0))
    table.cell_set_text_color(table_id=testTable1,column=0,row=0,text_color = color.white) 
if sell and In_Primary_Session and show_tbl_popup
    table.cell(table_id = testTable1, column = 0, row = 0, text = 'ATR Envelope SHORT CROSS   ATR Envelope SHORT CROSS')
    table.set_bgcolor(table_id=testTable1,bgcolor=color.rgb(192, 12, 12))
    table.cell_set_text_color(table_id=testTable1,column=0,row=0,text_color = color.white) 
if not buy and not sell
    table.cell(table_id = testTable1, column = 0, row = 0, text = '')
    table.set_bgcolor(table_id=testTable1,bgcolor=na)
    table.cell_set_text_color(table_id=testTable1,column=0,row=0,text_color = color.black)     

var testTable2 = table.new(position = position.bottom_left, columns = 1, rows = 2, bgcolor = color.yellow, border_width = 1)

table.cell(table_id = testTable2, column = 0, row = 0, text = "bbarindex: " + str.tostring(bbarindex) + " " + str.tostring(bprice) + " sbarindex: " + str.tostring(sbarindex) + " " + str.tostring(sprice))
table.set_bgcolor(table_id=testTable2,bgcolor=color.new(#329e6a, 0))
table.cell_set_text_color(table_id=testTable2,column=0,row=0,text_color = color.white)