poroblem with close positions in multicurrency EA

 
void closeall(ulong magic)
{
int total = PositionsTotal();

for  (int i=0 ; i < total ;i++ )
{

ulong ticket = PositionGetTicket(i);

Print("symbol =" , PositionGetString(POSITION_SYMBOL) );

trade.PositionClose(ticket,-1);  // CTrade trade 


}
}


Hello

 my EA is Multicurrency trdaing.

I have wrote this code to close all open positions . but some times the trade.positionclose(ticket,-1)  doesnt work . i wirite print symbol of index i and find that sometimes it print null for index i. 


the backtest log is so :

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =EURUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =AUDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =GBPUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =NZDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =EURUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =AUDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =GBPUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =NZDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =EURUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =AUDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =GBPUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =NZDUSDi




what is my problem . 

Im gratefull for your Help


 
hadi_hahsemi:


Hello

 my EA is Multicurrency trdaing.

I have wrote this code to close all open positions . but some times the trade.positionclose(ticket,-1)  doesnt work . i wirite print symbol of index i and find that sometimes it print null for index i. 


the backtest log is so :

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =EURUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =AUDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =GBPUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =NZDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.18 06:09:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =EURUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =AUDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =GBPUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =NZDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 05:39:00   symbol = = =

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =EURUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =AUDUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =GBPUSDi

2021.04.11 14:12:47.491 Core 1 2020.02.19 06:11:00   symbol = = =NZDUSDi




what is my problem . 

Im gratefull for your Help






You are missing a PositionSelect() or PositionSelectByTicket()

 

Dominik Egert:


You are missing a PositionSelect() or PositionSelectByTicket()

i added   PositionSelectByTicket() but the problem exist yet ; 

void closeall(ulong magic)
{
int total = PositionsTotal();

for  (int i=0 ; i < total ;i++ )
{

ulong ticket = PositionGetTicket(i);
PositionSelectByTicket(ticket);

Print("symbol =" , PositionGetString(POSITION_SYMBOL) );

trade.PositionClose(ticket,-1);  // CTrade trade 


}
}
 
hadi_hahsemi:

i added   PositionSelectByTicket() but the problem exist yet ; 

OK, here is the doc:


https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade

and for the function you are using:

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradepositionclose

For the parameters you are passing in, -1 is a signed long integer and the doc says its an unsigned long integer.


But to resolve your problem, read the note in the doc.

Check the result of your request and try to find the reason for a failed execution there.

Documentation on MQL5: Standard Library / Trade Classes / CTrade
Documentation on MQL5: Standard Library / Trade Classes / CTrade
  • www.mql5.com
CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5