搜档网
当前位置:搜档网 › 实用MT4编程教学完整版

实用MT4编程教学完整版

实用MT4编程教学完整版
实用MT4编程教学完整版

第一篇创建新文件

1、打开 MetaEditor (如何打开自己想办法)呵呵,如果这个都打不开,拜托下面的也不用看了。也许你不适合研究这个。:lol

2、选择文件-->新文件打开文件创建页面。

3、选择第二项客户指标然后点下一步。

注:这个页面可以创建6种文件。我们常用的有《客户指标》,《脚本》,《智能交易系统》三种。我们先从指标开始。

4、输入名字,作者等等。(支持中文)暂时不添加参数。

注:这个位置可以添加用户变量以后讲解。

5、下一步我们先建一个主窗口指标所以这个页面什么都不用操作

注:这个位置可以添加指标“线”。以后提及。

6、点击完成。

ok新的指标文件生成了。但还没有任何有用的代码。初始化代码齐全。

呵呵!下一篇继续。

新建文件的样子和各功能区。

1.//+------------------------------------------------------------------+

2.//| MT4指标编辑.mq4 |

3.//| ldj |

4.//| https://www.sodocs.net/doc/2117317487.html, |

5.//+------------------------------------------------------------------+

6.#property copyright "ldj"

7.#property link "https://www.sodocs.net/doc/2117317487.html,"

8.

9.#property indicator_chart_window

10.//+------------------------------------------------------------------+

11.//| Custom indicator initialization function |

12.//+------------------------------------------------------------------+

13.int init()

14. {

15.//---- indicators

16.//----

17. return(0);

18. }

19.//+------------------------------------------------------------------+

20.//| Custom indicator deinitialization function |

21.//+------------------------------------------------------------------+

22.int deinit()

23. {

24.//----

25.

26.//----

27. return(0);

28. }

29.//+------------------------------------------------------------------+

30.//| Custom indicator iteration function |

31.//+------------------------------------------------------------------+

32.int start()

33. {

34. int counted_bars=IndicatorCounted();

35.//----

36.

37.//----

38. return(0);

39. }

40.//+------------------------------------------------------------------+

复制代码

上面的就是刚刚新建的一个指标文件。

第一部分指标注释只是一个说明,有没有都不影响指标运行。本文出自MT4系统 https://www.sodocs.net/doc/2117317487.html,

1.//+------------------------------------------------------------------+

2.//| MT4指标编辑.mq4 |

3.//| ldj |

4.//| https://www.sodocs.net/doc/2117317487.html, |

5.//+------------------------------------------------------------------+

复制代码

这部分中前面的“//” 两个斜线说明后面的是注释语句,不参与实际运行。

第二部分预处理语句这部分规定了指标的窗口性质。如下:

1.#property copyright "ldj"

2.#property link "https://www.sodocs.net/doc/2117317487.html,"

3.

4.#property indicator_chart_window

复制代码

#号表示后面是预处理语句。

property 的意思是定义mt4内部变量的性质。变量名是mt4定义好的只能用固定的变量名。

例如:版权变量 copyright 链接变量 link 以及指标窗口类型变量indicator_chart_window等等。

其中窗口类型变量有indicator_chart_window(主窗口)indicator_separate_window(副窗口)两个这里只能用一个不能两个同时用

这部分内容一般不需要修改。

第三部分初始化函数(加载函数)

1.//+------------------------------------------------------------------+

2.//| Custom indicator initialization function |

3.//+------------------------------------------------------------------+

4.int init()

5. {

6.//---- indicators

7.//----

8. return(0);

9. }

复制代码

这个函数中的代码只在只在指标(EA)加载的时候执行一次。用于对一些变量的初始化。

去初注释函数体为

1.int init()

2. {

3. return(0);

4. }

复制代码

第三部分卸载函数

1.//+------------------------------------------------------------------+

2.//| Custom indicator deinitialization function |

3.//+------------------------------------------------------------------+

4.int deinit()

5. {

6.//----

7.

8.//----

9. return(0);

10. }

复制代码

当去初指标(EA)的时候执行一次。用于去除一些控件。

去除注释函数体为

1.int deinit()

2. {

3.return(0);

4. }

复制代码

第四部分主函数,每当价格变化时就调用执行一次。主要执行代码都在这里。

1.//+------------------------------------------------------------------+

2.//| Custom indicator iteration function |

3.//+------------------------------------------------------------------+

4.int start()

5. {

6. int counted_bars=IndicatorCounted();

7.//----

8.

9.//----

10. return(0);

11. }

复制代码

第五部分子函数。有些指标和EA含有子函数。我习惯写在后面。不知道我说得清楚不。

本文出自MT4系统 https://www.sodocs.net/doc/2117317487.html,

今天我们做一个双线MACD(带柱线)

代码部分!后面的帖子逐条语句分析

1.//+------------------------------------------------------------------+

2.//| macd vs macd[1] |

3.//+------------------------------------------------------------------+

4.#property indicator_buffers 3

5.#property indicator_separate_window

6.#property indicator_color1 White

7.#property indicator_color2 Red

8.#property indicator_color3 Silver

9.//---- buffers

10.double Buffer1[];

11.double Buffer2[];

12.double Buffer3[];

13.extern int Fast = 10;

14.extern int Slow = 22;

15.extern int Signal = 7;

16.//+------------------------------------------------------------------+

17.//| Custom indicator initialization function |

18.//+------------------------------------------------------------------+

19.int init()

20. {

21.//---- indicators

22. //IndicatorBuffers(3);

23. SetIndexStyle(0,DRAW_LINE,0,1);

24. SetIndexStyle(1,DRAW_LINE,0,1);

25. SetIndexStyle(2,DRAW_HISTOGRAM,0,1);

26. SetIndexBuffer(0,Buffer1);

27. SetIndexBuffer(1,Buffer2);

28. SetIndexBuffer(2,Buffer3);

29. IndicatorShortName("MACD("+Fast+","+Slow+","+Signal+")");

30. SetIndexLabel(0,"MACD_MAIN");

31. SetIndexLabel(1,"MACD_SIGNAL");

32. SetIndexLabel(2,"MAIN-SIGNAL");

33. IndicatorDigits(Digits+2);

34.//----

35. return(0);

36. }

37.//+------------------------------------------------------------------+

38.//| Custor indicator deinitialization function |

39.//+------------------------------------------------------------------+

40.int deinit()

41.{

42.return(0);

43.}

44.//+------------------------------------------------------------------+

45.//| Custom indicator iteration function |

46.//+------------------------------------------------------------------+

47.int start()

48. {

49. int limit,counted_bars=IndicatorCounted();

50.//---- check for possible errors

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

52.//---- last counted bar will be recounted

53. if(counted_bars>0) counted_bars--;

54. limit=Bars-counted_bars;

55. //---- main loop

56. for(int i=0; i

57. {

58. Buffer1[i]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_MAIN,i);

59. Buffer2[i]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_SIGNAL,i);

60. Buffer3[i]=Buffer1[i] - Buffer2[i];

61. }

62.//----

63. return(0);

64. }

65.//+------------------------------------------------------------------+

复制代码

MACD1

下面我们来逐步实现这个指标。

我们要形成双线MACD和一个柱状指标。

因此我们在副图上要形成3个指标线。

主指标

信号指标

柱状指标

1、第一步确认指标显示的窗口

1.#property indicator_separate_window

2.// #property indicator_chart_window

复制代码

这条代码决定了指标在副图窗口显示。下面那条注释语句表示在主图窗口也就是K线图上显示。

2、预定义3个缓冲区来显示这三个指标线

1.#property indicator_buffers 3

2.#property indicator_color1 White

3.#property indicator_color2 Red

4.#property indicator_color3 Silver

复制代码

#property indicator_buffers 3 语句预定义三个指标缓存区。

#property indicator_color1 White

#property indicator_color2 Red

#property indicator_color3 Silver

这三条语句为这三个指标预定义了三种颜色。

3、定义这三个指标的数组变量。

1.double Buffer1[];

2.double Buffer2[];

3.double Buffer3[];

复制代码

double定义一个浮点变量。因为是一组数所以要定义一个数组“[]”。

4、在init()函数中初始化这三个指标。

1.//IndicatorBuffers(3);

2. SetIndexStyle(0,DRAW_LINE,0,1);

3. SetIndexStyle(1,DRAW_LINE,0,1);

4. SetIndexStyle(2,DRAW_HISTOGRAM,0,1);

5. SetIndexBuffer(0,Buffer1);

6. SetIndexBuffer(1,Buffer2);

7. SetIndexBuffer(2,Buffer3);

8. IndicatorShortName("MACD("+Fast+","+Slow+","+Signal+")");

9. SetIndexLabel(0,"MACD_MAIN");

10. SetIndexLabel(1,"MACD_SIGNAL");

11. SetIndexLabel(2,"MAIN-SIGNAL");

12. IndicatorDigits(Digits+2);

复制代码

IndicatorBuffers(3);//定义缓冲区的数量最多八个。因为这个指标只需要三个主缓冲区。所有这个有无都可以。有些需要辅助数组就需要定义这个。以后用到的时候再提起。

SetIndexStyle(0,DRAW_LINE,0,1);//定义指标的显示形式。DRAW_LINE标示画线指标。

看下这个内置函数的定义

SetIndexStyle( int index, int type, int style=EMPTY, int width=EMPTY, color clr=CLR_NONE) index:索引号。0就是第一个指标线1就是第二个指标线。

type: 指标类型下面是可选参数都是MT4的标准常量。

DRAW_LINE = 0 画线

DRAW_SECTION = 1 画线段

DRAW_HISTOGRAM = 2 画柱状图

DRAW_ARROW = 3 画箭头符号(需要设置符号代码)

DRAW_ZIGZAG = 4 画锯齿图

DRAW_NONE = 12 不画图

style:指标线型 0~4的选择。也可以不要,默认为0。

Width:指标线宽 1~5的选择。也可以不要,默认为1。

clr: 指标颜色一般用#property indicator_color1 White语句定义。前面定义了所以我们这里没有定义。

SetIndexBuffer(0,Buffer1);//为定义的指标变量数组标记索引号。就是使他们一一对应。

0号索引对应Buffer1[]变量。依此类推

IndicatorShortName("MACD("+Fast+","+Slow+","+Signal+")");//设置指标显示的名称,内容是用+号连接的字符串。

就是当鼠标放在指标上所看到的指标名称。与文件名不相关。

SetIndexLabel(0,"MACD_MAIN");//设置指标的标记。就是当鼠标放在指标线上显示的第三行的名称。

这条语句的意思是0号索引对应的指标变量标记是MACD_MAIN。其他类推。

IndicatorDigits(Digits+2);//定义指标的小数点位数数值是整数。这里Digits是MT4的预定义变量。其值为当前货币兑的小数位。

主函数体

有点儿失眠,又静不下心写程序。所以来把最后一段说明下。

1.int start()

2. {

3. int limit,counted_bars=IndicatorCounted();

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

5. if(counted_bars>0) counted_bars--;

6. limit=Bars-counted_bars;

7. for(int i=0; i

8. {

9. Buffer1[i]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_MAIN,i);

10. Buffer2[i]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_SIGNAL,i);

11. Buffer3[i]=Buffer1[i] - Buffer2[i];

12. }

13. return(0);

14. }

复制代码

1、int limit,counted_bars=IndicatorCounted();//定义两个整形变量,并给counted_bars变量负值。

这里面IndicatorCounted()函数是mt4内置函数不需要参数,其返回值为已经计算过的指标数组数量。

如果指标错误则这个函数会返回一个负数。

2、if(counted_bars<0) return(-1); //如果条件成立说明指标调用运行错误。则退出程序。

3、if(counted_bars>0) counted_bars--;//从已经计算的指标中去除最后一条。

这条语句用来修正counted_bars使得已经计算的最后一个数值可以在接下来的运算中重新计算一次。

4、limit=Bars-counted_bars;//计算需要计算的指标数据的柱数。

这里需要说明。在mt4中指标数组的索引和K线的索引标记相同,是从后向前递增的从0开使的整数。

也就是说,最后一条K线的索引是0同时K线所对应的指标索引也是0。

那么倒数第2条的索引标记为1。倒数第三条的索引标记为2。

这一点一定要理解清楚。不然写程序的时候就会发生错误。

语句中的Bars是mt4预定义变量,其值是当前图表中K线的条数。

这里详细说下为什么有个counted_bars--;的语句,这个语句的意思是对变量counted_bars进行自减一操作。

因为主函数是每次价格变动就会运行一次。当运行完成后。IndicatorCounted()值应该等于Bars也就是K线的条数

如果没有上面的自减一操作,那么当价格变动有了新的收盘价但并没有生成新的K线。这时候计算limit 的值将=0.

那么下面的for循环体将不会再计算最后一条k线相对应的指标数值。

实际上这个是需要计算的(因为有了新的收盘价)。而有了自减一的操作就可以对最有一个,也就是当前K线对应的指标值进行运算。

(不知道能看明白不自己慢慢捉摸捉摸)。这个自减一是必需的。

5、for(int i=0; i

关于for循环的运行不作解释。各位自己找资料学习。:)

6、Buffer1[ i ]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_MAIN,i); //调用MACD指标函数为Buffer1数组负值。

看下iMACD()这个内置指标函数的定义。

iMACD( string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int

signal_period, int applied_price, int mode, int shift)

symbol:货币标识。通用指标用NULL常量。

timeframe:计算所依据的图表时间周期。0表示依据当前图表周期。

fast_ema_period:快线周期

slow_ema_period:慢线周期

signal_period:信号线周期

applied_price:计算所用价格模式

mode:指标索引模式。MACD指标有两条线,因此这个位置有0,1两个选择。也可以用mt4预定义常量。

shift:索引号

这里Buffer1取macd主线数据。Buffer2取macd信号线数据。

7、Buffer3[ i ]=Buffer1[ i ] - Buffer2[ i ];//计算MACD两条线之间的距离。

8、这里用到了外部变量。也可以叫用户自定义变量。这种变量在加载图表的时候可以修改。

1.extern int Fast = 10;

2.extern int Slow = 22;

3.extern int Signal = 7;

复制代码

extern 关键字说明后面定义的变量是外部变量。

这里贴下iMACD()函数的英文说明。

double iMACD( string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int signal_period, int applied_price, int mode, int shift)

Calculates the Moving averages convergence/divergence and returns its value. In the systems where OsMA is called MACD Histogram, this indicator is displayed as two lines. In the Client Terminal, the Moving Average Convergence/Divergence is drawn as a histogram.

Parameters:

symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.

timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.

fast_ema_period - Number of periods for fast moving average calculation.

slow_ema_period - Number of periods for slow moving average calculation.

signal_period - Number of periods for signal moving average calculation.

applied_price - Applied price. It can be any of Applied price enumeration values.

mode - Indicator line index. It can be any of the Indicators line identifiers enumeration value.

shift - Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).

Sample:

if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNA L,0)) return(0);

英文好的自己看吧。我看着很累。

再来一个MACD交叉信号显示!

后面只解释增加的代码部分!

macd.gif (16.14 KB)

1、为这两种箭头指标增加和修改相应的预定义部分。

1.#property indicator_buffers 5

2.#property indicator_color4 Yellow

3.#property indicator_color5 Blue

复制代码

#property indicator_buffers 5 //指标数量由3增加到5 为这两种箭头设置预定义颜色

#property indicator_color4 Yellow

#property indicator_color5 Blue[/code]

2、定义存放这两种箭头的变量数组。

1.double UP[];

2.double DO[];

复制代码

UP[]存放向上箭头变量DO[]存放向下箭头变量。

3、为这两个箭头指标在int()函数中设置初始化属性。

1. SetIndexStyle(3,DRAW_ARROW);

2. SetIndexStyle(4,DRAW_ARROW);

3. SetIndexArrow(3,233);

4. SetIndexArrow(4,234);

5. SetIndexBuffer(3,UP);

6. SetIndexBuffer(4,DO);

7. SetIndexLabel(3,"BUY_SIGNAL");

8. SetIndexLabel(4,"SELL-SIGNAL");

复制代码

SetIndexArrow(3,233);//设置箭头的样式。3是索引,233是向上的箭头234是向下的箭头。

SetIndexArrow()函数定义形式。

void SetIndexArrow( int index, int code)

Sets an arrow symbol for indicators line of the DRAW_ARROW type.

Arrow codes out of range 33 to 255 cannot be used.

Parameters:

index - Line index. Must lie between 0 and 7.

code - Symbol code from Wingdings font or Array constants.

4、在主函数start()为这两个指标负值。

1.for(i=0; i

2. {

3. if (Buffer1[i+0]>Buffer2[i+0] && Buffer1[i+1]

4. if (Buffer1[i+0]Buffer2[i+1]) DO[i]=Buffer2[i];

5. if (Buffer1[i+0]>Buffer2[i+0] && Buffer1[i+1]==Buffer2[i+1] && Buffer1[i+2]

6. if (Buffer1[i]Buffer2[i+2]) DO[i]=Buffer2[i];

7. }

复制代码

这两个指标的取值由MACD指标的两条曲线的相互关系决定。

就是看相邻两点或三个点之间的大小关系(有相等点的时候就要用3点判断)

就是0,1,2三点两条线数值的大小。

不明白的把上面判断条件的i去掉然后画画相互关系关系就明了了。

虽然MACD看着是两条线,实际是每条K线形成一个点。然后把所有点用光滑线连接起来的。

当条件满足的时候就将Buffer2的值赋予相应的指标数组(UP或者DO)。这样mt4就在Buffer2的相应位置画一个箭头。

对于不满足的位置就不会对UP[]和DO[]赋值,这时UP[]和DO[]的值默认为空,mt4就不会做任何操作(什么都不画)。

ok这个指标就这样了。

继续完善这个MACD

如果你挂过上面的指标你就会发现假突破的箭头不会消失。因为没有赋空值语句。

那么我们加上去。需要增加两行代码。

UP[ i ]=EMPTY_VALUE;

DO[ i ]=EMPTY_VALUE;

for语句循环体变成如下。

1. for(i=0; i

2. {

3. UP[i]=EMPTY_VALUE;

4. DO[i]=EMPTY_VALUE;

5. if (Buffer1[i]>Buffer2[i] && Buffer1[i+1]

6. UP[i]=Buffer2[i];

7. if (Buffer1[i]Buffer2[i+1])

8. DO[i]=Buffer2[i];

9. if (Buffer1[i]>Buffer2[i] && Buffer1[i+1]==Buffer2[i+1] && Buffer1[i+2]

10. UP[i]=Buffer2[i];

11. if (Buffer1[i]Buffer2[i+2])

12. DO[i]=Buffer2[i];

13. }

复制代码

EMPTY_VALUE是mt4内置常量,表示空值。没有任何值当然就不会画箭头了。

现在我们希望还能有个报警最好。

对于报警只需要判断最后三个点的位置关系。代码如下。

1.if (Buffer1[0]>Buffer2[0] && Buffer1[1]

2. Alert("MACD金叉");

3. if (Buffer1[0]Buffer2[1])

4. Alert("MACD死叉");

5. if (Buffer1[0]>Buffer2[0] && Buffer1[1]==Buffer2[1] && Buffer1[2]

6. Alert("MACD金叉");

7. if (Buffer1[0]Buffer2[2])

8. Alert("MACD死叉");

复制代码

这几行代码添加在上面for循环体的外面。一定不要放在上面的循环体内。

Alert()是内置的报警函数。当执行这条语句的时候会弹出报警窗口,并发出叮的声音报警。这个函数的参数是字符串变量,或者常量。

MT4自带的MACD交易程序分析。

接下来我们研究下mt4自带的MACD交易系统。

分析下其流程。

我们先来确认下其交易的规则。

入场点规则。

一、买单入场规则

1、MACD快线在零轴下方3个点(也就是0轴下方金叉)。

2、MACD金叉(MACD参数12,26,9)。

3、MA26均线方向向上。

二、卖单入场规则

1、MACD快线在零轴上方3个点(也就是0轴上方金叉)。

2、MACD死叉(MACD参数12,26,9)。

3、MA26均线方向向下。

出场规则。

一、止赢止损出场。

二、买单出场条件

1、MACD在零轴上方死叉

2、并且MACD快线在零轴上方2个点

三、卖单出场条件

1、MACD在零轴下方金叉

2、并且MACD快线在零轴下方2个点

并有移动止损功能。

[本帖最后由 xfxyldj 于 2007-9-19 03:03 编辑]

未命名.JPG (26.17 KB)

逐条解释这个EA代码。

先定义几个用户变量并赋默认值。(加载EA的时候用户可修改的)

1.extern double TakeProfit = 50; //止赢点数变量初值50点

2.extern double Lots = 0.1; //仓位的大小,初值0.1

3.extern double TrailingStop = 30;//移动止损点数初值30

4.extern double MACDOpenLevel=3; //开仓对MACD数值的过滤初值3个点

5.extern double MACDCloseLevel=2; //平仓对MACD数值的过滤初值2个点

6.extern double MATrendPeriod=26; //过滤移动平均线的参数初值26。

复制代码

这部分在代码中作了解释。

接下来我们看下对于信号判断前的准备

1.double MacdCurrent, MacdPrevious, SignalCurrent;

2. double SignalPrevious, MaCurrent, MaPrevious;

3. int cnt, ticket, total;

4. if(Bars<100)

5. {

6. Print("bars less than 100");

7. return(0);

8. }

9. if(TakeProfit<10)

10. {

11. Print("TakeProfit less than 10");

12. return(0); // check TakeProfit

13. }

14. MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);

15. MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

16. SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);

17. SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);

18. MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);

19. MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

复制代码

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

这个判断的作用是,如果当前图表的K线条数小于100

则1、Print("bars less than 100");//输出bars less than 100字符串。

Print( ...) 函数用来输出一个字符串,也就是一句话。

2、return();这个语句结束这个主函数,后面的程序代码不再执行。

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

这个判断是如果止赢小于10则输出TakeProfit less than 10 并推出主函数。

这个程序要求止赢大于10,也就是禁止拔头皮吧。

MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);

MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);

SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);

MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);

MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

这个6条语句取得了MACD两条线及26号均线的当前值,和前一点的数值。

MacdCurrent=MACD快线的当前值。

MacdPrevious=MACD快线的前一点值。

SignalCurrent=MACD慢线的当前值。

SignalPrevious=MACD慢线的前一点值。

MaCurrent=26EMA均线的当前值。

MaPrevious=26EMA均线的前一点值。

iMACD()内置指标函数前面介绍过。这里不再重复。下面详细说下iMA()内置指标函数

看下函数的定义。

double iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift) Calculates the Moving average indicator and returns its value.

Parameters:

symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol. timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe. period - Averaging period for calculation.

ma_shift - MA shift. Indicators line offset relate to the chart by timeframe.

ma_method - MA method. It can be any of the Moving Average method enumeration value.

applied_price - Applied price. It can be any of Applied price enumeration values.

shift - Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).

简单翻译下。(我挂着金山词霸翻译,并不是原味的翻译,英文好的看上面的原文)

symbol - 货币标识(NULL表示使用当前图表的货币标识)

timeframe - 时间周期,0表示使用的是图表的当前周期(小时图,日图等选择).

period - 均线周期,这里使用了用户变量MATrendPeriod=26.

ma_shift - 均线平移。0表示不平移.

ma_method - 均线模式(这里使用了EMA模式“MODE_EMA”)一共有4种模式。添加均线的时候可以看到。.

applied_price - 应用的价格模式。这里用的是收盘价“PRICE_CLOSE”。.

shift - 索引号。0当前这条,1向前数1条,依此类推).

基本的准备就绪了。

下单

现在解决下第一部分。下单操作。

1.total=OrdersTotal();

2. if(total<1)

3. {

4. if(AccountFreeMargin()<(1000*Lots))

5. {

6. Print("We have no money. Free Margin = ", AccountFreeMargin());

7. return(0);

8. }

9. if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious

10. MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)

11. {

12. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);

13. if(ticket>0)

14. {

15. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

16. }

17. else Print("Error opening BUY order : ",GetLastError());

18. return(0);

19. }

20. if(MacdCurrent>0 && MacdCurrentSignalPrevious &&

21. MacdCurrent>(MACDOpenLevel*Point) && MaCurrent

22. {

23. ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);

24. if(ticket>0)

25. {

26. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

27. }

28. else Print("Error opening SELL order : ",GetLastError());

29. return(0);

30. }

31. return(0);

32. }

相关主题