评论

收藏

彻底平仓的一个写法

avatar towwailok | 933 人阅读 | 0 人评论 | 2018-07-08

  1. bool CloseOrders(int orderMode,string myType)
  2. {
  3.   int OrderCount=0, gle=0;
  4.   int cnt,mode;
  5.   int TicketArray[100];
  6.   double ClosePrice=0;
  7.   string stringOrderMode;
  8.   
  9.   if (orderMode==OP_BUY)  stringOrderMode="BUY";
  10.   if (orderMode==OP_SELL) stringOrderMode="SELL";

  11. // first, we retrieve all ticket IDs for existing orders to close out  
  12.   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
  13.   {
  14.     if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
  15.     {
  16.       mode=OrderType();
  17.       if (isOrder(OrderSymbol(), OrderMagicNumber(), OrderComment(), myType))
  18.     {
  19.         if (mode==orderMode)
  20.         {
  21.           TicketArray[OrderCount]=OrderTicket();
  22.           Print("OrderCount: ",OrderCount,", Ticket: ",TicketArray[OrderCount]," Selected for closure.");
  23.           OrderCount++;
  24.      }
  25.       }
  26.   }
  27.   else
  28.   {
  29.     gle=GetLastError();
  30.       Print("Error selecting an order in CloseAllOrders!!! Error #",gle,": ",ErrorDescription(gle));
  31.       return(false); //Since returning false, the caller will retry this function again
  32.   }
  33.   }

  34. // second, we close out all applicable orders in the array.  this two step method prevents problems closing out all orders successfully at once.
  35.   int retries=0;
  36.   while(retries<20)
  37.   {
  38.     for(cnt=0;cnt<OrderCount;cnt++)
  39.     {
  40.       if (TicketArray[cnt]>0)
  41.       {
  42.         if (OrderSelect(TicketArray[cnt], SELECT_BY_TICKET, MODE_TRADES))
  43.         {
  44.           mode=OrderType();
  45.           GetTradeContext(); RefreshRates();
  46.           if (mode==OP_BUY)  ClosePrice=NormalizeDouble(Bid,Digits);
  47.           if (mode==OP_SELL) ClosePrice=NormalizeDouble(Ask,Digits);
  48.           if (mode == (orderMode+2) || mode == (orderMode+4))
  49.           {
  50.             if (OrderDelete(OrderTicket()))
  51.             {
  52.               TicketArray[cnt]=0;
  53.             }
  54.             else        
  55.             {
  56.               gle=GetLastError();
  57.               Print("Error closing pending order #",TicketArray[cnt],": Error #",gle,": ",ErrorDescription(gle));
  58.             }
  59.           }
  60.           else
  61.           {
  62.             if (CloseOrder(OrderTicket(), OrderLots(), OrderType()))
  63.             {
  64.               
  65.               TicketArray[cnt]=0;
  66.             }
  67.             else
  68.             {
  69.               gle=GetLastError();
  70.               Print("Error closing order #",TicketArray[cnt],": Error #",gle,": ",ErrorDescription(gle));
  71.             }
  72.           }
  73.         }
  74.         else
  75.         {
  76.           gle=GetLastError();
  77.           Print("Error selecting order #",TicketArray[cnt],": Error #",gle,": ",ErrorDescription(gle));
  78.         }
  79.       }
  80.     }
  81.   retries++;
  82.   }
  83.   
  84.   bool CloseAllSuccess=true;
  85.   for(cnt=0;cnt<OrderCount;cnt++)
  86.   {
  87.     if (TicketArray[cnt]>0)
  88.     {
  89.       CloseAllSuccess=false;
  90.       Alert("Could not close ticket #",TicketArray[cnt],"! Will keep retrying.");
  91.     }
  92.   }
  93.   
  94.   if(CloseAllSuccess)
  95.   {
  96.     return(true);
  97.   }
  98.   else
  99.   {
  100.     Print("There was a critical error closing one or more ",stringOrderMode," orders when trying to CLOSE ALL!");
  101.     return(false); //Since returning false, the caller will retry this function again
  102.   }
  103. }
复制代码


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

EA之家评论守则