设为首页 收藏本站 切换语言

如何利用DRAW_HISTOGRAM开发双均线彩带指标,替代DRAW_FILLING

| 发表于 2024-2-26 20:03:23 | 显示全部楼层 |复制链接
我们在开发自定义指标的时候,有时候需要开发彩带指标,如下图所示。彩带可能需要显示在附图,也可能需要显示的主图。

image.png


这样的需求,在MT5中是非常容易实现的,只要设置指标类型为DRAW_FILLING 即可。
  1. #property indicator_type1   DRAW_FILLING
复制代码

上图代码如下
  1. #property copyright "EACODE"
  2. #property link      "http://www.eacode.cn"
  3.   
  4. #property description "An indicator to demonstrate DRAW_FILLING"
  5. #property description "It draws a channel between two MAs in a separate window"
  6. #property description "The fill color is changed randomly"
  7. #property description "after every N ticks"
  8.   
  9. #property indicator_separate_window
  10. #property indicator_buffers 2
  11. #property indicator_plots   1
  12. //--- 标图交集
  13. #property indicator_label1  "Intersection"
  14. #property indicator_type1   DRAW_FILLING
  15. #property indicator_color1  clrRed,clrBlue
  16. #property indicator_width1  1
  17. //--- 输入参数
  18. input int      Fast=13;          // 快速MA的周期
  19. input int      Slow=21;          // 慢速MA的周期
  20. input int      shift=1;          // 向未来的MAs转移(正值)
  21. input int      N=5;              // 改变订单号数量  
  22. //--- 指标缓冲区
  23. double         IntersectionBuffer1[];
  24. double         IntersectionBuffer2[];
  25. int fast_handle;
  26. int slow_handle;
  27. //--- 存储颜色的数组0到5的
  28. color colors[]={clrRed,clrBlue,clrGreen,clrAquamarine,clrBlanchedAlmond,clrBrown,clrCoral,clrDarkSlateGray};
  29. //+------------------------------------------------------------------+
  30. //| 自定义指标初始化函数                                                |
  31. //+------------------------------------------------------------------+
  32. int OnInit()
  33.   {
  34. //--- 指标缓冲区映射
  35.    SetIndexBuffer(0,IntersectionBuffer1,INDICATOR_DATA);
  36.    SetIndexBuffer(1,IntersectionBuffer2,INDICATOR_DATA);
  37. //---
  38.    //PlotIndexSetInteger(0,PLOT_SHIFT,shift);
  39. //---
  40.    fast_handle=iMA(_Symbol,_Period,Fast,0,MODE_SMA,PRICE_CLOSE);
  41.    slow_handle=iMA(_Symbol,_Period,Slow,0,MODE_SMA,PRICE_CLOSE);
  42. //---
  43.    return(INIT_SUCCEEDED);
  44.   }
  45. //+------------------------------------------------------------------+
  46. //| 自定义指标迭代函数                                                  |
  47. //+------------------------------------------------------------------+
  48. int OnCalculate(const int rates_total,
  49.                 const int prev_calculated,
  50.                 const datetime &time[],
  51.                 const double &open[],
  52.                 const double &high[],
  53.                 const double &low[],
  54.                 const double &close[],
  55.                 const long &tick_volume[],
  56.                 const long &volume[],
  57.                 const int &spread[])
  58.   {
  59.    static int ticks=0;
  60. //--- 计算订单号改变样式,颜色和线型宽度
  61.    ticks++;
  62. //--- 如果足够数量的订单号被积累
  63.    if(ticks>=N)
  64.      {
  65.       //--- 改变线型属性
  66.       ChangeLineAppearance();
  67.       //--- 重置0计数器
  68.       ticks=0;
  69.      }
  70.   
  71. //--- 指标的第一次计算,或数据变化,需要一次完全重新计算
  72.    if(prev_calculated==0)
  73.      {
  74.       //--- 复制所有指标值至相应的缓冲区
  75.       int copied1=CopyBuffer(fast_handle,0,0,rates_total,IntersectionBuffer1);
  76.       int copied2=CopyBuffer(slow_handle,0,0,rates_total,IntersectionBuffer2);
  77.      }
  78.    else // 仅填充那些更新的数据
  79.      {
  80.       //--- 获得OnCalculate()当前和之前启动之间的柱形不同
  81.       int to_copy=rates_total-prev_calculated;
  82.       //--- 如果没有不同,我们仍然会复制一个值 - 在零柱
  83.       if(to_copy==0) to_copy=1;
  84.       //--- 复制_copy 值到指标缓冲区的最末端
  85.       int copied1=CopyBuffer(fast_handle,0,0,to_copy,IntersectionBuffer1);
  86.       int copied2=CopyBuffer(slow_handle,0,0,to_copy,IntersectionBuffer2);
  87.      }
  88. //--- 返回 prev_calculated值以便下次调用函数
  89.    return(rates_total);
  90.   }
  91. //+------------------------------------------------------------------+
  92. //| 改变通道填充颜色                                                   |
  93. //+------------------------------------------------------------------+
  94. void ChangeLineAppearance()
  95.   {
  96. //--- 形成线型属性信息的字符串
  97.    string comm="";
  98. //--- 改变线型颜色的模块
  99.    int number=MathRand(); // 获得随机数
  100. //--- 除数等于colors[]数组的大小
  101.    int size=ArraySize(colors);
  102.   
  103. //--- 获得选择新颜色作为整数除法余数的标引
  104.    int color_index1=number%size;
  105. //--- 设置第一个颜色为PLOT_LINE_COLOR 属性
  106.    PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,colors[color_index1]);
  107. //--- 写下第一个颜色
  108.    comm=comm+"\r\nColor1 "+(string)colors[color_index1];
  109.   
  110. //--- 获得选择新颜色作为整数除法余数的标引
  111.    number=MathRand(); // 获得随机数
  112.    int color_index2=number%size;
  113. //--- 设置第二个颜色为PLOT_LINE_COLOR 属性
  114.    PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,colors[color_index2]);
  115. //---写下第二个颜色
  116.    comm=comm+"\r\nColor2 "+(string)colors[color_index2];
  117. //--- 使用注释在图表上显示信息
  118.    Comment(comm);
  119.   }
复制代码


在MT4中由于不支持DRAW_FILLING指标类型,我们可以通过DRAW_HISTOGRAM来实现。

只需要在主图,定义两个DRAW_HISTOGRAM类型的Buffer,就可以实现该功能。
  1. #property indicator_chart_window
  2. #property indicator_buffers 2
  3. #property indicator_plots   2
  4. //--- 标图交集
  5. #property indicator_label1  "均线带"
  6. #property indicator_type1   DRAW_HISTOGRAM
  7. #property indicator_color1  clrRed
  8. #property indicator_width1  3
  9. #property indicator_label2  "均线带"
  10. #property indicator_type2   DRAW_HISTOGRAM
  11. #property indicator_color2  clrLime
  12. #property indicator_width2  3
复制代码

最终效果如下。
image.png

完整代码如下:
购买 已有0人购买, 当前内容需向作者支付 3 H币 才能浏览
如果有帮助,就支持一下我呗
举报

评论 使用道具

精彩评论2

xinhua123
DDD
| 发表于 2024-2-27 04:24:43 | 显示全部楼层
双移动平均带
举报

点赞 评论 使用道具

lzq2022
DD
| 发表于 2024-2-27 10:33:35 | 显示全部楼层
感谢分享 8888888 有点小贵啊
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

天眼云VPS
简体中文
繁體中文
English(英语)
日本語(日语)
Deutsch(德语)
Русский язык(俄语)
بالعربية(阿拉伯语)
Türkçe(土耳其语)
Português(葡萄牙语)
ภาษาไทย(泰国语)
한어(朝鲜语/韩语)
Français(法语)