closing profitable trades

 

Hi all, I am attempting to write a function that cycles through open positions and closes all those that are in profit by x amount and then returns true, and would return false in the case where no positions have been closed, I have the following code which seems to be ineffective, could someone please offer insight as to where I am going wrong?


  bool ProfitCheck()
  {
  
  //--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;   
//--- iterate over all open positions
   if(PositionSelect(_Symbol))
   {
   for(int i=openpositions-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);                                      // ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL);                        // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);              // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                  // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);                                 // volume of the position
      double profit=PositionGetDouble(POSITION_PROFIT);                                 //profit of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);    // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  magic);
      
      if(profit>=profitpara*_Point)
        {
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action   =TRADE_ACTION_DEAL;        // type of trade operation
         request.position =position_ticket;          // ticket of the position
         request.symbol   =position_symbol;          // symbol 
         request.volume   =volume;                   // volume of the position
         request.deviation=5;                        // allowed deviation from the price
         //--- set the price and order type depending on the position type
         if(type==POSITION_TYPE_BUY)
           {
            request.price=last_bid;
            request.type =ORDER_TYPE_SELL;
           }
         else
           {
            request.price=last_ask;
            request.type =ORDER_TYPE_BUY;
           }
         //--- output information about the closure
         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
           {
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         //---
           }  
         ProfitCheck();
         }
        else
        {
        return true;
       }
     return false;
     }
     return false;
    }
    return false;
   }
 

You could have saved a lot of your time if you would have searched for: close profitable positions => https://www.mql5.com/en/search#!keyword=close%20profitable%20positions&module=mql5_module_codebase.

E.g.: https://www.mql5.com/en/code/19364. Not exactly what you want but easy to adapt.

This:

 if(PositionSelect(_Symbol))

works only on netting positions I would use PositionGetTicket(idx). Type it place the cursor there and press F1 and read what it does and can.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Carl Schreiber #:

You could have saved a lot of your time if you would have searched for: close profitable positions => https://www.mql5.com/en/search#!keyword=close%20profitable%20positions&module=mql5_module_codebase.

E.g.: https://www.mql5.com/en/code/19364. Not exactly what you want but easy to adapt.

This:

works only on netting positions I would use PositionGetTicket(idx). Type it place the cursor there and press F1 and read what it does and can.

Many thanks Carl, I have this working now
Reason: