市场解说员 发表于 2019-11-22 23:53:21

MQL4自编指标学习01

外建指标的数据缓冲外建指标的主要底层机制,是把指标数组的数据,送往终端窗口的缓冲,用于画出指标线。缓冲是内存区,保存着指标数据。
MQL4 规定,一个指标最多可画出8条指标线。一个指标数组,存储一条指标线的数据,与一个缓冲关联。8个缓冲的索引从0开始,最后一个为 7。图115表示外建指标数据,如何通过缓冲,传入主图画出指标线。//+------------------------------------------------------------------+
//|                                                      HighLow.mq4 |
//|                                                            hufan |
//|                                             https://www.geekquant.com |
//+------------------------------------------------------------------+
#property copyright "hufan"
#property link      "https://www.geekquant.com"
#property version   "1.00"
#property strict
#property indicator_chart_window// 在主图画出指标


#property indicator_buffers 2       // 缓冲的数目
#property indicator_color1 Blue   // 第1条指标线的颜色
#property indicator_color2 Red      // 第2条指标线的颜色

double Buf_0[],Buf_1[];             // 定义两个全局变量,以数组当作指标缓冲
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,Buf_0);         // 第一个缓冲赋值
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2); // 第一条指标线的特点
   SetIndexBuffer(1,Buf_1);         // 第二个缓冲赋值
   SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// 第二条指标线的特点
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
//---
    int i,                        // 柱子 Bar 的索引
    Counted_bars;                // 当前柱子的数目
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // 历史柱子的数目
   i=Bars-Counted_bars-1;         // 第一个历史柱的索引
   while(i>=0)                      // 历史柱的循环
   {
      Buf_0=High;             // 第一个缓冲第 i 个柱的数据值
      Buf_1=Low;            // 第二个缓冲第 i 个柱的数据值
      i--;                        // 计算下一个柱
      Print(i);
   }
//--------------------------------------------------------------------

//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+


页: [1]
查看完整版本: MQL4自编指标学习01