持仓订单全部平仓包括删除挂掉的完整函数

楼主  收藏   举报   帖子创建时间:  2019-05-05 14:37 回复:0 关注量:425
持仓订单全部平仓包括删除挂掉的完整函数。

  1. bool CloseOrder(int Ticket, double Lots, int myOrderType,string myOrderSymbol)
  2. {
  3. bool res;
  4. int ErrorCode;
  5. double Price;
  6. bool Status = false;
  7. if(myOrderType == OP_BUYLIMIT || myOrderType == OP_BUYSTOP ||
  8. myOrderType == OP_SELLLIMIT || myOrderType == OP_SELLSTOP)
  9. {
  10. OrderDelete(OrderTicket());
  11. return(true);
  12. }
  13. if (myOrderType == OP_BUY) Price = MarketInfo(myOrderSymbol,MODE_BID);
  14. if (myOrderType == OP_SELL) Price = MarketInfo(myOrderSymbol,MODE_ASK);
  15. if (MarketInfo(myOrderSymbol,MODE_DIGITS) > 0)
  16. Price = NormalizeDouble(Price, MarketInfo(myOrderSymbol,MODE_DIGITS));
  17. int cnt = 0;
  18. while (!Status) {
  19. if (IsTradeAllowed() == true) {
  20. res = OrderClose(Ticket, Lots, Price, MarketInfo(myOrderSymbol,MODE_SPREAD),
  21. Violet);
  22. ErrorCode = GetLastError();
  23. } else cnt++;
  24. if (res == true) Status = true;
  25. ErrorCode = GetLastError();
  26. switch (ErrorCode) {
  27. case 0:
  28. Status = true;
  29. break;
  30. case 4:
  31. case 6:
  32. case 129:
  33. case 136:
  34. case 137:
  35. case 146:
  36. case 128:
  37. RefreshRates();
  38. cnt++;
  39. break;
  40. case 135:
  41. case 138:
  42. RefreshRates();
  43. continue;
  44. default:
  45. Status = TRUE;
  46. }
  47. if (cnt > 20) Status = TRUE; //10 time try
  48. if (!Status)
  49. {
  50. Sleep(500);
  51. RefreshRates();
  52. }
  53. }
  54. if (res == true || ErrorCode == 0) return (true);
  55. Print(" Error closing order : (", ErrorCode, ") " + ErrorDescription(ErrorCode));
  56. return (false);
  57. }
打赏