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指标
作者:秃鹫 来自:韬客外汇论坛http://www.talkforex.com
下面这个图是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
0) counted_bars--;
如果counted_bars值大于零,则counted_bars值减掉1。这是为了配合下一句,以避免limit相差1而出错
limit=Bars-counted_bars;
给limit赋值
Bars为图表中的柱数
counted_bars为已经赋值的柱数
这样limit的值就是未赋值的烛柱数
这样做的目的是避免重复运算,优化程序
for(int i=0; i0) counted_bars--;
limit=Bars-counted_bars-均线;
for(int i=0; itemp0 && Close[i+1]temp1) mk[i]=temp0;
}
return(0);
}
//源码到此已结束
=============================================
语句简要解释如下:
=============================================
#property indicator_chart_window
指标放在主图
#property indicator_buffers 1
设置指标线数为1条
#property indicator_color1 Yellow
设置第一条指标线颜色为黄色
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; itemp0 && Close[i+1]temp1) mk[i]=temp0;
与上一句相似
}
return(0);
start函数结束语句
把所有时段MACD一起显示
上一篇 / 下一篇 2008-04-12 14:14:46 / 个人分类:MT4
查看( 625 ) / 评论( 6 ) / 评分( 5 / 0 )
很久之前就有网友叫我写这个指标,并上传来能把所有时段KDJ一起显示的指标源码,说实在的,当时我第一次看到这个指标时相当吃惊,非常佩服源码作者能有这样的作品,也很希望自己能读懂这段源码增长自己见识,无奈不是专业出身,粗粗看了几遍没能理清这个程序编写思路,由于工作较忙的原因,这件事被搁在一旁。在年终结算工作基本完成的今天,我终于有时间静下心来认真阅读这段程序源码,边阅读边学习,在学习过程中也弥补了不少知识盲点。在弄明白了源码思路之后,感觉眼界大开,对程序构思惊叹不已。思路一通,问题就迎刃而解了。下面是参照All.Stochastic指标而编写的All.MACD指标截图和源码
HYPERLINK "http://www.talkforex.com/blog/batch.download.php?aid=138"
HYPERLINK "http://www.talkforex.com/blog/batch.download.php?aid=136"
//===============================================
//这是把所有时段MACD一起显示的指标源码
//由韬客www.talkforex.com秃鹫对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 Yellow
#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;i0; 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=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)
{
string s = str;
int lenght = StringLen(str) - 1;
int char;
while(lenght >= 0)
{
char = StringGetChar(s, lenght);
if((char > 96 && char < 123) || (char > 223 && char < 256)) s = StringSetChar(s, lenght, char - 32);
else if(char > -33 && char < 0) s = StringSetChar(s, lenght, char + 224);
lenght--;
}
return(s);
}
//--------------------------------------------------------------------源码到此已结束
原帖由masonjiao于2008-07-02 00:52:12发表
这份源代码我进行编译时出现如下错误提示:
'barsPerTimeFrame.' - variable not defined D:\Program ...
把2个货币的K线图重叠在一起
上一篇 / 下一篇 2008-06-28 10:36:11 / 个人分类:MT4
查看( 254 ) / 评论( 0 ) / 评分( 0 / 0 )
源码如下:
//---------------------------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#property indicator_color3 Aqua
#property indicator_color4 Yellow
extern string 添加商品="EURUSD";
extern int 对准均线=5;
double MyBuffer1[];
double MyBuffer2[];
double MyBuffer3[];
double MyBuffer4[];
double multp;
string my_symbol;
int init() {
SetIndexBuffer(0, MyBuffer1);
SetIndexBuffer(1, MyBuffer2);
SetIndexBuffer(2, MyBuffer3);
SetIndexBuffer(3, MyBuffer4);
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,0);
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,0);
my_symbol=添加商品;
int kk=PERIOD_D1/Period();
if(kk<1) kk=1;
multp=iMA(NULL,0,对准均线*kk,0,MODE_SMA,PRICE_CLOSE,0)/iMA(my_symbol,0,对准均线*kk,0,MODE_SMA,PRICE_CLOSE,0);
return(0);
}
int start() {
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int t=0; t=iClose(my_symbol,0,t)) {
MyBuffer3[t]=iHigh(my_symbol,0,t)*multp;
MyBuffer4[t]=iLow(my_symbol,0,t)*multp;
}
else if(iOpen(my_symbol,0,t)