上次写了一个简单的均线ea,在图标中同时显示SMA,WMA,EMA,今天写了一个基于SMA和EMA的简单ea,当然,想靠他盈利是不可能的,看代码吧
//+------------------------------------------------------------------+ //| ma_ea.mq4 | //| Copyright 2014, MetaQuotes Software Corp. | //| http://coderaladdin.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2014, MetaQuotes Software Corp." #property link "http://coderaladdin.com" #property version "1.00" #property strict #define MAGIC_NUM 20150312 //--- input parameters input int period=19; input int max_orders=10; input double per_lot=0.01; input double risk_rate=0.1; double calcLots() { double lot=per_lot; double minLot = MarketInfo(Symbol(),MODE_MINLOT); lot=NormalizeDouble(risk_rate*AccountFreeMargin()/AccountLeverage(),1); if(lot<0.1) lot=0.1; if(lot<minLot) lot=minLot; return(lot); } void CheckForOpen() { double ema; double sma; int res; //--- go trading only for first tiks of new bar // if(Volume[0]>1) return; //--- get Moving Average sma=iMA(NULL,0,period,0,MODE_SMA,PRICE_CLOSE,0); ema=iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,0); //--- sell conditions if(Open[1]>ema && Open[1]>sma && ema<sma && Close[1]<ema && Close[1]<sma) { res=OrderSend(Symbol(),OP_SELL,calcLots(),Bid,3,0,0,"",MAGIC_NUM,0,Red); return; } //--- buy conditions if(Open[1]<ema && Open[1]<sma && Close[1]>sma && Close[1]>ema && ema>sma) { res=OrderSend(Symbol(),OP_BUY,calcLots(),Ask,3,0,0,"",MAGIC_NUM,0,Blue); return; } //--- } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { double sma; double ema; //--- go trading only for first tiks of new bar // if(Volume[0]>1) return; //--- get Moving Average sma=iMA(NULL,0,period,0,MODE_SMA,PRICE_CLOSE,0); ema=iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,0); //--- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGIC_NUM || OrderSymbol()!=Symbol()) continue; //--- check order type if(OrderType()==OP_BUY) { if(Open[1]>sma && Open[1]>ema && Close[1]<ema && Close[1]<sma && ema<sma) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White)) Print("OrderClose error ",GetLastError()); } break; } if(OrderType()==OP_SELL) { if(Open[1]<sma && Open[1]<ema && Close[1]>ema && Close[1]>sma && ema>sma) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White)) Print("OrderClose error ",GetLastError()); } break; } } //--- } //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(Bars<100 || IsTradeAllowed()==false) return; if(OrdersTotal() <=10) CheckForOpen(); // else CheckForClose(); } //+------------------------------------------------------------------+