ShadeNY_v5b_v1
Download scriptThis tool is designed for traders using the MetaTrader 4 platform to visually identify specific trading sessions—such as the New York session—directly on their price charts. By highlighting these periods, it allows users to easily distinguish between different times of day, helping them focus on high-volume windows or specific market behaviors without needing to constantly check a clock.
Core Functionality
The script automatically draws shaded rectangular boxes over the chart to represent designated trading sessions. It works on various timeframes (ranging from 1 minute up to 4 hours) and covers a history of up to 5 days at a time. The shading helps "block out" the background, making it visually clear when a specific market session is active or has concluded.
Key Logic & Features
-
Time Zone Synchronization: One of the primary complexities this script handles is the difference between your trading platform's server time and the actual local time of the financial exchange (e.g., New York time). The logic calculates the offset between these two zones to ensure that the shaded boxes appear at the correct moments regardless of where your broker is located.
-
Dynamic Session Calculation: The script doesn't just draw a generic box; it calculates the exact duration of the session based on user-defined opening and closing hours and minutes. It is intelligent enough to handle sessions that might span across two different calendar days, ensuring the shading remains continuous and accurate.
-
Visual Customization: Users can personalize the appearance of the session highlights through several settings:
- Color: Choose the specific color of the shaded area.
- Style: Decide whether the session should appear as a solid block of color or as an outlined frame (using styles like dashed or dotted lines).
- Background Placement: Choose whether the shading sits on top of the price candles or behind them.
-
Real-Time Responsiveness: The script includes a "SetImmediacy" feature. This ensures that as soon as a new session begins, the visual shading appears instantly on the chart rather than waiting for a candle to fully close. It also includes "cleanup" logic to ensure that when you remove the indicator, it leaves no lingering objects or "ghost" boxes on your chart.
Technical Constraints
To maintain optimal performance and clarity, the script is designed to only activate on charts from 1 minute to 4 hours. If a user attempts to apply it to higher timeframes (like Daily or Weekly charts), the script will remain inactive as the visual representation would no longer be meaningful at that scale.
//+------------------------------------------------------------------+
//| ShadeNY_v5.mq4 |
//| Copyright © 2006, sx ted |
//| Purpose: shade New York or other sessions for chart time frames |
//| M1 to H4 (at a push). |
//| Notes..: To change chart time frame from M5 to M15 for example, |
//| click on M15, M5 and M15 again to force a chart refresh.|
//| Modify the values of MAX_DAYS_TO_SHADE, SESSION_OPEN_HH,|
//| SESSION_OPEN_MM, SESSION_CLOSE_HH and SESSION_CLOSE_MM |
//| to suit ***** THE LOCAL TIMES AT THE EXCHANGE *****. |
//| The values for SERVER_TIME_ZONE and EXCHANGE_TIME_ZONE |
//| are to be edited for DST when applicable. |
//| version: 2 - enhanced for speed but with MT4 beeing so fast no |
//| difference will be noticed, all the sessions are |
//| shaded in the init(), last session if it is current |
//| is widened in the start() in lieu of repainting all.|
//| 3 - 2006.03.22 added "SetImmediacyON" input parameter |
//| which forces shading after first tick is received |
//| in the new session. Corrected case where bar is not |
//| complete and session starts, and case when session |
//| covers two days when in Moscow (GMT+3 and greater). |
//| 4 - 2006.03.29 corrected for not taking into account the|
//| difference between Local time and Server time. |
//| 5 - 2006.04.03 corrected for Server time change, open |
//| and close times of exchange are now expressed in |
//| local times of the exchange. |
//| Added new SERVER_TIME_ZONE and EXCHANGE_TIME_ZONE in|
//| the #define section. Finally learnt about GMT time! |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, sx ted"
#property link ""
#property indicator_chart_window
//---- input parameters
extern color ShadeColor=Yellow;
extern bool SetImmediacyON=true; // if set ON forces shading after first tick in new session
extern string HelpText1 = "TRUE for solid, FALSE for frames"; // by Abhi
extern bool SolidRectangle = false; // by Abhi; if false shows rectangle frame
extern string HelpText2 = "0 - solid 1 - dash 2 - dot etc..."; // by Abhi
extern int FrameStyle = 0; // by Abhi
#define MAX_DAYS_TO_SHADE 5 // maximum number of days back from last chart date to be shaded
#define SERVER_TIME_ZONE +2 // MetaTrader server time zone, compare CurTime() with Greenwich GMT
#define EXCHANGE_TIME_ZONE -4 // Exchange time zone, example New York -4 GMT
#define SESSION_OPEN_HH 09 // session open hour (in local time at the exchange)
#define SESSION_OPEN_MM 30 // session open minutes (in local time at the exchange)
#define SESSION_CLOSE_HH 16 // session close hour (in local time at the exchange)
#define SESSION_CLOSE_MM 05 // session close minutes (in local time at the exchange)
//---- global variables to program
string obj[]; // array of object names
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
if(Period() > PERIOD_H4) return(0); // no shading required
// comment out or delete the following line when no longer required
Alert( "ServerTime at ", ServerAddress(), " is ", TimeToStr( CurTime(),TIME_MINUTES ), "\nAdjust #define SERVER_TIME_ZONE if required" );
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
for(int i=0; i < ArraySize(obj); i++)
{
if(ObjectFind(obj[i]) > -1) ObjectDelete(obj[i]); // tidy up
}
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if(Period() > PERIOD_H4) return(0); // no shading required
int i, iCount, iAdjust, // counters
iStart, iEnd; // x co-ordinates of object (index)
double dLow, dHigh; // y co-ordinates of object
string sName; // name for the object
int iSessionDuration; // session duration in seconds
bool ok;
// adjust for difference between Server time and Local time at the exchange (in minutes)
iAdjust=(SERVER_TIME_ZONE - EXCHANGE_TIME_ZONE) * 60;
// determine open time of first bar in the session matching chart time frame in format " hh:mi"
iStart=SESSION_OPEN_HH*60+SESSION_OPEN_MM+iAdjust;
if(iStart > 24*60) iStart-=24*60;
string sStart=" "+TimeToStr(StrToTime("2006.3.22")+(MathFloor(iStart/Period()) * Period() ) * 60, TIME_MINUTES);
// calculate session duration in seconds, check if session covers two days (cater for GMT+3 and above)
if(SESSION_OPEN_HH*60+SESSION_OPEN_MM >= SESSION_CLOSE_HH*60+SESSION_CLOSE_MM) iSessionDuration = ((23-SESSION_OPEN_HH+SESSION_CLOSE_HH)*60+59-SESSION_OPEN_MM+SESSION_CLOSE_MM+1)*60;
else iSessionDuration = ( (SESSION_CLOSE_HH*60+SESSION_CLOSE_MM) - (SESSION_OPEN_HH*60+SESSION_OPEN_MM) )*60;
// round session duration to suit an open time of chart time frame
datetime tStart =StrToTime(TimeToStr(Time[0], TIME_DATE)+sStart);
datetime tEnd =StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+DoubleToStr(SESSION_OPEN_HH ,0)+":"+DoubleToStr(SESSION_OPEN_MM ,0))+iSessionDuration+iAdjust*60;
iSessionDuration=MathCeil((tEnd-tStart)/Period())*Period();
// clear previous objects
for(i=0; i < ArraySize(obj); i++)
{
if(ObjectFind(obj[i]) > -1) ObjectDelete(obj[i]);
}
for(i=MAX_DAYS_TO_SHADE; i >= 0; i--)
{
tStart=StrToTime(TimeToStr(iTime(NULL, PERIOD_D1, i), TIME_DATE)+sStart);
tEnd =tStart+iSessionDuration;
iStart=iBarShift(NULL, 0, tStart, false);
ok=(iStart > 0 && iStart < Bars-1 && TimeDayOfYear(tStart) == TimeDayOfYear(Time[iStart]));
if(!ok && SetImmediacyON && CurTime() >= tStart && CurTime() < tEnd)
{
ok=true;
iStart=1;
}
if(ok)
{
iEnd=iBarShift(NULL, 0, tEnd, false);
if(iEnd >= Bars-1) iEnd=0; // end not found, therefore current session
while(iEnd < iStart && Time[iEnd] > tEnd) iEnd++; // cater for earlier close on Friday
dLow =Low[Lowest(NULL,0,MODE_LOW,iStart-iEnd,iEnd)];
dHigh=High[Highest(NULL,0,MODE_HIGH,iStart-iEnd,iEnd)];
sName="Session_"+TimeToStr(iTime(NULL, PERIOD_D1, i), TIME_DATE);
ObjectCreate(sName,OBJ_RECTANGLE,0,Time[iStart],dLow-Point,Time[iEnd],dHigh+Point);
ObjectSet(sName,OBJPROP_COLOR,ShadeColor);
ObjectSet(sName,OBJPROP_STYLE,FrameStyle);
ObjectSet(sName,OBJPROP_BACK,SolidRectangle);
// keep track of object names for tidying up upon exit
iCount=ArraySize(obj);
ArrayResize(obj, iCount+1);
obj[iCount]=sName;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments