Session Filter [Trendoscope]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed
//                                       ░▒             
//                                  ▒▒▒   ▒▒      
//                              ▒▒▒▒▒     ▒▒      
//                      ▒▒▒▒▒▒▒░     ▒     ▒▒          
//                  ▒▒▒▒▒▒           ▒     ▒▒          
//             ▓▒▒▒       ▒        ▒▒▒▒▒▒▒▒▒▒▒  
//   ▒▒▒▒▒▒▒▒▒▒▒ ▒        ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒         
//   ▒  ▒       ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░        
//   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒         
//   ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒                       
//    ▒▒▒▒▒         ▒▒▒▒▒▒▒                            
//                 ▒▒▒▒▒▒▒▒▒                           
//                ▒▒▒▒▒ ▒▒▒▒▒                          
//               ░▒▒▒▒   ▒▒▒▒▓      ████████╗██████╗ ███████╗███╗   ██╗██████╗  ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
//              ▓▒▒▒▒     ▒▒▒▒      ╚══██╔══╝██╔══██╗██╔════╝████╗  ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
//              ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        ██║   ██████╔╝█████╗  ██╔██╗ ██║██║  ██║██║   ██║███████╗██║     ██║   ██║██████╔╝█████╗ 
//             ▒▒▒▒▒       ▒▒▒▒▒       ██║   ██╔══██╗██╔══╝  ██║╚██╗██║██║  ██║██║   ██║╚════██║██║     ██║   ██║██╔═══╝ ██╔══╝  
//            ▒▒▒▒▒         ▒▒▒▒▒      ██║   ██║  ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║     ███████╗
//             ▒▒             ▒                        
//@version=5
indicator("Session Filter [Trendoscope]", overlay = true)
useCustomTimezone = input.bool(false, 'Custom Timezone', inline='tz', group='Session Entry Filter')
timezoneStr = input.string("UTC", "", ["UTC-10", "UTC-8", "UTC-7", "UTC-6", "UTC-5", "UTC-4", "UTC-3",
                                         "UTC", "UTC+1", "UTC+2", "UTC+3", "UTC+3:30", "UTC+4", "UTC+5", "UTC+5:30", "UTC+5:45",
                                         "UTC+6", "UTC+6:30", "UTC+7", "UTC+8", "UTC+9", "UTC+9:30", "UTC+10", "UTC+11", "UTC+12", "UTC+12:45", "UTC+13"], inline='tz', group='Session Entry Filter',
                                         tooltip='Use custom timezone. If not selected, default chart timezone is used')
allowMon = input.bool(true, 'Mon', inline = "weekdays", group='Session Entry Filter')
allowTue = input.bool(true, 'Tue', inline = "weekdays", group='Session Entry Filter')
allowWed = input.bool(true, 'Wed', inline = "weekdays", group='Session Entry Filter')
allowThu = input.bool(true, 'Thu', inline = "weekdays", group='Session Entry Filter')
allowFri = input.bool(true, 'Fri', inline = "weekdays", group='Session Entry Filter')
allowSat = input.bool(true, 'Sat', inline = "weekdays", group='Session Entry Filter')
allowSun = input.bool(true, 'Sun', inline = "weekdays", group='Session Entry Filter', tooltip = 'Filter entry based on days of the week')

filterBySession1 = input.bool(true, 'Session 1', inline='s1', group='Session Entry Filter')
session1 = input.session("2100-0600", '', inline='s1', group='Session Entry Filter', tooltip = 'Filter by custom session option1')

filterBySession2 = input.bool(true, 'Session 2', inline='s2', group='Session Entry Filter')
session2 = input.session("0000-0900", '', inline='s2', group='Session Entry Filter', tooltip = 'Filter by custom session option2')

filterBySession3 = input.bool(false, 'Session 3', inline='s3', group='Session Entry Filter')
session3 = input.session("0700-1600", '', inline='s3', group='Session Entry Filter', tooltip = 'Filter by custom session option3')

filterBySession4 = input.bool(false, 'Session 4', inline='s4', group='Session Entry Filter')
session4 = input.session("1300-2200", '', inline='s4', group='Session Entry Filter', tooltip = 'Filter by custom session option4')

forceExitOutsideSession = input.bool(true, 'Force Exit Outside Session Filter')
timezoneToUse = useCustomTimezone? timezoneStr : syminfo.timezone
allowEntry = (
             (not na(time(timeframe.period, session1, timezoneToUse))?filterBySession1:false) or
             (not na(time(timeframe.period, session2, timezoneToUse))?filterBySession2:false) or
             (not na(time(timeframe.period, session3, timezoneToUse))?filterBySession3:false) or
             (not na(time(timeframe.period, session4, timezoneToUse))?filterBySession4:false)
             ) and
             (
             (dayofweek(time, timezoneToUse) == 1? allowSun:false) or
             (dayofweek(time, timezoneToUse) == 2? allowMon:false) or
             (dayofweek(time, timezoneToUse) == 3? allowTue:false) or
             (dayofweek(time, timezoneToUse) == 4? allowWed:false) or
             (dayofweek(time, timezoneToUse) == 5? allowThu:false) or
             (dayofweek(time, timezoneToUse) == 6? allowFri:false) or
             (dayofweek(time, timezoneToUse) == 7? allowSat:false)
             )

forceExit = forceExitOutsideSession ? not allowEntry : false
bgcolor(allowEntry? color.new(color.green, 90) : forceExit? color.new(color.red, 90) : na)

signal = allowEntry? 2 : forceExit? -2 : 0

startTradeSignal = signal == 2?2:0
stopTradeSignal= signal == -2?2:0

plot(startTradeSignal, 'startTradeSignal', color=color.green, display = display.data_window)
plot(stopTradeSignal, 'stopTradeSignal', color=color.red, display = display.data_window)

alertcondition(ta.change(startTradeSignal)>0 and startTradeSignal == 2, 'Start Trade')
alertcondition(ta.change(stopTradeSignal)>0 and stopTradeSignal == 2, 'Stop Trade')
