Personal Trading Hours (timezone Europe/Amsterdam)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chrismariatrader
// Originaly created for the students of fxminds.nl for backtesting purposes on the days and times that they are able to trade on. 
// But it can also be used for live trading when you, for example, want to filter out opening times or times that important news is released.
// Can be used on timeframes that are LOWER than the Daily timeframe.
// If used on the Daily timeframe: in the above section of the settingsmenu all days must be unchecked and only the lower part of the settingsmenu can be used.
// Can NOT be used on timeframes higher than the Daily timeframe.
// Last updated 31-8-2023
// Version 1.0

//@version=5
indicator(title="Trading Hours with days and time input (timezone Europe/Amsterdam)", shorttitle="Trading Hours Europe/Amsterdam timezone", overlay=true)

// Create the user inputs
var G_Time_1             = "Trading Hours for Monday" 
monSession  = input.bool(false, title="Trade on Monday", group=G_Time_1, tooltip="If you want to trade on Monday, check this box.")
Mon1        = input.session(title="1st Session Monday", defval="0000-0000", group=G_Time_1, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Mon2        = input.session(title="2nd Session Monday", defval="0000-0000", group=G_Time_1)
Mon3        = input.session(title="3th Session Monday", defval="0000-0000", group=G_Time_1)

var G_Time_2             = "Trading Hours for Tuesday"  
tueSession  = input.bool(false, title="Trade on Tuesday", group=G_Time_2, tooltip="If you want to trade on Tuesday, check this box.")
Tue1        = input.session(title="1st Session Tuesday", defval="0000-0000", group=G_Time_2, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Tue2        = input.session(title="2nd Session Tuesday", defval="0000-0000", group=G_Time_2)
Tue3        = input.session(title="3th Session Tuesday", defval="0000-0000", group=G_Time_2)

var G_Time_3             = "Trading Hours for Wednesday"  
wedSession  = input.bool(false, title="Trade on Wednesday", group=G_Time_3, tooltip="If you want to trade on Wednesday, check this box.")
Wed1        = input.session(title="1st Session Wednesday", defval="0000-0000", group=G_Time_3, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Wed2        = input.session(title="2nd Session Wednesday", defval="0000-0000", group=G_Time_3)
Wed3        = input.session(title="3th Session Wednesday", defval="0000-0000", group=G_Time_3)

var G_Time_4             = "Trading Hours for Thursday"  
thuSession  = input.bool(false, title="Trade on Thursday", group=G_Time_4, tooltip="If you want to trade on Thursday, check this box.")
Thu1        = input.session(title="1st Session Thursday", defval="0000-0000", group=G_Time_4, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Thu2        = input.session(title="2nd Session Thursday", defval="0000-0000", group=G_Time_4)
Thu3        = input.session(title="3th Session Thursday", defval="0000-0000", group=G_Time_4)

var G_Time_5             = "Trading Hours for Friday"  
friSession  = input.bool(false, title="Trade on Friday", group=G_Time_5, tooltip="If you want to trade on Friday, check this box.")
Fri1        = input.session(title="1st Session Friday", defval="0000-0000", group=G_Time_5, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Fri2        = input.session(title="2nd Session Friday", defval="0000-0000", group=G_Time_5)
Fri3        = input.session(title="3th Session Friday", defval="0000-0000", group=G_Time_5)

var G_Time_6             = "Trading Hours for Saturday"  
satSession  = input.bool(false, title="Trade on Saturday", group=G_Time_6, tooltip="If you want to trade on Saturday, check this box.")
Sat1        = input.session(title="1st Session Saturday", defval="0000-0000", group=G_Time_6, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Sat2        = input.session(title="2nd Session Saturday", defval="0000-0000", group=G_Time_6)
Sat3        = input.session(title="3th Session Saturday", defval="0000-0000", group=G_Time_6)

var G_Time_7             = "Trading Hours for Sunday"  
sunSession  = input.bool(false, title="Trade on Sunday", group=G_Time_7, tooltip="If you want to trade on Sunday, check this box.")
Sun1        = input.session(title="1st Session Sunday", defval="0000-0000", group=G_Time_7, tooltip= "If you do not need the extra trading sessions below, make sure that the unused blocks have the same time periode as one of the used blocks")
Sun2        = input.session(title="2nd Session Sunday", defval="0000-0000", group=G_Time_7)
Sun3        = input.session(title="3th Session Sunday", defval="0000-0000", group=G_Time_7)


var G_daily   = "Want to trade only on Daily Chart?"
daily_only    = input.bool(false, title="Trade only on daily chart", group=G_daily, tooltip="If you want to trade only on the daily, check this box and make sure that all seperate days above are NOT checked, then check the boxes of the days you want to trade daily below. At least one of the days must be checked. You can't use these settings on other timeframes than the daily.")
monSessionDay = input.bool(false, title="Mon", group=G_daily, inline="1")
tueSessionDay = input.bool(false, title="Tue", group=G_daily, inline="1")
wedSessionDay = input.bool(false, title="Wed", group=G_daily, inline="1")
thuSessionDay = input.bool(false, title="Thu", group=G_daily, inline="1")
friSessionDay = input.bool(false, title="Fri", group=G_daily, inline="2")
weekend       = input.bool(false, title="Weekend", group=G_daily, inline="2")

// Create function
Tradingtime(tradinghours, TimeZone = "Europe/Amsterdam")=>
    not na(time(timeframe.period, tradinghours, TimeZone))

Monday = ""
if monSession == true 
    Monday += "2"
tradingSessionMon = Mon1 + "," + Mon2 + "," + Mon3 + "," + ":" + Monday
MondaySession = Tradingtime(tradingSessionMon)
bgcolor_mon = MondaySession and monSession ? color.new(color.fuchsia, 90) : na

Tuesday = ""
if tueSession == true 
    Tuesday += "3"
tradingSessionTue = Tue1 + "," + Tue2 + "," + Tue3 + "," + ":" + Tuesday
TuesdaySession = Tradingtime(tradingSessionTue)
bgcolor_tue = TuesdaySession and tueSession ? color.new(color.fuchsia, 90) : na

Wednesday = ""
if wedSession == true 
    Wednesday += "4"
tradingSessionWed = Wed1 + "," + Wed2 + "," + Wed3 + "," + ":" + Wednesday
WednesdaySession = Tradingtime(tradingSessionWed)
bgcolor_wed = WednesdaySession and wedSession ? color.new(color.fuchsia, 90) : na

Thursday = ""
if thuSession == true 
    Thursday += "5"
tradingSessionThu = Thu1 + "," + Thu2 + "," + Thu3 + "," + ":" + Thursday
ThursdaySession = Tradingtime(tradingSessionThu)
bgcolor_thu = ThursdaySession and thuSession ? color.new(color.fuchsia, 90) : na

Friday = ""
if friSession == true 
    Friday += "6"
tradingSessionFri = Fri1 + "," + Fri2 + "," + Fri3 + "," + ":" + Friday
FridaySession = Tradingtime(tradingSessionFri)
bgcolor_fri   = FridaySession and friSession ? color.new(color.fuchsia, 90) : na

Saturday = ""
if satSession == true 
    Saturday += "7"
tradingSessionSat = Sat1 + "," + Sat2 + "," + Sat3 + "," + ":" + Saturday
SaturdaySession = Tradingtime(tradingSessionSat)
bgcolor_sat   = SaturdaySession and satSession ? color.new(color.fuchsia, 90) : na

Sunday = ""
if sunSession == true 
    Sunday += "1"
tradingSessionSun = Sun1 + "," + Sun2 + "," + Sun3 + "," + ":" + Sunday
SundaySession = Tradingtime(tradingSessionSun)
bgcolor_sun   = SundaySession and sunSession ? color.new(color.fuchsia, 90) : na

//Highlight background Trading Hours
bgcolor(bgcolor_mon, title="Monday Sessions")
bgcolor(bgcolor_tue, title="Tuesday Sessions")
bgcolor(bgcolor_wed, title="Wednesday Sessions")
bgcolor(bgcolor_thu, title="Thursday Sessions")
bgcolor(bgcolor_fri, title="Friday Sessions")
bgcolor(bgcolor_sat, title="Saturday Sessions")
bgcolor(bgcolor_sun, title="Sunday Sessions")

// Create function
TradingtimeDaily(tradinghours, TimeZone = "Europe/Amsterdam")=>
    not na(time(timeframe.period, tradinghours, TimeZone))

DailySessions = ""
if monSessionDay
    DailySessions += "1"
if tueSessionDay
    DailySessions += "2"
if wedSessionDay
    DailySessions += "3"
if thuSessionDay
    DailySessions += "4"
if friSessionDay
    DailySessions += "5"
if weekend
    DailySessions += "67"

tradingSessionDaily = string(na)
if daily_only == true
    tradingSessionDaily := "0000-0000" + "," + ":" + DailySessions
dailySession = TradingtimeDaily(tradingSessionDaily) 
colordaily=dailySession and daily_only ? color.new(#fbdf40, 90) : na

//Highlight background Trading Hours
bgcolor(colordaily, title="Daily Sessions")


