指标: 指标的 "新柱线" 事件处理器

 

指标的 "新柱线" 事件处理器:

本指标将允许您在图表新柱线出现时执行重新计算指标的数据。

作者: Konstantin Gruzdev

 
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);

但在这种情况下,我遇到了同步问题。我必须等待历史记录加载。

 

感谢您的自定义指标,但是当我使用这个指标 时,只发出一次警报就停止调试了!请帮帮我!

如果我使用 OnTick 代替(并删除 OnInit 和 OnCaculate 函数),就像这篇文章 https://www.mql5.com/zh/articles/159 所写的那样,运行效果很好,但指标不会显示在导航窗口中。当我添加 OnInit 和 OnCaculate 函数时,它也不运行。如果我添加 OnInit 和 OnCaculate 函数,指标是否会显示在导航窗口中?

谢谢