OrderClose function MQL5

To add comments, please log in or register
David Diez
1207
David Diez  

Hi,I have this orderclose function working in MQ4 but as the tester results doesnt show any closed position must be a translation mistake, if someone can check this issue I thank you in advance.

void OrdersCloseLong(){
   for(int i=0;i<OrdersTotal();i++){
      if(PositionSelectByTicket(i)){
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY&&
            OrderGetString(ORDER_SYMBOL)==Symbol()&&OrderGetInteger(ORDER_MAGIC)==MagicNumber){
            if(!trade.PositionClose(Symbol(),ULONG_MAX)){ // Tried also with PositionGetTicket(i)
               Print("OrderClose error ",GetLastError());
               return;
               }
            else{i--;}
            }
         }
      }
   }
Strategy Testing - Algorithmic Trading, Trading Robots - MetaTrader 5
Strategy Testing - Algorithmic Trading, Trading Robots - MetaTrader 5
  • www.metatrader5.com
The Strategy Tester allows you to test and optimize trading strategies (Expert Advisors) before using them for live trading. During testing, an Expert Advisor with initial parameters is once run on history data. During optimization, a trading strategy is run several times with different sets of parameters which allows selecting the most...
Konstantin Nikitin
12220
Konstantin Nikitin  
void OrdersCloseLong(){
   for(int i=0;i<PositionsTotal();i++){
      if(PositionGetTicket(i)>0){
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY&&
            PositionGetString(POSITION_SYMBOL)==_Symbol&&PositionGetInteger(POSITION_MAGIC)==MagicNumber){
            if(!trade.PositionClose(_Symbol,ULONG_MAX)){
               Print("OrderClose error ",GetLastError());
               return;
               }
            else{i--;}
            }
         }
      }
   }

Order - pending orders.
Position - trading positions
In MQL5 it matters.

David Diez
1207
David Diez  
Konstantin Nikitin:

Order - pending orders.
Position - trading positions
In MQL5 it matters.

Ok, seems the code is good, but for some reason expert isn't still closing orders.
To add comments, please log in or register