//+------------------------------------------------------------------+ //| OrdMgrSL.mq4 | //| Ted Goulden | //| | //| | // An EA that stops loss upon touching the input MA. There is flexibility // in specifying the method for calculation of the MA and the price type. // Also has a partial close function. // // Input Parameters: // // MAperiods - the number of periods to be used by the MA calculation. // MAmethod - the method to be used by the MA calculation ('S', 'W', 'E', 'M'). // MAapply - the price type to be used by the MA calculation ('O', 'C', 'H', 'L'). // Lotsize - Part of the order can be closed. 0 means close the all lots. // // Modification History: // Nov 3 09 - Ted Goulden, Initial Version //+------------------------------------------------------------------+ #property copyright "Ted Goulden" #property link "" #include #define SLIP 2 extern int MAperiods=10; extern string MAmethod="E"; extern string MAapply="C"; extern double Lotsize=0; datetime barStart = 0; int MAprice = 0, MAmode = 0, totOpen = 0; bool inputOK = true, buy1 = false, sell1 = false; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- inputOK = true; sell1 = false; buy1 = false; if (MAmethod == "E" || MAmethod == "e") MAmode = MODE_EMA; else if (MAmethod == "S" || MAmethod == "s") MAmode = MODE_SMA; else if (MAmethod == "M" || MAmethod == "m") MAmode = MODE_SMMA; else if (MAmethod == "W" || MAmethod == "w") MAmode = MODE_LWMA; else { inputOK = false; MessageBox("Allowed values for MA Method are E(Exponential), S(Simple), M(Smoothed) or W(Weighted)","",MB_ICONERROR); } if (MAapply == "O" || MAapply == "o") MAprice = PRICE_OPEN; else if (MAapply == "C" || MAapply == "c") MAprice = PRICE_CLOSE; else if (MAapply == "H" || MAapply == "h") MAprice = PRICE_HIGH; else if (MAapply == "L" || MAapply == "l") MAprice = PRICE_LOW; else { inputOK = false; MessageBox("Allowed values for MA Apply are O(Open), C(Close), H(High) or L(Low)","",MB_ICONERROR); } //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Close Order function - close order specified by POS | //+------------------------------------------------------------------+ bool closeOrder(int pos, double lotsize) { if (!inputOK) return(0); if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) { int ticket = OrderTicket(); if (lotsize == 0) double lots = OrderLots(); else lots = lotsize; if (OrderType() == OP_BUY) { OrderClose(ticket, lots, NormalizeDouble(Bid,Digits), SLIP, Violet); if (GetLastError() == 0) { Print(Symbol(), " Order Closed : ",ticket, " ", OrderClosePrice()); return (true); } else Print(Symbol(), " Order Not Closed : ",ticket, " ", GetLastError()); } if (OrderType() == OP_SELL) { OrderClose(ticket, lots, NormalizeDouble(Ask,Digits), SLIP, Violet); if (GetLastError() == 0) { Print(Symbol(), " Order Closed : ",ticket, " ", OrderClosePrice()); return (true); } else Print(Symbol(), " Order Not Closed : ",ticket, " ", GetLastError()); } } return (false); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if (MAperiods > 0) double MAval = iMA(NULL, 0, MAperiods, 0, MAmode, MAprice, 0); double spread = MarketInfo(Symbol(),MODE_SPREAD)*Point; // check on open orders to close if MA is crossed int allOrders=OrdersTotal(); for(int pos=allOrders-1;pos==0;pos--) { if(OrderSelect(pos,SELECT_BY_POS, MODE_TRADES)) { if ((OrderSymbol() == Symbol()) && (OrderType() == OP_BUY || OrderType() == OP_SELL)) { // Have found an open order for this pair. // May have to close. double openPrice = OrderOpenPrice(); int orderOpenTime = OrderOpenTime() - MathMod(OrderOpenTime(),Period()*60) ; int nbarsOpen = MathRound((Time[0] - orderOpenTime)/(Period()*60)); double highVal = High[ArrayMaximum(High, nbarsOpen, 0)]; double lowVal = Low[ArrayMinimum(Low, nbarsOpen, 0)]; bool closeBuy = Bid + Point < MAval && ((openPrice > MAval) || (highVal - openPrice > spread)); if (MAval > 0 && closeBuy && OrderType() == OP_BUY && !buy1) { if (closeOrder(pos, Lotsize)) buy1 = true; Alert(OrderTicket()," Buy Order closed at ",DoubleToStr(Bid, Digits)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool closeSell = Ask - Point > MAval && ((openPrice < MAval) || (openPrice - lowVal > spread)); if (MAval > 0 && closeSell && OrderType() == OP_SELL && !sell1) { if (closeOrder(pos, Lotsize)) sell1 = true; Alert(OrderTicket()," Sell Order closed at ", DoubleToStr(Ask, Digits)); } } // end if OrderSymbol } // end if OrderSelect } // end for if (allOrders == 0) { sell1 = false; buy1 = false; } //---- return(0); } //+------------------------------------------------------------------+-------------+