Expanded Camarilla Levels (R5 + R6)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator('Expanded Camarilla Levels', overlay=true)
mode = input.string(title='HTF Method', defval='Auto', options=['Auto', 'User Defined'])
HTFm = input.timeframe('D', title='Time Frame (if HTF Method=User Defined)')
showlast = input(title='Show Only Last Period', defval=true)
showlabels = input(title='Show Labels', defval=true)
lstyle = input.string(title='Line Style', options=['Solid', 'Circles', 'Cross'], defval='Solid')
showhl1 = input(defval=false, title='Show R1/S1')
showhl2 = input(defval=false, title='Show R2/S2')
paddingTicks = input.int(defval = 0, title='# of ticks to allow after stop')

//auto higher time frame
HTFo = timeframe.period == '1' ? '30' : timeframe.period == '3' ? '60' : timeframe.period == '5' ? '240' : timeframe.period == '15' ? 'D' : timeframe.period == '30' ? 'D' : timeframe.period == '45' ? 'D' : timeframe.period == '60' ? 'D' : timeframe.period == '120' ? 'D' : timeframe.period == '180' ? 'D' : timeframe.period == '240' ? 'D' : timeframe.period == 'D' ? 'W' : timeframe.period == 'W' ? '4W' : 'D'

HTF = mode == 'Auto' ? HTFo : HTFm

highhtf = request.security(syminfo.tickerid, HTF, high[1], lookahead=barmerge.lookahead_on)
lowhtf = request.security(syminfo.tickerid, HTF, low[1], lookahead=barmerge.lookahead_on)
closehtf = request.security(syminfo.tickerid, HTF, close[1], lookahead=barmerge.lookahead_on)

RANGE = highhtf - lowhtf

// is this last bar for HTF?
islast = showlast ? request.security(syminfo.tickerid, HTF, barstate.islast, lookahead=barmerge.lookahead_on) : true

// Line Style
linestyle = lstyle == 'Solid' ? plot.style_line : lstyle == 'Circle' ? plot.style_circles : plot.style_cross

R6 = highhtf / lowhtf * closehtf
R4 = closehtf + RANGE * 1.1 / 2
R3 = closehtf + RANGE * 1.1 / 4
R5 = R4 + 1.168 * (R4 - R3)
R2 = closehtf + RANGE * 1.1 / 6
R1 = closehtf + RANGE * 1.1 / 12
S1 = closehtf - RANGE * 1.1 / 12
S2 = closehtf - RANGE * 1.1 / 6
S3 = closehtf - RANGE * 1.1 / 4
S4 = closehtf - RANGE * 1.1 / 2
S5 = S4 - 1.168 * (S3 - S4)
S6 = closehtf - (R6 - closehtf)


SellFrom = input.string(title='Sell from ', defval='R3', options=['R1', 'R2', 'R3', 'R4'])
BuyFrom = input.string(title='Buy from ', defval='S3', options=['S1', 'S2', 'S3', 'S4'])

pos = 0

iff_1 = BuyFrom == 'S4' ? S4 : 0
iff_2 = BuyFrom == 'S3' ? S3 : iff_1
iff_3 = BuyFrom == 'S2' ? S2 : iff_2
B = BuyFrom == 'S1' ? S1 : iff_3

iff_4 = SellFrom == 'R4' ? R4 : 0
iff_5 = SellFrom == 'R3' ? R3 : iff_4
iff_6 = SellFrom == 'R2' ? R2 : iff_5
S = SellFrom == 'R1' ? R1 : iff_6

iff_7 = close < B - paddingTicks ? -1 : nz(pos[1], 0)
pos := close > S + paddingTicks ? 1 : iff_7

int signal = if pos == 1
    1
else if pos == -1
    -1
else 
    0

// for tying into a strategy on tradingview
plot(signal, title="Ext Cam Strategy Connector", color=color.new(color.white, 100))

plot(islast ? R6 : na, title='R6', color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast ? R5 : na, title='R5', color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast ? R4 : na, title='R4', color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast ? R3 : na, title='R3', color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast ? S3 : na, title='S3', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
plot(islast ? S4 : na, title='S4', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
plot(islast ? S5 : na, title='S5', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
plot(islast ? S6 : na, title='S5', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
plot(islast and showhl1 ? R1 : na, title='R1', color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast and showhl1 ? S1 : na, title='S1', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
plot(islast and showhl2 ? R2 : na, title='R2', color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast and showhl2 ? S2 : na, title='S2', color=color.new(color.lime, 0), linewidth=1, style=linestyle)

// Labels
if showlabels
    var exlabels = array.new_label(0)
    for x = 0 to array.size(exlabels) > 0 ? array.size(exlabels) - 1 : na by 1
        label.delete(array.pop(exlabels))

    array.push(exlabels, label.new(x=bar_index + 20, y=S3, text='S3= ' + str.tostring(math.round_to_mintick(S3)), color=color.lime, textcolor=color.black, style=label.style_label_up, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=S4, text='S4= ' + str.tostring(math.round_to_mintick(S4)), color=color.lime, textcolor=color.black, style=label.style_label_up, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=S5, text='S5= ' + str.tostring(math.round_to_mintick(S5)), color=color.lime, textcolor=color.black, style=label.style_label_up, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=S6, text='S6= ' + str.tostring(math.round_to_mintick(S6)), color=color.lime, textcolor=color.black, style=label.style_label_up, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=R3, text='R3= ' + str.tostring(math.round_to_mintick(R3)), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=R4, text='R4= ' + str.tostring(math.round_to_mintick(R4)), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=R5, text='R5= ' + str.tostring(math.round_to_mintick(R5)), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price))
    array.push(exlabels, label.new(x=bar_index + 20, y=R6, text='R6= ' + str.tostring(math.round_to_mintick(R6)), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price))
    
    if showhl1
        array.push(exlabels, label.new(x=bar_index + 20, y=S1, text='S1= ' + str.tostring(math.round_to_mintick(S1)), color=color.lime, textcolor=color.black, style=label.style_label_up, yloc=yloc.price))
        array.push(exlabels, label.new(x=bar_index + 20, y=R1, text='R1= ' + str.tostring(math.round_to_mintick(R1)), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price))
    if showhl2
        array.push(exlabels, label.new(x=bar_index + 20, y=S2, text='S2= ' + str.tostring(math.round_to_mintick(S2)), color=color.lime, textcolor=color.black, style=label.style_label_up, yloc=yloc.price))
        array.push(exlabels, label.new(x=bar_index + 20, y=R2, text='R2= ' + str.tostring(math.round_to_mintick(R2)), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price))

