//+------------------------------------------------------------------+ //| EMA_CROSS.mq4 | //| Coders Guru | //| http://www.forex-tsd.com | //| Modified by Robert Hill as follows | //| 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 = 25; extern int ShortEma = 10; extern int LongEma = 80; 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); } //+------------------------------------------------------------------+ //| 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 Crossed (double line1 , double line2) { static int last_direction = 0; static int current_direction = 0; //Don't work in the first load, wait for the first cross! static bool first_time = false; if(first_time == true) { first_time = false; return (0); } //---- if(line1 > line2) current_direction = 1; //up if(line1 < line2) current_direction = 2; //down //---- if(current_direction != last_direction) //changed { last_direction = current_direction; return(last_direction); } else { return (0); //not changed } } int OpenTrade(int cmd) { int res; double sl;//= NormalizeDouble(iSAR(Symbol(), 0, STEP, MAXIMUM, 0), Digits); double tp; double mLots; mLots = GetLots(StopLoss); if (cmd == OP_BUY) { res = OrderSend(Symbol(), OP_BUY, mLots, Ask, 3, 0, 0, "EMA_CROSS_2", 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_2", 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 SEma, LEma; //---- if(Bars < 100) { Print("bars less than 100"); return(0); } //---- if(TakeProfit < 10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit } //---- SEma = iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_CLOSE, 0); LEma = iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 0); //---- static int isCrossed = 0; isCrossed = Crossed (LEma, SEma); //---- 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 = 0; cnt < total; cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) { if(OrderType() == OP_BUY) // long position is opened { // check for trailing stop if(TrailingStop > 0) { if(Bid - OrderOpenPrice() > myPoint*TrailingStop) { if(OrderStopLoss() < Bid - myPoint*TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - myPoint*TrailingStop, OrderTakeProfit(), 0, Green); return(0); } } } } else // go to short position { // check for trailing stop if(TrailingStop > 0) { if((OrderOpenPrice() - Ask) > (myPoint*TrailingStop)) { if((OrderStopLoss() > (Ask + myPoint*TrailingStop)) || (OrderStopLoss() == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + myPoint*TrailingStop, OrderTakeProfit(), 0, Red); return(0); } } } } } } //---- return(0); } //+------------------------------------------------------------------+