Trading Hours
Download scriptThis visual tool is designed to help traders identify specific market sessions on their trading charts by highlighting the difference between active and inactive trading hours. Instead of just showing price movement, this indicator focuses on the "time" dimension, providing a clear chronological reference for your trading day.
How the Logic Works
The script functions by analyzing the relationship between your broker's server time and the actual local time of a specific market (such as the USA). It performs several behind-the-scenes calculations to ensure accuracy:
- Timezone Synchronization: The tool takes into account two different time offsets. It looks at the source of your data feed (the server time) and compares it against the local timezone of the currency or market you are currently viewing.
- Dynamic Hour Calculation: For every candle on your chart, the script calculates the current hour based on these timezones. It ensures that the math remains accurate even when the time rolls over at midnight.
- Market Window Detection: You can define specific "Open" and "Close" hours. The logic constantly checks if the current hour falls within this designated window.
Visual Representation
The indicator displays this information in a separate window below your main chart using a histogram format:
- Active Trading Hours (Blue Bars): When the market is open during your specified timeframe, the indicator displays blue bars. The height of these bars corresponds to the hour of the day.
- Inactive Trading Hours (Red Bars): When the market is closed or outside of your specified timeframe, the indicator switches to red bars. These are displayed in a downward direction, creating a clear visual contrast between "working" hours and "off" hours.
Key Benefits for the User
By using this tool, you can instantly see whether the current price action is occurring during peak liquidity or during quiet periods. It removes the guesswork of manual time conversions, allowing you to maintain a disciplined trading routine by visually confirming when the market is officially open based on your specific preferences.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//by Gilani
// currencyTimeZone - adjust to the timezone of the relevant currency.
// serverTimeZone - timezone of the source of data source feed (+1 Russian zone for MetaTrader)
// blue bars represent open market hours
// red bars represent close mrket hours
// the bars grows according to the hours of the day
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
extern int currencyTimeZone = -5;
extern string market = "USA";
int serverTimeZone = +1;
int marketOpensAt = 8;
int marketClosesAt = 16;
double open[];
double close[];
int init()
{
//---- indicators
//----
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,open);
SetIndexLabel(0,"Blue");
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(1,close);
SetIndexLabel(1,"Red");
//----
IndicatorShortName("Trading Hours: "+market);
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int ExtCountedBars=IndicatorCounted();
if(ExtCountedBars<0) return(-1);
if(ExtCountedBars>0) ExtCountedBars--;
limit=Bars-ExtCountedBars;
for (int x=limit;x>=0;x--) {
int gmt = MathMod (24+TimeHour(Time[x])-serverTimeZone, 24);
int hour = MathMod(24+currencyTimeZone+gmt, 24);
if ((hour >= marketOpensAt) && (hour <= marketClosesAt)) {
if (hour==0) hour=24;
open[x]=hour;
close[x]=0;
} else {
if (hour==0) hour=24;
open[x]=0;
close[x]= -hour;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments