外汇通|外汇论坛,外汇保证金,交易商,交易平台,网友讨论
MetaTrader编程入门教程 Word版下载
[upload=doc]viewFile.asp?ID=1640[/upload]
MetaTrader编程入门教程(1)
指标文件构成
[此贴子已经被作者于2007-9-6 11:23:29编辑过]
例2
//+--------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 Red
#property indicator_color2 Yellow
#property indicator_color3 Blue
#property indicator_color4 Green
#property indicator_color5 Gray
#property indicator_color6 SkyBlue
#property indicator_color7 Tan
extern int MA_Period=13;
extern int MA_Shift=0;
extern int MA_Method=2;
extern int MA_Price = 6;
double Buffer0[];
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];
//----
int init()
{
//-设置编号为0的线的线形等参数, 0~6,对应indicator_color1~7
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
SetIndexStyle(4,DRAW_LINE);
SetIndexStyle(5,DRAW_LINE);
SetIndexStyle(6,DRAW_LINE);
//---- 设置编号为0的线 与数组的对应关系, 0~6
SetIndexBuffer(0,Buffer0);
SetIndexBuffer(1,Buffer1);
SetIndexBuffer(2,Buffer2);
SetIndexBuffer(3,Buffer3);
SetIndexBuffer(4,Buffer4);
SetIndexBuffer(5,Buffer5);
SetIndexBuffer(6,Buffer6);
return(0);
}
//
int start() //指标计算主函数,每次计算调用
{
ma();
ma1();
return(0);
}
void ma()
{
int pos=Bars;
while(pos>=0)
{
Buffer0[pos]=iMA(NULL,0,MA_Period,MA_Shift,MA_Method,MA_Price,pos);
Buffer1[pos]=iMA(NULL,0,MA_Period*2,MA_Shift,MA_Method,MA_Price,pos);
Buffer2[pos]=iMA(NULL,0,MA_Period*3,MA_Shift,MA_Method,MA_Price,pos);
pos--;
}
}
void ma1()
{
int pos=Bars;
while(pos>=0)
{
Buffer3[pos]=iMA(NULL,0,MA_Period*4,MA_Shift,MA_Method,MA_Price,pos);
Buffer4[pos]=iMA(NULL,0,MA_Period*5,MA_Shift,MA_Method,MA_Price,pos);
Buffer5[pos]=iMA(NULL,0,MA_Period*6,MA_Shift,MA_Method,MA_Price,pos);
Buffer6[pos]=iMA(NULL,0,MA_Period*7,MA_Shift,MA_Method,MA_Price,pos);
pos--;
}
}
MetaTrader编程入门(1)--智能交易系统基础
MT编程入门教程(2)----指标画哪里?价格窗口还是独立窗口?
其实,都可以,不过是坐标的规格化,使得它们在相近的数据范围,就可以画在一起。
看你自己的习惯。画在价格窗口,方便和价格同时感受。
//+本例展示了如何把MA转换后画到独立窗口,同样也可以把独立窗口的指标改画在价格窗口
//要达到这些目的,需要考虑两个方面 1 估计数据的范围,以确定以什么为单位? 2 确定坐标偏移,也就是确定坐标零点。
//目的就是让你要画的多条线在同样的数值范围,这样才能同时画出,达到参考目的
//比如-1000到+1000和0到+4这样两套数据要变换后才能在同图画出,
//转换方法复习中学数学直角坐标系的缩放和平移部分-------------------------+
// 共画2条线
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Yellow
// Parameter
// MA-Properties
extern double MovingFast = 7;
extern double MovingSlow = 13;
extern double MovingSlow02 = 13;
extern int MaM=2;
extern int MaP=2;
extern int MaM0=3;
extern int MaP0=3;
extern int range1 = 11;
extern int range2 = 4;
int i;
// Buffer
//// 共画2条线,用2个数组存储线数据
double bufferMA1[];
double bufferMA2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//// 设置2条线的顺序编号,画法,对应数组,
SetIndexStyle( 0, DRAW_LINE );
SetIndexBuffer( 0, bufferMA1 );
SetIndexStyle( 1, DRAW_LINE );
SetIndexBuffer( 1, bufferMA2 );
return(0);
}
int deinit() { return(0);}
int start()
{
double Buffer10,Buffer11,Buffer20,Buffer21,Buffer30, Buffer31;
int countedBars = IndicatorCounted();
//---- check for possible errors
if ( countedBars < 0 ) { return(-1); }
if ( countedBars > 0 ) { countedBars--; }
int barsToCount = Bars - countedBars;
for ( int i = barsToCount; i >= 0; i-- )
{
//// 开始计算
Buffer20 = iMA(NULL,0,MovingFast,0,MaM,MaP, i) ;//MovingFast
Buffer30 = iMA(NULL,0,MovingSlow,0,MaM,MaP, i) ;//MovingSlow
Buffer21 = iMA(NULL,0,MovingFast,0,MaM,MaP, i+range1) ;
Buffer31 = iMA(NULL,0,MovingSlow02,0,MaM,MaP, i+range2) ;
//赋值给数组以便画线,使用Point使得坐标以点为单位,保证本指标对不同货币的一致y轴单位和视觉效果
bufferMA1 = MathRound((Buffer20-Buffer21)/Point);
bufferMA2 = MathRound((Buffer30-Buffer31)/Point);
}
return(0);
}
上面把两个MA的差值画在独立窗口,当然若把差值再加上一个MA,就可以画在价格窗口。
MA及其各种变化是最常用的指标基础。MA一般常用来替代价格本身 再做进一步计算。
如改
#property indicator_chart_window
......
Buffer10 = iMA(NULL,0,MovingFast*6,0,MaM,MaP, i)
bufferMA1 = MathRound(Buffer20-Buffer21)+Buffer10 ;
bufferMA2 = MathRound(Buffer30-Buffer31)+Buffer10 ;
.........
就可以把它们跌加到价格上画了。
你只需修改Buffer10 ,Buffer20, Buffer21 Buffer30, Buffer31为你需要的,在考虑一下数据范围坐标问题,你可以画任意的指标了。
使用timeframes参数可获得其它时间周期的数据。
使用Symbol( ) 可获得当前货币对的名称。
使用Point可获得当前货币对的点值。
谢谢分享。