Average Daily Range x 3 by Serge
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Serge_Trades

//@version=5

indicator(title='ADRx3 by Serge', shorttitle='ADRx3 by Serge', overlay=true)

text_ADR1_high = 'ADR1 High'
text_ADR2_high = 'ADR2 High'
text_ADR3_high = 'ADR3 High'
text_ADR1_low = 'ADR1 Low'
text_ADR2_low = 'ADR2 Low'
text_ADR3_low = 'ADR3 Low'
text_open = 'Open'

var mint        = #00FF80 //ADR3 Low
var lime        = #00FF00 //ADR2 Low
var chartreuse  = #80FF00 //ADR1 Low
var orange      = #FF8000 //ADR1 High
var redorange   = #FF4000 //ADR2 High
var red         = #FF0000 //ADR3 High
var gray        = #808080 //Open

//***Start of Inputs
var labels_enabled = input.bool(defval=true, title='Show labels')
var plot_history = input.bool(defval=true, title='Historical levels')
var use_ema = input.bool(defval=false, title='Use EMA')

var adr_1 = input.int(title='ADR 1', defval = 14, minval = 1, group='ADR Periods')
var adr_2 = input.int(title='ADR 2', defval = 7, minval = 1, group='ADR Periods')
var adr_3 = input.int(title='ADR 3', defval = 5, minval = 1, group='ADR Periods')

var color_ADR1_high = input.color(defval=color.new(orange  ,0), title=text_ADR1_high, group='Colors')
var color_ADR2_high = input.color(defval=color.new(redorange,0), title=text_ADR2_high, group='Colors')
var color_ADR3_high = input.color(defval=color.new(red,0), title=text_ADR3_high, group='Colors')
var color_ADR1_low = input.color(defval=color.new(chartreuse  ,0),   title=text_ADR1_low, group='Colors')
var color_ADR2_low = input.color(defval=color.new(lime,0),  title=text_ADR2_low, group='Colors')
var color_ADR3_low = input.color(defval=color.new(mint ,0), title=text_ADR3_low, group='Colors')
var color_open = input.color(defval=color.new(gray ,0), title=text_open, group='Colors')
//***End of Inputs

//***Start of local functions definiton***

draw_line(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width) =>
    dline = line.new(x1=_x1, y1=_y1, x2=_x2, y2=_y2, xloc=_xloc, extend=_extend, color=_color, style=_style, width=_width)
    line.delete(dline[1])

draw_label(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip) =>
    dlabel = label.new(x=_x, y=_y, text=_text, xloc=_xloc, yloc=_yloc, color=_color, style=_style, textcolor=_textcolor, size=_size, textalign=_textalign, tooltip=_tooltip)
    label.delete(dlabel[1])

//If security is futures - replace the ticker with continuous contract
tickerid_func() =>
    tickerid = syminfo.prefix + ':' + syminfo.tickerid
    if syminfo.type == 'futures'
        tickerid := syminfo.prefix  + ':' +  syminfo.root + '1!'

//Function to calculate ADR levels
//Parameters:
//  * lengthInput - ADR period
//  * hi_low - true-->High, else-->Low 
adr_func(lengthInput,hi_low) =>
    float result = 0
    float adr = 0
    open_ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)
	adr_High_sma = request.security(tickerid_func(), "D", ta.sma(high[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr_Low_sma = request.security(tickerid_func(), "D", ta.sma(low[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
	adr_High_ema = request.security(tickerid_func(), "D", ta.ema(high[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr_Low_ema = request.security(tickerid_func(), "D", ta.ema(low[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    
    if use_ema
        adr := adr_High_ema - adr_Low_ema
    else
        adr := adr_High_sma - adr_Low_sma
    
    if hi_low//High
        result := adr/2 + open_
    else    //Low
        result := open_ - adr/2    
  
//Workaround to disable color management on the standard tab Style for plots
//Custom inputs for colors should be used instead
transp_func() =>
    transp_0 = 0

//***End of local functions definiton***

//***Start of getting data  
start_time = request.security(tickerid_func(), "D", time_close[1],barmerge.gaps_off,barmerge.lookahead_on)
open__ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)

adr1_high = adr_func(adr_1,true)
adr1_low = adr_func(adr_1,false)
adr2_high = adr_func(adr_2,true)
adr2_low = adr_func(adr_2,false)
adr3_high = adr_func(adr_3,true)
adr3_low = adr_func(adr_3,false)

//***End of getting data

float _adr1_high = na
float _adr1_low = na
float _adr2_high = na
float _adr2_low = na
float _adr3_high = na
float _adr3_low = na
float _open = na
//*********************
//***Start of plotting*

//Decide if we can show the chart:
//Daily levels should be visible on intraday chart only

var show_chart = false

if timeframe.isintraday
    show_chart := true

if show_chart == false
    runtime.error("Change timeframe to intraday to see ADRx3 levels.")

//Plot all days
if show_chart and plot_history
    _adr1_high := adr1_high
    _adr1_low  := adr1_low
    _adr2_high := adr2_high
    _adr2_low  := adr2_low
    _adr3_high := adr3_high
    _adr3_low  := adr3_low
    _open      := open__

plot(_adr1_high, title=text_ADR1_high, color=color_ADR1_high, linewidth=1)
plot(_adr2_high, title=text_ADR2_high, color=color_ADR2_high, linewidth=1)
plot(_adr3_high, title=text_ADR3_high, color=color_ADR3_high, linewidth=1)
plot(_adr1_low, title=text_ADR1_low, color=color_ADR1_low, linewidth=1)
plot(_adr2_low, title=text_ADR2_low, color=color_ADR2_low, linewidth=1)
plot(_adr3_low, title=text_ADR3_low, color=color_ADR3_low, linewidth=1)
plot(_open, title=text_open, color=color_open, linewidth=2)

////Plot today only
if show_chart and not plot_history
    draw_line(start_time, adr1_high, time, adr1_high, xloc.bar_time, extend.none, color_ADR1_high, line.style_solid, 1)
    draw_line(start_time, adr2_high, time, adr2_high, xloc.bar_time, extend.none, color_ADR2_high, line.style_solid, 1)
    draw_line(start_time, adr3_high, time, adr3_high, xloc.bar_time, extend.none, color_ADR3_high, line.style_solid, 1)  
    draw_line(start_time, adr1_low, time, adr1_low, xloc.bar_time, extend.none, color_ADR1_low, line.style_solid, 1)
    draw_line(start_time, adr2_low, time, adr2_low, xloc.bar_time, extend.none, color_ADR2_low, line.style_solid, 1)  
    draw_line(start_time, adr3_low, time, adr3_low, xloc.bar_time, extend.none, color_ADR3_low, line.style_solid, 1)    
    draw_line(start_time, open__, time, open__, xloc.bar_time, extend.none, color_open, line.style_solid, 2)    

//***End of plotting***
//*********************

//*********************
//***Start of Labels***

label_ADR1_high = ''
label_ADR2_high = ''
label_ADR3_high = ''
label_ADR1_low = ''
label_ADR2_low = ''
label_ADR3_low = ''
label_open = ''

if labels_enabled == true
    label_ADR1_high := str.tostring(math.round_to_mintick(adr1_high))
    label_ADR2_high := str.tostring(math.round_to_mintick(adr2_high))
    label_ADR3_high := str.tostring(math.round_to_mintick(adr3_high))
    label_ADR1_low  := str.tostring(math.round_to_mintick(adr1_low))
    label_ADR2_low  := str.tostring(math.round_to_mintick(adr2_low))
    label_ADR3_low  := str.tostring(math.round_to_mintick(adr3_low))
    label_open      := str.tostring(math.round_to_mintick(open__))

string_ADR1_high = ' (' + label_ADR1_high + ')'
string_ADR2_high = ' (' + label_ADR2_high + ')'
string_ADR3_high = ' (' + label_ADR3_high + ')'
string_ADR1_low = ' (' + label_ADR1_low + ')'
string_ADR2_low = ' (' + label_ADR2_low + ')'
string_ADR3_low = ' (' + label_ADR3_low + ')'
string_open_low = ' (' + label_open + ')'

//Labels
if show_chart and labels_enabled == true
    draw_label(bar_index, adr1_high, text_ADR1_high + string_ADR1_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR1_high,transp_func()), size.normal, text.align_left, '') 
    draw_label(bar_index, adr2_high, text_ADR2_high + string_ADR2_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR2_high,transp_func()), size.normal, text.align_left, '')
    draw_label(bar_index, adr3_high, text_ADR3_high + string_ADR3_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR3_high,transp_func()), size.normal, text.align_left, '')
    draw_label(bar_index, adr1_low,  text_ADR1_low + string_ADR1_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR1_low,transp_func()), size.normal, text.align_left, '')   
    draw_label(bar_index, adr2_low,  text_ADR2_low + string_ADR2_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR2_low,transp_func()), size.normal, text.align_left, '')  
    draw_label(bar_index, adr3_low,  text_ADR3_low + string_ADR3_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR3_low,transp_func()), size.normal, text.align_left, '') 
    draw_label(bar_index, open__,  text_open + string_open_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_open,transp_func()), size.normal, text.align_left, '') 
//*********************

