OrderClose Template

Back to topics list To post a new topic, please log in or register
avatar
37
waterhorse 2015.04.17 17: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;         
            }
         }

      }
   }
   

 

 

 

avatar
736
honest_knave 2015.04.18 00: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()

 

 

 

avatar
37
waterhorse 2015.04.20 17:36 #
 
Thanks for the info. Greatly appreciated. 
Back to topics list  

To add comments, please log in or register