📅 财经日历 📊 实时波动 📈 大盘云图 📶 行情走势 🆚 投机情绪 🚀 今日热点

    mql4如何自定义画图

    老周 LV16
    2016-10-31 · 4491 阅读
    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的指标图
    复制代码

    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. //+------------------------------------------------------------------+ 创建这个脚本的时候请选择Custom indicator 模板来创建,编译后,就可以运行这个脚本了。
    44. 希望这篇文章能够对学习mql的朋友有所帮助.
    复制代码


    ""
    还没有人打赏,支持一下
    回复

    举报

     

    回答|共 1 个

    kooii999 LV1

    发表于 2019-3-22 09:31:55 | 显示全部楼层

    币安交易所:http://www.lelev.com 世界领先的区块链资产交易平台
    您需要登录后才可以回帖 登录 | 注册

    提醒: 禁止引战、谩骂、灌水内容

    微信二维码

    有问题联系客服