找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 13|回复: 0

MQL4自编指标学习1

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-21 21:58:00 | 显示全部楼层 |阅读模式
外建指标的数据缓冲外建指标的主要底层机制,是把指标数组的数据,送往终端窗口的缓冲,用于画出指标线。缓冲是内存区,保存着指标数据。

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



//+------------------------------------------------------------------+//|                                                      HighLow.mq4 |//|                                                            hufan |//|                                             https://www.mql5.com |//+------------------------------------------------------------------+#property copyright "hufan"#property link      "https://www.mql5.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);  }//+------------------------------------------------------------------+
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-11 14:06 , Processed in 0.102430 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表