您现在的位置:程序化交易>> 外汇现货>> MT5>> MT5知识>>正文内容

MQL5变色线画法(比MQL4更简单) [MT4]

  • MQL5里有一种特殊指标数组“颜色数组”,他是和画线的指标数组配合使用的。通过对他的简单赋值可以使画出的线变色。
    首先要在指标头部定义里指定一条线对应的数组是要使用变色画线方式,指定方法是:
    #property indicator_typeX DRAW_COLOR_LINE
    这里X代表画线的数组序号
    DRAW_COLOR_LINE代表画线,此外还可以有如下画线方式:
    复制代码
    1. DRAW_COLOR_LINE
    2. Colorful Line彩色线
    3. DRAW_COLOR_SECTION
    4. Multicolored section彩色块
    5. DRAW_COLOR_HISTOGRAM
    6. Multicolored histogram from the zero line彩色柱状图
    7. DRAW_COLOR_HISTOGRAM2
    8. Multicolored histogram of the two indicator buffers彩色柱状图2
    9. DRAW_COLOR_ARROW
    10. Drawing colored arrows彩色箭头
    11. DRAW_COLOR_ZIGZAG
    12. Colorful ZigZag彩色ZigZag
    13. DRAW_COLOR_BARS
    14. Multi-colored bars彩色竹线图
    15. DRAW_COLOR_CANDLES
    16. 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. //| Test.mq5 |
    3. //| Copyright 2009, MetaQuotes Software Corp. |
    4. //| http://bbs.520fx.com |
    5. //+------------------------------------------------------------------+
    6. #property copyright "2009, 520FX"
    7. #property link "http://www.mql5.com"
    8. #property version "1.00"
    9. #property indicator_chart_window
    10. #property indicator_buffers 4
    11. #property indicator_plots 2
    12. #property indicator_color1 Red,Green
    13. #property indicator_type1 DRAW_COLOR_LINE
    14. #property indicator_style1 STYLE_SOLID
    15. #property indicator_width1 2
    16. #property indicator_color2 Yellow,Blue
    17. #property indicator_type2 DRAW_COLOR_LINE
    18. #property indicator_style2 STYLE_SOLID
    19. #property indicator_width2 2
    20. input int MaPeriod=13;
    21. double bMaBuffer[],bMaBuffer1[],bColorBuffer[],bColorBuffer1[];
    22. int iMaHandle,iMaHandle1;
    23. //+------------------------------------------------------------------+
    24. //| Custom indicator initialization function |
    25. //+------------------------------------------------------------------+
    26. int OnInit()
    27. {
    28. //--- indicator buffers mapping
    29. SetIndexBuffer(0,bMaBuffer,INDICATOR_DATA);
    30. SetIndexBuffer(1,bColorBuffer,INDICATOR_COLOR_INDEX);
    31. SetIndexBuffer(2,bMaBuffer1,INDICATOR_DATA);
    32. SetIndexBuffer(3,bColorBuffer1,INDICATOR_COLOR_INDEX);
    33. IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
    34. iMaHandle=iMA(NULL,0,MaPeriod,0,MODE_SMA,PRICE_CLOSE);
    35. iMaHandle1=iMA(NULL,0,MaPeriod+50,0,MODE_SMA,PRICE_CLOSE);
    36. //---
    37. return(0);
    38. }
    39. //+------------------------------------------------------------------+
    40. //| Custom indicator iteration function |
    41. //+------------------------------------------------------------------+
    42. int OnCalculate(const int rates_total,
    43. const int prev_calculated,
    44. const datetime& time[],
    45. const double& open[],
    46. const double& high[],
    47. const double& low[],
    48. const double& close[],
    49. const long& tick_volume[],
    50. const long& volume[],
    51. const int& spread[])
    52. {
    53. //--- return value of prev_calculated for next call
    54. //--- checking for bars count
    55. if(rates_total<MaPeriod)
    56. return(0);
    57. //--- detect start position
    58. int start;
    59. //if(prev_calculated>1) start=prev_calculated-1;
    60. //else start=1;
    61. if(prev_calculated<0)return(-1);else start=rates_total-prev_calculated+1;
    62. int to_copy;
    63. if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
    64. else
    65. {
    66. to_copy=rates_total-prev_calculated;
    67. if(prev_calculated>0) to_copy++;
    68. }
    69. if(CopyBuffer(iMaHandle,0,0,to_copy,bMaBuffer)<=0)
    70. {
    71. Print("Getting fast SMA is failed! Error",GetLastError());
    72. return(0);
    73. }
    74. if(CopyBuffer(iMaHandle1,0,0,to_copy,bMaBuffer1)<=0)
    75. {
    76. Print("Getting fast SMA1 is failed! Error",GetLastError());
    77. return(0);
    78. }
    79. //--- main cycle
    80. for(int i=start;i<rates_total;i++)
    81. {
    82. if(bMaBuffer[i]>close[i-1])
    83. bColorBuffer[i]=1.0;
    84. else bColorBuffer[i]=0.0;
    85. if(bMaBuffer1[i]>close[i-1])
    86. bColorBuffer1[i]=1.0;
    87. else bColorBuffer1[i]=0.0;
    88. }
    89. return(rates_total);
    90. }
    91. //+------------------------------------------------------------------+

    文章栽自:www.520fx.com

【字体: 】【打印文章】【查看评论

相关文章

    没有相关内容