Problems on Closing Trades

 

Hello,

 

i try to convert my EA from MT4 to MT5 and now i have the problem to close all position, in MT4 it was easy to say at a spezific time to close everything.

now on mt5 i use following code

 

   if(Minute()==00 && Sekunde()==00)
     {
      if(PositionSelect(_Symbol)==true)
        {
         for(k=0;k<=PositionsTotal();k--)
           {
            ClosePosition();
           }
        }
     }

in this case i use for the time

int Minute()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.min);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Sekunde()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.sec);
  }


and for Position Close

 

bool ClosePosition()
  {

   MqlTradeRequest CloseOrder={0};
   MqlTradeResult result;
//--- setting request
   CloseOrder.symbol       = _Symbol;
   CloseOrder.deviation    = 300;


   return(OrderSendAsync(CloseOrder,result));
  }

 

does anyone have an idea why its not working?

 

amando 



 

 

If you come from MT4, I suppose you are using an hedging account (please check as it could also be a netting account which is working differently).

With an hedging account, you have to specify the position ticket to close a given position, also you have to specify an opposite deal.

Forum on trading, automated trading systems and testing trading strategies

Closing a position

Alain Verleyen, 2016.04.15 08:07

When modifying or closing a position in the hedging system, make sure to specify its ticket (MqlTradeRequest::ticket). You can specify a ticket in the netting system as well, however positions are identified by a symbol name.
As a side note, your time checking can lead to bugs depending of how your EA is working, as you can have several ticks in the same second.
MetaTrader 5 features hedging position accounting system
MetaTrader 5 features hedging position accounting system
  • 2016.03.21
  • MetaQuotes Software Corp.
  • www.mql5.com
In order to expand possibilities of retail Forex traders, we have added the second accounting system — hedging. Now, it is possible to have multiple positions per symbol, including oppositely directed ones. This paves the way to implementing trading strategies based on the so-called "locking" — if the price moves against a trader, they can open a position in the opposite direction.
 

Thanks Alain,

 

i have an heding account, but what is wrong on my code?

accorrding the MQL documentation it should work, but it doesnt

 

Amando 

 
amando:

Thanks Alain,

 

i have an heding account, but what is wrong on my code?

accorrding the MQL documentation it should work, but it doesnt

 

Amando 

Not it should not work, I suggest you to read the links I provided, it's all explained. Come back with your modified code if you still have difficulties.
 
for(k=PositionsTotal()-1;k>=0;k--)