外汇EA程序注解解析全攻略

以下是注解

//+------------------------------------------------------------------+

//| Moving Average.mq4 |

//+------------------------------------------------------------------+

#define MAGICMA 20050610 //定义本EA操作的订单的唯一标识号码

extern double Lots = 0.1;//每单的交易量

extern double MaximumRisk = 0.02;//作者定义的最大风险参数

extern double DecreaseFactor = 3;//作者定义的参数,作用要看程序中的用法

extern double MovingPeriod = 10;//EA中使用的均线的周期

extern double MovingShift =3;//EA中使用的均线向左的K线偏移量

//+------------------------------------------------------------------+

//| Calculate open positions |

//+------------------------------------------------------------------+

int CalculateCurrentOrders(string symbol)//函数作用,计算当前持仓订单的数量

{

int buys=0,sells=0;//定义两个临时变量,用于多空订单个数计算

//----

for(int i=0;i

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持仓单的每一个订单位置

if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根据订单,判断是否为当前商品及标识号是否一致

{

if(OrderType()==OP_BUY) buys++;//若为多单,buys增加1

if(OrderType()==OP_SELL) sells++;//若为空单,sells增加1

}

}

//---- return orders volume

if(buys>0) return(buys);

else return(-sells);//返回持仓单个数

}

//+------------------------------------------------------------------+

//| Calculate optimal lot size |

//+------------------------------------------------------------------+

double LotsOptimized()//函数目的,计算最佳订单交易量

{

double lot=Lots;

int orders=HistoryTotal(); // history orders total 历史出场订单个数

int losses=0; // number of losses orders without a break

//---- select lot size

lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通过风险系数计算入场交易量

//---- calcuulate number of losses orders without a break

if(DecreaseFactor>0)

{

for(int i=orders-1;i>=0;i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }//循环查询出场单

if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;

//----

if(OrderProfit()>0) break;

if(OrderProfit()<0) losses++;//亏损单计数

}

if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);//调整交易量

}

//---- return lot size

if(lot<0.1) lot=0.1;//最低交易手数0.1

return(lot);

}

//+------------------------------------------------------------------+

//| Check for open order conditions |

//+------------------------------------------------------------------+

void CheckForOpen()//检查入场条件

{

double ma;

int res;

//---- go trading only for first tiks of new bar

if(Volume[0]>1) return;//仅在新K线开盘时交易

//---- get Moving Average

ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//获取均线值

//---- sell conditions

if(Open[1]>ma && Close[1]

{

res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);

return;

}

//---- buy conditions

if(Open[1]ma) //多单入场条件

{

res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);

return;

}

//----

}

//+------------------------------------------------------------------+

//| Check for close order conditions |

//+------------------------------------------------------------------+

void CheckForClose()//检查出场条件

{

double ma;

//---- go trading only for first tiks of new bar

if(Volume[0]>1) return;

//---- get Moving Average

ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

//----

for(int i=0;i

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;

//---- check order type

if(OrderType()==OP_BUY)

{

if(Open[1]>ma && Close[1]

break;

}

if(OrderType()==OP_SELL)

{

if(Open[1]ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);//平空单

break;

}

}

//----

}

//+------------------------------------------------------------------+

//| Start function |

//+------------------------------------------------------------------+

void start()//主循环过程

{

//---- check for history and trading

if(Bars<100 || IsTradeAllowed()==false) return;

//---- calculate open orders by current symbol

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();

else CheckForClose();

//----

}

//+------------------------------------------------------------------+