逐条解释这个EA代码。
先定义几个用户变量并赋默认值。(加载EA的时候用户可修改的)
复制内容到剪贴板
代码:
extern double TakeProfit = 50; //止赢点数变量 初值50点
extern double Lots = 0.1; //仓位的大小, 初值0.1
extern double TrailingStop = 30;//移动止损点数 初值30
extern double MACDOpenLevel=3; //开仓对MACD数值的过滤 初值3个点
extern double MACDCloseLevel=2; //平仓对MACD数值的过滤 初值2个点
extern double MATrendPeriod=26; //过滤移动平均线的参数 初值26。这部分在代码中作了解释。
接下来我们看下对于信号判断前的准备
复制内容到剪贴板
代码:
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
int cnt, ticket, total;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
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);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条,依此类推).
基本的准备就绪了。