评论

收藏

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

avatar 老周 | 1008 人阅读 | 0 人评论 | 2017-03-20

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

  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/* NO_ERROR */:
  28. Status = true;
  29. break;
  30. case 4/* SERVER_BUSY */:
  31. case 6/* NO_CONNECTION */:
  32. case 129/* INVALID_PRICE */:
  33. case 136/* OFF_QUOTES */:
  34. case 137/* BROKER_BUSY */:
  35. case 146/* TRADE_CONTEXT_BUSY */:
  36. case 128/* TRADE_TIMEOUT */:
  37. RefreshRates();
  38. cnt++;
  39. break;
  40. case 135/* PRICE_CHANGED */:
  41. case 138/* REQUOTE */:
  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/* NO_ERROR */) return (true);
  55. Print(" Error closing order : (", ErrorCode, ") " + ErrorDescription(ErrorCode));
  56. return (false);
  57. }
复制代码


""
还没有人打赏,支持一下
您需要登录后才可以回帖 登录 | 注册 微信登录

EA之家评论守则