Forex Sessions UTC + customise (DST Support)This Pine Script is designed to highlight forex trading sessions on a TradingView chart, showing which session (Asia, London, New York, Frankfurt, Sydney) is active based on your selected timezone offset and daylight saving time (DST) settings. It also includes an option to highlight session overlaps, like the London-New York and Asia-London overlaps.
Let's break down the script in detail:
### 1. **Inputs**:
* **`timezoneOffset`**:
* This input allows you to select your local timezone offset in hours relative to UTC (e.g., -2 for UTC-2 or +3 for UTC+3). It helps adjust the session times based on your local time.
* **`isDST`**:
* A boolean input that determines if Daylight Saving Time (DST) is active or not. If DST is enabled, the session times will be adjusted by one hour.
### 2. **Function: `timeInDailyRange`**:
This function checks if the current time (bar) is within a given session's start and end time.
* **`startH, startM`**: Start time of the session (hours and minutes).
* **`endH, endM`**: End time of the session (hours and minutes).
* **`tzOffset`**: The timezone offset in hours (which adjusts the start and end times for different regions).
The function handles sessions that cross over midnight. For example, the Asia session starts at 23:00 UTC and ends at 07:00 UTC, so it checks if the current time falls within that period.
### 3. **Session Times (in UTC)**:
Each trading session has specific times defined in UTC:
* **Asia Session**: 23:00 UTC to 07:00 UTC
* **London Session**: 08:00 UTC to 16:00 UTC
* **New York Session**: 13:00 UTC to 21:00 UTC
* **Frankfurt Session**: 07:00 UTC to 15:00 UTC
* **Sydney Session**: 21:00 UTC to 05:00 UTC
These times are defined in 24-hour format, and they represent the opening and closing hours for each market.
The script uses specific colors to represent different sessions:
* **Asia**: Yellow (`asiaColor`)
* **London**: Red (`londonColor`)
* **New York**: Navy Blue (`nyColor`)
* **Frankfurt**: Blue (`frankfurtColor`)
* **Sydney**: Green (`sydneyColor`)
* **Overlaps**:
* London-NY overlap: Purple (`overlapLN`)
* Asia-London overlap: Orange (`overlapAL`)
These colors are used to highlight the background during active sessions.
### 5. **Session Highlighting**:
The script uses the `bgcolor` function to change the background color of the chart when a session is active.
* For each session, it checks whether the current time (bar) falls within the session's time range (adjusted by the `timezoneOffset`).
* If a session is active, it will highlight the chart background in the corresponding session color.
### 6. **Overlaps**:
* **London-NY Overlap**: The script checks if both the London session and the New York session are active at the same time (i.e., if their time ranges overlap). If so, it highlights the overlap area in purple.
* **Asia-London Overlap**: Similarly, the script checks if both the Asia and London sessions are active at the same time, and if so, it highlights the overlap area in orange.
### 7. **Daylight Saving Time (DST) Adjustment**:
* If the **`isDST`** flag is enabled (i.e., DST is active), the script adjusts the session start and end times by adding one hour to each session. This is because during DST, trading hours shift by one hour (usually in regions that observe DST).
* This adjustment is applied to all session times, so the script accounts for the time change and ensures that the session times are correctly displayed.
### 8. **Final Background Color Logic**:
* The `bgcolor` function checks whether each session is active by calling `timeInDailyRange` for each session and then colors the chart accordingly.
* It also checks for overlapping sessions and colors the chart with the appropriate overlap color.
### Example Scenario:
1. **Timezone Offset**: If you select `timezoneOffset = 3` (UTC+3), the session times will be adjusted by adding 3 hours to the start and end times.
2. **Daylight Saving Time (DST)**: If `isDST = true` is selected, the session start times will shift by 1 hour forward (e.g., 23:00 UTC becomes 00:00 UTC for Asia).
3. **Visual Outcome**: The chart will display different colors in the background depending on which trading session is active (e.g., red for London, navy for New York), and purple or orange for overlap sessions.
### Key Features:
* **Customizable Timezone Offset**: Adjust session times to reflect your local timezone.
* **Daylight Saving Time Support**: Automatically adjusts session times during DST.
* **Highlighting Sessions**: Color the chart background to visualize when different trading sessions are active.
* **Overlap Highlighting**: Highlights the periods when certain sessions overlap (London-NY, Asia-London).
In summary, this script is useful for traders who want to visually see when different forex trading sessions are active on the chart, with flexibility for timezone and DST adjustments.
Utctime
TimezoneFormatIANAUTCLibrary "TimezoneFormatIANAUTC"
Provides either the full IANA timezone identifier or the corresponding UTC offset for TradingView’s built-in variables and functions.
tz(_tzname, _format)
Parameters:
_tzname (string) : "London", "New York", "Istanbul", "+1:00", "-03:00" etc.
_format (string) : "IANA" or "UTC"
Returns: "Europe/London", "America/New York", "UTC+1:00"
Example Code
import ARrowofTime/TimezoneFormatIANAUTC/1 as libtz
sesTZInput = input.string(defval = "Singapore", title = "Timezone")
example1 = libtz.tz("London", "IANA") // Return Europe/London
example2 = libtz.tz("London", "UTC") // Return UTC+1:00
example3 = libtz.tz("UTC+5", "IANA") // Return UTC+5:00
example4 = libtz.tz("UTC+4:30", "UTC") // Return UTC+4:30
example5 = libtz.tz(sesTZInput, "IANA") // Return Asia/Singapore
example6 = libtz.tz(sesTZInput, "UTC") // Return UTC+8:00
sesTime1 = time("","1300-1700", example1) // returns the UNIX time of the current bar in session time or na
sesTime2 = time("","1300-1700", example2) // returns the UNIX time of the current bar in session time or na
sesTime3 = time("","1300-1700", example3) // returns the UNIX time of the current bar in session time or na
sesTime4 = time("","1300-1700", example4) // returns the UNIX time of the current bar in session time or na
sesTime5 = time("","1300-1700", example5) // returns the UNIX time of the current bar in session time or na
sesTime6 = time("","1300-1700", example6) // returns the UNIX time of the current bar in session time or na
Parameter Format Guide
This section explains how to properly format the parameters for the tz(_tzname, _format) function.
_tzname (string) must be either;
A valid timezone name exactly as it appears in the chart’s lower-right corner (e.g. New York, London).
A valid UTC offset in ±H:MM or ±HH:MM format. Hours: 0–14 (zero-padded or not, e.g. +1:30, +01:30, -0:00). Minutes: Must be 00, 15, 30, or 45
examples;
"New York" → ✅ Valid chart label
"London" → ✅ Valid chart label
"Berlin" → ✅ Valid chart label
"America/New York" → ❌ Invalid chart label. (Use "New York" instead)
"+1:30" → ✅ Valid offset with single-digit hour
"+01:30" → ✅ Valid offset with zero-padded hour
"-05:00" → ✅ Valid negative offset
"-0:00" → ✅ Valid zero offset
"+1:1" → ❌ Invalid (minute must be 00, 15, 30, or 45)
"+2:50" → ❌ Invalid (minute must be 00, 15, 30, or 45)
"+15:00" → ❌ Invalid (hour must be 14 or below)
_tztype (string) must be either;
"IANA" → returns full IANA timezone identifier (e.g. "Europe/London"). When a time function call uses an IANA time zone identifier for its timezone argument, its calculations adjust automatically for historical and future changes to the specified region’s observed time, such as daylight saving time (DST) and updates to time zone boundaries, instead of using a fixed offset from UTC.
"UTC" → returns UTC offset string (e.g. "UTC+01:00")

