datetime new_time=TimeCurrent()/period_seconds*period_seconds; // 当前图表上的条形图打开时间
这里有个错误。是一周还是一个月?
如果周期是一周,那么一周的开始就是星期三,因为 1970.01.01 是星期四。
如果时间段是一个月,而当前月份没有 30 天,那么计算结果也是错误的,因为period_seconds 对应的是 30 天。
Maratori:
这里有个错误。如果时间是一周或一个月。
如果时间段是一周,那么一周的开始就是星期三,因为 1970.01.01 是星期四。
如果时间段是一个月,而当前月份没有 30 天,那么计算结果也是错误的,因为 period_seconds 对应的是 30 天。
同意。您可以在 OnNewBarCalculate.mqh 中使用此方法来计算这些周期:
//+------------------------------------------------------------------+ //| 自定义指标迭代函数 //+------------------------------------------------------------------+ 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[]) { //--- // 当出现新条形图时,运行 NewBar 事件处理程序 if(current_chart.isNewBar()>0) OnNewBarCalculate(rates_total,prev_calculated,time,open,high,low,close,tick_volume,volume,spread); return(rates_total); }
有点其他用途,但我现在用的是这样一个函数:
datetime getHTFTime(datetime time, ENUM_TIMEFRAMES highTimeFrame) { int periodSeconds = PeriodSeconds(highTimeFrame); if(periodSeconds <= 60*60*24) return datetime(time/periodSeconds*periodSeconds); MqlDateTime sTime; TimeToStruct(time, sTime); if(highTimeFrame == PERIOD_W1) { TimeToStruct(time - datetime(sTime.day_of_week * 24 * 60 * 60), sTime); sTime.hour = 0; sTime.min = 0; sTime.sec = 0; } else if(highTimeFrame == PERIOD_MN1) { sTime.day = 1; sTime.hour = 0; sTime.min = 0; sTime.sec = 0; } return StructToTime(sTime); }
输入一个日期/时间和一个时间框架。输出时,我们会得到该时间框架中包含该时间的条形图的开始时间。
如果您有任何优化代码 的建议,我很乐意倾听。
当然你也可以这样做:
datetime timeOut[1]; CopyTime(_Symbol, highTimeFrame, timeIn, 1, timeOut);
但在这种情况下,我遇到了同步问题。我必须等待历史记录加载。
指标的 "新柱线" 事件处理器:
作者: Konstantin Gruzdev