MQL4自编指标学习01

0
回复
6030
查看
[复制链接]

360

主题

15

回帖

2471

积分

超级版主

积分
2471
来源: 2019-11-22 23:53:21 显示全部楼层 |阅读模式
外建指标的数据缓冲

外建指标的主要底层机制,是把指标数组的数据,送往终端窗口的缓冲,用于画出指标线。

缓冲是内存区,保存着指标数据。
MQL4 规定,一个指标最多可画出8条指标线。一个指标数组,存储一条指标线的数据,与一个缓冲关联。8个缓冲的索引从0开始,最后一个为 7。图115表示外建指标数据,如何通过缓冲,传入主图画出指标线。

22019112203.png

  1. //+------------------------------------------------------------------+
  2. //|                                                      HighLow.mq4 |
  3. //|                                                            hufan |
  4. //|                                             https://www.geekquant.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "hufan"
  7. #property link      "https://www.geekquant.com"
  8. #property version   "1.00"
  9. #property strict
  10. #property indicator_chart_window// 在主图画出指标


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

  14. double Buf_0[],Buf_1[];             // 定义两个全局变量,以数组当作指标缓冲
  15. //+------------------------------------------------------------------+
  16. //| Custom indicator initialization function                         |
  17. //+------------------------------------------------------------------+
  18. int OnInit()
  19.   {
  20. //--- indicator buffers mapping
  21.    SetIndexBuffer(0,Buf_0);         // 第一个缓冲赋值
  22.    SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2); // 第一条指标线的特点
  23.    SetIndexBuffer(1,Buf_1);         // 第二个缓冲赋值
  24.    SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// 第二条指标线的特点
  25. //---
  26.    return(INIT_SUCCEEDED);
  27.   }
  28. //+------------------------------------------------------------------+
  29. //| Custom indicator iteration function                              |
  30. //+------------------------------------------------------------------+
  31. int OnCalculate(const int rates_total,
  32.                 const int prev_calculated,
  33.                 const datetime &time[],
  34.                 const double &open[],
  35.                 const double &high[],
  36.                 const double &low[],
  37.                 const double &close[],
  38.                 const long &tick_volume[],
  39.                 const long &volume[],
  40.                 const int &spread[])
  41.   {
  42. //---
  43.     int i,                        // 柱子 Bar 的索引
  44.     Counted_bars;                // 当前柱子的数目
  45. //--------------------------------------------------------------------
  46.    Counted_bars=IndicatorCounted(); // 历史柱子的数目
  47.    i=Bars-Counted_bars-1;           // 第一个历史柱的索引
  48.    while(i>=0)                      // 历史柱的循环
  49.    {
  50.       Buf_0[i]=High[i];             // 第一个缓冲第 i 个柱的数据值
  51.       Buf_1[i]=Low[i];              // 第二个缓冲第 i 个柱的数据值
  52.       i--;                          // 计算下一个柱
  53.       Print(i);
  54.    }
  55. //--------------------------------------------------------------------

  56. //--- return value of prev_calculated for next call
  57.    return(rates_total);
  58.   }
  59. //+------------------------------------------------------------------+
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信