//+------------------------------------------------------------------+
//| Moving Average.mq4 |//| mt4 |//+------------------------------------------------------------------+#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<OrdersTotal();i++)//循环检测当时的订单行列 { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持仓单的每一个订单方位 if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//依据订单方位,比较是否是当时K线产品以及订单仅有标识号是否和本程序设置的共同(用于防止EA误操作其他程序控制的持仓单) { if(OrderType()==OP_BUY) buys++;//找到契合条件的持仓单后,假如是多单,则暂时变量buys添加1 if(OrderType()==OP_SELL) sells++;//找到契合条件的持仓单后,假如是空单,则暂时变量sells添加1 } }//---- return orders volumeif(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 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-lotlosses/DecreaseFactor,1);//假如亏本额大于1,则下一进场单的买卖量修正为新的核算结果。 }//---- return lot sizeif(lot<0.1) lot=0.1;//假如核算出的买卖量小于帐户最小手数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 barif(Volume[0]>1) return;//假如当时K线持仓量大于1,阐明不是K线的开盘时刻点,则直接回来 否则是K线第一个价格,则持续下面的进程//---- get Moving Averagema=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_Close,0);//取得当时的均线数值//---- sell conditionsif(Open[1]>ma && Close[1]<ma) //如当时K开盘价大于均线,而前一K收盘价小于均线,则宣布进场多单 { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); return; }//---- buy conditionsif(Open[1]<ma && Close[1]>ma) //如当时K开盘价小于均线,而前一K收盘价大于均线,则宣布进场空单 { 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 barif(Volume[0]>1) return;//---- get Moving Averagema=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//----for(int i=0;i<OrdersTotal();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]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);//假如持仓是多单,则当当时K开盘价小于均线,而前一K收盘价大于均线,则宣布平仓指令 break; } if(OrderType()==OP_SELL) { if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White););//假如持仓是空单,则当当时K开盘价大于均线,而前一K收盘价小于均线,则宣布平仓指令 break; } }//---- }//+------------------------------------------------------------------+//| Start function |//+------------------------------------------------------------------+void start()//主循环进程 {//---- check for history and tradingif(Bars<100 || IsTradeAllowed()==false) return;//---- calculate open orders by current symbolif(CalculateCurrentOrders(Symbol())==0) CheckForOpen();else CheckForClose();//---- }//+------------------------------------------------------------------+