//+------------------------------------------------------------------+ //| EMA_CROSS.mq4 | //| Coders Guru | //| http://www.forex-tsd.com | //| | //| Modified by Robert Hill as follows | //| 6/4/2006 Fixed bugs and added exit on fresh cross option | //| Added use of TakeProfit of 0 | //| Modified for trade on open of closed candle | //| Added Trades in this symbol and MagicNumber check | //| to allow trades on different currencies at same time | //| 3/18/2011 Modified code to allow ECN brokers | //| Added Money Management | //| Added correct handling of 5 digit prices | //+------------------------------------------------------------------+ #property copyright "Coders Guru" #property link "http://www.forex-tsd.com" //---- input parameters extern double TakeProfit=100; extern double StopLoss = 200; //+---------------------------------------------------+ //|Money Management | //+---------------------------------------------------+ extern string mm = "--- Money Management ---"; extern double Lots=0.1; extern bool UseMM = true; extern double Risk = 1; // Change to whatever percent of equity you wish to risk. extern double TrailingStop=50; extern int ShortEma = 5; extern int LongEma = 15; extern bool ExitOnCross = true; extern int SignalCandle = 3; extern int MagicNumber = 12345; double myPoint; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- myPoint = SetPoint(); //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } int FreshCross () { double SEma, LEma,SEmaP, LEmaP; SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,SignalCandle); LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,SignalCandle); SEmaP = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,SignalCandle+1); LEmaP = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,SignalCandle+1); //Don't work in the first load, wait for the first cross! if(SEma>LEma && SEmaP < LEmaP) return(1); //up if(SEma LEmaP) return(2); //down return (0); //not changed } //+------------------------------------------------------------------+ //| Check Open Position Controls | //+------------------------------------------------------------------+ int CheckOpenTrades() { int cnt; int NumTrades; // Number of buy and sell trades in this symbol NumTrades = 0; for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES); if ( OrderSymbol() != Symbol()) continue; if ( OrderMagicNumber() != MagicNumber) continue; if(OrderType() == OP_BUY ) NumTrades++; if(OrderType() == OP_SELL ) NumTrades++; } return (NumTrades); } int OpenTrade(int cmd) { int res; double sl; double tp; double mLots; mLots = GetLots(StopLoss); if (cmd == OP_BUY) { res = OrderSend(Symbol(), OP_BUY, mLots, Ask, 3, 0, 0, "EMA_CROSS", MagicNumber, 0, Green); if (res > 0) { if(OrderSelect(res, SELECT_BY_TICKET)) { sl= OrderOpenPrice()-StopLoss*myPoint; tp = OrderOpenPrice()+TakeProfit*myPoint; sl = NormalizeDouble(sl, Digits); tp = NormalizeDouble(tp, Digits); OrderModify(res, OrderOpenPrice(), sl, tp, OrderExpiration(), White); } } } if (cmd == OP_SELL) { res=OrderSend(Symbol(), OP_SELL, mLots, Bid, 3, 0, 0, "EMA_CROSS", MagicNumber, 0, Red); if (res > 0) { if(OrderSelect(res, SELECT_BY_TICKET)) { sl= OrderOpenPrice()+ StopLoss*myPoint; tp = OrderOpenPrice()-TakeProfit*myPoint; sl = NormalizeDouble(sl, Digits); tp = NormalizeDouble(tp, Digits); OrderModify(res, OrderOpenPrice(), sl, tp, OrderExpiration(), White); } } } } //+------------------------------------------------------------------+ //| Get number of lots for this trade | //+------------------------------------------------------------------+ double GetLots(double StopInPips) { int Decimals = 0; double lot, AccountValue; double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT); double myMinLot = MarketInfo(Symbol(), MODE_MINLOT); double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double LotSize = MarketInfo(Symbol(), MODE_LOTSIZE); double LotTickValue = MarketInfo(Symbol(), MODE_TICKVALUE); if(LotStep == 0.01) Decimals = 2; if(LotStep == 0.1) Decimals = 1; if (UseMM == false) return(Lots); //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ AccountValue = AccountBalance(); lot = ( AccountValue * (Risk/100) )/( LotTickValue * StopInPips * 10.0 ); lot = StrToDouble(DoubleToStr(lot,Decimals)); if (lot < myMinLot) lot = myMinLot; if (lot > myMaxLot) lot = myMaxLot; return(lot); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- int cnt, ticket, total; double TP; if(Bars<100) { Print("bars less than 100"); return(0); } /* if(TakeProfit<10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit } */ static int isCrossed = 0; isCrossed = FreshCross (); total = CheckOpenTrades(); if(total < 1) { if(isCrossed == 1) { ticket=OpenTrade(OP_BUY); return(0); } if(isCrossed == 2) { ticket=OpenTrade(OP_SELL); return(0); } return(0); } for(cnt = total-1; cnt >= 0; cnt--) { RefreshRates(); OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if ((OrderSymbol() == Symbol()) ) //&& (OrderMagicNumber() == MagicNumber)) { // OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); //OrderPrint(); if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType()==OP_BUY) // long position is opened { // should it be closed? /* REMOVED - Trailling stop only close */ if(ExitOnCross && isCrossed == 2) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Black); // close position return(0); // exit } /**/ // check for trailing stop if ((TrailingStop > 0) && (MathAbs(OrderClosePrice() - OrderOpenPrice()) > TrailingStop*myPoint) && (OrderProfit() > 0.00)) { if ( ((OrderType() == OP_BUY) && (OrderClosePrice() - TrailingStop*myPoint) > OrderStopLoss()) || ((OrderStopLoss() == 0) && (OrderClosePrice() - TrailingStop*myPoint) >= OrderOpenPrice()) ) OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()-TrailingStop*myPoint,OrderTakeProfit(),0,Black); } } } } else // go to short position { // should it be closed? /* REMOVED - Trailling stop only close */ if(ExitOnCross && isCrossed == 1) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Black); // close position return(0); // exit } /* */ // check for trailing stop if ((TrailingStop > 0) && (MathAbs(OrderClosePrice() - OrderOpenPrice()) > TrailingStop*myPoint) && (OrderProfit() > 0.00)) { if ( ((OrderType() == OP_SELL) && (OrderClosePrice() + TrailingStop*myPoint) < OrderStopLoss()) || ((OrderStopLoss() == 0) && (OrderClosePrice() + TrailingStop*myPoint) <= OrderOpenPrice()) ) OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()+TrailingStop*myPoint,OrderTakeProfit(),0,Black); } } } return(0); } //+------------------------------------------------------------------+