持仓订单全部平仓包括删除挂掉的完整函数。
- bool CloseOrder(int Ticket, double Lots, int myOrderType,string myOrderSymbol)
- {
- 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 = MarketInfo(myOrderSymbol,MODE_BID);
- if (myOrderType == OP_SELL) Price = MarketInfo(myOrderSymbol,MODE_ASK);
- if (MarketInfo(myOrderSymbol,MODE_DIGITS) > 0)
- Price = NormalizeDouble(Price, MarketInfo(myOrderSymbol,MODE_DIGITS));
- int cnt = 0;
- while (!Status) {
- if (IsTradeAllowed() == true) {
- res = OrderClose(Ticket, Lots, Price, MarketInfo(myOrderSymbol,MODE_SPREAD),
- Violet);
- ErrorCode = GetLastError();
- } else cnt++;
- if (res == true) Status = true;
- ErrorCode = GetLastError();
- switch (ErrorCode) {
- case 0:
- Status = true;
- break;
- case 4:
- case 6:
- case 129:
- case 136:
- case 137:
- case 146:
- case 128:
- RefreshRates();
- cnt++;
- break;
- case 135:
- case 138:
- RefreshRates();
- continue;
- default:
- Status = TRUE;
- }
- if (cnt > 20) Status = TRUE; //10 time try
- if (!Status)
- {
- Sleep(500);
- RefreshRates();
- }
- }
- if (res == true || ErrorCode == 0) return (true);
- Print(" Error closing order : (", ErrorCode, ") " + ErrorDescription(ErrorCode));
- return (false);
- }
|