当前位置:文档之家› MT4平台指标编程

MT4平台指标编程

MT4平台指标编程
MT4平台指标编程

MT4编程实例:会变色的均线

这里实际上有两条均线,一条红线,一条绿线:

当价格在均线上方时,红线显示,绿线隐藏;

当价格在均线下方时,红线隐藏,绿线显示,

所以看起来就只是一条会变色的均线。

要隐藏一条指标线中的其中一段,也很简单,只要把这一段指标线的值赋为空值(EMPTY_VALUE)就行了。

说说容易,但实际操作的时候,我们又遇到了一个小问题,那就是红绿转换点的“断点”问题,红线和绿线不连续了。图:

这个问题着实困扰了我好一阵,后来终于想通了。

原来,画线的条件是:前后两个时点上都必须有值,才能画出线来。而上图中2和3之间应该画上红线,但在3点位上,红线的值为空,所以,红线画不出来。

要让红线、绿线连续,必须使3点位上,既有绿线值又有红线值,这样2和3之间才能出现红线,红线绿才会连续。

为了做到这一点,我们在给指标线循环赋值的时候:

1、在i 时点上,除了给i 时点赋值,同时还要给i+1时点赋同色值(以上图为例:我们在给3时点赋绿线值时,同时给4时点赋绿线值;在给2时点赋红线值时,同时再给3点赋红线值;这样3时点上就既有红线值,又有绿线值);

2、赋值的顺序为从左向右,即递减循环,以避免前一操作所赋的值被清空。

这样我们的目的就达到了。

下面这是经过测试的源代码

---------------------------------------------------------------------------------------------------

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Red

#property indicator_color2 Green

extern int 变色均线=18;

double duo[];

double kong[];

int init()

{

SetIndexBuffer(0,duo);

SetIndexBuffer(1,kong);

SetIndexStyle(0,DRAW_LINE);

SetIndexStyle(1,DRAW_LINE);

SetIndexDrawBegin(0,变色均线);

SetIndexDrawBegin(1,变色均线);

IndicatorDigits(Digits);

return(0);

}

int start()

{

double temp0,temp1;

int limit;

int counted_bars=IndicatorCounted();

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(int i=limit; i>=0; i--)

{

duo[i]=EMPTY_VALUE;

kong[i]=EMPTY_VALUE;

temp0=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i);

temp1=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i+1);

if(iClose(NULL,0,i)>=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i))

{duo[i]=temp0; duo[i+1]=temp1;}

else {kong[i]=temp0; kong[i+1]=temp1;}

}

return(0);

}

-----------------------------------------------------------------------------------------------------------

当然,上面这个是以价格在均线上下方为条件的,我们也可以以MACD、KDJ、RSI等指标作为均线变色的条件。我们还可以更进一步,把双色线改为三色线等等

===================================================

语句简要解释如下:

===================================================

#property indicator_chart_window

指标放在主图

#property indicator_buffers 2

设置指标线数组为2个

#property indicator_color1 Red

#property indicator_color2 Green

设置第一条指标线颜色值为Red,第二条颜色值为Green

extern int 变色均线=18;

设立一个自定义变量,允许外部值修改,整数形,变量名为"变色均线",默认值18

double duo[];

设立一个自定义数组,双精度型,名称为duo

该数组在后面用于存储红线数据

double kong[];

设立一个自定义数组,双精度型,名称为kong

该数组在后面用于存储绿线数据

int init()

设立初始化函数init。init为系统规定函数名,函数内容自定义。该函数在指标被加载时运行一次{

SetIndexBuffer(0,duo);

SetIndexBuffer(1,kong);

设置第一、二条指标线的数组为duo和kong

SetIndexStyle(0,DRAW_LINE);

SetIndexStyle(1,DRAW_LINE);

设置第一、二条指标线的样式,线型为连续曲线

SetIndexDrawBegin(0,变色均线);

SetIndexDrawBegin(1,变色均线);

设置第一、二条指标线的最左边的起始划线位置

IndicatorDigits(Digits);

设置指标精确到的小数位数

Digits是当前汇率小数位,日系Digits=2,其他币对Digits=4

return(0);

init函数结束,返回零值

}

int start()

设立触发函数start。start为系统规定函数名,函数内容自定义。当数据变动时,start函数被触发

{

double temp0,temp1;

设立双精度型自定义变量temp0、temp1

int limit;

设立自定义变量limit,整数型

int counted_bars=IndicatorCounted();

设立整数型自定义变量counted_bars,并将IndicatorCounted()的值赋给counted_bars

IndicatorCounted()为缓存数,即已经计算过值的烛柱数

if(counted_bars<0) return(-1);

如果counted_bars值小于零,start函数结束

if(counted_bars>0) counted_bars--;

如果counted_bars值大于零,则counted_bars值减掉1。这是为了配合下一句,以避免limit相差1而出错

limit=Bars-counted_bars;

给limit赋值

Bars为图表中的烛柱数

counted_bars为缓存数,即已经运算过的烛柱数

这样limit的值就是未经运算的烛柱数

这样做的目的是避免重复运算,优化程序

for(int i=limit; i>=0; i--)

循环语句,括号中有三个语句:

第一句int i=limit; 表示循环从i=limit开始

第二句i>=0; 这是循环的条件,如果条件满足则执行大括号中的循环体,如果条件不满足,则中止循环,跳到大括号下面的语句执行

第三句i--,这是循环步调控制语句,每循环一次后执行一次此语句。

i--相当于i=i-1,即i值在原有数值上减少1

{

duo[i]=EMPTY_VALUE;

kong[i]=EMPTY_VALUE;

给数组duo和kong在i位置上赋空值

EMPTY_VALUE:空值

temp0=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i);

temp1=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i+1);

把均线在i和i+1位置上均线值,分别赋给temp0和temp1

这是为了使后面的语句看起来简洁

if(iClose(NULL,0,i)>=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i))

判断条件语句:如果价格高于均线

{duo[i]=temp0; duo[i+1]=temp1;}

判断执行语句:给数组元素duo[i]、duo[i+1]分别赋值

else {kong[i]=temp0; kong[i+1]=temp1;}

如果判断条件不成立,即价格低于均线:则给数组元素kong[i]、kong[i+1]分别赋值}

return(0);

start函数结束,返回零值

}

MT4编程实例:在欧元图上显示英磅的RSI指标

作者:秃鹫来自:韬客外汇论坛https://www.doczj.com/doc/559854327.html,

下面这个图是AUD图上,叠加了英磅的RSI指标。

(当然也可以不叠加,分两个窗口)

从RSI指标图上我们看到,英磅强势,而澳元很弱

下面是指标源码

------------------------------------------------------------------------------------------------------- #property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 Aqua

#property indicator_level1 30

#property indicator_level2 70

extern int RSI=12;

extern string 商品="GBPUSD";

double ind_buffer[];

int init()

{

SetIndexBuffer(0,ind_buffer);

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);

IndicatorShortName("RSI("+商品+"," +RSI+")");

return(0);

}

int start()

{

int limit;

int counted_bars=IndicatorCounted();

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(int i=0; i

ind_buffer[i]=iRSI(商品,0,RSI,PRICE_CLOSE,i);

return(0);

}

-------------------------------------------------------------------------------------------------------

下面是指标叠加的操作方法:

下面是指标叠加的操作方法:

当然这里用的是RSI指标,别的指标如KDJ、威廉等指标也可以类似操作,只要把上面源码中的取值函数

和参数换一个行了。

=============================================

语句简要解释如下:

=============================================

#property indicator_separate_window

指标放在副图

#property indicator_buffers 1

设置指标线数组为1个

#property indicator_color1 Aqua

设置第一条指标线颜色值为Aqua,即介于蓝绿之间的一种颜色

#property indicator_level1 30

在副图中,30值位置上画一条水平直线

#property indicator_level2 70

在副图中,70值位置上画一条水平直线

extern int RSI=12;

设立一个自定义变量,允许外部值修改,整数型,变量名为"RSI",默认值12

extern string 商品="GBPUSD";

设立一个自定义变量,允许外部值修改,字符串型,变量名为"商品",默认值"GBPUSD"

double ind_buffer[];

设立一个自定义数组,双精度型

int init()

设立初始化函数init。init为系统规定函数名,函数内容自定义。该函数在指标被加载时运行一次{

SetIndexBuffer(0,ind_buffer);

设置第一条指标线的数组为ind_buffer

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);

设置第一条指标线的样式,DRAW_LINE表示连续曲线,STYLE_SOLID表示实心线,1号粗线IndicatorShortName("RSI("+商品+"," +RSI+")");

设置指标线的显示简称

return(0);

初始化函数结束

int start()

设立触发函数start。start为系统规定函数名,函数内容自定义。当数据变动时,start函数被触发

{

int limit;

设立自定义变量limit,整数型

int counted_bars=IndicatorCounted();

设立整数型自定义变量counted_bars,并将IndicatorCounted()的值赋给counted_bars

IndicatorCounted()为缓存数量,即已经计算过值的烛柱数

(注:可能这里解释得不是很准确,大致就这个意思)

if(counted_bars<0) return(-1);

如果counted_bars值小于零,start函数结束

if(counted_bars>0) counted_bars--;

如果counted_bars值大于零,则counted_bars值减掉1。这是为了配合下一句,以避免limit相差1而出错

limit=Bars-counted_bars;

给limit赋值

Bars为图表中的柱数

counted_bars为已经赋值的柱数

这样limit的值就是未赋值的烛柱数

这样做的目的是避免重复运算,优化程序

for(int i=0; i

循环语句,括号中有三个语句:

第一句int i=0; 表示循环从i=0开始

第二句i

第三句i++,这是循环步调控制语句,每循环一次后执行一次此语句。

i++相当于i=i+1,即i值在原有数值上增加1

ind_buffer[i]=iRSI(商品,0,RSI,PRICE_CLOSE,i);

此语句为循环体,由于只有一个语句,所以省略花括号

i为图表烛柱的序号,从0开始,右边第1柱序号为0,从右向左递增

iRSI为RSI指标的取值函数

return(0);

start函数结束

MT4编程实例:箭头符号(1)

上一篇/ 下一篇2008-03-16 21:49:48 / 个人分类:MT4

查看( 808 ) / 评论( 0 ) / 评分( 0 / 0 ) 作者:秃鹫来自:韬客外汇论坛https://www.doczj.com/doc/559854327.html,

符号是一种断断续续的指标线。在指标线有值区域,指标线显示;在指标线无值区域,指标线隐藏。例如下图:当价格上穿、下穿均线时,均线上出现一个标记符号。

原理也很简单:在上图中,存在二条指标线,一条是均线,另一条是笑脸符号线。

均线在任何位置都有值,所以均线是一条连续的曲线;

当价格上、下穿均线时,符号线被赋予了均线值;而在其他位置上,符号线被赋了空值。所以只是在价格

上、下穿均线的位置上,符号线才有值,才能出现笑脸符号。因为符号线只是个别位置有值,所以符号线仅在个别位置显现。

符号也正好落在均线上,因为符号线值=均线值。如果符号线被赋了别的值,例如(符号线=均线+10点),那么符号水平坐标不变,垂直坐标将是均线上方的10点位置。

源码如下:

//======================================================

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 Y ellow

extern int 均线=10;

double mk[];

double temp0,temp1;

int init()

{

IndicatorBuffers(1);

SetIndexBuffer(0,mk);

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0,204);

IndicatorDigits(Digits);

return(0);

}

int start()

{

int limit;

int counted_bars=IndicatorCounted();

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars-均线;

for(int i=0; i

{

temp0=iMA(NULL,0,均线,0,0,0,i);

temp1=iMA(NULL,0,均线,0,0,0,i+1);

mk[i]=EMPTY_VALUE;

if(Close[i]>temp0 && Close[i+1]

if(Close[i]temp1) mk[i]=temp0;

}

return(0);

}

//源码到此已结束

=============================================

语句简要解释如下:

=============================================

#property indicator_chart_window

指标放在主图

#property indicator_buffers 1

设置指标线数为1条

#property indicator_color1 Y ellow

设置第一条指标线颜色为黄色

extern int 均线=10;

设立一个自定义变量,允许外部值修改,整数形,变量名为"均线",默认值10

double mk[];

设立一个自定义数组,双精度型

double temp0,temp1;

设立二个自定义变量,双精度型,变量名为temp0、temp1

int init()

设立初始化函数init。init为系统规定函数名,函数内容自定义。该函数在指标被加载时仅运行一次

{

IndicatorBuffers(1);

指标线数量为1条

SetIndexBuffer(0,mk);

第一条指标线的数组为mk

SetIndexStyle(0,DRAW_ARROW);

第一条指标线的线型为箭头符号

SetIndexArrow(0,204);

第一条指标线的箭头符号为第204种符号,如果换一个编号,那出现的就是另一种符号。箭头符号的编码详见《MT4编程参考》

IndicatorDigits(Digits);

设置指标线的小数位数

Digits=当前汇率的小数位数,如日元叉盘Digits=2,其他币对Digits==4

return(0);

函数结束,返回零值

}

int start()

设立触发函数start。start为系统规定函数名,函数内容自定义。当数据变动时,start函数被触发

{

int limit;

设立整数型自定义变量limit

int counted_bars=IndicatorCounted();

设立整数型自定义变量counted_bars,并将IndicatorCounted()的值赋给counted_bars

IndicatorCounted()为缓存数量,即已经计算过值的烛柱数

if(counted_bars>0) counted_bars--;

如果counted_bars大于零,则将counted_bars的值减掉1

这是为了配合下一句,以避免limit相差1而发生出错

limit=Bars-counted_bars-均线;

这是给limit赋值

Bars为图表中的柱数

counted_bars为已经赋值的柱数

这样limit的结果就是未赋值的烛柱数

再减去“均线”是因为图表最右边段均线无意义

for(int i=0; i

循环语句,括号中有三个语句:

第一句int i=0; 表示循环从i=0开始

第二句i

第三句i++,这是循环步调控制语句,每循环一次后执行一次此语句。

i++相当于i=i+1,即i值在原有数值上增加1

{

temp0=iMA(NULL,0,均线,0,0,0,i);

把均线在i位置上的值赋给temp0

iMA:均线取值函数

temp1=iMA(NULL,0,均线,0,0,0,i+1);

把均线在(i+1)位置上的值赋给temp1

mk[i]=EMPTY_VALUE;

给数组mk在i位置上赋空值

EMPTY_VALUE就是空值

if(Close[i]>temp0 && Close[i+1]

条件判断语句。如果在i位置上价格大于均线,并且在(i+1)位置上价格小于均线(即价格上穿均线),则给数组mk在i位置上赋该位置的均线值

Close[i]:在i位置上收盘价。

&&:逻辑运算符“并且”

if(Close[i]temp1) mk[i]=temp0;

与上一句相似

}

return(0);

start函数结束语句

把所有时段MACD一起显示

上一篇/ 下一篇2008-04-12 14:14:46 / 个人分类:MT4

查看( 625 ) / 评论( 6 ) / 评分( 5 / 0 )

很久之前就有网友叫我写这个指标,并上传来能把所有时段KDJ一起显示的指标源码,说实在的,当时我第一次看到这个指标时相当吃惊,非常佩服源码作者能有这样的作品,也很希望自己能读懂这段源码增长自己见识,无奈不是专业出身,粗粗看了几遍没能理清这个程序编写思路,由于工作较忙的原因,这件事被搁在一旁。在年终结算工作基本完成的今天,我终于有时间静下心来认真阅读这段程序源码,边阅读边学习,在学习过程中也弥补了不少知识盲点。在弄明白了源码思路之后,感觉眼界大开,对程序构思惊叹不已。思路一通,问题就迎刃而解了。下面是参照All.Stochastic指标而编写的All.MACD指标截图和源码

//=============================================== //这是把所有时段MACD一起显示的指标源码

//由韬客https://www.doczj.com/doc/559854327.html,秃鹫对All.Stochastic指标修改而来//=============================================== #define indicatorName "All MACD"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_color1 DarkGray

#property indicator_color2 Red

#property indicator_color3 Green

#property indicator_color4 Y ellow

#property indicator_level1 0

#property indicator_levelcolor DimGray

extern int FastEMA=12;

extern int SlowEMA=26;

extern int SignalSMA=9;

extern string timeFrames = "M1;M5;M15;M30;H1;H4;D1;W1;MN"; extern int barsPerTimeFrame = 50;

extern bool shiftRight = False;

extern bool currentFirst = False;

extern color txtColor = Silver;

extern color separatorColor = Red;

int multiple[9]={128,128,64,32,16,8,4,2,1};

double MacdBuffer0[];

double MacdBuffer1[];

double MacdBuffer2[];

double SignalBuffer[];

string shortName;

string labels[];

int periods[];

int Shift;

int init()

{

if (shiftRight) Shift = 1; else Shift = 0;

barsPerTimeFrame= MathMax(barsPerTimeFrame,15);

shortName = "MACD2线3色("+FastEMA+","+SlowEMA+","+SignalSMA+")";

IndicatorShortName(shortName);

SetIndexBuffer(0,MacdBuffer0);

SetIndexBuffer(1,MacdBuffer1);

SetIndexBuffer(2,MacdBuffer2);

SetIndexBuffer(3,SignalBuffer);

SetIndexStyle(0,DRAW_LINE);

SetIndexStyle(1,DRAW_HISTOGRAM);

SetIndexStyle(2,DRAW_HISTOGRAM);

SetIndexStyle(3,DRAW_LINE);

SetIndexDrawBegin(3,SignalSMA);

IndicatorDigits(Digits+1);

SetIndexShift(0,Shift*(barsPerTimeFrame+1));

SetIndexShift(1,Shift*(barsPerTimeFrame+1));

SetIndexShift(2,Shift*(barsPerTimeFrame+1));

SetIndexShift(3,Shift*(barsPerTimeFrame+1));

SetIndexLabel(0,"MACD");

SetIndexLabel(1,"MACD");

SetIndexLabel(2,"MACD");

SetIndexLabel(3,"Signal");

timeFrames = StringUpperCase(StringTrimLeft(StringTrimRight(timeFrames)));

if (StringSubstr(timeFrames,StringLen(timeFrames),1) != ";") timeFrames = StringConcatenate(timeFrames,";");

int s = 0;

int i = StringFind(timeFrames,";",s);

int time;

string current;

while (i > 0)

{

current = StringSubstr(timeFrames,s,i-s);

time = stringToTimeFrame(current);

if (time > 0) {

ArrayResize(labels ,ArraySize(labels)+1);

ArrayResize(periods,ArraySize(periods)+1);

labels[ArraySize(labels)-1] = current;

periods[ArraySize(periods)-1] = time; }

s = i + 1;

i = StringFind(timeFrames,";",s);

}

if(currentFirst)

for (i=1;i

if (Period()==periods[i])

{

string tmpLbl = labels[i];

int tmpPer = periods[i];

for (int k=i ;k>0; k--)

{

labels[k] = labels[k-1];

periods[k] = periods[k-1];

}

labels[0] = tmpLbl;

periods[0] = tmpPer;

}

return(0);

}

int deinit()

{

for(int l=0;l

ObjectDelete(indicatorName+l);

ObjectDelete(indicatorName+l+"label");

}

return(0);

}

int start()

{

string separator;

int window=WindowFind(shortName);

int k=0;

for(int p=0; p

{

for(int i=0; i

{

MacdBuffer0[k] =

iMACD(NULL,periods[p],FastEMA,SlowEMA,SignalSMA,0,0,i)*multiple[p];

SignalBuffer[k]=

iMACD(NULL,periods[p],FastEMA,SlowEMA,SignalSMA,0,1,i)*multiple[p];

double temp=(MacdBuffer0[k]-SignalBuffer[k])*1.3;

if(temp>=0) {MacdBuffer1[k]=temp;MacdBuffer2[k]=EMPTY_VALUE;} else {MacdBuffer2[k]=temp;MacdBuffer1[k]=EMPTY_VALUE;}

}

MacdBuffer0[k] = EMPTY_VALUE;

SignalBuffer[k] = EMPTY_VALUE;

MacdBuffer1[k] = EMPTY_VALUE;

MacdBuffer2[k] = EMPTY_VALUE;

k += 1;

separator = indicatorName+p;

if(ObjectFind(separator)==-1) ObjectCreate(separator,OBJ_TREND,window,0,0);

ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-1));

ObjectSet(separator,OBJPROP_TIME2,barTime(k-Shift*(barsPerTimeFrame+1)-1));

ObjectSet(separator,OBJPROP_PRICE1,-0.1);

ObjectSet(separator,OBJPROP_PRICE2,0.1);

ObjectSet(separator,OBJPROP_COLOR ,separatorColor);

ObjectSet(separator,OBJPROP_WIDTH ,2);

separator = indicatorName+p+"label";

if(ObjectFind(separator)==-1) ObjectCreate(separator,OBJ_TEXT,window,0,0);

ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-5));

ObjectSet(separator,OBJPROP_PRICE1,0);

ObjectSetText(separator,labels[p],9,"Arial",txtColor);

}

SetIndexDrawBegin(0,Bars-k);

SetIndexDrawBegin(1,Bars-k);

return(0);

}

int barTime(int a)

{

if(a<0) return(Time[0]+Period()*60*MathAbs(a));

else return(Time[a]);

}

int stringToTimeFrame(string TimeFrame)

{

int TimeFrameInt=0;

if (TimeFrame=="M1") TimeFrameInt=PERIOD_M1;

if (TimeFrame=="M5") TimeFrameInt=PERIOD_M5;

if (TimeFrame=="M15") TimeFrameInt=PERIOD_M15;

if (TimeFrame=="M30") TimeFrameInt=PERIOD_M30;

if (TimeFrame=="H1") TimeFrameInt=PERIOD_H1;

if (TimeFrame=="H4") TimeFrameInt=PERIOD_H4;

if (TimeFrame=="D1") TimeFrameInt=PERIOD_D1;

if (TimeFrame=="W1") TimeFrameInt=PERIOD_W1;

if (TimeFrame=="MN") TimeFrameInt=PERIOD_MN1;

return(TimeFrameInt);

}

string StringUpperCase(string str)

{

相关主题
文本预览
相关文档 最新文档