资源描述
MT4编程实例:在欧元图上显示英磅的RSI指标
(2008-07-05 21:38:36)
转载
下面这个图是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<limit; 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<limit; i++)
循环语句,括号中有三个语句:
第一句int i=0; 表示循环从i=0开始
第二句i<limit; 这是循环的条件,如果条件满足则执行大括号中的循环体,如果条件不满足,则中止循环,跳到大括号下面的语句执行
第三句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函数结束
}
展开阅读全文