Sw1tchFX - Average Daily Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sw1tchFX

//   _________        ____  __         .__    _______________  ___
//  /   _____/_  _  _/_   |/  |_  ____ |  |__ \_   _____/\   \/  /
//  \_____  \\ \/ \/ /|   \   __\/ ___\|  |  \ |    __)   \     / 
//  /        \\     / |   ||  | \  \___|      \|     \    /     \ 
// /_______  / \/\_/  |___||__|  \___  >___|  /\___  /   /___/\  \
//         \/                        \/     \/     \/          \_/ 

//@version=5
indicator(title = "Sw1tchFX - Average Daily Range", shorttitle = "SADR", overlay = true)

//#region -------------------- Inputs
hist_values = input.bool(defval = false, title = 'Show Past ADRs?', tooltip = 'This will show the past ADR values based off your selected resolution', group = 'Inputs')
show_swing  = input.bool(defval = false, title = 'Show Long Term Ranges?', tooltip = 'This will show you longer term ranges. These ranges are a baseline for longer term trades (hours, days)', group = 'Inputs')
show_scalp  = input.bool(defval = false, title = 'Show Short Term Ranges?', tooltip = 'These will show you shorter term ranges. These ranges are a baseline for scalping based trades where profit levels and ratios are set (i.e. 3:1, 2:1 Risk to Reward)', group = 'Inputs')
res         = input.timeframe(defval = 'D', title = 'Resolution', options = ['D','W','M'], tooltip = 'Allow you to change the resolution of the ADR from Daily, Weekly, or Monthly', group = 'Resolution')
//#endregion

//#region -------------------- Functions
daily_calc() =>
    [day_one_high, day_one_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[1], low[1]], lookahead = barmerge.lookahead_on)
    [day_two_high, day_two_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[2], low[2]], lookahead = barmerge.lookahead_on)
    [day_three_high, day_three_low] = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[3], low[3]], lookahead = barmerge.lookahead_on)
    [day_four_high, day_four_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[4], low[4]], lookahead = barmerge.lookahead_on)
    [day_five_high, day_five_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[5], low[5]], lookahead = barmerge.lookahead_on)

    adr = ((day_one_high - day_one_low) + (day_two_high - day_two_low) + (day_three_high - day_three_low) + (day_four_high - day_four_low) + (day_five_high - day_five_low)) / 5

yesterday_calc() =>
    [day_one_high, day_one_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[1], low[1]], lookahead = barmerge.lookahead_on)

    y_move = day_one_high - day_one_low

yesterday_adr() =>
    [day_one_high, day_one_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[2], low[2]], lookahead = barmerge.lookahead_on)
    [day_two_high, day_two_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[3], low[3]], lookahead = barmerge.lookahead_on)
    [day_three_high, day_three_low] = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[4], low[4]], lookahead = barmerge.lookahead_on)
    [day_four_high, day_four_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[5], low[5]], lookahead = barmerge.lookahead_on)
    [day_five_high, day_five_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[6], low[6]], lookahead = barmerge.lookahead_on)

    y_adr = ((day_one_high - day_one_low) + (day_two_high - day_two_low) + (day_three_high - day_three_low) + (day_four_high - day_four_low) + (day_five_high - day_five_low)) / 5

nround(x) =>
    n = math.round(x / syminfo.mintick) * syminfo.mintick

pip_to_whole(number) =>
    atr = ta.atr(14)
    if syminfo.type == 'forex'
        pips = atr < 1.0 ? (number / syminfo.mintick) / 10 : number
        pips := atr >= 1.0 and atr < 100.0 and (syminfo.currency == 'JPY' or syminfo.currency == 'HUF' or syminfo.currency == 'INR') ? pips * 100 : pips
    else if syminfo.type == 'crypto'
        pips = atr < 1.0 ? (number / syminfo.mintick) / 10 : number
        pips := atr >= 1.0 ? pips * 100 : pips
    else
        number

//#endregion

//#region -------------------- Function Calls
adr = daily_calc()
adr_pips = pip_to_whole(adr)
adr_pips := math.round(adr_pips)

y_move = yesterday_calc()
y_move_pips = pip_to_whole(y_move)
y_move_pips := math.round(y_move_pips)

y_move_75 = yesterday_adr()
y_move_p = pip_to_whole(y_move_75) 
y_move_p := math.round(y_move_p)
y_move_75_pips = y_move_p * 0.75
y_move_75_pips := math.round(y_move_75_pips)
//#endregion

//#region -------------------- Daily Open Calls
//Calc Open
dopen  = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, open, lookahead = barmerge.lookahead_on)
[dday, dclose_y, dclose_t] = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [time_tradingday, time_close(res)[1], time_close(res)], lookahead = barmerge.lookahead_on)
//#endregion

//#region -------------------- Line / Label Vars and ADR Calculations
var line tl        = na
var line high_100  = na
var line high_75   = na
var line high_50   = na
var line high_25   = na
var line low_25    = na
var line low_50    = na
var line low_75    = na
var line low_100   = na

//Labels
var label label_t1_open  = na
var label label_high_100 = na
var label label_high_75  = na
var label label_high_50  = na
var label label_high_25  = na
var label label_low_25   = na
var label label_low_50   = na
var label label_low_75   = na
var label label_low_100  = na

//ADR Percentage Calculations
high_100_p = dopen + adr
high_75_p  = dopen + adr * 0.75 
high_50_P  = dopen + adr * 0.50
high_25_p  = dopen + adr * 0.25

low_25_p   = dopen - adr * 0.25
low_50_p   = dopen - adr * 0.50
low_75_p   = dopen - adr * 0.75
low_100_p  = dopen - adr
//#endregion

//#region -------------------- Lines and Labels
if hist_values
    if ta.change(dclose_y)
        tl       := line.new(dclose_y, dopen, dclose_t, dopen , xloc = xloc.bar_time, color = color.new(color.white, 40), style = line.style_solid)
        high_100 := line.new(dclose_y, high_100_p, dclose_t, high_100_p,      color = color.new(#5bd50f, 10),   style = line.style_solid, xloc = xloc.bar_time)
        high_75  := line.new(dclose_y, high_75_p,  dclose_t, high_75_p,       color = color.new(#5bd50f, 20),   style = line.style_solid, xloc = xloc.bar_time)
        high_50  := line.new(dclose_y, high_50_P,  dclose_t, high_50_P,       color = color.new(#5bd50f, 30),   style = line.style_solid, xloc = xloc.bar_time)
        high_25  := line.new(dclose_y, high_25_p,  dclose_t, high_25_p,       color = color.new(#5bd50f, 40),   style = line.style_solid, xloc = xloc.bar_time)
        low_25   := line.new(dclose_y, low_25_p,   dclose_t, low_25_p,        color = color.new(#ea2700, 40),   style = line.style_solid, xloc = xloc.bar_time)
        low_50   := line.new(dclose_y, low_50_p,   dclose_t, low_50_p,        color = color.new(#ea2700, 30),   style = line.style_solid, xloc = xloc.bar_time)
        low_75   := line.new(dclose_y, low_75_p,   dclose_t, low_75_p,        color = color.new(#ea2700, 20),   style = line.style_solid, xloc = xloc.bar_time)
        low_100  := line.new(dclose_y, low_100_p,  dclose_t, low_100_p,       color = color.new(#ea2700, 10),   style = line.style_solid, xloc = xloc.bar_time)
else
    tl       := line.new(dclose_y, dopen, last_bar_time, dopen , xloc = xloc.bar_time, color = color.new(color.white, 40), style = line.style_solid)
    high_100 := line.new(dclose_y, high_100_p, last_bar_time, high_100_p,     color = color.new(#5bd50f, 10),   style = line.style_solid, xloc = xloc.bar_time)
    high_75  := line.new(dclose_y, high_75_p,  last_bar_time, high_75_p,      color = color.new(#5bd50f, 20),   style = line.style_solid, xloc = xloc.bar_time)
    high_50  := line.new(dclose_y, high_50_P,  last_bar_time, high_50_P,      color = color.new(#5bd50f, 30),   style = line.style_solid, xloc = xloc.bar_time)
    high_25  := line.new(dclose_y, high_25_p,  last_bar_time, high_25_p,      color = color.new(#5bd50f, 40),   style = line.style_solid, xloc = xloc.bar_time)
    low_25   := line.new(dclose_y, low_25_p,   last_bar_time, low_25_p,       color = color.new(#ea2700, 40),   style = line.style_solid, xloc = xloc.bar_time)
    low_50   := line.new(dclose_y, low_50_p,   last_bar_time, low_50_p,       color = color.new(#ea2700, 30),   style = line.style_solid, xloc = xloc.bar_time)
    low_75   := line.new(dclose_y, low_75_p,   last_bar_time, low_75_p,       color = color.new(#ea2700, 20),   style = line.style_solid, xloc = xloc.bar_time)
    low_100  := line.new(dclose_y, low_100_p,  last_bar_time, low_100_p,      color = color.new(#ea2700, 10),   style = line.style_solid, xloc = xloc.bar_time)

    //Labels
    label_t1_open  := label.new(last_bar_time, dopen,      size = size.normal, text = '                                       ' + 'Open | ' + str.tostring(dopen, format.mintick), style = label.style_none, textcolor = color.new(color.white, 10), xloc = xloc.bar_time)
    label_high_100 := label.new(last_bar_time, high_100_p, size = size.normal, text = '                                                      ' + '100% | ' + str.tostring(math.round(adr_pips)) + ' Pips' + ' | ' + str.tostring(high_100_p, format.mintick), style = label.style_none, textcolor = color.new(#5bd50f, 10), xloc = xloc.bar_time)
    label_high_75  := label.new(last_bar_time, high_75_p,  size = size.normal, text = '                                                      ' + '75%  | '  + str.tostring(math.round(adr_pips * 0.75)) + ' Pips' + ' | ' + str.tostring(high_75_p, format.mintick), style = label.style_none, textcolor = color.new(#5bd50f, 10), xloc = xloc.bar_time)
    label_high_50  := label.new(last_bar_time, high_50_P,  size = size.normal, text = '                                                      ' + '50%  | '  + str.tostring(math.round(adr_pips * 0.50)) + ' Pips' + ' | ' + str.tostring(high_50_P, format.mintick), style = label.style_none, textcolor = color.new(#5bd50f, 10), xloc = xloc.bar_time)
    label_high_25  := label.new(last_bar_time, high_25_p,  size = size.normal, text = '                                                      ' + '25%  | '  + str.tostring(math.round(adr_pips * 0.25)) + ' Pips' + ' | ' + str.tostring(high_25_p, format.mintick), style = label.style_none, textcolor = color.new(#5bd50f, 10), xloc = xloc.bar_time)
    label_low_25   := label.new(last_bar_time, low_25_p,   size = size.normal, text = '                                                      ' + '25%  | '  + str.tostring(math.round(adr_pips * 0.25)) + ' Pips' + ' | ' + str.tostring(low_25_p, format.mintick), style = label.style_none, textcolor = color.new(#ea2700, 10), xloc = xloc.bar_time)
    label_low_50   := label.new(last_bar_time, low_50_p,   size = size.normal, text = '                                                      ' + '50%  | '  + str.tostring(math.round(adr_pips * 0.50)) + ' Pips' + ' | ' + str.tostring(low_50_p, format.mintick), style = label.style_none, textcolor = color.new(#ea2700, 10), xloc = xloc.bar_time)
    label_low_75   := label.new(last_bar_time, low_75_p,   size = size.normal, text = '                                                      ' + '75%  | '  + str.tostring(math.round(adr_pips * 0.75)) + ' Pips' + ' | ' + str.tostring(low_75_p, format.mintick), style = label.style_none, textcolor = color.new(#ea2700, 10), xloc = xloc.bar_time)
    label_low_100  := label.new(last_bar_time, low_100_p,  size = size.normal, text = '                                                      ' + '100% | ' + str.tostring(math.round(adr_pips)) + ' Pips' + ' | ' + str.tostring(low_100_p, format.mintick), style = label.style_none, textcolor = color.new(#ea2700, 10), xloc = xloc.bar_time)

    //Delete Label
    label.delete(label_t1_open[1])
    label.delete(label_high_100[1])
    label.delete(label_high_75[1])
    label.delete(label_high_50[1])
    label.delete(label_high_25[1])
    label.delete(label_low_25[1])
    label.delete(label_low_50[1])
    label.delete(label_low_75[1])
    label.delete(label_low_100[1])

    //Delete Line
    line.delete(tl[1])
    line.delete(high_100[1])
    line.delete(high_75[1])
    line.delete(high_50[1])
    line.delete(high_25[1])
    line.delete(low_25[1])
    line.delete(low_50[1])
    line.delete(low_75[1])
    line.delete(low_100[1])
//#endregion

//#region -------------------- Table Plots
//Plot strategy information in the table
strat_tbl = table.new(position.top_right, 2, 9)

//Colors for table
table.set_border_color(table_id = strat_tbl, border_color = color.white)
table.set_frame_color(strat_tbl, color.white)

//Logic for table info
//Current will be for the current ADR reading
current = res == 'D' ? '5 Day ADR' : res == 'W' ? '5 Week ADR' : res == 'M' ? '5 Month ADR' : na

//Yesterday will be for the previous ADR reading
yesterday = res == 'D' ? 'Yesterdays 5 Day ADR' : res == 'W' ? 'Last Weeks 5 Week ADR' : res == 'M' ? 'Last Months 5 Month ADR' : na

//Move will be the actual movement according to the previous ADR reading
move = res == 'D' ? 'Yesterdays Move' : res == 'W' ? 'Last Weeks Move' : res == 'M' ? 'Last Months Move' : na

//ht is for the recommended high time frame value for scalping
scalp_ht = 'Recommended HT Range'
scalp_ht_v = adr_pips * 0.03
scalp_ht_v := math.round(scalp_ht_v)

//mt is for the recommended medium time frame value for scalping
scalp_mt = 'Recommended MT Range'
scalp_mt_v = scalp_ht_v * 0.5
scalp_mt_v := math.round(scalp_mt_v)

//lt is for the recommended lower time frame value for scalping
scalp_lt = 'Recommended LT Range'
scalp_lt_v = scalp_mt_v * 0.5
scalp_lt_v := math.round(scalp_lt_v)

//ht is for the recommended high time frame value for swings
swing_ht = 'Recommended HT Range'
swing_ht_v = adr_pips * 0.1
swing_ht_v := math.round(swing_ht_v)

//mt is for the recommended medium time frame value for swings
swing_mt = 'Recommended MT Range'
swing_mt_v = swing_ht_v * 0.5
swing_mt_v := math.round(swing_mt_v)

//lt is for the recommended lower time frame value for swings
swing_lt = 'Recommended LT Range'
swing_lt_v = swing_mt_v * 0.5
swing_lt_v := math.round(swing_lt_v)


//Table info
table.cell(strat_tbl, 0, 0, str.tostring(current), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
table.cell(strat_tbl, 0, 1, str.tostring(yesterday), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
table.cell(strat_tbl, 0, 2, str.tostring(move), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
if show_swing
    table.cell(strat_tbl, 0, 3, str.tostring(swing_ht), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
    table.cell(strat_tbl, 0, 4, str.tostring(swing_mt), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
    table.cell(strat_tbl, 0, 5, str.tostring(swing_lt), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
else if show_scalp
    table.cell(strat_tbl, 0, 3, str.tostring(scalp_ht), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
    table.cell(strat_tbl, 0, 4, str.tostring(scalp_mt), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
    table.cell(strat_tbl, 0, 5, str.tostring(scalp_lt), text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)

//Table value
//Anything less than 100 pips probably not worth the time, so turns red in these cases
bg_adr = adr_pips > 100 ? color.new(#5bd50f, 40) : color.new(#ea2700, 40)
//If yesterdays value is greater than todays, it will be green
bg_y_move = y_move_p > adr_pips ? color.new(#5bd50f, 40) : color.new(#ea2700, 40)
//Green if price move 75% of yesterdays ADR
bg_y_move_75 = y_move_pips > y_move_75_pips ? color.new(#5bd50f, 40) : color.new(#ea2700, 40)

table.cell(strat_tbl, 1, 0, str.tostring(adr_pips), text_size = size.auto, text_color = color.white, bgcolor = bg_adr, text_halign = text.align_left)
table.cell(strat_tbl, 1, 1, str.tostring(y_move_p), text_size = size.auto, text_color = color.white, bgcolor = bg_y_move, text_halign = text.align_left)
table.cell(strat_tbl, 1, 2, str.tostring(y_move_pips), text_size = size.auto, text_color = color.white, bgcolor = bg_y_move_75, text_halign = text.align_left)

if syminfo.type == 'crypto'
    if show_swing
        table.cell(strat_tbl, 1, 3, str.tostring(swing_ht_v) + ' R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 4, str.tostring(swing_mt_v) + ' R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 5, str.tostring(swing_lt_v) + ' R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
    if show_scalp
        table.cell(strat_tbl, 1, 3, str.tostring(scalp_ht_v) + ' R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 4, str.tostring(scalp_mt_v) + ' R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 5, str.tostring(scalp_lt_v) + ' R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
if syminfo.type == 'forex'
    if show_swing
        table.cell(strat_tbl, 1, 3, str.tostring(swing_ht_v) + '0 R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 4, str.tostring(swing_mt_v) + '0 R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 5, str.tostring(swing_lt_v) + '0 R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
    if show_scalp
        table.cell(strat_tbl, 1, 3, str.tostring(scalp_ht_v) + '0 R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 4, str.tostring(scalp_mt_v) + '0 R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
        table.cell(strat_tbl, 1, 5, str.tostring(scalp_lt_v) + '0 R', text_size = size.auto, text_color = color.white, bgcolor = color.rgb(35, 35, 35), text_halign = text.align_left)
//#endregion

//#region -------------------- Alerts
//Section if you would like to input custom alerts
//#endregion




