外汇百科
MQL5变色线的画法(比MQL4更加简单)
MQL5里有一种特殊指标数组“颜色数组”,他是和画线的指标数组配合使用的。通过对他的简单赋值可以使画出的线变色。
首先要在指标头部定义里指定一条线对应的数组是要使用变色画线方式,指定方法是:
#property indicator_typeX DRAW_COLOR_LINE
这里X代表画线的数组序号
DRAW_COLOR_LINE代表画线,此外还可以有如下画线方式:
复制代码 DRAW_COLOR_LINE Colorful Line彩色线 DRAW_COLOR_SECTION Multicolored section彩色块 DRAW_COLOR_HISTOGRAM Multicolored histogram from the zero line彩色柱状图 DRAW_COLOR_HISTOGRAM2 Multicolored histogram of the two indicator buffers彩色柱状图2 DRAW_COLOR_ARROW Drawing colored arrows彩色箭头 DRAW_COLOR_ZIGZAG Colorful ZigZag彩色ZigZag DRAW_COLOR_BARS Multi-colored bars彩色竹线图 DRAW_COLOR_CANDLES 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时,对应画线数组的颜色为 第二个颜色
完。
程序举例源码如下:【画出两个变色线】
复制代码 //+------------------------------------------------------------------+ // Test.mq5 // Copyright 2009, MetaQuotes Software Corp. // //+------------------------------------------------------------------+ #property copyright 2009, 520FX #property link #property version 1.00 #property indicator_chart_window #property indicator_buffers 4 #property indicator_plots 2 #property indicator_color1 Red,Green #property indicator_type1 DRAW_COLOR_LINE #property indicator_style1 STYLE_SOLID #property indicator_width1 2 #property indicator_color2 Yellow,Blue #property indicator_type2 DRAW_COLOR_LINE #property indicator_style2 STYLE_SOLID #property indicator_width2 2 input int MaPeriod=13; double bMaBuffer[],bMaBuffer1[],bColorBuffer[],bColorBuffer1[]; int iMaHandle,iMaHandle1; //+------------------------------------------------------------------+ // Custom indicator initialization function //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,bMaBuffer,INDICATOR_DATA); SetIndexBuffer(1,bColorBuffer,INDICATOR_COLOR_INDEX); SetIndexBuffer(2,bMaBuffer1,INDICATOR_DATA); SetIndexBuffer(3,bColorBuffer1,INDICATOR_COLOR_INDEX); IndicatorSetInteger(INDICATOR_DIGITS,_Digits); iMaHandle=iMA(NULL,0,MaPeriod,0,MODE_SMA,PRICE_CLOSE); iMaHandle1=iMA(NULL,0,MaPeriod+50,0,MODE_SMA,PRICE_CLOSE); //--- return(0); } //+------------------------------------------------------------------+ // Custom indicator iteration function //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime time[], const double open[], const double high[], const double low[], const double close[], const long tick_volume[], const long volume[], const int spread[]) { //--- return value of prev_calculated for next call //--- checking for bars count if(rates_total<MaPeriod) return(0); //--- detect start position int start; //if(prev_calculated>1) start=prev_calculated-1; //else start=1; if(prev_calculated<0)return(-1);else start=rates_total-prev_calculated+1; int to_copy; if(prev_calculated>rates_total prev_calculated<0) to_copy=rates_total; else { to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++; } if(CopyBuffer(iMaHandle,0,0,to_copy,bMaBuffer)<=0) { Print(Getting fast SMA is failed! Error,GetLastError()); return(0); } if(CopyBuffer(iMaHandle1,0,0,to_copy,bMaBuffer1)<=0) { Print(Getting fast SMA1 is failed! Error,GetLastError()); return(0); } //--- main cycle for(int i=start;i<rates_total;i++) { if(bMaBuffer[i]>close[i-1]) bColorBuffer[i]=1.0; else bColorBuffer[i]=0.0; if(bMaBuffer1[i]>close[i-1]) bColorBuffer1[i]=1.0; else bColorBuffer1[i]=0.0; } return(rates_total); } //+------------------------------------------------------------------+
上一篇:新手炒外汇爆仓七种不良心态
下一篇:黄金市场走势需关注的基本面问题
- FXTM爆仓率与利息:如何理解及管理风险03/04
- FXTM公司简介:全球领先的在线外汇交易平03/04
- 最新:Exness 开户流程03/04
- IC Market监管有哪些?IC Market监管查询03/04
- Exness监管查询-Exness外汇监管03/04
- fxpro监管-fxpro外汇监管查询03/04
- XM外汇监管03/04
- IC Market利息爆仓比例03/04
- IC Markets 点差对照表03/04
- IC Markets简介03/04
- 理论篇-外汇日内操作高手03/03
- 外汇开户流程一文详细介绍从零到一10/12
- 【外汇入门】外汇投资出现不良心态的原02/10
- 外汇:中国未来最大的投资理财市场02/10
- 外汇基础知识 外汇交易中常见的外汇专用02/03
- 【突破、回踩、确认】鉴别回撤和倒退02/01
- 外汇隔离账户与外汇分离账户的区别01/31
- 和讯期货分享 炒期货与玩牌局的本质区别01/24
- 搞定外汇市场的五大技巧04/27
- 外汇交易时如何保持正确的心态04/20
- 消息对炒外汇的重要性06/19
- 基本面交易策略:杠杆套息交易06/19
- 外汇EA编程教程――数据类型06/19
- 技术形态篇之跳空形态06/19
- 外汇之趋势线06/19
- 四种在外汇市场交易的方法06/19
- 货币主义汇率理论――汇率决定理论与经06/19
- 最受欢迎的5个技术分析指标06/19
- 交易的毒刺——对随机性报酬上瘾06/19
- 外汇交易基本面分析:什么是利率平价06/19