手机版
交易开户

MQL5变色线的画法(比MQL4更加简单)

经纪商平台
正规经纪商平台
阅读 : 5788 次
在正规平台开户,开始您的交易之旅
外_汇_邦 WaiHuiBang.com
MQL5里有一种特殊指标数组“颜色数组”,他是和画线的指标数组配合使用的。通过对他的简单赋值可以使画出的线变色。
首先要在指标头部定义里指定一条线对应的数组是要使用变色画线方式,指定方法是:
#property indicator_typeX DRAW_COLOR_LINE
这里X代表画线的数组序号
DRAW_COLOR_LINE代表画线,此外还可以有如下画线方式: 
复制代码
  1.  
  2. DRAW_COLOR_LINE
  3. Colorful Line彩色线
  4. DRAW_COLOR_SECTION
  5. Multicolored section彩色块
  6. DRAW_COLOR_HISTOGRAM
  7. Multicolored histogram from the zero line彩色柱状图
  8. DRAW_COLOR_HISTOGRAM2
  9. Multicolored histogram of the two indicator buffers彩色柱状图2
  10. DRAW_COLOR_ARROW
  11. Drawing colored arrows彩色箭头
  12. DRAW_COLOR_ZIGZAG
  13. Colorful ZigZag彩色ZigZag
  14. DRAW_COLOR_BARS
  15. Multi-colored bars彩色竹线图
  16. DRAW_COLOR_CANDLES
  17. Multi-colored candles彩色蜡烛图

然后紧跟一个颜色的定义语句:
#property indicator_colorX Red,Green
两个颜色之间用逗号分隔
============================================
针对上面程序头部的定义,之后要开始全局数组的定义。
这里要注意实现变色需要针对一条线使用两个数组,
例如:
double bMaBuffer[],bColorBuffer[];
然后进入OnInit事件进行两个数组的分别设定:
SetIndexBuffer(0,bMaBuffer,INDICATOR_DATA);//INDICATOR_DATA表示是用于画线的数组
SetIndexBuffer(1,bColorBuffer,INDICATOR_COLOR_INDEX);//INDICATOR_COLOR_INDEX表示是用于变色的颜色数组
注意:
如果这里要画多条彩色线,则画线数组和颜色数组的序号要紧邻。
============================================
下一步就是在OnCaculate事件里进行画线数组的计算,同时根据自定义的条件对颜色数组进行赋值。
赋值规则是:
当对应K线序号的颜色数组被赋值1.0时,对应画线数组的颜色为 第一个颜色
当对应K线序号的颜色数组被赋值0.0时,对应画线数组的颜色为 第二个颜色
完。
程序举例源码如下:【画出两个变色线】
复制代码
  1.  
  2. //+------------------------------------------------------------------+
  3. //| Test.mq5 |
  4. //| Copyright 2009, MetaQuotes Software Corp. |
  5. //| http://bbs.520fx.com |
  6. //+------------------------------------------------------------------+
  7. #property copyright "2009, 520FX"
  8. #property link "http://www.mql5.com"
  9. #property version "1.00"
  10. #property indicator_chart_window
  11. #property indicator_buffers 4
  12. #property indicator_plots 2
  13. #property indicator_color1 Red,Green
  14. #property indicator_type1 DRAW_COLOR_LINE
  15. #property indicator_style1 STYLE_SOLID
  16. #property indicator_width1 2
  17. #property indicator_color2 Yellow,Blue
  18. #property indicator_type2 DRAW_COLOR_LINE
  19. #property indicator_style2 STYLE_SOLID
  20. #property indicator_width2 2
  21. input int MaPeriod=13;
  22. double bMaBuffer[],bMaBuffer1[],bColorBuffer[],bColorBuffer1[];
  23. int iMaHandle,iMaHandle1;
  24. //+------------------------------------------------------------------+
  25. //| Custom indicator initialization function |
  26. //+------------------------------------------------------------------+
  27. int OnInit()
  28. {
  29. //--- indicator buffers mapping
  30. SetIndexBuffer(0,bMaBuffer,INDICATOR_DATA);
  31. SetIndexBuffer(1,bColorBuffer,INDICATOR_COLOR_INDEX);
  32. SetIndexBuffer(2,bMaBuffer1,INDICATOR_DATA);
  33. SetIndexBuffer(3,bColorBuffer1,INDICATOR_COLOR_INDEX);
  34. IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
  35. iMaHandle=iMA(NULL,0,MaPeriod,0,MODE_SMA,PRICE_CLOSE);
  36. iMaHandle1=iMA(NULL,0,MaPeriod+50,0,MODE_SMA,PRICE_CLOSE);
  37. //---
  38. return(0);
  39. }
  40. //+------------------------------------------------------------------+
  41. //| Custom indicator iteration function |
  42. //+------------------------------------------------------------------+
  43. int OnCalculate(const int rates_total,
  44. const int prev_calculated,
  45. const datetime& time[],
  46. const double& open[],
  47. const double& high[],
  48. const double& low[],
  49. const double& close[],
  50. const long& tick_volume[],
  51. const long& volume[],
  52. const int& spread[])
  53. {
  54. //--- return value of prev_calculated for next call
  55. //--- checking for bars count
  56. if(rates_total<MaPeriod)
  57. return(0);
  58. //--- detect start position
  59. int start;
  60. //if(prev_calculated>1) start=prev_calculated-1;
  61. //else start=1;
  62. if(prev_calculated<0)return(-1);else start=rates_total-prev_calculated+1;
  63. int to_copy;
  64. if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
  65. else
  66. {
  67. to_copy=rates_total-prev_calculated;
  68. if(prev_calculated>0) to_copy++;
  69. }
  70. if(CopyBuffer(iMaHandle,0,0,to_copy,bMaBuffer)<=0)
  71. {
  72. Print("Getting fast SMA is failed! Error",GetLastError());
  73. return(0);
  74. }
  75. if(CopyBuffer(iMaHandle1,0,0,to_copy,bMaBuffer1)<=0)
  76. {
  77. Print("Getting fast SMA1 is failed! Error",GetLastError());
  78. return(0);
  79. }
  80. //--- main cycle
  81. for(int i=start;i<rates_total;i++)
  82. {
  83. if(bMaBuffer[i]>close[i-1])
  84. bColorBuffer[i]=1.0;
  85. else bColorBuffer[i]=0.0;
  86. if(bMaBuffer1[i]>close[i-1])
  87. bColorBuffer1[i]=1.0;
  88. else bColorBuffer1[i]=0.0;
  89. }
  90. return(rates_total);
  91. }
  92. //+------------------------------------------------------------------+
外_汇_邦 WaiHuiBang.com
       稳定专业、接受多国监管的交易经纪商

本文标题:MQL5变色线的画法(比MQL4更加简单) - MT5平台MQL5编程学习
本文地址:https://www.waihuibang.com/fxschool/autotrading/mql5/113675.html

免责声明: 本文内容不代表外汇邦网站观点,内容仅供参考,不构成投资建议。投资者若据此操作,风险自担。

相关文章

点击咨询交易开户相关问题点击查看低点差、稳定快速的交易平台多重监管的在线交易平台

  • 外汇EA程序化交易系统设计心得体会

    投机就像山岳一般古老。毋庸费言,外汇零售市场的属性,必然会让国内的大多数外汇交易者,以中短期投机的方式居多,长期价值投资的偏少。而作为波动市场的王者,外汇市场给了手工交易和程序化交易最丰富的可能性,此种优势,其他市...

    在线交易开户 MQL5编程学习
  • 编写一个稳定盈利的外汇EA到底难不难?好的外汇EA长什么样?

    在一段时间,我们一直在研究赌博的概率与胜率,包括一些赌场高手的注码策略,对外汇的资金管理很有帮助,除了下单的胜率之外,您还要考虑平仓,您下单之后有浮赢了,能否变成真正的利润,还要取决于您的平仓策略,平仓策略不好,赚钱会变...

    交易开户知识 MQL5编程学习
  • 一文读懂量化投资、算法交易、程序化交易​​​​​​​、高频交易和统计套利
    一文读懂量化投资、算法交易、程序化交易​​​​​​​、高频交易和统计套利

    在央行发布的《中国金融稳定报告(2016)》中,对于高频交易的解释为程序化交易的频率超过一定程度,就成为高频交易。而对程序化交易的解释为程序化交易指依托计算机为技术工具,按照既定程序,高速、大规模自动执行的交易。...

    在线交易平台 MQL5编程学习
  • 外汇EA是什么?外汇智能交易软件能实现稳定盈利吗?

    经济全球化后,任何一个国家的经济出现波动都会影响到每一个投资者的利益,特别是做外汇、期货的投资者,应该深有体会。一部分投资者发现依靠自己有限的时间和精力应对24小时不断变化的外汇市场已经越来越力不从心了,于是他...

    低点差交易平台 MQL5编程学习
  • MQL5编程MT5程序代码讲解 之 旋转的时间

    //+-----------------------------------------------------+ ytg_Time_exp.mq5 | //Copyright Yuriy Tokman | //yuriytokman@gmail.com | //+-----------------------------------------------------+ #property co...

    交易开户咨询 MQL5编程学习
你可能感兴趣