外汇EA
mql4如何自定义画图
回复:0  浏览:102
  • 楼主admin 圈主
  • 2019-05-05 07:49
mql4里很多自定义指标,将自定义指标拖入途中就能看到曲线图,俗话说,有图有真像,图是如何画的呢?我一直比较好奇,比如iMACD技术参数的图如下:

这个图是如何画的呢?

问题1,银色的线垂直的线如何画,红色的曲线如何画

问题2, 两条线的数据是如何获取的 ?

带着这2个问题,我们看下这个源码

还好Mql开放了这个源代码,我们可以研究下, 源码如下


  1. //+------------------------------------------------------------------+
  2. //|                                                  Custom MACD.mq4 |
  3. //|                      Copyright ?2004, metaQuotes Software Corp. |
  4. //|                                       http://www.metaquotes.net/ |
  5. //+------------------------------------------------------------------+
  6. #property  copyright "Copyright ?2004, metaQuotes Software Corp."
  7. #property  link      "http://www.metaquotes.net/"
  8. //---- indicator settings
  9. #property  indicator_separate_window
  10. #property  indicator_buffers 2
  11. #property  indicator_color1  Silver
  12. #property  indicator_color2  Red
  13. #property  indicator_width1  2
  14. //---- indicator parameters
  15. extern int FastEMA=12;
  16. extern int SlowEMA=26;
  17. extern int SignalSMA=9;
  18. //---- indicator buffers
  19. double     MacdBuffer[];
  20. double     SignalBuffer[];

  21. //+------------------------------------------------------------------+
  22. //| Custom indicator initialization function                         |
  23. //+------------------------------------------------------------------+
  24. int init()
  25.   {
  26. //---- drawing settings
  27.    SetIndexStyle(0,DRAW_HISTOGRAM);
  28.    SetIndexStyle(1,DRAW_LINE);
  29.    SetIndexDrawBegin(1,SignalSMA);
  30.    IndicatorDigits(Digits+1);
  31. //---- indicator buffers mapping
  32.    SetIndexBuffer(0,MacdBuffer);
  33.    SetIndexBuffer(1,SignalBuffer);
  34. //---- name for DataWindow and indicator subwindow label
  35.    IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
  36.    SetIndexLabel(0,"MACD");
  37.    SetIndexLabel(1,"Signal");
  38. //---- initialization done
  39.    return(0);
  40.   }
  41. //+------------------------------------------------------------------+
  42. //| Moving Averages Convergence/Divergence                           |
  43. //+------------------------------------------------------------------+
  44. int start()
  45.   {
  46.    int limit;
  47.    int counted_bars=IndicatorCounted();
  48. //---- last counted bar will be recounted
  49.    if(counted_bars>0) counted_bars--;
  50.    limit=Bars-counted_bars;
  51. //---- macd counted in the 1-st buffer
  52.    for(int i=0; i<limit; i++)
  53.       MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
  54. //---- signal line counted in the 2-nd buffer
  55.    for(i=0; i<limit; i++)
  56.       SignalBuffer[i]=iMAonArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
  57. //---- done
  58.    return(0);
  59.   }
  60. //+------------------------------------------------------------------+
  61. #property  indicator_buffers 2定义了有2条线
  62. #property  indicator_color1  Silver#property  indicator_color2  Red
  63. 定义颜色 ,银色和红色 #property  indicator_width1  2定义线的宽度。 extern int FastEMA=12;
  64. extern int SlowEMA=26;
  65. extern int SignalSMA=9;iMACD的参数,我也不太明白,但是这对理解图的画法不影响,先不去管它。
  66. //---- indicator buffers
  67. double     MacdBuffer[];
  68. double     SignalBuffer[];这里是重点,存放数据的数组。
  69.   int init()
  70.   {
  71.    //---- drawing settings   SetIndexStyle(0,DRAW_HISTOGRAM);//设置第一条线的显示方式,这里就是垂直
  72.    SetIndexStyle(1,DRAW_LINE);//水平线显示
  73.    SetIndexDrawBegin(1,SignalSMA); //设置开始画的位置
  74.    IndicatorDigits(Digits+1);
  75. //---- indicator buffers mapping
  76.    SetIndexBuffer(0,MacdBuffer);
  77.    SetIndexBuffer(1,SignalBuffer);
  78. //---- name for DataWindow and indicator subwindow label
  79.    IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
  80.    SetIndexLabel(0,"MACD");
  81.    SetIndexLabel(1,"Signal");
  82. //---- initialization done
  83.    return(0);
  84.   } 具体我就不一一解释了,后面有英文的注释,应该比较容易明白 ,因为系统没有ima的指标图,通过修改这个代码,我们可以画出iMA的指标图