You are on page 1of 22

indicator("Central Pivot Range", "CPR",

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ajithcpas

//@version=5
indicator("Central Pivot Range", "CPR", overlay=true, max_lines_count=500, max_labels_count=500)

AUTO = "Auto"
DAILY = "Daily"
WEEKLY = "Weekly"
MONTHLY = "Monthly"
QUARTERLY = "Quarterly"
YEARLY = "Yearly"

TRADITIONAL = "Traditional"
FIBONACCI = "Fibonacci"
CLASSIC = "Classic"
CAMARILLA = "Camarilla"

kind = input.string(title="Type", defval="Traditional", options=[TRADITIONAL, FIBONACCI, CLASSIC,


CAMARILLA])
cpr_time_frame = input.string(title="CPR Timeframe", defval=AUTO, options=[AUTO, DAILY, WEEKLY,
MONTHLY, QUARTERLY, YEARLY])
look_back = input.int(title="Number of CPR Back", defval=2, minval=1, maxval=5000)
is_daily_based = input.bool(title="Use Daily-based Values", defval=true, tooltip="When this option is
unchecked, CPR will use intraday data while calculating on intraday charts. If Extended Hours are displayed on
the chart, they will be taken into account during the CPR level calculation. If intraday OHLC values are
different from daily-based values (normal for stocks), the CPR levels will also differ.")
show_labels = input.bool(title="Show Labels", defval=true, group="labels")
show_prices = input.bool(title="Show Prices", defval=true, group="labels")
position_labels = input.string("Right", "Labels Position", options=["Left", "Right"], group="labels")
line_width = input.int(title="Line Width", defval=1, minval=1, maxval=100, group="levels")

var DEF_COLOR = #FB8C00


var arr_time = array.new_int()
var p = array.new_float()
var tp = array.new_float()
var bp = array.new_float()
cpr_show = input.bool(true, "", inline="CPR", group="levels")
cpr_color = input.color(color.purple, "CPR", inline="CPR", group="levels")
dev_cpr_show = input.bool(true, "", inline="CPR", group="levels")
dev_cpr_color = input.color(color.teal, "Dev CPR", inline="CPR", group="levels")

sr_show = input.bool(false, "Show SR Levels", inline="Show SR Levels", group="levels")


var r1 = array.new_float()
var s1 = array.new_float()
s1_show = input.bool(true, "", inline="S1/R1", group="levels")
s1_color = input.color(DEF_COLOR, "S1", inline="S1/R1" , group="levels")
r1_show = input.bool(true, "", inline="S1/R1", group="levels")
r1_color = input.color(DEF_COLOR, "R1", inline="S1/R1", group="levels")
var r1_5 = array.new_float()
var s1_5 = array.new_float()
s1_5_show = input.bool(true, "", inline="S1.5/R1.5", group="levels")
s1_5_color = input.color(DEF_COLOR, "S1.5", inline="S1.5/R1.5" , group="levels")
r1_5_show = input.bool(true, "", inline="S1.5/R1.5", group="levels")
r1_5_color = input.color(DEF_COLOR, "R1.5", inline="S1.5/R1.5", group="levels")
var r2 = array.new_float()
var s2 = array.new_float()
s2_show = input.bool(true, "", inline="S2/R2", group="levels")
s2_color = input.color(DEF_COLOR, "S2", inline="S2/R2", group="levels")
r2_show = input.bool(true, "", inline="S2/R2", group="levels")
r2_color = input.color(DEF_COLOR, "R2", inline="S2/R2", group="levels")
var r2_5 = array.new_float()
var s2_5 = array.new_float()
s2_5_show = input.bool(true, "", inline="S2.5/R2.5", group="levels")
s2_5_color = input.color(DEF_COLOR, "S2.5", inline="S2.5/R2.5" , group="levels")
r2_5_show = input.bool(true, "", inline="S2.5/R2.5", group="levels")
r2_5_color = input.color(DEF_COLOR, "R2.5", inline="S2.5/R2.5", group="levels")
var r3 = array.new_float()
var s3 = array.new_float()
s3_show = input.bool(true, "", inline="S3/R3", group="levels")
s3_color = input.color(DEF_COLOR, "S3", inline="S3/R3", group="levels")
r3_show = input.bool(true, "", inline="S3/R3", group="levels")
r3_color = input.color(DEF_COLOR, "R3", inline="S3/R3", group="levels")
var r4 = array.new_float()
var s4 = array.new_float()
s4_show = input.bool(true, "", inline="S4/R4", group="levels")
s4_color = input.color(DEF_COLOR, "S4", inline="S4/R4", group="levels")
r4_show = input.bool(true, "", inline="S4/R4", group="levels")
r4_color = input.color(DEF_COLOR, "R4", inline="S4/R4", group="levels")
var r5 = array.new_float()
var s5 = array.new_float()
s5_show = input.bool(true, "", inline="S5/R5", group="levels")
s5_color = input.color(DEF_COLOR, "S5", inline="S5/R5", group="levels")
r5_show = input.bool(true, "", inline="S5/R5", group="levels")
r5_color = input.color(DEF_COLOR, "R5", inline="S5/R5", group="levels")

pivotX_open = float(na)
pivotX_open := nz(pivotX_open[1], open)
pivotX_high = float(na)
pivotX_high := nz(pivotX_high[1], high)
pivotX_low = float(na)
pivotX_low := nz(pivotX_low[1], low)
pivotX_prev_open = float(na)
pivotX_prev_open := nz(pivotX_prev_open[1])
pivotX_prev_high = float(na)
pivotX_prev_high := nz(pivotX_prev_high[1])
pivotX_prev_low = float(na)
pivotX_prev_low := nz(pivotX_prev_low[1])
pivotX_prev_close = float(na)
pivotX_prev_close := nz(pivotX_prev_close[1])
get_pivot_resolution() =>
resolution = "M"
if cpr_time_frame == AUTO
if timeframe.isintraday
resolution := timeframe.multiplier <= 15 ? "D" : "W"
else if timeframe.isweekly or timeframe.ismonthly
resolution := "12M"
else if cpr_time_frame == DAILY
resolution := "D"
else if cpr_time_frame == WEEKLY
resolution := "W"
else if cpr_time_frame == MONTHLY
resolution := "M"
else if cpr_time_frame == QUARTERLY
resolution := "3M"
else if cpr_time_frame == YEARLY
resolution := "12M"
resolution

var lines = array.new_line()


var labels = array.new_label()

draw_line(i, pivot, col, line_style=line.style_solid) =>


if array.size(arr_time) > 1
array.push(lines, line.new(array.get(arr_time, i), array.get(pivot, i), array.get(arr_time, i + 1),
array.get(pivot, i), color=col, xloc=xloc.bar_time, width=line_width, style=line_style))

draw_label(i, y, txt, txt_color) =>


if show_labels or show_prices
display_text = ""
if show_labels and show_prices and txt != ''
display_text := str.format(" {0} - {1}", txt, math.round_to_mintick(y))
else if show_labels and txt != ''
display_text := " " + txt
else
display_text := str.format(" {0}", math.round_to_mintick(y))
x = position_labels == "Left" ? array.get(arr_time, i) : array.get(arr_time, i + 1)
array.push(labels, label.new(x=x, y=y, text=display_text, textcolor=txt_color, style=label.style_none,
color=#00000000, xloc=xloc.bar_time))

traditional() =>
pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3

_r1 = pivotX_Median * 2 - pivotX_prev_low


_s1 = pivotX_Median * 2 - pivotX_prev_high
_r2 = pivotX_Median + 1 * (pivotX_prev_high - pivotX_prev_low)
_s2 = pivotX_Median - 1 * (pivotX_prev_high - pivotX_prev_low)
_r3 = pivotX_Median * 2 + (pivotX_prev_high - 2 * pivotX_prev_low)
_s3 = pivotX_Median * 2 - (2 * pivotX_prev_high - pivotX_prev_low)
array.push(r1, _r1)
array.push(s1, _s1)
array.push(r1_5, (_r1 + _r2) / 2)
array.push(s1_5, (_s1 + _s2) / 2)
array.push(r2, _r2)
array.push(s2, _s2)
array.push(r2_5, (_r2 + _r3) / 2)
array.push(s2_5, (_s2 + _s3) / 2)
array.push(r3, _r3)
array.push(s3, _s3)
array.push(r4, pivotX_Median * 3 + (pivotX_prev_high - 3 * pivotX_prev_low))
array.push(s4, pivotX_Median * 3 - (3 * pivotX_prev_high - pivotX_prev_low))
array.push(r5, pivotX_Median * 4 + (pivotX_prev_high - 4 * pivotX_prev_low))
array.push(s5, pivotX_Median * 4 - (4 * pivotX_prev_high - pivotX_prev_low))

fibonacci() =>
pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
pivot_range = pivotX_prev_high - pivotX_prev_low

array.push(r1, pivotX_Median + 0.382 * pivot_range)


array.push(s1, pivotX_Median - 0.382 * pivot_range)
array.push(r2, pivotX_Median + 0.618 * pivot_range)
array.push(s2, pivotX_Median - 0.618 * pivot_range)
array.push(r3, pivotX_Median + 1 * pivot_range)
array.push(s3, pivotX_Median - 1 * pivot_range)

classic() =>
pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
pivot_range = pivotX_prev_high - pivotX_prev_low

array.push(r1, pivotX_Median * 2 - pivotX_prev_low)


array.push(s1, pivotX_Median * 2 - pivotX_prev_high)
array.push(r2, pivotX_Median + 1 * pivot_range)
array.push(s2, pivotX_Median - 1 * pivot_range)
array.push(r3, pivotX_Median + 2 * pivot_range)
array.push(s3, pivotX_Median - 2 * pivot_range)
array.push(r4, pivotX_Median + 3 * pivot_range)
array.push(s4, pivotX_Median - 3 * pivot_range)

camarilla() =>
pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
pivot_range = pivotX_prev_high - pivotX_prev_low

array.push(r1, pivotX_prev_close + pivot_range * 1.1 / 12.0)


array.push(s1, pivotX_prev_close - pivot_range * 1.1 / 12.0)
array.push(r2, pivotX_prev_close + pivot_range * 1.1 / 6.0)
array.push(s2, pivotX_prev_close - pivot_range * 1.1 / 6.0)
array.push(r3, pivotX_prev_close + pivot_range * 1.1 / 4.0)
array.push(s3, pivotX_prev_close - pivot_range * 1.1 / 4.0)
array.push(r4, pivotX_prev_close + pivot_range * 1.1 / 2.0)
array.push(s4, pivotX_prev_close - pivot_range * 1.1 / 2.0)
r5_val = pivotX_prev_high / pivotX_prev_low * pivotX_prev_close
array.push(r5, r5_val)
array.push(s5, 2 * pivotX_prev_close - r5_val)

calc_pivot() =>
pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
pivotX_Bottom = (pivotX_prev_high + pivotX_prev_low) / 2
pivotX_Top = pivotX_Median * 2 - pivotX_Bottom

if pivotX_Bottom > pivotX_Top


temp = pivotX_Bottom
pivotX_Bottom := pivotX_Top
pivotX_Top := temp

array.push(p, pivotX_Median)
array.push(tp, pivotX_Top)
array.push(bp, pivotX_Bottom)

if kind == TRADITIONAL
traditional()
else if kind == FIBONACCI
fibonacci()
else if kind == CLASSIC
classic()
else if kind == CAMARILLA
camarilla()

resolution = get_pivot_resolution()

calc_high(prev, curr) =>


if na(prev) or na(curr)
nz(prev, nz(curr, na))
else
math.max(prev, curr)

calc_low(prev, curr) =>


if not na(prev) and not na(curr)
math.min(prev, curr)
else
nz(prev, nz(curr, na))

[sec_open, sec_high, sec_low, prev_sec_open, prev_sec_high, prev_sec_low, prev_sec_close, prev_sec_time] =


request.security(syminfo.tickerid, resolution, [open, high, low, open[1], high[1], low[1], close[1], time[1]],
lookahead = barmerge.lookahead_on)
sec_open_gaps_on = request.security(syminfo.tickerid, resolution, open, gaps = barmerge.gaps_on, lookahead =
barmerge.lookahead_on)

var is_change = false


var uses_current_bar = false
var change_time = int(na)
is_time_change = ta.change(time(resolution))
if is_time_change
change_time := time

var start_time = time

without_time_change = barstate.islast and array.size(arr_time) == 0


is_can_calc_pivot = (not uses_current_bar and is_time_change) or (uses_current_bar and not
na(sec_open_gaps_on)) or without_time_change
enough_bars_for_calculate = prev_sec_time >= start_time or is_daily_based

if is_can_calc_pivot and enough_bars_for_calculate


if array.size(arr_time) == 0 and is_daily_based
pivotX_prev_open := prev_sec_open[1]
pivotX_prev_high := prev_sec_high[1]
pivotX_prev_low := prev_sec_low[1]
pivotX_prev_close := prev_sec_close[1]
pivotX_open := sec_open[1]
pivotX_high := sec_high[1]
pivotX_low := sec_low[1]
array.push(arr_time, start_time)
calc_pivot()

if is_daily_based
pivotX_prev_open := prev_sec_open
pivotX_prev_high := prev_sec_high
pivotX_prev_low := prev_sec_low
pivotX_prev_close := prev_sec_close
pivotX_open := sec_open
pivotX_high := sec_high
pivotX_low := sec_low
else
pivotX_prev_high := pivotX_high
pivotX_prev_low := pivotX_low
pivotX_prev_open := pivotX_open
pivotX_prev_close := close[1]
pivotX_open := open
pivotX_high := high
pivotX_low := low

if barstate.islast and not is_change and array.size(arr_time) > 0 and not without_time_change
array.set(arr_time, array.size(arr_time) - 1, change_time)
else if without_time_change
array.push(arr_time, start_time)
else
array.push(arr_time, nz(change_time, time))

calc_pivot()

if array.size(arr_time) > look_back


if array.size(arr_time) > 0
array.shift(arr_time)
if array.size(p) > 0 and cpr_show
array.shift(p)
if array.size(tp) > 0 and cpr_show
array.shift(tp)
if array.size(bp) > 0 and cpr_show
array.shift(bp)

if sr_show
if array.size(r1) > 0 and r1_show
array.shift(r1)
if array.size(s1) > 0 and s1_show
array.shift(s1)
if array.size(r1_5) > 0 and r1_5_show
array.shift(r1_5)
if array.size(s1_5) > 0 and s1_5_show
array.shift(s1_5)
if array.size(r2) > 0 and r2_show
array.shift(r2)
if array.size(s2) > 0 and s2_show
array.shift(s2)
if array.size(r2_5) > 0 and r2_5_show
array.shift(r2_5)
if array.size(s2_5) > 0 and s2_5_show
array.shift(s2_5)
if array.size(r3) > 0 and r3_show
array.shift(r3)
if array.size(s3) > 0 and s3_show
array.shift(s3)
if array.size(r4) > 0 and r4_show
array.shift(r4)
if array.size(s4) > 0 and s4_show
array.shift(s4)
if array.size(r5) > 0 and r5_show
array.shift(r5)
if array.size(s5) > 0 and s5_show
array.shift(s5)
is_change := true
else if not is_daily_based
pivotX_high := math.max(pivotX_high, high)
pivotX_low := math.min(pivotX_low, low)

if barstate.islast and not is_daily_based and array.size(arr_time) == 0


runtime.error("Not enough intraday data to calculate CPR. Lower the CPR Timeframe or turn on the 'Use
Daily-based Values' option in the indicator settings.")

if barstate.islast and array.size(arr_time) > 0 and is_change


is_change := false
array.push(arr_time, time_close(resolution))

for i = 0 to array.size(lines) - 1
if array.size(lines) > 0
line.delete(array.shift(lines))
if array.size(labels) > 0
label.delete(array.shift(labels))

for i = 0 to array.size(arr_time) - 2
if array.size(p) > 0 and cpr_show
draw_line(i, p, cpr_color)
if array.size(tp) > 0 and cpr_show
draw_line(i, tp, cpr_color)
if array.size(bp) > 0 and cpr_show
draw_line(i, bp, cpr_color)

for i = array.size(arr_time) - 2 to array.size(arr_time) - 2


if array.size(p) > 0 and cpr_show
draw_label(i, array.get(p, i), "CPR", cpr_color)
if array.size(tp) > 0 and cpr_show
draw_label(i, array.get(tp, i), "", cpr_color)
if array.size(bp) > 0 and cpr_show
draw_label(i, array.get(bp, i), "", cpr_color)

if sr_show
if array.size(r1) > 0 and r1_show
draw_line(i, r1, r1_color)
draw_label(i, array.get(r1, i), "R1", r1_color)
if array.size(s1) > 0 and s1_show
draw_line(i, s1, s1_color)
draw_label(i, array.get(s1, i), "S1", s1_color)
if array.size(r1_5) > 0 and r1_5_show
draw_line(i, r1_5, r1_5_color, line.style_dashed)
draw_label(i, array.get(r1_5, i), "R1.5", r1_5_color)
if array.size(s1_5) > 0 and s1_5_show
draw_line(i, s1_5, s1_5_color, line.style_dashed)
draw_label(i, array.get(s1_5, i), "S1.5", s1_5_color)
if array.size(r2) > 0 and r2_show
draw_line(i, r2, r2_color)
draw_label(i, array.get(r2, i), "R2", r2_color)
if array.size(s2) > 0 and s2_show
draw_line(i, s2, s2_color)
draw_label(i, array.get(s2, i), "S2", s2_color)
if array.size(r2_5) > 0 and r2_5_show
draw_line(i, r2_5, r2_5_color, line.style_dashed)
draw_label(i, array.get(r2_5, i), "R2.5", r2_5_color)
if array.size(s2_5) > 0 and s2_5_show
draw_line(i, s2_5, s2_5_color, line.style_dashed)
draw_label(i, array.get(s2_5, i), "S2.5", s2_5_color)
if array.size(r3) > 0 and r3_show
draw_line(i, r3, r3_color)
draw_label(i, array.get(r3, i), "R3", r3_color)
if array.size(s3) > 0 and s3_show
draw_line(i, s3, s3_color)
draw_label(i, array.get(s3, i), "S3", s3_color)
if array.size(r4) > 0 and r4_show
draw_line(i, r4, r4_color)
draw_label(i, array.get(r4, i), "R4", r4_color)
if array.size(s4) > 0 and s4_show
draw_line(i, s4, s4_color)
draw_label(i, array.get(s4, i), "S4", s4_color)
if array.size(r5) > 0 and r5_show
draw_line(i, r5, r5_color)
draw_label(i, array.get(r5, i), "R5", r5_color)
if array.size(s5) > 0 and s5_show
draw_line(i, s5, s5_color)
draw_label(i, array.get(s5, i), "S5", s5_color)

drawCPRLine(_price, _line_color, _text, _text_color) =>


end_of_day = time_close(resolution)
_aLine = line.new(x1=time(resolution), y1=_price, x2=end_of_day, y2=_price, xloc=xloc.bar_time,
color=_line_color, width=line_width)
line.delete(_aLine[1])

if show_labels or show_prices
display_text = ""
if show_labels and show_prices and _text != ''
display_text := str.format(" {0} - {1}", _text, math.round_to_mintick(_price))
else if show_labels and _text != ''
display_text := " " + _text
else
display_text := str.format(" {0}", math.round_to_mintick(_price))

_aLabel = label.new(end_of_day, _price, xloc=xloc.bar_time, text=display_text, textcolor=_text_color,


style=label.style_none)
label.delete(_aLabel[1])

drawCPR(resolution, cpr_text, line_color) =>


_high = request.security(syminfo.tickerid, resolution, high)
_low = request.security(syminfo.tickerid, resolution, low)
_close = request.security(syminfo.tickerid, resolution, close)
dpp = (_high + _low + _close) / 3.0
dbc = (_high + _low) / 2.0
dtc = (dpp * 2) - dbc
drawCPRLine(dtc > dbc? dtc : dbc, line_color, '', line_color)
drawCPRLine(dpp, line_color, cpr_text, line_color)
drawCPRLine(dbc < dtc? dbc : dtc, line_color, '', line_color)

if dev_cpr_show
drawCPR(resolution, "Dev CPR", dev_cpr_color)

indicator('OSPL Volume'
//@version=5
//Feel free to reuse or modify this code. Thanks to opensource pinescript indicators that I referred extensively
and learnt a lot.
//Thanks to Options Scalper Siva for all the free knowledge and info he imparts to the community. Follow him
in twitter @justsiva123
//This volume indicator is inspired by the paid/premium OSPL Volume indicator and works 100% the same.
indicator('OSPL Volume', '', false, format.volume)
showMA = input.bool(true,"Show Moving Average (MA)?",inline = "01", group='Moving Average')
MALen = input.int(20,"MA Length",inline = "01", group='Moving Average')
NiftyVol = input.int(125000,"Nifty",inline = "01", group='High Volume Treshold')
BNVol = input.int(50000,"Nifty Bank",inline = "01", group='High Volume Treshold')
highvolbull=input.color(color.rgb(91, 255, 96),"High Volume Positive Candle",group="Colors")
highvolbear=input.color(color.rgb(253, 44, 7),"High Volume Negative Candle",group="Colors")
stdbull=input.color(color.rgb(76, 175, 79, 64),"Standard Positive Candle",group="Colors")
stdbear=input.color(color.rgb(255, 82, 82, 64),"Standard Negative Candle",group="Colors")

string i_maType2 = syminfo.prefix + ":" + syminfo.ticker


hi_vol= i_maType2=="NSE:BANKNIFTY1!"?BNVol:i_maType2=="NSE:NIFTY1!"?NiftyVol:na
colorofbar = open < close and volume>hi_vol? highvolbull:open > close and volume>hi_vol? highvolbear:open
< close?stdbull:open > close?stdbear:na
plot(volume, color=colorofbar, title='Volume', style=plot.style_columns)
plot(showMA? ta.sma(volume, MALen):na, color=input.color(color.new(color.white, 65),"Moving
Average",group="Colors"), title='Volume MA', style=plot.style_line,linewidth = 1)

Indicator - camarilla
//@version=4
study(title="HCAM Pivots", overlay=true)
TimeFrame = input("D", type=input.resolution)
xx = input(true, "Regular or HACAM, True = Regular Cam Pivot")
/////////////////////////////////////////
//TF for multi time frame and back period
/////////////////////////////////////////
ha_handle = heikinashi(syminfo.tickerid)
prevOpenHTF = security(xx ? syminfo.tickerid : ha_handle, TimeFrame, open[1], lookahead=true)
prevCloseHTF = security(xx ? syminfo.tickerid : ha_handle, TimeFrame, close[1], lookahead=true)
prevHighHTF = security(xx ? syminfo.tickerid : ha_handle, TimeFrame, high[1], lookahead=true)
prevLowHTF = security(xx ? syminfo.tickerid : ha_handle, TimeFrame, low[1], lookahead=true)

/////////////////////////////////////////
// Function outputs 1 when it's the first bar of the D/W/M/Y
is_newbar(res) =>
ch = 0
if(res == 'Y')
t = year(time('D'))
ch := change(t) != 0 ? 1 : 0
else
t = time(res)
ch := change(t) != 0 ? 1 : 0
ch
// Rounding levels to min tick
nround(x) =>
n = round(x / syminfo.mintick) * syminfo.mintick

////////////
// INPUTS //
////////////
pp_type = input(title = "**Pivot Type**", defval = "Camarilla")
show_level_value = input(title = "Show Levels Value?", defval = true,type = input.bool)
sm = input(true, title="Show R3S3?")
sgg = input(false, title="Show Label 5 & 6?")

///////////////////////////
// CALCULATE PIVOT POINTS //
////////////////////////////
Range = 0.0
PP = 0.0
R3 = 0.0, R4 = 0.0, R5 = 0.0, R6 = 0.0
S3 = 0.0, S4 = 0.0, S5 = 0.0, S6 = 0.0
Range := (prevHighHTF - prevLowHTF)

if (pp_type == "Camarilla")
PP := (prevHighHTF + prevLowHTF + prevCloseHTF) / 3

R3 := prevCloseHTF + (Range) * 1.1/4


S3 := prevCloseHTF - (Range) * 1.1/4
R4 := prevCloseHTF + (Range) * 1.1/2
S4 := prevCloseHTF - (Range) * 1.1/2
R5 := prevCloseHTF + (Range) * 0.8244
S5 := prevCloseHTF - (Range) * 0.8244
R6 := prevCloseHTF + (Range) * 1.0076
S6 := prevCloseHTF - (Range) * 1.0992

//////////////
// PLOTTING //

bars_sinse = 0
bars_sinse := is_newbar(TimeFrame) ? 0 : bars_sinse[1] + 1

////////////////////////
// PLOT PIVOTS LEVELS //

vpp_p = line.new(bar_index[min(bars_sinse, 300)], PP, bar_index, PP, color=color.white, style =


line.style_solid)

vs3_p = line.new(sm ? bar_index[min(bars_sinse, 300)] :na, S3, bar_index, S3, color=color.red, style =
line.style_solid)
vr3_p = line.new(sm ? bar_index[min(bars_sinse, 300)] :na, R3, bar_index, R3, color=color.green, style =
line.style_solid)
// delete previous lines in the same period
if (is_newbar(TimeFrame))
line.delete(vpp_p[1])
line.delete(vs3_p[1])
line.delete(vr3_p[1])

// Add labels
dt = 6*(time - time[1])
//if barstate.islast
// label.new(time + 3*dt, close, xloc=xloc.bar_time)

label_vpp = label.new(time + 3*dt, PP, text=show_level_value ? ("P" + " " + tostring(nround(PP))) : "P",
style= label.style_labeldown, size=size.small, color= color.green, xloc=xloc.bar_time)
label_vs3 = label.new(time + 3*dt, S3, text=show_level_value ? ("S3" + " " + tostring(nround(S3))) : "S3",
style= label.style_labelup, size = size.small, xloc=xloc.bar_time)
label_vs4 = label.new(time + 3*dt, S4, text=show_level_value ? ("S4" + " " + tostring(nround(S4))) : "S4",
style= label.style_labelup, size = size.small, xloc=xloc.bar_time)
label_vs5 = label.new(time + 3*dt, sgg ? S5 :na, text=show_level_value ? ("S5" + " " + tostring(nround(S5))) :
"S5", style= label.style_labelup, size = size.small, xloc=xloc.bar_time)
label_vs6 = label.new(time + 3*dt, sgg ? S6 :na, text=show_level_value ? ("S6" + " " + tostring(nround(S6))) :
"S6", style= label.style_labelup, size = size.small, xloc=xloc.bar_time)

label_vr3 = label.new(time + 3*dt, R3, text=show_level_value ? ("R3" + " " + tostring(nround(R3))) : "R3",
style= label.style_labeldown, size = size.small, color = color.red, xloc=xloc.bar_time)
label_vr4 = label.new(time + 3*dt, R4, text=show_level_value ? ("R4" + " " + tostring(nround(R4))) : "R4",
style= label.style_labeldown, size = size.small, color = color.red, xloc=xloc.bar_time)
label_vr5 = label.new(time + 3*dt, sgg ? R5 :na, text=show_level_value ? ("R5" + " " + tostring(nround(R5))) :
"R5", style= label.style_labeldown, size = size.small, color = color.red, xloc=xloc.bar_time)
label_vr6 = label.new(time + 3*dt, sgg ? R6 :na, text=show_level_value ? ("R6" + " " + tostring(nround(R6))) :
"R6", style= label.style_labeldown, size = size.small, color = color.red, xloc=xloc.bar_time)

label.delete(label_vpp[1])
label.delete(label_vs3[1])
label.delete(label_vs4[1])
label.delete(label_vs5[1])
label.delete(label_vs6[1])
label.delete(label_vr3[1])
label.delete(label_vr4[1])
label.delete(label_vr5[1])
label.delete(label_vr6[1])

//@version=4
//Indicator for Daily CPR, Monthly Pivots, Weekly Pivots, Highs and Lows,
Moving averages and Camarilla pivots.
//Number of days pivots need to be shown can be set
study(title="CPR Camarilla & MA", shorttitle="CPR & MA", overlay=true)
//Inputs
daily_cpr = input (title="Number of Daily CPR to show", type= input.integer , defval=1, minval=0)
weekly_pivot_back = input (title="Number of Weekly pivot to show ", type = input.integer , defval=1,
minval=0)
monthly_pivot_back = input (title="Number of Monthly pivot to show ", type = input.integer , defval=1,
minval=0)

showCPR = input(true, title="Show Daily CPR")


showWeekly = input(true, title="Show Weekly Pivots")
showMonthly = input(true, title="Show Monthly Pivots")

showPrevDayHL = input(true, title="Show Prev Day HL")


showPrevWeekHL = input(true, title="Show Prev Week HL")
showPrevMonthHL = input(true, title="Show Prev Month HL")

showTomorrowCPR = input(false, title="Show Tomorrow CPR")


tomorrowCPRType = input(title="Tomorrow CPR Type", defval="D", options=["D", "W", "M"])

showEMA = input(false, title="Show EMA")

showSMA = input(true, title="Show SMA")


showCamarilla = input(false, title="Show Camarilla levels")
showCamarillaInner = input(false, title="Show Camarilla inner levels")

new_bar(res) => change(time(res)) != 0


new_period(condition, src) =>
result = 0.0
result := condition ? src : result[1]
result

// Pivot calculation
pivot = (high + low + close) / 3.0
bc = (high + low) / 2.0
tc = (pivot - bc) + pivot
R1 = (2*pivot) - low
S1 = (2*pivot) - high
R2 = pivot + ( high - low)
S2 = pivot - ( high - low)
R3 = high + (2*(pivot - low))
S3 = low - (2*(high - pivot ))
R4 = R3 + (R2 - R1)
S4 = S3 + (S2 - S1)
PH = high
PL= low

//Daily Central Pivot Range


dpp = security( syminfo.tickerid , 'D', pivot[1], lookahead=barmerge.lookahead_on)
dbc = security( syminfo.tickerid , 'D', bc[1], lookahead=barmerge.lookahead_on)
dtc = security( syminfo.tickerid , 'D', tc[1], lookahead=barmerge.lookahead_on)
dR1 = security( syminfo.tickerid , 'D', R1[1], lookahead=barmerge.lookahead_on)
dS1 = security( syminfo.tickerid , 'D', S1[1], lookahead=barmerge.lookahead_on)
dR2 = security( syminfo.tickerid , 'D', R2[1], lookahead=barmerge.lookahead_on)
dS2 = security( syminfo.tickerid , 'D', S2[1], lookahead=barmerge.lookahead_on)
dR3 = security( syminfo.tickerid , 'D', R3[1], lookahead=barmerge.lookahead_on)
dS3 = security( syminfo.tickerid , 'D', S3[1], lookahead=barmerge.lookahead_on)
dR4 = security( syminfo.tickerid , 'D', R4[1], lookahead=barmerge.lookahead_on)
dS4 = security( syminfo.tickerid , 'D', S4[1], lookahead=barmerge.lookahead_on)
dPH = security( syminfo.tickerid , 'D', PH[1], lookahead=barmerge.lookahead_on)
dPL = security( syminfo.tickerid , 'D', PL[1], lookahead=barmerge.lookahead_on)

one_day = 1000 * 60 * 60 * 24

//Daily pivots based on number of pivots back selection


new_day = daily_cpr > 0 and timenow - time < one_day * daily_cpr and new_bar("D")
dpp_ = new_period(new_day, dpp)
dtc_ = new_period(new_day, dtc)
dbc_ = new_period(new_day, dbc)
dR1_ = new_period(new_day, dR1)
dS1_ = new_period(new_day, dS1)
dR2_ = new_period(new_day, dR2)
dS2_ = new_period(new_day, dS2)
dR3_ = new_period(new_day, dR3)
dS3_ = new_period(new_day, dS3)
dR4_ = new_period(new_day, dR4)
dS4_ = new_period(new_day, dS4)
dPH_ = new_period(new_day, dPH)
dPL_ = new_period(new_day, dPL)

plot( (timeframe.isintraday and showCPR) ? (dtc_ >= dbc_ ? dtc_ : dbc_) : na, title="Daily TC", style=
plot.style_circles , color=#2196f3, linewidth=2)
plot( (timeframe.isintraday and showCPR) ? dpp_ : na, title="Daily PP", style= plot.style_circles,
color=#9C27B0, linewidth=2)
plot( (timeframe.isintraday and showCPR) ? (dtc_ >= dbc_ ? dbc_ : dtc_) : na, title="Daily BC", style=
plot.style_circles, color=#2196f3, linewidth=2)

plot(showCPR ? dR1_ : na, title='R1', title="Daily R1", style= plot.style_circles , color=#ff0000, linewidth=3)
plot(showCPR ? dR2_ : na, title='R2', title="Daily R2", style= plot.style_circles , color=#ff0000, linewidth=2)
plot(showCPR ? dR3_ : na, title='R3', title="Daily R3", style= plot.style_circles , color=#ff0000, linewidth=2)
plot(showCPR ? dR4_ : na, title='R4', title="Daily R4", style= plot.style_circles , color=#ff0000, linewidth=2)
plot(showCPR ? dS1_ : na, title='S1', title="Daily S1", style= plot.style_circles , color=#008000, linewidth=3)
plot(showCPR ? dS2_ : na, title='S2', title="Daily S2", style= plot.style_circles , color=#008000, linewidth=2)
plot(showCPR ? dS3_ : na, title='S3', title="Daily S3", style= plot.style_circles , color=#008000, linewidth=2)
plot(showCPR ? dS4_ : na, title='S4', title="Daily S4", style= plot.style_circles , color=#008000, linewidth=2)
plot(showPrevDayHL ? dPH_ : na, title='PDH', title="Previous Day High", style= plot.style_circles ,
color=#ff9800, linewidth=2)
plot(showPrevDayHL ? dPL_ : na, title='PDL', title="Previous Day Low", style= plot.style_circles ,
color=#ff9800, linewidth=2)
//Weekly

wtime_pvt = security(syminfo.tickerid, 'W', pivot[1], lookahead=barmerge.lookahead_on)


wtime_R1 = security(syminfo.tickerid, 'W', R1[1], lookahead=barmerge.lookahead_on)
wtime_S1 = security(syminfo.tickerid, 'W', S1[1], lookahead=barmerge.lookahead_on)
wtime_R2 = security(syminfo.tickerid, 'W', R2[1], lookahead=barmerge.lookahead_on)
wtime_S2 = security(syminfo.tickerid, 'W', S2[1], lookahead=barmerge.lookahead_on)
wtime_R3 = security(syminfo.tickerid, 'W', R3[1], lookahead=barmerge.lookahead_on)
wtime_S3 = security(syminfo.tickerid, 'W', S3[1], lookahead=barmerge.lookahead_on)
wtime_R4 = security(syminfo.tickerid, 'W', R4[1], lookahead=barmerge.lookahead_on)
wtime_S4 = security(syminfo.tickerid, 'W', S4[1], lookahead=barmerge.lookahead_on)
whigh = security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
wlow = security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)

//Weekly pivots based on number of pivots back selection


one_week = one_day * 7
new_week = weekly_pivot_back > 0 and timenow - time < one_week * weekly_pivot_back and new_bar("W")

wtime_pvt_ = new_period(new_week, wtime_pvt)


wtime_R1_ = new_period(new_week, wtime_R1)
wtime_S1_ = new_period(new_week, wtime_S1)
wtime_R2_ = new_period(new_week, wtime_R2)
wtime_S2_ = new_period(new_week, wtime_S2)
wtime_R3_ = new_period(new_week, wtime_R3)
wtime_S3_ = new_period(new_week, wtime_S3)
wtime_R4_ = new_period(new_week, wtime_R4)
wtime_S4_ = new_period(new_week, wtime_S4)

//week high low


whigh_ = new_period(new_week, whigh)
wlow_ = new_period(new_week, wlow)

//Plotting weekly pivots


plot(showWeekly and wtime_pvt_ ? wtime_pvt_ : na, title="Weekly pvt",style=plot.style_circles,
color=#ffffff,linewidth=2)
plot(showWeekly and wtime_R1_ ? wtime_R1_ : na, title="Weekly R1",style=plot.style_circles,
color=#a5d6a7,linewidth=2)
plot(showWeekly and wtime_S1_ ? wtime_S1_ : na, title="Weekly S1",style=plot.style_circles,
color=#ef9a9a,linewidth=2)
plot(showWeekly and wtime_R2_ ? wtime_R2_ : na, title="Weekly R2",style=plot.style_circles,
color=#a5d6a7,linewidth=2)
plot(showWeekly and wtime_S2_ ? wtime_S2_ : na, title="Weekly S2",style=plot.style_circles,
color=#ef9a9a,linewidth=2)
plot(showWeekly and wtime_R3_ ? wtime_R3_ : na, title="Weekly R3",style=plot.style_circles,
color=#a5d6a7,linewidth=2)
plot(showWeekly and wtime_S3_ ? wtime_S3_ : na, title="Weekly S3",style=plot.style_circles,
color=#ef9a9a,linewidth=2)
plot(showWeekly and wtime_R4_ ? wtime_R4_ : na, title="Weekly R4",style=plot.style_circles,
color=#a5d6a7,linewidth=2)
plot(showWeekly and wtime_S4_ ? wtime_S4_ : na, title="Weekly S4",style=plot.style_circles,
color=#ef9a9a,linewidth=2)

//plotting weekly high lows


plot(showPrevWeekHL and whigh_ ? whigh_ : na, title="Prev Week High",style=plot.style_cross,
color=#ffffff,linewidth=2)
plot(showPrevWeekHL and wlow_ ? wlow_ : na, title="Prev Week Low",style=plot.style_cross,
color=#ffffff,linewidth=2)

//Monthly pvts

mtime_pvt = security(syminfo.tickerid, 'M', pivot[1], lookahead=barmerge.lookahead_on)


mtime_R1 = security(syminfo.tickerid, 'M', R1[1], lookahead=barmerge.lookahead_on)
mtime_S1 = security(syminfo.tickerid, 'M', S1[1], lookahead=barmerge.lookahead_on)
mtime_R2 = security(syminfo.tickerid, 'M', R2[1], lookahead=barmerge.lookahead_on)
mtime_S2 = security(syminfo.tickerid, 'M', S2[1], lookahead=barmerge.lookahead_on)
mtime_R3 = security(syminfo.tickerid, 'M', R3[1], lookahead=barmerge.lookahead_on)
mtime_S3 = security(syminfo.tickerid, 'M', S3[1], lookahead=barmerge.lookahead_on)
//mtime_R4 = security(syminfo.tickerid, 'M', R4[1], lookahead=barmerge.lookahead_on)
//mtime_S4 = security(syminfo.tickerid, 'M', S4[1], lookahead=barmerge.lookahead_on)

//Prev Month High Low


mhigh = security(syminfo.tickerid, "M", high[1], lookahead=barmerge.lookahead_on)
mlow = security(syminfo.tickerid, "M", low[1], lookahead=barmerge.lookahead_on)

//Monthly pivots based on number of pivots back selection


one_month = one_day * 30
new_month = monthly_pivot_back > 0 and timenow - time < one_month * monthly_pivot_back and
new_bar("M")

mtime_pvt_ = new_period(new_month, mtime_pvt)


mtime_R1_ = new_period(new_month, mtime_R1)
mtime_S1_ = new_period(new_month, mtime_S1)
mtime_R2_ = new_period(new_month, mtime_R2)
mtime_S2_ = new_period(new_month, mtime_S2)
mtime_R3_ = new_period(new_month, mtime_R3)
mtime_S3_ = new_period(new_month, mtime_S3)
//mtime_R4_ = new_period(new_month, mtime_R4)
//mtime_S4_ = new_period(new_month, mtime_S4)
mhigh_ = new_period(new_month, mhigh)
mlow_ = new_period(new_month, mlow)

//Plotting monthly pivots


plot(showMonthly and mtime_pvt_ ? mtime_pvt_ : na, title="Monthly pvt",style=plot.style_circles,
color=#f8bbd0,linewidth=2)
plot(showMonthly and mtime_R1_ ? mtime_R1_ : na, title="Monthly R1",style=plot.style_circles,
color=#26a69a,linewidth=2)
plot(showMonthly and mtime_S1_ ? mtime_S1_ : na, title="Monthly S1",style=plot.style_circles,
color=#26a69a,linewidth=2)
plot(showMonthly and mtime_R2_ ? mtime_R2_ : na, title="Monthly R2",style=plot.style_circles,
color=#26a69a,linewidth=2)
plot(showMonthly and mtime_S2_ ? mtime_S2_ : na, title="Monthly S2",style=plot.style_circles,
color=#26a69a,linewidth=2)
plot(showMonthly and mtime_R3_ ? mtime_R3_ : na, title="Monthly R3",style=plot.style_circles,
color=#26a69a,linewidth=2)
plot(showMonthly and mtime_S3_ ? mtime_S3_ : na, title="Monthly S3",style=plot.style_circles,
color=#26a69a,linewidth=2)
//plot(showMonthly and mtime_R4_ ? mtime_R4_ : na, title="Monthly R4",style=plot.style_circles,
color=#26a69a,linewidth=2)
//plot(showMonthly and mtime_S4_ ? mtime_S4_ : na, title="Monthly S4",style=plot.style_circles,
color=#26a69a,linewidth=2)

//plotting Monthly high lows


plot(showPrevMonthHL and mhigh_ ? mhigh_ : na, title="Prev Month High",style=plot.style_cross,
color=#f8bbd0,linewidth=2)
plot(showPrevMonthHL and mlow_ ? mlow_ : na, title="Prev Month Low",style=plot.style_cross,
color=#f8bbd0,linewidth=2)

//Tomorrow CPR

//Tomorrow's Pivot Calculation


tpopen = security(syminfo.tickerid, tomorrowCPRType, open, barmerge.gaps_off, barmerge.lookahead_on)
tphigh = security(syminfo.tickerid, tomorrowCPRType, high, barmerge.gaps_off, barmerge.lookahead_on)
tplow = security(syminfo.tickerid, tomorrowCPRType, low, barmerge.gaps_off, barmerge.lookahead_on)
tpclose = security(syminfo.tickerid, tomorrowCPRType, close, barmerge.gaps_off, barmerge.lookahead_on)
tprange = tphigh - tplow

tppivot = (tphigh + tplow + tpclose) / 3.0


tpbc = (tphigh + tplow) / 2.0
tptc = tppivot - tpbc + tppivot
tpr1 = tppivot * 2 - tplow
tps1 = tppivot * 2 - tphigh

//Tommorow Pivots plotting and labels


plot(showTomorrowCPR and tppivot ? tppivot : na, title="Tomrrow Pivot", color=#2196f3,
style=plot.style_circles, transp=0, linewidth=2)
plot(showTomorrowCPR and tpbc ? tpbc : na, title="Tomrrow BC", color=#2196f3, style=plot.style_circles,
transp=0, linewidth=2)
plot(showTomorrowCPR and tptc ? tptc : na, title="Tomrrow TC", color=#2196f3, style=plot.style_circles,
transp=0, linewidth=2)
plot(showTomorrowCPR and tpr1 ? tpr1 : na, title="Tomrrow R1", color=#ff0000, style=plot.style_circles,
transp=0, linewidth=2)
plot(showTomorrowCPR and tps1 ? tps1 : na, title="Tomrrow S1", color=#008000, style=plot.style_circles,
transp=0, linewidth=2)

////// Moving Aaverages


//EMA

//EMA 1
lenE1 = input(9, minval=1, title="EMA 1 Length")
srcE1 = input(close, title="Source")
outE1 = ema(srcE1, lenE1)
plot(showEMA ? outE1 : na, color=#fb41ff, title="EMA 1", linewidth=2)

//EMA 2
lenE2 = input(20, minval=1, title="EMA 2 Length")
srcE2 = input(close, title="Source")
outE2 = ema(srcE2, lenE2)
plot(showEMA ? outE2 : na, color=#fb41ff, title="EMA 2", linewidth=2)

//EMA 3
lenE3 = input(50, minval=1, title="EMA 3 Length")
srcE3 = input(close, title="Source")
outE3 = ema(srcE3, lenE3)
plot(showEMA ? outE3 : na, color=#fb41ff, title="EMA 3", linewidth=2)

//EMA 4
lenE4 = input(100, minval=1, title="EMA 4 Length")
srcE4 = input(close, title="Source")
outE4 = ema(srcE4, lenE4)
plot(showEMA ? outE4 : na, color=#fb41ff, title="EMA 4", linewidth=2)

//EMA 5
lenE5 = input(200, minval=1, title="EMA 5 Length")
srcE5 = input(close, title="Source")
outE5 = ema(srcE5, lenE5)
plot(showEMA ? outE5 : na, color=#fb41ff, title="EMA 5", linewidth=2)

//SMA

//SMA 1
lenS1 = input(9, minval=1, title="SMA 1 Length")
srcS1 = input(close, title="Source")
outS1 = sma(srcS1, lenS1)
plot(showSMA ? outS1 : na, color=#ffeb3b, title="SMA 1", linewidth=2)

//SMA 2
lenS2 = input(20, minval=1, title="SMA 2 Length")
srcS2 = input(close, title="Source")
outS2 = sma(srcS2, lenS2)
plot(showSMA ? outS2 : na, color=#008000, title="SMA 2", linewidth=2)

//SMA 3
lenS3 = input(50, minval=1, title="SMA 3 Length")
srcS3 = input(close, title="Source")
outS3 = sma(srcS3, lenS3)
plot(showSMA ? outS3 : na, color=#ff0000, title="SMA 3", linewidth=2)

//SMA 4
lenS4 = input(100, minval=1, title="SMA 4 Length")
srcS4 = input(close, title="Source")
outS4 = sma(srcS4, lenS4)
plot(showSMA ? outS4 : na, color=#00bcd4, title="SMA 4", linewidth=2)

//SMA 5
lenS5 = input(200, minval=1, title="SMA 5 Length")
srcS5 = input(close, title="Source")
outS5 = sma(srcS5, lenS5)
plot(showSMA ? outS5 : na, color=#ffffff, title="SMA 5", linewidth=2)

//// Camarilla Pivots ////

//Get previous day/week bar and avoiding realtime calculation by taking the previous to current bar

sopen = security(syminfo.tickerid, "D", open[1], barmerge.gaps_off, barmerge.lookahead_on)


shigh = security(syminfo.tickerid, "D", high[1], barmerge.gaps_off, barmerge.lookahead_on)
slow = security(syminfo.tickerid, "D", low[1], barmerge.gaps_off, barmerge.lookahead_on)
sclose = security(syminfo.tickerid, "D", close[1], barmerge.gaps_off, barmerge.lookahead_on)
r = shigh-slow

// //Calculate pivots
//center=(sclose)
h1=sclose + r*(1.1/12)
h2=sclose + r*(1.1/6)
h3=sclose + r*(1.1/4)
h4=sclose + r*(1.1/2)
//h5=(shigh/slow)*sclose
l1=sclose - r*(1.1/12)
l2=sclose - r*(1.1/6)
l3=sclose - r*(1.1/4)
l4=sclose - r*(1.1/2)
//l5=sclose - (h5-sclose)

// //Showing camarilla based on daily pivots back count


h1_ = new_period(new_day, h1)
h2_ = new_period(new_day, h2)
h3_ = new_period(new_day, h3)
h4_ = new_period(new_day, h4)
l1_ = new_period(new_day, l1)
l2_ = new_period(new_day, l2)
l3_ = new_period(new_day, l3)
l4_ = new_period(new_day, l4)

// //Colors (<ternary conditional operator> expression prevents continuous lines on history)


// //c5=sopen != sopen[1] ? na : color.red
c4=sopen != sopen[1] ? na : color.fuchsia
c3=sopen != sopen[1] ? na : color.green
c2=sopen != sopen[1] ? na : color.blue
c1=sopen != sopen[1] ? na : color.gray
//cc=sopen != sopen[1] ? na : color.black

//Plotting

//plot(center, title="Central",color=cc, linewidth=2)


//plot(h5, title="H5",color=c5, linewidth=1)

// //Camarilla levels
plot(showCamarilla ? h4_ : na, title="H4",color = (sopen != sopen[1] ? na : #bcae2d), style=plot.style_cross,
linewidth=2)
plot(showCamarilla ? h3_ : na, title="H3",color = (sopen != sopen[1] ? na : #f0da19), style=plot.style_cross,
linewidth=2)
plot(showCamarilla ? l3_ : na, title="L3",color = (sopen != sopen[1] ? na : #00ff0e), style=plot.style_cross,
linewidth=2)
plot(showCamarilla ? l4_ : na, title="L4",color = (sopen != sopen[1] ? na : #389d3d), style=plot.style_cross,
linewidth=2)

// //Inner Camarilla levels


plot(showCamarillaInner ? h2_ : na, title="H2",color = (sopen != sopen[1] ? na : #fff176),
style=plot.style_cross, linewidth=1)
plot(showCamarillaInner ? h1_ : na, title="H1",color = (sopen != sopen[1] ? na : #fff176),
style=plot.style_cross, linewidth=1)
plot(showCamarillaInner ? l1_ : na, title="L1",color = (sopen != sopen[1] ? na : #66bb6a),
style=plot.style_cross, linewidth=1)
plot(showCamarillaInner ? l2_ : na, title="L2",color = (sopen != sopen[1] ? na : #66bb6a),
style=plot.style_cross, linewidth=1)

// //plot(l5, title="L5",color=c5, linewidth=1)

study(title="Elder Impulse System",


// LICENSE: CC0 (https://creativecommons.org/publicdomain/zero/1.0/)

study(title="Elder Impulse System", shorttitle="Elder Impulse", overlay=true)

source = close

// MACD Options
macd_length_fast = input(defval=12, minval=1, title="MACD Fast Length")
macd_length_slow = input(defval=26, minval=1, title="MACD Slow Length")
macd_length_signal = input(defval=9, minval=1, title="MACD Signal Length")
// Calculate MACD
macd_ma_fast = ema(source, macd_length_fast)
macd_ma_slow = ema(source, macd_length_slow)
macd = macd_ma_fast - macd_ma_slow
macd_signal = ema(macd, macd_length_signal)
macd_histogram = macd - macd_signal

// EMA Option
ema_length = input(defval=13, minval=1, title="EMA Length")
// Calculate EMA
ema = ema(source, ema_length)

// Calculate Elder Impulse


elder_bulls = (ema[0] > ema[1]) and (macd_histogram[0] > macd_histogram[1])
elder_bears = (ema[0] < ema[1]) and (macd_histogram[0] < macd_histogram[1])
elder_color = elder_bulls
? green // If Bulls Control Trend and Momentum
: elder_bears
? red // If Bears Control Trend and Mementum
: blue // If Neither Bulls or Bears Control the Market

barcolor(elder_color)

study("ORB"
//@version=4
study("ORB", overlay = true)

inputMax = input(5, title= "ORB total time (minutes)")


sess = input("0915-0920", type=input.session, title="Session Time")
t = time(timeframe.period, sess + ":1234567")
hide = timeframe.isintraday and timeframe.multiplier <= inputMax

is_newbar(res) => change(time(res)) != 0


in_session = not na(t)
is_first = in_session and not in_session[1]

orb_high = float(na)
orb_low = float(na)

if is_first
orb_high := high
orb_low := low
else
orb_high := orb_high[1]
orb_low := orb_low[1]
if high > orb_high and in_session
orb_high := high
if low < orb_low and in_session
orb_low := low

plot(hide ? orb_high : na , style=plot.style_line, color=orb_high[1] != orb_high ? na : color.green, title="ORB


High", linewidth=2)
plot(hide ? orb_low : na , style=plot.style_line, color=orb_low[1] != orb_low ? na : color.red, title="ORB Low",
linewidth=2)

You might also like