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

//@version=5

// @description Time filters for trading strategies.
library(title="Time_Filter", overlay = true)

// @function f_isInWeekDay - Filter by week day or by time delimited session.
// @param _timeZone - Time zone to use when filter allowed trading by days of the week.
// @param _byWeekDay - Filter allowed trading time by days of the week.
// @param _byMon - Is Monday a trading day?
// @param _byTue - Is Tuesday a trading day?
// @param _byWed - Is Wednesday a trading day?
// @param _byThu - Is Thursday a trading day?
// @param _byFri - Is Friday a trading day?
// @param _bySat - Is Saturday a trading day?
// @param _bySun - Is Sunday a trading day?
// @returns series of bool whether or not the time is inside the current day.
export f_isInWeekDay( string _timeZone, simple bool _byWeekDay, simple bool _byMon,
        			  simple bool _byTue, simple bool _byWed, simple bool _byThu, 
             		  simple bool _byFri, simple bool _bySat, simple bool _bySun) =>
    _today = dayofweek(time, _timeZone)
    _isWeekDay = not _byWeekDay ? true :
                 _byMon and _today == dayofweek.monday    ? true :
                 _byTue and _today == dayofweek.tuesday   ? true :
                 _byWed and _today == dayofweek.wednesday ? true :
                 _byThu and _today == dayofweek.thursday  ? true :
                 _byFri and _today == dayofweek.friday    ? true :
                 _bySat and _today == dayofweek.saturday  ? true :
                 _bySun and _today == dayofweek.sunday    ? true :
                 false


// @function f_isInSession - Is the current time with in the allowed trading session time.
// @param _timeZone - Time zone to use when filter allowed trading by days of the week.
// @param _bySession_1 - Filter allowed trading time with in hours defined in _timeSession_1
// @param _timeSession_1 - Hours with in trading is allowed.
// @param _bySession_2 - Filter allowed trading time with in hours defined in _timeSession_2
// @param _timeSession_2 - Hours with in trading is allowed.
// @returns series of bool whether or not the time is inside selected session.
export f_isInSession(simple string _timeZone, 
					 simple bool _bySession_1,
					 simple string _timeSession_1,
					 simple bool _bySession_2,
					 simple string _timeSession_2) =>
    _inSession = not _bySession_1 ? true :
                 _bySession_1 and time(timeframe.period, _timeSession_1, _timeZone) ? true :
                 _bySession_2 and time(timeframe.period, _timeSession_2, _timeZone) ? true :
                 false

// @function f_isTradingAllowed - Is the current time with in the allowed.
// @param _timeZone - Time zone to use when filter allowed trading by days of the week.
// @param _byWeekDay - Filter allowed trading time by days of the week.
// @param _byMon - Is Monday a trading day?
// @param _byTue - Is Tuesday a trading day?
// @param _byWed - Is Wednesday a trading day?
// @param _byThu - Is Thursday a trading day?
// @param _byFri - Is Friday a trading day?
// @param _bySat - Is Saturday a trading day?
// @param _bySun - Is Sunday a trading day?
// @param _bySession_1 - Filter allowed trading time with in hours defined in _timeSession_1
// @param _timeSession_1 - Hours with in trading is allowed.
// @param _bySession_2 - Filter allowed trading time with in hours defined in _timeSession_2
// @param _timeSession_2 - Hours with in trading is allowed.
// @returns series of bool whether or not trading is allowed at the current time.
export f_isTradingAllowed(simple string _timeZone,
						  simple bool _byWeekDay, 
						  simple bool _byMon, 
						  simple bool _byTue,
						  simple bool _byWed,
						  simple bool _byThu,
						  simple bool _byFri,
						  simple bool _bySat,
						  simple bool _bySun, 
	                      simple bool _bySession_1, 
						  simple string _timeSession_1, 
						  simple bool _bySession_2,
						  simple string _timeSession_2) =>
    _inWeekDay  = f_isInWeekDay(_timeZone, _byWeekDay, 
                              _byMon, _byTue, _byWed, _byThu, _byFri, _bySat, _bySun)
    
    _inSession = f_isInSession(_timeZone, 
                               _bySession_1, _timeSession_1,
                               _bySession_2, _timeSession_2)                    
    
    _tradingTime = _inWeekDay and _inSession








// ==========================================================================================================
// EXAMPLE 
// ==========================================================================================================

// === INPUT ===
filterTime_timeZone = input.string("GMT+0", "Time Zone", group =  "⏱️ Time Filter" , 
     tooltip = "GMT and UTC is the same thing \nMatch this setting to bottom right time",
     options = [ "GMT-10", "GMT-9", "GMT-8", "GMT-7", "GMT-6", "GMT-5", 
                 "GMT-4", "GMT-3", "GMT+0", "GMT+1", "GMT+2", "GMT+3", 
                 "GMT+4", "GMT+5", "GMT+6", "GMT+7", "GMT+8", "GMT+9", 
                 "GMT+10", "GMT+10:30","GMT+11", "GMT+12", "GMT+13", "GMT+13:45"])

// Weekdays Filter
filterTime_byWeekDay = input.bool(true, "Use Weekdays Filter?", group =  "⏱️ Time Filter" ,
     tooltip = "Disable to allow all weekdays, Enable to choose certain days")
filterTime_Mon  = input.bool(true, "Mon  ", inline = "Days", group =  "⏱️ Time Filter" )
filterTime_Tue  = input.bool(true, "Tue  ", inline = "Days", group =  "⏱️ Time Filter" )
filterTime_Wed  = input.bool(true, "Wed  ", inline = "Days", group =  "⏱️ Time Filter" )
filterTime_Thu  = input.bool(true, "Thu  ", inline = "Days", group =  "⏱️ Time Filter" )
filterTime_Fri  = input.bool(true, "Fri  ", inline = "Days", group =  "⏱️ Time Filter" )
filterTime_Sat  = input.bool(false, "Sat  ", inline = "Days", group =  "⏱️ Time Filter" )
filterTime_Sun  = input.bool(false, "Sun",   inline = "Days", group =  "⏱️ Time Filter" )

// Hour Filter by session
filterTime_bySession_1   = input.bool(false, "Use Hourly Filter?",    group =  "⏱️ Time Filter" )
filterTime_timeSession_1 = input.session("0900-1730", "Hour Session", group =  "⏱️ Time Filter" )

filterTime_bySession_2   = input.bool(false, "Use 2ND Hourly Filter?",  group =  "⏱️ Time Filter" )
filterTime_timeSession_2 = input.session("1930-2100", "Hour Session 2", group =  "⏱️ Time Filter" )

// === LOGIC ===
bool isInTime = f_isTradingAllowed(filterTime_timeZone,
                             filterTime_byWeekDay,
                             filterTime_Mon,
                             filterTime_Tue, 
                             filterTime_Wed,
                             filterTime_Thu,
                             filterTime_Fri, 
                             filterTime_Sat,
                             filterTime_Sun,
                             filterTime_bySession_1,
                             filterTime_timeSession_1,
                             filterTime_bySession_2,
                             filterTime_timeSession_2)

// === PLOT ===
bgcolor(isInTime ? na : color.new(color.gray, 90), title =  "⏱️ Time Filter" )
