World Markets Open/Close Background
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goofoffgoose

// @version=5
indicator("World Markets", shorttitle="Wrd Mkts", overlay=true)
 
t = timestamp("01 Jan 2022 00:00")


// My intentions for this script is to use it and its varible value to plot the open, close, highs and lows in the specific times of a 
// session and to more easily visualize those sessions with color background. 


// ***Fixed the Timezone issue. The default timeframe of sessions should update to the users current timezone set on the platform. 
//    Although times will still need to be adjusted for DST. 
// ***Cleaned up the code and menu layout to be more User/Coder friendly.
// ***Added option to color background for a lookahead period in minutes. The default is false with lookahead set for 1 day (1380 mins) 
//    with times set on the hourly chart. This code is however intended for a chart in minute(s). Manually input the time to change this.
//    Selecting "Future Sessions" can take a moment to load on the 1 min and under chart. 
// ****PLEASE NOTE*** "Future Sessions" is VERY picky on the 1 hour chart. Selected times HAVE TO BE at least 1 hour and sometimes even
//     ON THE HOUR. Change "Future Sessions" INPUT to 1379(mins) for an hourly chart otherwise it won't show the first selected session
//     It will take increments sometimes, and other times it just won't. Please don't ask me to fix this, belive me I have tried. 

// Indicator fills background on the chart for diffent markets around the world. 
// Times can be changed for each session to include the entire session or a specific time. 
// Sessions are default to be displayed on the 1 hour chart. All default times for each market are in CST. 
// Background color can be turned off or changed and this can just be used for source. 


// Market open and close inputs

///// US Markets/////
USOpenSession = input.session(title="US Open", defval="0800-0845", tooltip="US(NYSE) Open", group="US Session")
USCloseSession = input.session(title="US Close", defval="1445-1515", tooltip="US(NYSE) Close")

Show_US_Session = input.bool(title="Show US Session?", defval=true) // show session ?

// Colors
USOpenColor = input.color(color.rgb(0,153,0,90), title="US Open", inline="UsSess color")
USCloseColor = input.color(color.rgb(0,102,0,90), title="US Close", inline="UsSess color" )



///// Asia Markets/////
AsiaOpenSession = input.session(title="Asia Open", defval="1800-1930", tooltip="Open is for Multiple Markets: Tokyo(TSE), Hong Kong(HKEX) and Shanghai(SSX) Open", group="Asia Session")
AsiaCloseSession = input.session(title="Asia Close", defval="0000-0200", tooltip="Close is for Multiple Markets: Tokyo(TSE), Hong Kong(HKEX) and Shanghai(SSX) Close")

Show_Asia_Session = input.bool(title="Show Asia Session?", defval=true)// show session ?

// Colors
AsiaOpenColor = input.color(color.rgb(204,0,0,91), title="Asia Open", inline="AsiaSess Color")
AsiaCloseColor = input.color(color.rgb(153,0,0,93), title="Asia Close", inline="AsiaSess Color" )



///// India Markets/////
IndiaOpenSession = input.session(title="India Open", defval="2230-2330", tooltip="India(NSE), (BSE), and Nifty Open", group="India Session")
IndiaCloseSession = input.session(title="India Close", defval="0445-0515", tooltip="India(NSE), (BSE), and Nifty Close")

Show_India_Session = input.bool(title="Show India Session?", defval=true)// show session ?

// Colors
IndiaOpenColor = input.color(color.rgb(204,102,0,90), title="India Open", inline="IndiaSess Color")
IndiaCloseColor = input.color(color.rgb(204,102,0,92), title="India Close", inline="IndiaSess Color" )



///// London Market/////
LondonOpenSession = input.session(title="London Open", defval="0200-0300", tooltip="London(LSE) Open", group="London Session")
LondonCloseSession = input.session(title="London Close", defval="1000-1100", tooltip="London(LSE) Close")

Show_London_Session = input.bool(title="Show London Session?", defval=true)// show session ?

// Colors
LondonOpenColor = input.color(color.rgb(0,102,204,90), title="London Open", inline="LondonSess Color")
LondonCloseColor = input.color(color.rgb(0,76,153,90), title="London Close", inline="LondonSess Color" )




// Returns true if the current bar falls within the given time session (:123456 Sunday - Friday)

inSession(session, sessionTimeZone=syminfo.timezone) => 
    na(time(timeframe.period, session + ":1234567")) == false 


// Conditions to print future sessions on chart

Show_Future_Session = input.bool(false,  title="Future Sessions", tooltip="This will show sessions into the future on the chart.")
var print_fut = false
var dont_print_fut = false
if Show_Future_Session
    print_fut := true
else
    dont_print_fut := true

// Input for future session in minutes
FutureSess = input(1380, title="Future Sessions", tooltip="Number of minutes in your chart. Default is for 23 Hours(1380 mins) for ETH charts including Globex sessions. Change to 1379 for hourly chart")




    

// Background colors for each market session

// New York BG print
// Open
bgcolor(inSession(USOpenSession) and Show_US_Session and dont_print_fut ? USOpenColor : na, editable=false)
bgcolor(inSession(USOpenSession) and Show_US_Session and print_fut ? USOpenColor : na, offset=FutureSess /timeframe.multiplier, editable=false)
// Close
bgcolor(inSession(USCloseSession) and Show_US_Session and dont_print_fut ? USCloseColor : na, editable=false)
bgcolor (inSession(USCloseSession) and Show_US_Session and print_fut ? USCloseColor : na, offset=FutureSess /timeframe.multiplier, editable=false)

// Asia BG print
// Open
bgcolor(inSession(AsiaOpenSession) and Show_Asia_Session and dont_print_fut ? AsiaOpenColor : na, editable=false)
bgcolor(inSession(AsiaOpenSession) and Show_Asia_Session and print_fut ? AsiaOpenColor : na, offset=FutureSess /timeframe.multiplier, editable=false)
// Close
bgcolor(inSession(AsiaCloseSession) and Show_Asia_Session and dont_print_fut ? AsiaCloseColor : na, editable=false)
bgcolor(inSession(AsiaCloseSession) and Show_Asia_Session and print_fut ? AsiaCloseColor : na, offset=FutureSess /timeframe.multiplier, editable=false)

// India BG print
// Opem
bgcolor(inSession(IndiaOpenSession) and Show_India_Session and dont_print_fut ? IndiaOpenColor : na, editable=false)
bgcolor(inSession(IndiaOpenSession) and Show_India_Session and print_fut ? IndiaOpenColor : na, offset=FutureSess /timeframe.multiplier, editable=false)
// Close
bgcolor(inSession(IndiaCloseSession) and Show_India_Session and dont_print_fut ? IndiaCloseColor : na, editable=false)
bgcolor(inSession(IndiaCloseSession) and Show_India_Session and print_fut ? IndiaCloseColor : na, offset=FutureSess /timeframe.multiplier, editable=false)

// London BG print
// Open
bgcolor(inSession(LondonOpenSession) and Show_London_Session and dont_print_fut ? LondonOpenColor : na, editable=false)
bgcolor(inSession(LondonOpenSession) and Show_London_Session and print_fut ? LondonOpenColor : na, offset=FutureSess /timeframe.multiplier, editable=false)
// Close
bgcolor(inSession(LondonCloseSession) and Show_London_Session and dont_print_fut ? LondonCloseColor : na, editable=false)
bgcolor(inSession(LondonCloseSession) and Show_London_Session and print_fut? LondonCloseColor : na, offset=FutureSess/timeframe.multiplier, editable=false)















