#define SYMBOL_NAME Symbol()
#define SESSION_INDEX 0
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 记录带有交易品种和SESSION_INDEX的标题
//--- 从周一到周五循环,在日志中记录交易时段的开始和结束时间
PrintFormat("Symbol %s, Trade session %d:", SYMBOL_NAME, SESSION_INDEX);
for(int i=MONDAY; i<SATURDAY; i++)
SymbolInfoSessionTradePrint(SYMBOL_NAME, (ENUM_DAY_OF_WEEK)i, SESSION_INDEX);
/*
result:
Symbol GBPUSD, Trade session 0:
- Monday 00:15 - 23:55
- Tuesday 00:15 - 23:55
- Wednesday 00:15 - 23:55
- Thursday 00:15 - 23:55
- Friday 00:15 - 23:55
*/
}
//+------------------------------------------------------------------+
//| 发送指定交易时段的开始和结束时间 |
//| 到日志(指定交易品种,工作日) |
//+------------------------------------------------------------------+
void SymbolInfoSessionTradePrint(const string symbol, const ENUM_DAY_OF_WEEK day_of_week, const uint session_index)
{
//--- 声明变量来记录交易时段的开始和结束
datetime date_from; // 时段开始时间
datetime date_to; // 时段结束时间
//--- 从交易时段按交易品种和星期几获取数据
if(!SymbolInfoSessionTrade(symbol, day_of_week, session_index, date_from, date_to))
{
Print("SymbolInfoSessionTrade() failed. Error ", GetLastError());
return;
}
//--- 从枚举常量创建星期名称
string week_day=EnumToString(day_of_week);
if(week_day.Lower())
week_day.SetChar(0, ushort(week_day.GetChar(0)-32));
//--- 将指定交易时段的数据发送到日志
PrintFormat("- %-10s %s - %s", week_day, TimeToString(date_from, TIME_MINUTES), TimeToString(date_to, TIME_MINUTES));
}
|