mql4如何自定义画图

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

53

主题

8

回帖

836

积分

管理员

积分
836
来源: 2019-11-26 23:32:22 显示全部楼层 |阅读模式
mql4里很多自定义指标,将自定义指标拖入途中就能看到曲线图,俗话说,有图有真像,图是如何画的呢?我一直比较好奇,比如iMACD技术参数的图如下:
macdPic.png
这个图是如何画的呢?
问题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
  62. 定义了有2条线

  63. #property  indicator_color1  Silver
  64. #property  indicator_color2  Red



  65. 定义颜色 ,银色和红色


  66. #property  indicator_width1  2

  67. 定义线的宽度。



  68. extern int FastEMA=12;
  69. extern int SlowEMA=26;
  70. extern int SignalSMA=9;
  71. iMACD的参数,我也不太明白,但是这对理解图的画法不影响,先不去管它。



  72. //---- indicator buffers
  73. double     MacdBuffer[];
  74. double     SignalBuffer[];
  75. 这里是重点,存放数据的数组。





  76. int init()
  77.   {
  78.    //---- drawing settings
  79.    SetIndexStyle(0,DRAW_HISTOGRAM);//设置第一条线的显示方式,这里就是垂直
  80.    SetIndexStyle(1,DRAW_LINE);//水平线显示
  81.    SetIndexDrawBegin(1,SignalSMA); //设置开始画的位置

  82.    IndicatorDigits(Digits+1);
  83. //---- indicator buffers mapping
  84.    SetIndexBuffer(0,MacdBuffer);
  85.    SetIndexBuffer(1,SignalBuffer);
  86. //---- name for DataWindow and indicator subwindow label
  87.    IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
  88.    SetIndexLabel(0,"MACD");
  89.    SetIndexLabel(1,"Signal");
  90. //---- initialization done
  91.    return(0);
  92.   }


复制代码
具体我就不一一解释了,后面有英文的注释,应该比较容易明白 ,因为系统没有ima的指标图,通过修改这个代码,我们可以画出iMA的指标图
  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_color1  Red

  11. #property  indicator_width1  2

  12. extern int SlowEMA=26;
  13. extern int ma_shift=0;

  14. double     MacdBuffer[];
  15. double     SignalBuffer[];

  16. //+------------------------------------------------------------------+
  17. //| Custom indicator initialization function                         |
  18. //+------------------------------------------------------------------+
  19. int init()
  20.   {

  21.    SetIndexStyle(0,DRAW_HISTOGRAM);

  22.    IndicatorDigits(Digits+1);

  23.    SetIndexBuffer(0,MacdBuffer);

  24.    SetIndexLabel(0,"iMA");

  25.    return(0);
  26.   }
  27. //+------------------------------------------------------------------+
  28. //| Moving Averages Convergence/Divergence                           |
  29. //+------------------------------------------------------------------+
  30. int start()
  31.   {
  32.    int limit;
  33.    int counted_bars=IndicatorCounted();
  34. //---- last counted bar will be recounted
  35.    if(counted_bars>0) counted_bars--;
  36.    limit=Bars-counted_bars;
  37. //---- macd counted in the 1-st buffer
  38.    for(int i=0; i<limit; i++)
  39.       MacdBuffer[i]=iMA(NULL,0,SlowEMA,ma_shift,MODE_EMA,PRICE_CLOSE,i);
  40. //---- done
  41.    return(0);
  42.   }
  43. //+------------------------------------------------------------------+
复制代码

回复

使用道具 举报

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