Pivot Points Standard Higher Timeframe
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5
indicator('Pivot Points Standard Higher Timeframe', shorttitle='PPS_HTF', overlay=true)

// 0. Inputs
// 1. Formulas
// 2. Variable
// 3. Switches
// 4. Constructs

// ————————————————————————————————————————————————————————————————————————————— 0. Inputs {
T0          = 'Small font size recommended for mobile app or multiple layout'
higher_TF   = input.timeframe(    'M', 'Timeframe')
i_b_label   = input.bool(        true,     'Label', inline = '0')
i_b_price   = input.bool(        true,     'Price', inline = '0')
i_b_percent = input.bool(        true,   'Percent', inline = '0', tooltip = 'Tick to show')
i_c_R1      = input.color(  color.red,        'R1', inline = '1')
i_c_PV      = input.color( color.blue,        'PV', inline = '1')
i_c_S1      = input.color(color.green,        'S1', inline = '1', tooltip = 'Color for labels and lines')
i_s_extend  = input.string('right', 'Extend line for last pivot', options = ['none', 'right'])
i_s_font    = input.string('normal',             'Font size', options = ['tiny', 'small',  'normal', 'large', 'huge'], tooltip = T0)
// }
// ————————————————————————————————————————————————————————————————————————————— 1. Formulas {
ohlc()           => [open[1], high[1], low[1], close[1]]
[pO, pH, pL, pC] =  request.security(syminfo.tickerid, higher_TF, ohlc(), lookahead=barmerge.lookahead_on)
PV_lvl           = math.avg(pH, pL, pC)
R1_lvl           = PV_lvl * 2 - pL
S1_lvl           = PV_lvl * 2 - pH
R1_pct           = (R1_lvl - PV_lvl) / PV_lvl
S1_pct           = (S1_lvl - PV_lvl) / PV_lvl
// }
// ————————————————————————————————————————————————————————————————————————————— 2. Variables {
var line R1_line = na, var line PV_line = na, var line S1_line = na
// }
// ————————————————————————————————————————————————————————————————————————————— 3. Switches {
[str_R1, str_PV, str_S1] = switch i_b_price
    true => [str.tostring(R1_lvl, '\n#.#'), str.tostring(PV_lvl, '\n#.#'), '\n- ' + str.tostring(S1_lvl, '#.#')]
[pct_R1, pct_S1] = switch i_b_percent
    true => [str.tostring(R1_pct, '\n#.#% ▲'),'\n' + str.tostring(S1_pct, '#.#%') +  ' ▼']
// }
// ————————————————————————————————————————————————————————————————————————————— 4. Constructs {    
if PV_lvl[1] != PV_lvl
    // Each line except for last pivot
	line.set_x2(R1_line, bar_index), line.set_extend(R1_line, extend.none)
	line.set_x2(PV_line, bar_index), line.set_extend(PV_line, extend.none)
	line.set_x2(S1_line, bar_index), line.set_extend(S1_line, extend.none)
    // Line for Last Pivot with extend right
	R1_line := line.new(bar_index, R1_lvl, bar_index, R1_lvl, color = i_c_R1, extend = i_s_extend == 'right' ? extend.right : extend.none)
	PV_line := line.new(bar_index, PV_lvl, bar_index, PV_lvl, color = i_c_PV, extend = i_s_extend == 'right' ? extend.right : extend.none)
	S1_line := line.new(bar_index, S1_lvl, bar_index, S1_lvl, color = i_c_S1, extend = i_s_extend == 'right' ? extend.right : extend.none)
    // Label for all pivot
    if i_b_label
    	label.new(bar_index, R1_lvl, 'R1' + str_R1 + pct_R1, color = i_c_R1, style=label.style_label_right, size = i_s_font)
    	label.new(bar_index, PV_lvl, 'PV' + str_PV,          color = i_c_PV, style=label.style_label_right, size = i_s_font)
    	label.new(bar_index, S1_lvl, 'S1' + str_S1 + pct_S1, color = i_c_S1, style=label.style_label_right, size = i_s_font)
if not na(PV_line) and line.get_x2(PV_line) != bar_index
	line.set_x2(R1_line, bar_index)
	line.set_x2(PV_line, bar_index)
	line.set_x2(S1_line, bar_index)
// }