bool CloseOrder(int Ticket, double Lots, int myOrderType) { bool res; int ErrorCode; double Price; bool Status = false; if(myOrderType == OP_BUYLIMIT || myOrderType == OP_BUYSTOP || myOrderType == OP_SELLLIMIT || myOrderType == OP_SELLSTOP) { OrderDelete(OrderTicket()); return(true); } if (myOrderType == OP_BUY) Price = Bid; if (myOrderType == OP_SELL) Price = Ask; if (Digits > 0) Price = NormalizeDouble(Price, Digits); int cnt = 0; while (!Status) { if (IsTradeAllowed() == true) { res = OrderClose(Ticket, Lots, Price, MarketInfo(Symbol(),MODE_SPREAD), Violet); ErrorCode = GetLastError(); } else cnt++; if (res == true) Status = true; ErrorCode = GetLastError(); switch (ErrorCode) { case 0/* NO_ERROR */: Status = true; break; case 4/* SERVER_BUSY */: case 6/* NO_CONNECTION */: case 129/* INVALID_PRICE */: case 136/* OFF_QUOTES */: case 137/* BROKER_BUSY */: case 146/* TRADE_CONTEXT_BUSY */: case 128/* TRADE_TIMEOUT */: RefreshRates(); cnt++; break; case 135/* PRICE_CHANGED */: case 138/* REQUOTE */: RefreshRates(); continue; default: Status = TRUE; } if (cnt > 20) Status = TRUE; //10 time try if (!Status) { Sleep(500); RefreshRates(); } } if (res == true || ErrorCode == 0/* NO_ERROR */) return (true); Print(" Error closing order : (", ErrorCode, ") " + ErrorDescription(ErrorCode)); return (false); }
发表评论