Tosch Market Sessions (US/GB/JP)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AirshipPirate

//@version=5

// @description Returns if the NYSE, London SE, Tokyo SE are open
library("Tosch_Market_Sessions", overlay=true)

// @function Returns if the NYSE (US), London SE (GB), Tokyo SE (JP) are open
// @returns 
// bus,bgb,bjp - bool:  true if the corresponding exchange is open
// count       - int:   count of how many exchanges are open at the moment
// ccolor      - color: color that indicates the number of open exchanges
//        0 => color.black
//        1 => color.blue
//        2 => color.orange
//        3 => color.white

export show_markets() =>
    // GMT-5 / GMT-4 (06.11-13.03)  -> GMT 0430-1100 / GMT 0530-1200
	bus = not na(time(timeframe.period,"0930-1600:1234567", "America/New_York"))
	// GMT   / GMT+1 (27.03-30.10)  -> GMT 0800-1630 / GMT 0900-1730
	bgb = not na(time(timeframe.period,"0800-1630:1234567", "Europe/London"))
	// GMT+9                        -> GMT 0000-0230,0330-0600
	bjp = not na(time(timeframe.period,"0900-1130,1230-1500:1234567", "Asia/Tokyo"))
    // jp 20:00-22:30 23:30-02:00 (02:00-03:00 0) 1h 
    // gb 03:00-11:30             (09:30-11:30 2) 2h    
    // us 09:30-16:00             (16:00-20:00 0) 4h
    count  = 0
    count += bus ? 1 : 0
    count += bgb ? 1 : 0
    count += bjp ? 1 : 0
    ccolor = switch count
        0 => color.black
        1 => color.blue
        2 => color.orange
        3 => color.white
	[bus,bgb,bjp,count,ccolor]
	
[pus,pgb,pjp,count,ccolor] = show_markets()

// indicator at top of the pane
plotshape(count, location=location.top,style=shape.circle, size=size.tiny, color=color.new(ccolor, 20))
bgcolor(count==1 ? color.new(color.blue,   90) : na)
bgcolor(count==2 ? color.new(color.orange, 90) : na)
