Next Igor Session
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © a.unterweger

//@version=4
study("Next Igor Session", overlay=true)

high_ = highest(high, 20)
low_ = lowest(low, 20)


drawTime(is, color_) => 
    line.new(is, high_, is, low_, xloc = xloc.bar_time, extend = extend.both, style=line.style_dotted, color=color_)
    
millis24h = 24 * 60 * 60 * 1000    


is1 = timestamp("UTC", year, month, dayofmonth, 00, 00, 00)
is2 = timestamp("UTC", year, month, dayofmonth, 06, 00, 00)
is3 = timestamp("UTC", year, month, dayofmonth, 12, 00, 00)
is4 = timestamp("UTC", year, month, dayofmonth, 20, 00, 00)

prev = 0

next = if(timenow < is1)
    prev := is4
    is1
else if(timenow < is2)
    prev := is1
    is2
else if(timenow < is3)
    prev := is2
    is3
else
    prev := is3
    is4
    
//handle case after last session    
if timenow > next
    is1 := is1+millis24h
    next := is1
    prev := is4
    
if(timenow < next)    
    drawTime(next, color.gray)
    drawTime(prev, color.gray)
    
    
//ny open    
showNyOpen = input(title="Show NY open", type=input.bool, defval=true)
nyOpen = timestamp("America/New_York", year, month, dayofmonth, 09, 30, 00)

//handle case after last ny open    
if timenow > nyOpen
    nyOpen := nyOpen+millis24h

if(showNyOpen and timenow < nyOpen)
    drawTime(nyOpen, color.blue)
    
//continental sessions    
showContSessions = input(title="Show continental sessions", type=input.bool, defval=true)
asia = timestamp("UTC", year, month, dayofmonth, 00, 00, 00)
london = timestamp("UTC", year, month, dayofmonth, 08, 00, 00)
ny = timestamp("UTC", year, month, dayofmonth, 14, 30, 00)
close_ = timestamp("UTC", year, month, dayofmonth, 19, 15, 00)

nextCSession = if(timenow < asia)
    asia
else if(timenow < london)
    london
else if(timenow < ny)
    ny
else
    close_
    
//handle case after last continental session    
if timenow > nextCSession
    asia := asia+millis24h
    nextCSession := asia
    
if(showContSessions and timenow < nextCSession)    
    drawTime(nextCSession, color.fuchsia)


    
    
//history = input(title="Show history", type=input.bool, defval=true)    
    
//if(history)
//    drawTime(is1)
//    drawTime(is2) 
//    drawTime(is3) 
//    drawTime(is4) 
    