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

//@version=5
indicator("ICT Sessions")

override_offset = input.bool(false, title="Override defaul offset", inline = "utc_offset")
utc_offset_input = input.int(-6, title="Offset of UTC time", minval=-12, maxval=12, inline = "utc_offset")
if not override_offset
    if syminfo.type == "index" or syminfo.type == "futures"
        utc_offset_input := -7
    if syminfo.type == "forex" or syminfo.ticker == "DXY"
        utc_offset_input := -6

plotting_offset_hours = input.int(2, title="Forward plotting hours", minval=0, maxval=12)
use_assettype_filter_inverted = input.bool(true, title="Filter Timewindows by asset type")
use_assettype_filter = not use_assettype_filter_inverted
show_weekly_separator = input.bool(true, title="Show vertical weekly separator line")

number_to_4char_string(int_number) =>
    normalized_int_number = int_number
    //label.new(bar_index, 1, str.format("{0, number, ###}", int_number))
    if int_number <= 0
        normalized_int_number := 2400 + int_number

    string initial_convert = str.format("{0, number,###}", normalized_int_number)
    
    // we cant use a for loop here because that would make the simple string a series string
    if str.length(initial_convert) <= 3
        initial_convert := "0" + initial_convert
    if str.length(initial_convert) <= 3
        initial_convert := "0" + initial_convert
    if str.length(initial_convert) <= 3
        initial_convert := "0" + initial_convert
    
    initial_convert
    
    
n_bars_to_shift = math.round((plotting_offset_hours*60) / str.tonumber(timeframe.period))

// times are in UTC so one has to add 2 hours to get to german
utc_offset = (utc_offset_input - plotting_offset_hours) * 100
london_timewindow = number_to_4char_string(0900 + utc_offset) + "-" + number_to_4char_string(1701 + utc_offset)
newyork_timewindow = number_to_4char_string(1430 + utc_offset) + "-" + number_to_4char_string(2201 + utc_offset)
asian_timewindow = number_to_4char_string(0000 + utc_offset) + "-" + number_to_4char_string(1001 + utc_offset)

london_entry_kill_zone = number_to_4char_string(0800 + utc_offset) + "-" + number_to_4char_string(1101 + utc_offset)
london_entry_kill_zone_extended = number_to_4char_string(0700 + utc_offset) + "-" + number_to_4char_string(1101 + utc_offset)
london_exit_kill_zone = number_to_4char_string(1600 + utc_offset) + "-" + number_to_4char_string(1701 + utc_offset)

newyork_kill_zone = number_to_4char_string(1300 + utc_offset) + "-" + number_to_4char_string(1601 + utc_offset)
newyork_kill_zone_extended = number_to_4char_string(1200 + utc_offset) + "-" + number_to_4char_string(1601 + utc_offset)

sydney_open =  number_to_4char_string(0000 + utc_offset) + "-" + number_to_4char_string(0001 + utc_offset)
london_midnight =  number_to_4char_string(0100 + utc_offset) + "-" + number_to_4char_string(0101 + utc_offset)
newyork_midnight =  number_to_4char_string(0600 + utc_offset) + "-" + number_to_4char_string(0601 + utc_offset)
newyork_830 = number_to_4char_string(1430 + utc_offset) + "-" + number_to_4char_string(1431 + utc_offset)
newyork_930 = number_to_4char_string(1530 + utc_offset) + "-" + number_to_4char_string(1531 + utc_offset)

london_lunch_timewindow = number_to_4char_string(1300 + utc_offset) + "-" + number_to_4char_string(1431 + utc_offset)
newyork_lunch_timewindow = number_to_4char_string(1800 + utc_offset) + "-" + number_to_4char_string(1901 + utc_offset)
newyork_lunch_until_1330 = number_to_4char_string(1900 + utc_offset) + "-" + number_to_4char_string(1931 + utc_offset)


first_30min_of_trading_window = number_to_4char_string(1530 + utc_offset) + "-" + number_to_4char_string(1601 + utc_offset)
silverbullet_am_window = number_to_4char_string(1600 + utc_offset) + "-" + number_to_4char_string(1701 + utc_offset)
silverbullet_pm_window = number_to_4char_string(2000 + utc_offset) + "-" + number_to_4char_string(2101 + utc_offset)
am850_makro = number_to_4char_string(1450 + utc_offset) + "-" + number_to_4char_string(1511 + utc_offset)
am950_makro = number_to_4char_string(1550 + utc_offset) + "-" + number_to_4char_string(1611 + utc_offset)
pre_lunch_makro = number_to_4char_string(1650 + utc_offset) + "-" + number_to_4char_string(1711 + utc_offset)
lunch_makro = number_to_4char_string(1750 + utc_offset) + "-" + number_to_4char_string(1811 + utc_offset)
last_hour = number_to_4char_string(2100 + utc_offset) + "-" + number_to_4char_string(2201 + utc_offset)
last_hour_makro = number_to_4char_string(2115 + utc_offset) + "-" + number_to_4char_string(2146 + utc_offset)




// ################################
// map the timeintervalls to values
// ---------------------------------

// sessions
float plot_london = na(time(timeframe.period, london_timewindow)) ? na : 1
float plot_newyork = na(time(timeframe.period, newyork_timewindow)) ? na : 2
float plot_asia = na(time(timeframe.period, asian_timewindow)) ? na : 0

// kill zones
float plot_london_entry_kill_zone = na(time(timeframe.period, london_entry_kill_zone)) ? na : 1.3
float plot_london_entry_kill_zone_extended = na(time(timeframe.period, london_entry_kill_zone_extended)) ? na : 1.27
float plot_london_exit_kill_zone = na(time(timeframe.period, london_exit_kill_zone)) ? na : 1.3
    
float plot_newyork_kill_zone = na(time(timeframe.period, newyork_kill_zone)) ? na : 2.3
float plot_newyork_kill_zone_extended = na(time(timeframe.period, newyork_kill_zone_extended)) ? na : 2.27

// reference points -- 9:30 + midnights
float scatter_sydney = na(time(timeframe.period, sydney_open)) ? na : 0  
float scatter_london_midnight = na(time(timeframe.period, london_midnight)) ? na : 1
float scatter_newyork_midnight = na(time(timeframe.period, newyork_midnight)) ? na : 2
float scattter_newyork_830 = na(time(timeframe.period, newyork_830)) ? na : 2.3
float scattter_newyork_930 = na(time(timeframe.period, newyork_930)) ? na : 2.3

// lunch breaks
float plot_london_lunch = na(time(timeframe.period, london_lunch_timewindow)) ? na : 1.15
float plot_newyork_lunch_until_1330= na(time(timeframe.period, newyork_lunch_until_1330)) ? na : 2.15
float plot_newyork_lunch = na(time(timeframe.period, newyork_lunch_timewindow)) ? na : 2.15

float plot_first_30min_of_trading_window = na(time(timeframe.period, first_30min_of_trading_window)) ? na : 2.15
float plot_silverbullet_am_window = na(time(timeframe.period, silverbullet_am_window)) ? na : 2.15
float plot_silverbullet_pm_window = na(time(timeframe.period, silverbullet_pm_window)) ? na : 2.15
float plot_am850_makro = na(time(timeframe.period, am850_makro)) ? na : 2.6
float plot_am950_makro = na(time(timeframe.period, am950_makro)) ? na : 2.6
float plot_pre_lunch_makro = na(time(timeframe.period, pre_lunch_makro)) ? na : 2.6
float plot_lunch_makro = na(time(timeframe.period, lunch_makro)) ? na : 2.6
float plot_last_hour = na(time(timeframe.period, last_hour)) ? na : 2.6
float plot_last_hour_makro = na(time(timeframe.period, last_hour_makro)) ? na : 2.6


// ################################
// start painting the plots
// ---------------------------------

// weekly separator
offset_in_ms = utc_offset_input * 60 * 60 * 1000
if show_weekly_separator and weekofyear(time-offset_in_ms) != weekofyear(time-1-offset_in_ms)
    line.new(bar_index, 3, bar_index, -1, color=color.black, width=2, style=line.style_dashed)


// futures & indices -- full sessions
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_london : na, title="London Session" ,style=plot.style_linebr, color=color.blue, linewidth=3, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_newyork : na, title="New York Session", style=plot.style_linebr, color=#557588, linewidth=3, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_asia : na, title="Asia Session", style=plot.style_linebr, color=color.orange, linewidth=3, offset=n_bars_to_shift)


// forex -- killzones
plot(plot_london_entry_kill_zone, title="London killzone" ,style=plot.style_linebr, color=color.aqua, linewidth=4, offset=n_bars_to_shift)
plot(plot_london_entry_kill_zone_extended, title="London killzone ext" ,style=plot.style_linebr, color=color.aqua, linewidth=2, offset=n_bars_to_shift)
plot((syminfo.type == "forex" or use_assettype_filter) ? plot_london_exit_kill_zone : na, title="London Exitzone" ,style=plot.style_linebr, color=color.maroon, linewidth=4, offset=n_bars_to_shift)

plot((syminfo.type == "forex" or use_assettype_filter) ? plot_newyork_kill_zone : na, title="New York killzone" ,style=plot.style_linebr, color=color.teal, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "forex" or use_assettype_filter) ? plot_newyork_kill_zone_extended : na, title="New York killzone ext" ,style=plot.style_linebr, color=color.teal, linewidth=2, offset=n_bars_to_shift)

// reference points -- 9:30 + midnights
//plot(scatter_sydney, title="Sydney midnight", style=plot.style_circles, color=color.black, linewidth=3, offset=n_bars_to_shift)
plot(scatter_london_midnight, title="London midnight", style=plot.style_cross, color=color.black, linewidth=2, offset=n_bars_to_shift)
plot(scatter_newyork_midnight, title="New York midnight", style=plot.style_cross, color=color.black, linewidth=2, offset=n_bars_to_shift)


// lunch breaks
plot(plot_london_lunch, title="London Lunch" ,style=plot.style_linebr, color=color.red, linewidth=6, offset=n_bars_to_shift)
plot(plot_newyork_lunch, title="New York Lunch" ,style=plot.style_linebr, color=#d42626, linewidth=6, offset=n_bars_to_shift)
plot(plot_newyork_lunch_until_1330, title="New York Lunch 13" ,style=plot.style_linebr, color=color.red, linewidth=6, offset=n_bars_to_shift)




plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_first_30min_of_trading_window : na, title="First 30min of trading", style=plot.style_linebr, color=#ff9400, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_silverbullet_am_window : na, title="Silberbullet AM", style=plot.style_linebr, color=#4c6a77, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_silverbullet_pm_window : na, title="Silberbullet PM", style=plot.style_linebr, color=#4c6a77, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_am850_makro : na, title="8:50 makro", style=plot.style_linebr, color=#1b5e20, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_am950_makro : na, title="9:50 makro", style=plot.style_linebr, color=#1b5e20, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_pre_lunch_makro : na, title="Pre NY Lunch makro", style=plot.style_linebr, color=#1b5e20, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_lunch_makro : na, title="NY Lunch makro", style=plot.style_linebr, color=#490000, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_last_hour : na, title="Last NY hour of trading", style=plot.style_linebr, color=#16d323, linewidth=4, offset=n_bars_to_shift)
plot((syminfo.type == "index" or syminfo.type == "futures" or use_assettype_filter) ? plot_last_hour_makro : na, title="Last NY hour of trading makro", style=plot.style_linebr, color=#1b5e20, linewidth=4, offset=n_bars_to_shift)

plot(scattter_newyork_830, title="New York 8:30", style=plot.style_cross, color=color.black, linewidth=3, offset=n_bars_to_shift)
plot(scattter_newyork_930, title="New York 9:30", style=plot.style_circles, color=color.black, linewidth=3, offset=n_bars_to_shift)

// vertical delimiters
plot(3, title="Upper line", style=plot.style_line, color=color.silver, linewidth=1, offset=n_bars_to_shift)
plot(-1, title="Lower line", style=plot.style_line, color=color.silver, linewidth=1, offset=n_bars_to_shift)

