You are on page 1of 9

// 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('AM&PM Sessions', overlay=true, max_boxes_count=500,
max_labels_count=500, max_lines_count=500, max_bars_back=1000)

///////////////
// Groups
///////////////
g1 = '♯1 Session'
g2 = '♯2 Session'
g3 = '♯3 Session'
g4 = '♯4 Session'
g5 = '♯5 Session'
g6 = '♯6 Session'
g7 = '♯7 Session'
g8 = '♯8 Session'
g9 = 'Box'
g10 = 'Labels'
g11 = 'Opening range'
g12 = 'EQ Style Options'
g13 = 'Options'
g14 = 'Fill'
g15 = 'Oscillator mode'

///////////////
// Defined
///////////////
show = true
pips = syminfo.mintick * 10
max_bars = 500

option_yes = 'Yes'
option_no = 'No'
option_extend1 = 'Extend'

fmt_price = '{0,number,#.#####}'
fmt_pips = '{0,number,#.#}'

icon_separator = ' • '

c_none = color.new(color.black, 100)

is_weekends = dayofweek == 7 or dayofweek == 1

///////////////
// Functions
///////////////
f_get_time_by_bar(bar_count) => timeframe.multiplier * bar_count * 60 * 1000

f_get_period (_session, _start, _lookback) =>


result = math.max(_start, 1)
for i = result to _lookback
if na(_session[i+1]) and _session[i]
result := i+1
break
result
f_get_label_position (_y, _side) =>
switch _y
'top' => _side == 'outside' ? label.style_label_lower_left :
label.style_label_upper_left
'bottom' => _side == 'outside' ? label.style_label_upper_left :
label.style_label_lower_left

f_get_day (n) =>


switch n
1 => 'Sun'
2 => 'Mon'
3 => 'Tue'
4 => 'Wed'
5 => 'Thu'
6 => 'Fri'
7 => 'Sat'

f_get_started (_session) => na(_session[1]) and _session

f_get_ended (_session) => na(_session) and _session[1]

///////////////
// Inputs
///////////////
// Timezone
i_tz = input.string('GMT-5', title='Timezone', options=['GMT-11', 'GMT-10', 'GMT-
9', 'GMT-8', 'GMT-7', 'GMT-6', 'GMT-5', 'GMT-4', 'GMT-3', 'GMT-2', 'GMT-1', 'GMT',
'GMT+1', 'GMT+2', 'GMT+3', 'GMT+4', 'GMT+5', 'GMT+6', 'GMT+7', 'GMT+8', 'GMT+9',
'GMT+10', 'GMT+11', 'GMT+12'], tooltip='e.g. \'America/New_York\',
\'Asia/Tokyo\', \'GMT-4\', \'GMT+9\'...', group='General')
i_show_history = input.string(option_yes, 'History', options=[option_yes,
option_no], group="General") == option_yes
i_lookback = 12 * 60

// Sessions
i_show_sess1 = input.bool(true, 'Session 1: ', group=g1, inline='session1_1') and
show
i_sess1_label = input.string('AM Session', ' ', group=g1, inline='session1_1')
i_sess1_color = input.color(#2962ff, ' ', group=g1, inline='session1_1')
i_sess1 = input.session('0700-1200', 'Time', group=g1)
i_sess1_extend = input.string(option_no, option_extend1, options=[option_no,
option_extend1], group=g1)
i_sess1_fib = input.string(option_no, 'EQ', group=g1, options=[option_yes,
option_no]) != option_no
i_sess1_op = input.string(option_no, 'Opening range', group=g1,
options=[option_yes, option_no]) != option_no and show

i_show_sess2 = input.bool(true, 'Session 2: ', group=g2, inline='session2_1') and


show
i_sess2_label = input.string('PM Session', ' ', group=g2, inline='session2_1')
i_sess2_color = input.color(#f23645, ' ', group=g2, inline='session2_1')
i_sess2 = input.session('1300-1600', 'Time', group=g2)
i_sess2_extend = input.string(option_no, option_extend1, options=[option_no,
option_extend1], group=g2)
i_sess2_fib = input.string(option_no, 'EQ', group=g2, options=[option_yes,
option_no]) != option_no
i_sess2_op = input.string(option_no, 'Opening range', group=g2,
options=[option_yes, option_no]) != option_no and show
i_show_sess3 = input.bool(true, 'Session 3: ', group=g3, inline='session3_1') and
show
i_sess3_label = input.string('Birincil Likidite', ' ', group=g3,
inline='session3_1')
i_sess3_color = input.color(#000000, ' ', group=g3, inline='session3_1')
i_sess3 = input.session('0200-0500', 'Time', group=g3)
i_sess3_extend = input.string(option_no, option_extend1, options=[option_no,
option_extend1], group=g3)
i_sess3_fib = input.string(option_no, 'EQ', group=g3, options=[option_yes,
option_no]) != option_no
i_sess3_op = input.string(option_no, 'Opening range', group=g3,
options=[option_yes, option_no]) != option_no and show

i_show_sess4 = input.bool(true, 'Session 4: ', group=g4, inline='session4_1') and


show
i_sess4_label = input.string('İkincil Likidite', ' ', group=g4,
inline='session4_1')
i_sess4_color = input.color(#000000, ' ', group=g4, inline='session4_1')
i_sess4 = input.session('2000-0000', 'Time', group=g4)
i_sess4_extend = input.string(option_no, option_extend1, options=[option_no,
option_extend1], group=g4)
i_sess4_fib = input.string(option_no, 'EQ', group=g4, options=[option_yes,
option_no]) != option_no
i_sess4_op = input.string(option_no, 'Opening range', group=g4,
options=[option_yes, option_no]) != option_no and show

// Show & Styles


i_sess_border_style = input.string(line.style_dashed, 'Style',
options=[line.style_solid, line.style_dotted, line.style_dashed], group=g9)
i_sess_border_width = input.int(1, 'Thickness', minval=0, group=g9)
i_sess_bgopacity = input.int(94, 'Transp', minval=0, maxval=100, step=1, group=g9,
tooltip='Setting the 100 is no background color')

// Labels
i_label_show = input.bool(true, 'Show labels', inline='label_show', group=g11) and
show
i_label_size = str.lower(input.string('Small', '', options=['Auto', 'Tiny',
'Small', 'Normal', 'Large', 'Huge'], inline='label_show', group=g11))
i_label_position_y = str.lower(input.string('Top', '', options=['Top', 'Bottom'],
inline='label_show', group=g11))
i_label_position_s = str.lower(input.string('Outside', '', options=['Inside',
'Outside'], inline='label_show', group=g11))
i_label_position = f_get_label_position(i_label_position_y, i_label_position_s)
i_label_format_name = input.bool(true, 'Name', inline='label_format', group=g11)
i_label_format_day = input.bool(true, 'Day', inline='label_format', group=g11)
i_label_format_price = input.bool(false, 'Price', inline='label_format', group=g11)
i_label_format_pips = input.bool(true, 'Pips', inline='label_format', group=g11)

// EQ Style Options
i_f_linestyle = input.string(line.style_solid, title="EQ Line Style",
options=[line.style_solid, line.style_dotted, line.style_dashed], group=g12)
i_f_linewidth = input.int(1, title="EQ Line Thickness", minval=1, group=g12)
i_f_linecolor = input.color(#FF0000, "EQ Color")
// Opening range
i_o_minutes = input.int(15, title='Periods (minutes)', minval=1, step=1, group=g10)
i_o_minutes := math.max(i_o_minutes, timeframe.multiplier + 1)
i_o_transp = input.int(88, title='Transp', minval=0, maxval=100, step=1, group=g10)
// Fill area
i_area_show = input.bool(false, title='Filled area', inline='area_display',
group=g14)
i_area_bull = input.color(#A6E22E, title='', inline='area_display', group=g14)
i_area_bear = input.color(#F92672, title='', inline='area_display', group=g14)
i_area_transp = 86
i_sess_open_transp = 75

// ------------------------
// Drawing labels
// ------------------------
draw_label (_show, _session, _is_started, _color, _top, _bottom, _text,
_delete_history) =>
var label my_label = na
var int start_time = na

v_position_y = (i_label_position_y == 'top') ? _top : _bottom


v_label = array.new_string()
v_chg = _top - _bottom

if _is_started
start_time := time

if i_label_format_name and not na(_text)


array.push(v_label, _text)

if i_label_format_day
array.push(v_label, f_get_day(dayofweek(start_time, i_tz)))

if i_label_format_price
array.push(v_label, str.format(fmt_price, v_chg))

if i_label_format_pips
array.push(v_label, str.format(fmt_pips, v_chg / pips) + ' pips')

if _show
if _is_started
my_label := label.new(time, v_position_y, array.join(v_label,
icon_separator), textcolor=_color, color=c_none, size=i_label_size,
style=i_label_position, xloc=xloc.bar_time)

if _delete_history
label.delete(my_label[1])

if _session
label.set_y(my_label, v_position_y)
label.set_text(my_label, array.join(v_label, icon_separator))

// ------------------------
// Drawing Fibonacci levels
// ------------------------
draw_fib (_show, _session, _is_started, _x1, _x2, _color, _top, _bottom, _level,
_width, _style, _is_extend, _delete_history) =>
var line my_line = na

if _show
y = (_top - _bottom) * _level + _bottom

if _is_started
my_line := line.new(_x1, y, _x2, y, width=_width,
color=color.new(_color, 30), style=_style)

if _is_extend
line.set_extend(my_line, extend.right)

if _delete_history
line.delete(my_line[1])

if _session
line.set_y1(my_line, y)
line.set_y2(my_line, y)

// ------------------------
// Drawing Opening range
// ------------------------
draw_op (_show, _session, _is_started, _x1, _x2, _color, _max, _is_extend,
_delete_history) =>
var int start_time = na
var box my_box = na
top = ta.highest(high, _max)
bottom = ta.lowest(low, _max)
is_crossover = ta.crossover(close, box.get_top(my_box))
is_crossunder = ta.crossunder(close, box.get_bottom(my_box))

if _show
if _is_started
start_time := time
my_box := na

if _delete_history
box.delete(my_box[1])

if _session
time_op = start_time + (i_o_minutes * 60 * 1000)
time_op_delay = time_op - f_get_time_by_bar(1)

if time <= time_op and time > time_op_delay


my_box := box.new(_x1, top, _x2, bottom, border_width=0,
bgcolor=color.new(_color, i_o_transp))

if _is_extend
box.set_extend(my_box, extend.right)
my_box

else
//box.set_right(my_box, bar_index + 1)

if is_crossover
alert('Price crossed over the opening range',
alert.freq_once_per_bar)
if is_crossunder
alert('Price crossed under the opening range',
alert.freq_once_per_bar)

my_box

// ------------------------
// Drawing market
// ------------------------
draw_market (_show, _session, _is_started, _is_ended, _color, _top, _bottom,
_extend, _is_extend, _delete_history) =>
var box my_box = na

x0_1 = ta.valuewhen(na(_session[1]) and _session, bar_index, 1)


x0_2 = ta.valuewhen(na(_session) and _session[1], bar_index, 0)
var x1 = 0
var x2 = 0
var v_session_open = 0.0
var v_session_high = 0.0
var v_session_low = 0.0

if _show
if _is_started
diff = math.abs(x0_2 - x0_1)
x1 := bar_index
x2 := bar_index + (math.min(diff, max_bars))
my_box := box.new(x1, _top, x2, _bottom)
box.set_border_style(my_box, i_sess_border_style)
box.set_border_width(my_box, i_sess_border_width)
box.set_border_color(my_box, _color)
box.set_bgcolor(my_box, color.new(_color, i_sess_bgopacity))

v_session_open := open
v_session_high := _top
v_session_low := _bottom

if _is_extend
box.set_extend(my_box, extend.right)

if _delete_history
box.delete(my_box[1])

// Proccesing
else if _session
box.set_top(my_box, _top)
box.set_bottom(my_box, _bottom)

v_session_high := _top
v_session_low := _bottom

else if _is_ended
v_session_open := na
box.set_right(my_box, bar_index)

[x1, x2, v_session_open, v_session_high, v_session_low]

// ------------------------
// Drawing
// ------------------------

draw (_show, _session, _color, _label, _extend, _show_fib, _show_op, _lookback) =>
max = f_get_period(_session, 1, _lookback)
top = ta.highest(high, max)
bottom = ta.lowest(low, max)

is_started = f_get_started(_session)
is_ended = f_get_ended(_session)
is_extend = _extend != option_no

delete_history = (not i_show_history) or is_extend

[x1, x2, _open, _high, _low] = draw_market(_show, _session, is_started,


is_ended, _color, top, bottom, _extend, is_extend, delete_history)

if i_label_show
draw_label(_show, _session, is_started, _color, top, bottom, _label,
delete_history)

if _show_op
draw_op(_show, _session, is_started, x1, x2, _color, max, is_extend,
delete_history)

if _show_fib
draw_fib(_show, _session, is_started, x1, x2, i_f_linecolor, top, bottom,
0.500, i_f_linewidth, i_f_linestyle, is_extend, delete_history)

// draw_fib(_show, _session, is_started, x1, x2, _color, top, bottom, 0.628,


i_f_linewidth, i_f_linestyle, is_extend, delete_history)
// draw_fib(_show, _session, is_started, x1, x2, _color, top, bottom, 0.705,
i_f_linewidth, i_f_linestyle, is_extend, delete_history)

[_session, _open, _high, _low]

///////////////////
// Calculating
///////////////////
string tz = (i_tz == option_no or i_tz == '') ? na : i_tz
int sess1 = time(timeframe.period, i_sess1, tz)
int sess2 = time(timeframe.period, i_sess2, tz)
int sess3 = time(timeframe.period, i_sess3, tz)
int sess4 = time(timeframe.period, i_sess4, tz)

///////////////////
// Plotting
///////////////////
[is_sess1, sess1_open, sess1_high, sess1_low] = draw(i_show_sess1, sess1,
i_sess1_color, i_sess1_label, i_sess1_extend, i_sess1_fib, i_sess1_op, i_lookback)
i_color = input.color(#4169E1, "NYMO Color")
i_time = input.session ('0820-0821:1234567', "NYMO Time")
i_linestyle = input.string ("Solid", "NYMO Line", options=["Solid",
"Dotted", "Dashed"])
// MISC
nymid = time ("1", i_time)
linestyle = i_linestyle == "Solid" ? line.style_solid : i_linestyle ==
"Dotted" ? line.style_dotted : line.style_dashed

// CALC
var openprice = 0.0
if nymid
if not nymid[1]
openprice := open
else
openprice := math.max(open, openprice)

// OBJECTS
var label lb = na
var line lne = na
if openprice != openprice[1]
if barstate.isconfirmed
line.set_x2(lne, nymid)
// line.set_extend(lne, extend.none)
lne := line.new(nymid, openprice, last_bar_time + 14400000/2, openprice,
xloc.bar_time, extend.none, i_color, linestyle, 1)
lb := label.new(last_bar_time + 14400000/2, openprice, "08:20",
xloc.bar_time, yloc.price, na, label.style_none, i_color, size.normal,
text.align_right)

label.delete(lb[1])
line.delete(lne[1])
[is_sess2, sess2_open, sess2_high, sess2_low] = draw(i_show_sess2, sess2,
i_sess2_color, i_sess2_label, i_sess2_extend, i_sess2_fib, i_sess2_op, i_lookback)
[is_sess3, sess3_open, sess3_high, sess3_low] = draw(i_show_sess3, sess3,
i_sess3_color, i_sess3_label, i_sess3_extend, i_sess3_fib, i_sess3_op, i_lookback)
[is_sess4, sess4_open, sess4_high, sess4_low] = draw(i_show_sess4, sess4,
i_sess4_color, i_sess4_label, i_sess4_extend, i_sess4_fib, i_sess4_op, i_lookback)

// Filled area
plot_sess1_color = close > sess1_open ? i_area_bull : i_area_bear
plot_sess2_color = close > sess2_open ? i_area_bull : i_area_bear
plot_sess3_color = close > sess3_open ? i_area_bull : i_area_bear
plot_sess4_color = close > sess4_open ? i_area_bull : i_area_bear

plot_sess1_close = plot(i_area_show and i_show_sess1 and is_sess1 ? close : na,


title='', style=plot.style_linebr, color=color.new(plot_sess1_color,
i_sess_open_transp), display=display.all)
plot_sess2_close = plot(i_area_show and i_show_sess2 and is_sess2 ? close : na,
title='', style=plot.style_linebr, color=color.new(plot_sess2_color,
i_sess_open_transp), display=display.all)
plot_sess3_close = plot(i_area_show and i_show_sess3 and is_sess3 ? close : na,
title='', style=plot.style_linebr, color=color.new(plot_sess3_color,
i_sess_open_transp), display=display.all)
plot_sess4_close = plot(i_area_show and i_show_sess4 and is_sess4 ? close : na,
title='', style=plot.style_linebr, color=color.new(plot_sess4_color,
i_sess_open_transp), display=display.all)
plot_sess1_open = plot(i_area_show and i_show_sess1 and is_sess1 ? sess1_open : na,
title='', style=plot.style_circles, color=color.new(plot_sess1_color,
i_sess_open_transp), display=display.all)
plot_sess2_open = plot(i_area_show and i_show_sess2 and is_sess2 ? sess2_open : na,
title='', style=plot.style_circles, color=color.new(plot_sess2_color,
i_sess_open_transp), display=display.all)
plot_sess3_open = plot(i_area_show and i_show_sess3 and is_sess3 ? sess3_open : na,
title='', style=plot.style_circles, color=color.new(plot_sess3_color,
i_sess_open_transp), display=display.all)
plot_sess4_open = plot(i_area_show and i_show_sess4 and is_sess4 ? sess4_open : na,
title='', style=plot.style_circles, color=color.new(plot_sess4_color,
i_sess_open_transp), display=display.all)

fill(plot_sess1_open, plot_sess1_close, color=color.new(plot_sess1_color,


i_area_transp))
fill(plot_sess2_open, plot_sess2_close, color=color.new(plot_sess2_color,
i_area_transp))
fill(plot_sess3_open, plot_sess3_close, color=color.new(plot_sess3_color,
i_area_transp))
fill(plot_sess4_open, plot_sess4_close, color=color.new(plot_sess4_color,
i_area_transp))

You might also like