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

//@version=5

// @description    FUNCTION TO AUTOMATICALLY SET SESSION TIMES FOR SYMBOLS AND EVENTUALLY TIMEZONE
//                  USEFUL MAINLY FOR FUTURES CONTRACTS, TO DIFFERENTIATE BETWEEN PIT AND OVERNIGHT SESSIONS, AND FOR 24 HOURS SYMBOLS IF YOU WANT TO "CREATE" SESSIONS FOR THEM
//                  THIS LIBRARY ONLY RETURNS CORRECT SESSION TIMES TO THE CALLING SCRIPT AND DOES NOTHING BY ITSELF ON THE CHART. THE CALLING SCRIPT MUST THEN USE THE RETURNED SESSION TIMES TO DO ANYTHING.
//                  FOR EXAMPLE, IN THE ATTACHED CHART THIS LIBRARY IS USED BY MY INITIAL BALANCE INDICATOR, WHICH CALLS IT TO RETRIEVE THE CORRECT SESSION TIMES FOR THE SELECTED SYMBOL IN THE CHART, GIVEN
//                  THAT DIFFERENT FUTURES CONTRACTS HAVE DIFFERENT PIT SESSION TIMES (RTH TIMES) AND TRADINGVIEW HASN'T IMPLEMENTED THAT YET.
library("SetSessionTimes")

// @param session_type_input: set an input.string paramenter in your script with any or all of the following possible values: ['Custom', 'FX-Tokyo', 'FX-London', 'FX-New York', 'Overnight Session (ON)', 'Day Session (RTH)'] and pass its input here
// @param custom_session_times_input: set an input.session parameter in your script to be able to set the custom session times for the 'Custom' option in previous param
// @param syminfo_XXX: simply pass the corresponding syminfo.XXX for the chart symbol
// @returns [session times for chart symbol, session timezone for chart symbol (only useful for Forex sessions)]

export SetSessionTimes(simple string session_type_input, simple string custom_session_times_input, simple string syminfo_type = syminfo.type, simple string syminfo_root = syminfo.root, simple string syminfo_timezone = syminfo.timezone)=>
    ret_times = if session_type_input == 'Custom'
        custom_session_times_input
    else if syminfo_type == 'stock'
        ''
    else if session_type_input == 'FX-Tokyo'
        '0900-1800'
    else if session_type_input == 'FX-London'
        '0800-1600'
    else if session_type_input == 'FX-New York'
        '0830-1600'
    else if syminfo_type == 'futures'
        if syminfo_root == 'FESX' or syminfo_root == 'FDXM' or syminfo_root == 'FDAX' or syminfo_root == 'FDXS' or syminfo_root == 'FSXE'
            if session_type_input == 'Overnight Session (ON)'
                '1730-0900'
            else
                '0900-1730'
        else if syminfo_root == 'FGBL'
            if session_type_input == 'Overnight Session (ON)'
                '1715-0900'
            else
                '0900-1715'
        else if syminfo_root == 'ES' or syminfo_root == 'NQ' or syminfo_root == 'YM' or syminfo_root == 'RTY' or syminfo_root == 'MES' or syminfo_root == 'MNQ' or syminfo_root == 'MRTY'
            if session_type_input == 'Overnight Session (ON)'
                '1500-0830'
            else
                '0830-1500'
        else if syminfo_root == 'CL' or syminfo_root == 'MCL' or syminfo_root == 'RB' or syminfo_root == 'NG' or syminfo_root == 'YF'
            if session_type_input == 'Overnight Session (ON)'
                '1430-0900'
            else
                '0900-1430'
        else if syminfo_root == 'GC' or syminfo_root == 'MGC'
            if session_type_input == 'Overnight Session (ON)'
                '1330-0820'
            else
                '0820-1330'
        else if syminfo_root == 'SI'
            if session_type_input == 'Overnight Session (ON)'
                '1325-0825'
            else
                '0825-1325'
        else if syminfo_root == 'PL'
            if session_type_input == 'Overnight Session (ON)'
                '1305-0820'
            else
                '0820-1305'
        else if syminfo_root == 'HG'
            if session_type_input == 'Overnight Session (ON)'
                '1300-0810'
            else
                '0810-1300'
        else if syminfo_root == 'ZN' or syminfo_root == 'ZB' or syminfo_root == 'TN' or syminfo_root == 'UB'
            if session_type_input == 'Overnight Session (ON)'
                '1400-0720'
            else
                '0720-1400'
        else if syminfo_root == '6B' or syminfo_root == '6C' or syminfo_root == '6E' or syminfo_root == '6J'
            if session_type_input == 'Overnight Session (ON)'
                '1400-0720'
            else
                '0720-1400'
        else if syminfo_root == '6A'
            if session_type_input == 'Overnight Session (ON)'
                '1400-0700'
            else
                '0700-1400'
        else if syminfo_root == 'ZC' or syminfo_root == 'ZW' or syminfo_root == 'ZS' or syminfo_root == 'ZL' or syminfo_root == 'ZM'
            if session_type_input == 'Overnight Session (ON)'
                '1320-0830'
            else
                '0830-1320'
        else if syminfo_root == 'LE' or syminfo_root == 'HE'
            if session_type_input == 'Overnight Session (ON)'
                '1300-0830'
            else
                '0830-1300'
        else
            custom_session_times_input
    else
        custom_session_times_input
        

    ret_tz = session_type_input == 'FX-Tokyo' ? 'Asia/Tokyo' :
      session_type_input == 'FX-London' ? 'Europe/London' :
      session_type_input == 'FX-New York' ? 'America/New_York' :
      syminfo_timezone
    
    [ret_times, ret_tz]
///******
