
下方是注解
//+------------------------------------------------------------------+//| Moving Average.mq4 |//|http://funion.com//+------------------------------------------------------------------+#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 http://funion.com//+------------------------------------------------------------------+int CalculateCurrentOrders(string symbol)//函数作用,计算目前持仓订单的数量{int buys=0,sells=0;//定义两个临时变量,准备用于后面的多空订单的个数计算//----for(int i=0;i//+------------------------------------------------------------------+
double LotsOptimized()//函数目的,依据要求 计算出订单交易量{double lot=Lots;int orders=HistoryTotal();// history orders total历史出场订单的个数int losses=0; // number of losses orders without a break//---- select lot sizelot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通过风险系数的计算得到目前入场单应当采取的交易量//---- calcuulate number of losses orders without a breakif(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);//假使亏损额大于1,则下一入场单的交易量修正为新的计算结果。}//---- return lot sizeif(lot<0.1) lot=0.1;//假使计算出的交易量差于帐户最小手数0.1,则下一入场单的交易手数运用0.1作为交易量return(lot);}//+------------------------------------------------------------------+//| Check for open order conditions | http://funion.com//+------------------------------------------------------------------+void CheckForOpen()//检查入场条件的情形并作处理{double ma;int res;//---- go trading only for first tiks of new barif(Volume[0]>1) return;//假使目前K线持仓量大于1,表明不是K线的开盘时间点,则直接返回 否则是K线第一个价格,则继续下面的过程//---- get Moving Averagema=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PR洲际交易所_CLOSE,0);//得到目前的均线数值//---- sell conditionsif(Open[1]>ma && Close[1]