update to my mt5 libs to include better comments
This commit is contained in:
+150
-118
@@ -1,144 +1,176 @@
|
||||
#property library
|
||||
#include <Trade/Trade.mqh>
|
||||
#include <MyLibs/Utils/DealingWithTime.mqh>
|
||||
#include <Trade/Trade.mqh>
|
||||
|
||||
class TimeZones: public CObject{
|
||||
|
||||
protected:
|
||||
string dt_s;
|
||||
int len;
|
||||
string dt_string;
|
||||
datetime tC, tGMT, tNY, tLon, tFfm, tMosc, tSyd, tTok;
|
||||
datetime tz_time;
|
||||
string tz_date;
|
||||
datetime time_start;
|
||||
datetime time_end;
|
||||
bool is_time;
|
||||
datetime tGIVEN;
|
||||
datetime tREQ;
|
||||
datetime tzt;
|
||||
datetime tz_req;
|
||||
double ny_daily_close_protected(string symbol, int shift_days, bool print_data=false);
|
||||
double required_close;
|
||||
class TimeZones : public CObject {
|
||||
protected:
|
||||
string dt_s;
|
||||
int len;
|
||||
string dt_string;
|
||||
datetime tC, tGMT, tNY, tLon, tFfm, tMosc, tSyd, tTok;
|
||||
datetime tz_time;
|
||||
string tz_date;
|
||||
datetime time_start;
|
||||
datetime time_end;
|
||||
bool is_time;
|
||||
datetime tGIVEN;
|
||||
datetime tREQ;
|
||||
datetime tzt;
|
||||
datetime tz_req;
|
||||
double required_close;
|
||||
|
||||
public:
|
||||
string get_date_string_from_datetime(datetime dt);
|
||||
datetime get_timezone_time(string time_zone, bool print_time);
|
||||
datetime timezone_conversions(string time_zone_known, datetime time_given, string time_zone_required);
|
||||
double ny_daily_close(string symbol, int shift_days, bool print_data=false);
|
||||
double ny_daily_close_protected(string symbol, int shift_days, bool print_data = false);
|
||||
|
||||
public:
|
||||
string get_date_string_from_datetime(datetime dt);
|
||||
datetime get_timezone_time(string time_zone, bool print_time);
|
||||
datetime timezone_conversions(string time_zone_known, datetime time_given, string time_zone_required);
|
||||
double ny_daily_close(string symbol, int shift_days, bool print_data = false);
|
||||
};
|
||||
|
||||
string TimeZones::get_date_string_from_datetime(datetime dt){
|
||||
dt_s = TimeToString(dt);
|
||||
len = StringLen(dt_s);
|
||||
dt_string = StringSubstr(dt_s, 0, len-5);
|
||||
return dt_string;
|
||||
// ---------------------------------------------------------------------
|
||||
// Converts a datetime to a string excluding seconds.
|
||||
//
|
||||
// Parameters:
|
||||
// - dt : Datetime object.
|
||||
//
|
||||
// Returns:
|
||||
// - A string in the format "yyyy.mm.dd hh:mi".
|
||||
// ---------------------------------------------------------------------
|
||||
string TimeZones::get_date_string_from_datetime(datetime dt) {
|
||||
dt_s = TimeToString(dt);
|
||||
len = StringLen(dt_s);
|
||||
dt_string = StringSubstr(dt_s, 0, len - 5);
|
||||
return dt_string;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Gets the current time in the specified time zone.
|
||||
//
|
||||
// Parameters:
|
||||
// - time_zone : One of "NY", "Lon", "Ffm", "Syd", "Mosc", "Tok".
|
||||
// - print_time : If true, logs various times for debugging.
|
||||
//
|
||||
// Returns:
|
||||
// - Current time in the specified time zone.
|
||||
// ---------------------------------------------------------------------
|
||||
datetime TimeZones::get_timezone_time(string time_zone, bool print_time) {
|
||||
checkTimeOffset(TimeCurrent()); // Adjust DST
|
||||
|
||||
datetime TimeZones::get_timezone_time(string time_zone, bool print_time){
|
||||
// https://www.mql5.com/en/code/45287
|
||||
// https://www.mql5.com/en/articles/9926
|
||||
// https://www.mql5.com/en/articles/9929
|
||||
tC = TimeCurrent();
|
||||
tGMT = TimeCurrent() + OffsetBroker.actOffset;
|
||||
tNY = tGMT - (NYShift + DST_USD);
|
||||
tLon = tGMT - (LondonShift + DST_EUR);
|
||||
tFfm = tGMT - (FfmShift + DST_EUR);
|
||||
tSyd = tGMT - (SidneyShift + DST_AUD);
|
||||
tMosc = tGMT - (MoskwaShift + DST_RUS);
|
||||
tTok = tGMT - (TokyoShift);
|
||||
|
||||
checkTimeOffset(TimeCurrent()); // check changes of DST
|
||||
// cto();
|
||||
if (print_time) {
|
||||
Print("----------------------------------");
|
||||
Print("Broker: ", tC);
|
||||
Print("GMT: ", tGMT);
|
||||
Print("time in New York: ", tNY);
|
||||
Print("time in London: ", tLon);
|
||||
Print("time in Frankfurt: ", tFfm);
|
||||
Print("time in Sidney: ", tSyd);
|
||||
Print("time in Moscow: ", tMosc);
|
||||
Print("time in Tokyo: ", tTok);
|
||||
}
|
||||
|
||||
tC = TimeCurrent();
|
||||
tGMT = TimeCurrent() + OffsetBroker.actOffset; // GMT
|
||||
tNY = tGMT - (NYShift+DST_USD); // time in New York (EST)
|
||||
tLon = tGMT - (LondonShift+DST_EUR); // time in London
|
||||
tFfm = tGMT - (FfmShift+DST_EUR); // time in Frankfurt
|
||||
tSyd = tGMT - (SidneyShift+DST_AUD); // time in Sidney
|
||||
tMosc = tGMT - (MoskwaShift+DST_RUS); // time in Moscow
|
||||
tTok = tGMT - (TokyoShift); // time in Tokyo - no DST
|
||||
if (time_zone == "NY") return tNY;
|
||||
if (time_zone == "Lon") return tLon;
|
||||
if (time_zone == "Ffm") return tFfm;
|
||||
if (time_zone == "Syd") return tSyd;
|
||||
if (time_zone == "Mosc") return tMosc;
|
||||
if (time_zone == "Tok") return tTok;
|
||||
|
||||
if(print_time==true){
|
||||
Print("----------------------------------");
|
||||
Print("Broker: ", tC);
|
||||
Print("GMT: ", tGMT);
|
||||
Print("time in New York: ", tNY);
|
||||
Print("time in London: ", tLon);
|
||||
Print("time in Frankfurt: ", tFfm);
|
||||
Print("time in Sidney: ", tSyd);
|
||||
Print("time in Moscow: ", tMosc);
|
||||
Print("time in Tokyo: ", tTok);
|
||||
}
|
||||
|
||||
if(time_zone=="NY"){return tNY;}
|
||||
if(time_zone=="Lon"){return tLon;}
|
||||
if(time_zone=="Ffm"){return tFfm;}
|
||||
if(time_zone=="Syd"){return tSyd;}
|
||||
if(time_zone=="Mosc"){return tMosc;}
|
||||
if(time_zone=="Tok"){return tTok;}
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Converts a datetime from one timezone to another.
|
||||
//
|
||||
// Parameters:
|
||||
// - time_zone_known : Original timezone of the datetime.
|
||||
// - time_given : The datetime to convert.
|
||||
// - time_zone_required : Desired output timezone.
|
||||
//
|
||||
// Returns:
|
||||
// - The equivalent datetime in the target timezone.
|
||||
// ---------------------------------------------------------------------
|
||||
datetime TimeZones::timezone_conversions(string time_zone_known, datetime time_given, string time_zone_required) {
|
||||
tGIVEN = time_given;
|
||||
checkTimeOffset(tGIVEN); // Adjust DST
|
||||
|
||||
datetime TimeZones::timezone_conversions(string time_zone_known, datetime time_given, string time_zone_required){
|
||||
// https://www.mql5.com/en/code/45287
|
||||
// https://www.mql5.com/en/articles/9926
|
||||
// https://www.mql5.com/en/articles/9929
|
||||
// Step 1: Convert known timezone to GMT
|
||||
if (time_zone_known == "GMT") tGMT = tGIVEN;
|
||||
if (time_zone_known == "Broker") tGMT = tGIVEN + OffsetBroker.actOffset;
|
||||
if (time_zone_known == "NY") tGMT = tGIVEN + (NYShift + DST_USD);
|
||||
if (time_zone_known == "Lon") tGMT = tGIVEN + (LondonShift + DST_EUR);
|
||||
if (time_zone_known == "Ffm") tGMT = tGIVEN + (FfmShift + DST_EUR);
|
||||
if (time_zone_known == "Syd") tGMT = tGIVEN + (SidneyShift + DST_AUD);
|
||||
if (time_zone_known == "Mosc") tGMT = tGIVEN + (MoskwaShift + DST_RUS);
|
||||
if (time_zone_known == "Tok") tGMT = tGIVEN + (TokyoShift);
|
||||
|
||||
tGIVEN = time_given; //StringToTime(time_given);
|
||||
|
||||
checkTimeOffset(tGIVEN); // check changes of DST
|
||||
// Step 2: Convert GMT to required timezone
|
||||
if (time_zone_required == "GMT") tREQ = tGMT;
|
||||
if (time_zone_required == "Broker") tREQ = tGMT - OffsetBroker.actOffset;
|
||||
if (time_zone_required == "NY") tREQ = tGMT - (NYShift + DST_USD);
|
||||
if (time_zone_required == "Lon") tREQ = tGMT - (LondonShift + DST_EUR);
|
||||
if (time_zone_required == "Ffm") tREQ = tGMT - (FfmShift + DST_EUR);
|
||||
if (time_zone_required == "Syd") tREQ = tGMT - (SidneyShift + DST_AUD);
|
||||
if (time_zone_required == "Mosc") tREQ = tGMT - (MoskwaShift + DST_RUS);
|
||||
if (time_zone_required == "Tok") tREQ = tGMT - (TokyoShift);
|
||||
|
||||
// Get GMT:
|
||||
if(time_zone_known=="GMT" ){tGMT = tGIVEN;}
|
||||
if(time_zone_known=="Broker" ){tGMT = tGIVEN + OffsetBroker.actOffset;}
|
||||
if(time_zone_known=="NY" ){tGMT = tGIVEN + (NYShift+DST_USD);}
|
||||
if(time_zone_known=="Lon" ){tGMT = tGIVEN + (LondonShift+DST_EUR);}
|
||||
if(time_zone_known=="Ffm" ){tGMT = tGIVEN + (FfmShift+DST_EUR);}
|
||||
if(time_zone_known=="Syd" ){tGMT = tGIVEN + (SidneyShift+DST_AUD);}
|
||||
if(time_zone_known=="Mosc" ){tGMT = tGIVEN + (MoskwaShift+DST_RUS);}
|
||||
if(time_zone_known=="Tok" ){tGMT = tGIVEN + (TokyoShift);}
|
||||
|
||||
// define the required time:
|
||||
tREQ = NULL;
|
||||
if(time_zone_required=="GMT" ){tREQ = tGMT;}
|
||||
if(time_zone_required=="Broker" ){tREQ = tGMT - OffsetBroker.actOffset;}
|
||||
if(time_zone_required=="NY" ){tREQ = tGMT - (NYShift+DST_USD);}
|
||||
if(time_zone_required=="Lon" ){tREQ = tGMT - (LondonShift+DST_EUR);}
|
||||
if(time_zone_required=="Ffm" ){tREQ = tGMT - (FfmShift+DST_EUR);}
|
||||
if(time_zone_required=="Syd" ){tREQ = tGMT - (SidneyShift+DST_AUD) ;}
|
||||
if(time_zone_required=="Mosc" ){tREQ = tGMT - (MoskwaShift+DST_RUS);}
|
||||
if(time_zone_required=="Tok" ){tREQ = tGMT - (TokyoShift);}
|
||||
|
||||
return tREQ;
|
||||
return tREQ;
|
||||
}
|
||||
|
||||
// Calculte NY close time:
|
||||
double TimeZones::ny_daily_close(string symbol, int shift_days, bool print_data=false){
|
||||
required_close = ny_daily_close_protected(symbol, shift_days, print_data);
|
||||
return required_close;
|
||||
// ---------------------------------------------------------------------
|
||||
// Returns the most recent NY daily close price.
|
||||
//
|
||||
// Parameters:
|
||||
// - symbol : Trading symbol.
|
||||
// - shift_days : How many NY daily closes back to return.
|
||||
// - print_data : If true, logs debug information.
|
||||
//
|
||||
// Returns:
|
||||
// - The NY close price.
|
||||
// ---------------------------------------------------------------------
|
||||
double TimeZones::ny_daily_close(string symbol, int shift_days, bool print_data) {
|
||||
required_close = ny_daily_close_protected(symbol, shift_days, print_data);
|
||||
return required_close;
|
||||
}
|
||||
double TimeZones::ny_daily_close_protected(string symbol, int shift_days, bool print_data=false){
|
||||
|
||||
// Get the brokers times for when NY openend today and tomorrow:
|
||||
datetime time_5pm = iTime(symbol, PERIOD_D1 , 0) - (PeriodSeconds(PERIOD_H1) * 7);
|
||||
datetime ny_close_in_brokers_time = timezone_conversions("NY", time_5pm, "Broker");
|
||||
datetime ny_close_time = ny_close_in_brokers_time + PeriodSeconds(PERIOD_D1); // ny close tomorrow
|
||||
// ---------------------------------------------------------------------
|
||||
// Internal implementation to compute NY daily close price.
|
||||
//
|
||||
// Logic:
|
||||
// - Defines NY close as 5pm NY time = 00:00 broker + 17H back.
|
||||
// - Adjusts for day shifts if required.
|
||||
// - Returns the close price of the NY daily session.
|
||||
// ---------------------------------------------------------------------
|
||||
double TimeZones::ny_daily_close_protected(string symbol, int shift_days, bool print_data) {
|
||||
datetime time_5pm = iTime(symbol, PERIOD_D1, 0) - (PeriodSeconds(PERIOD_H1) * 7);
|
||||
datetime ny_close_in_brokers_time = timezone_conversions("NY", time_5pm, "Broker");
|
||||
datetime ny_close_time = ny_close_in_brokers_time + PeriodSeconds(PERIOD_D1);
|
||||
|
||||
if(TimeCurrent()<ny_close_time){
|
||||
ny_close_time = ny_close_time - PeriodSeconds(PERIOD_D1); // ny close today
|
||||
}
|
||||
if (TimeCurrent() < ny_close_time) ny_close_time -= PeriodSeconds(PERIOD_D1);
|
||||
|
||||
// Get the number of hours since NY closed:
|
||||
int shift = iBarShift(symbol, PERIOD_H1, ny_close_time, false) + 1;
|
||||
shift = shift + (24 * (shift_days - 1)); // shift days if required:
|
||||
int shift = iBarShift(symbol, PERIOD_H1, ny_close_time, false) + 1;
|
||||
shift += (24 * (shift_days - 1));
|
||||
|
||||
double ny_close = iClose(symbol,PERIOD_H1, shift);
|
||||
double br_close = iClose(symbol,PERIOD_H1, 1);
|
||||
double ny_close = iClose(symbol, PERIOD_H1, shift);
|
||||
double br_close = iClose(symbol, PERIOD_H1, 1);
|
||||
|
||||
if(print_data==true){
|
||||
Print("shift ",shift);
|
||||
Print("time_5pm ",time_5pm);
|
||||
Print("ny_close_in_brokers_time ",ny_close_in_brokers_time);
|
||||
Print("ny_close_time ",ny_close_time);
|
||||
Print("ny_close ", ny_close);
|
||||
Print("br_close ",br_close);
|
||||
}
|
||||
return ny_close;
|
||||
if (print_data) {
|
||||
Print("shift ", shift);
|
||||
Print("time_5pm ", time_5pm);
|
||||
Print("ny_close_in_brokers_time ", ny_close_in_brokers_time);
|
||||
Print("ny_close_time ", ny_close_time);
|
||||
Print("ny_close ", ny_close);
|
||||
Print("br_close ", br_close);
|
||||
}
|
||||
|
||||
return ny_close;
|
||||
}
|
||||
Reference in New Issue
Block a user