SessionTime/SessionBoxes.mq5
133 lines
5.7 KiB
MQL5
//+------------------------------------------------------------------+
//| SessionBoxes.mq5 |
//| Minimal example for SessionTime.mqh: one session drawn as a box, |
//| hours given in the session's own time zone. |
//+------------------------------------------------------------------+
#property copyright "Ghebanoaei Ciprian"
#property link "https://www.mql5.com/en/users/ciprian252"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0
#include <SessionTime.mqh>
input ENUM_ST_ZONE InpZone = ST_ZONE_LONDON; // Session time zone
input string InpStart = "08:00"; // Session start (zone time)
input string InpEnd = "16:30"; // Session end (zone time)
input int InpWinter = 99; // Broker winter offset (99 = detect on live charts)
input ENUM_ST_DST InpDst = ST_DST_US; // Broker DST rule
input int InpDays = 20; // Days to draw
input color InpColor = clrOrange; // Box colour
input string InpLabel = "London"; // Name (also keeps several copies apart)
#define PFX ("SBX_" + InpLabel + "_")
ST_Broker g_b;
int g_start, g_end;
bool g_ready = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
g_start = ST_ParseHM(InpStart);
g_end = ST_ParseHM(InpEnd);
if(g_start < 0 || g_end < 0 || g_start == g_end || InpDays < 1)
return INIT_PARAMETERS_INCORRECT;
g_b.dst = InpDst;
g_b.winter = (InpWinter == 99) ? 2 : InpWinter; // 2 = fallback until detected
g_ready = (InpWinter != 99) || MQLInfoInteger(MQL_TESTER);
EventSetTimer(2); // retry until connected and history loaded
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Remove the boxes when the indicator is removed |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer();
ObjectsDeleteAll(0, PFX);
}
//+------------------------------------------------------------------+
//| returns false when the M5 history is not ready yet |
//+------------------------------------------------------------------+
bool DrawDay(const datetime zoneDay)
{
datetime zs = zoneDay + g_start * 60;
datetime ze = zoneDay + g_end * 60 + (g_end <= g_start ? 86400 : 0); // crosses midnight
datetime t1 = ST_ZoneToServer(g_b, InpZone, zs);
datetime t2 = ST_ZoneToServer(g_b, InpZone, ze);
if(t1 > TimeCurrent()) return true;
MqlRates r[];
int n = CopyRates(_Symbol, PERIOD_M5, t1, MathMin(t2, TimeCurrent()) - 1, r);
if(n < 0) return false; // still loading (error 4401)
if(n == 0) return true; // weekend or holiday
double hi = r[0].high, lo = r[0].low;
for(int k = 1; k < n; k++) { hi = MathMax(hi, r[k].high); lo = MathMin(lo, r[k].low); }
string name = PFX + TimeToString(zoneDay, TIME_DATE);
if(ObjectFind(0, name) < 0) ObjectCreate(0, name, OBJ_RECTANGLE, 0, t1, hi, t2, lo);
ObjectMove(0, name, 0, t1, hi);
ObjectMove(0, name, 1, t2, lo);
ObjectSetInteger(0, name, OBJPROP_COLOR, InpColor);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, name, OBJPROP_BACK, true);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
ObjectSetString(0, name, OBJPROP_TOOLTIP, StringFormat("%s %s - %s server time", InpLabel,
TimeToString(t1, TIME_MINUTES), TimeToString(t2, TIME_MINUTES)));
return true;
}
//+------------------------------------------------------------------+
//| Draw the session for the last InpDays days |
//+------------------------------------------------------------------+
bool DrawAll()
{
datetime utcNow = ST_ServerToUtc(g_b, TimeCurrent());
datetime zoneNow = utcNow + ST_ZoneOffset(InpZone, utcNow) * 3600;
datetime today = zoneNow - zoneNow % 86400;
bool ok = true;
for(int d = 0; d <= InpDays; d++)
{
datetime day = today - d * 86400;
MqlDateTime m;
TimeToStruct(day, m);
if(m.day_of_week == 0 || m.day_of_week == 6) continue;
if(!DrawDay(day)) ok = false;
}
ChartRedraw();
return ok;
}
//+------------------------------------------------------------------+
//| Timer: wait for the connection, then (re)draw |
//+------------------------------------------------------------------+
void OnTimer()
{
if(!g_ready)
{
int w;
if(!ST_DetectWinterOffset(InpDst, w)) return; // wait for the connection
g_b.winter = w;
g_ready = true;
ObjectsDeleteAll(0, PFX);
}
DrawAll();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
if(g_ready && (prev_calculated == 0 || rates_total != prev_calculated))
DrawAll();
return rates_total;
}
//+------------------------------------------------------------------+