10、20日均线交叉自动交易系统
//+------------------------------------------------------------------+//| My MA System.mq4 |//| Copyright 2008.fangruifeng |//| http://fangruifeng.zhan.cn.yahoo.com/index.html//+------------------------------------------------------------------+//#define MAGICMA extern double Lots = 1;extern double TrailingStop = 150;extern double TakeProfit = 0;extern double StopLoss = 80;extern double Short_MA_value = 10;extern double Long_MA_value= 20; //+------------------------------------------------------------------+//| 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) { if(OrderType()==OP_BUY)buys++; if(OrderType()==OP_SELL) sells++; } }//---- return orders volume if(buys>0) return(buys); else return(-sells);} //+------------------------------------------------------------------+//| Check for open order conditions |//+------------------------------------------------------------------+void CheckForOpen() { int ticket; double shortEma12,longEma22,shortEma11,longEma21; if(Bars<100) { Print("bars less than 100"); return(0); } shortEma12=iMA(NULL,0,Short_MA_value,0,MODE_EMA,PRICE_CLOSE,2); longEma22=iMA(NULL,0,Long_MA_value ,0,MODE_EMA,PRICE_CLOSE,2); shortEma11=iMA(NULL,0,Short_MA_value,0,MODE_EMA,PRICE_CLOSE,1); longEma21=iMA(NULL,0,Long_MA_value ,0,MODE_EMA,PRICE_CLOSE,1); if(shortEma12<longEma22&&shortEma11>longEma21) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,0,"My EA",12345,0,Blue); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Alert("BUY order ",Symbol()," opened : ",OrderOpenPrice()); PlaySound("alert.wav"); } } else Print("Error opening BUY order : ",GetLastError()); return; } if(shortEma12>longEma22&&shortEma11<longEma21) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,0,"My EA",12345,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Alert("SELL order ",Symbol()," opened : ",OrderOpenPrice()); PlaySound("alert.wav"); } } else Print("Error opening SELL order : ",GetLastError()); return; } } //+------------------------------------------------------------------+//| Check for close order conditions |//+------------------------------------------------------------------+void CheckForClose() { double shortEma12,longEma22,shortEma11,longEma21; shortEma12=iMA(NULL,0,Short_MA_value,0,MODE_EMA,PRICE_CLOSE,2); longEma22=iMA(NULL,0,Long_MA_value ,0,MODE_EMA,PRICE_CLOSE,2); shortEma11=iMA(NULL,0,Short_MA_value,0,MODE_EMA,PRICE_CLOSE,1); longEma21=iMA(NULL,0,Long_MA_value ,0,MODE_EMA,PRICE_CLOSE,1); for(int i=0;i<OrdersTotal();i++) { //if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES); //==false) break; if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) { //---- check order type if(OrderType()==OP_BUY) { if(shortEma12>longEma22 && shortEma11<longEma21) { OrderClose(OrderTicket(),OrderLots(),Bid,3,White); return(0); } // if(TrailingStop>0) //{ // if(Bid-OrderOpenPrice()>Point*TrailingStop) //{ //if(OrderStopLoss()<Bid-Point*TrailingStop) //{ //OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); //return(0); //} //} //} } else { if(OrderType()==OP_SELL) { if(shortEma12<longEma22 && shortEma11>longEma21) { OrderClose(OrderTicket(),OrderLots(),Ask,3,White); return(0); } //if(TrailingStop>0) // { //if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) //{ // if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) // { //OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); //return(0); //} //} //} } } } }} //+------------------------------------------------------------------+//| 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();//----} //+------------------------------------------------------------------+日均线交叉自动交易系统 帮你顶下哈!! 谢谢楼主,共同发展 难得一见的好帖 路过,支持一下啦 有原码的必须支持一下子{:1_184:} 前排支持下 好好 学习了 确实不错 看帖回帖是美德!:lol