Download MetaTrader 5
To add comments, please log in or register
Do you wonder how to use widgets? Read the article!
Winsor Hoang
4743
Winsor Hoang 2015.04.17 15:34 

Does anyone have an OrderClose function that you can share with the forum? I am using the following two close functions (CareCloseOrder & CloseTrade)but they are not working very well. Do you have any recommendation to improve them? Do you think that one close function is better than the other. 

 

int CareCloseOrder(int buyorsell, double lotsize) 
   {
   bool checkroutine  = FALSE;
   color colorselect  = CLR_NONE;
   int cnt  = 0;
   int errorcheck = 0;
   double currenypricing = 0;
   if (buyorsell == 0) 
      {
      RefreshRates();
      currenypricing = Bid;
      colorselect  = Green;
      } 
  else 
      {
      RefreshRates();
      currenypricing = Ask;
      colorselect  = Red;
      }
   bool currentordercount = FALSE;

      for (cnt  = 0; cnt  < MaxCount ; cnt ++) 
         {
         currentordercount = OrderClose(OrderTicket(), lotsize, NormalizeDouble(currenypricing,Digits), 90, colorselect );
         errorcheck = 0;
         if (!currentordercount) errorcheck = GetLastError();
         if (!currentordercount) 
            {
            Sleep(delay_30s);
            RefreshRates();
            if (buyorsell == 0) 
               {
               currenypricing = Bid;
               continue;
               }
               currenypricing = Ask;
            } 
        else 
         {
            checkroutine  = TRUE;
            break;
         }
      }

   return (checkroutine );
}



void CloseTrade(int _type, int _magic) 
   {
   int ordtotal = OrdersTotal();
   for (int cnt = ordtotal - 1; cnt >= 0; cnt--)
      {
      if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) == 0) continue;
      if (OrderSymbol() != Symbol()) continue;
      int ordmagic = OrderMagicNumber();
      if (ordmagic == _magic)
         {
         if (OrderType()==_type)
            {
            if (OrderClose(OrderTicket(),lot,OrderClosePrice(),90,Red)) continue;         
            }
         }

      }
   }
   
honest_knave
954
honest_knave 2015.04.17 22:43  
for (cnt  = 0; cnt  < MaxCount ; cnt ++)
 

Don't count up, always count down. See here, which also gives you some sample code for correctly closing orders.


currentordercount = OrderClose(OrderTicket(), lotsize, NormalizeDouble(currenypricing,Digits), 90, colorselect );
if (OrderClose(OrderTicket(),lot,OrderClosePrice(),90,Red)) continue; 

 Check out OrderLots() and OrderClosePrice()

Winsor Hoang
4743
Winsor Hoang 2015.04.20 15:36  
Thanks for the info. Greatly appreciated. 
/
To add comments, please log in or register