a problem on my EA.

 

Dear experts,


I find a problem on my simple EA, which is when i want to close all positions in my account, the positions were not able to be closed at first price change. For example, if I have 10 positions, once I turn on my EA, the price changed from 1.9864 to 1.9865(1st run cycle),, only 5positions were closed. Then when price changed from 1.9865 to 1.9866(2nd run cycle), there were 3 positions closed. Finally, when the price changed from 1.9866 to 1.9865(3rd run cycle), the last two position were closed. Therefore, my 10 positions have not been closed at the same time.


After detailed debug, I knew that, in the first run, the for loop only was executed 4 loops although OrdersTotal()=10 here. And in the second cycle, there were only 3 loops were executed, although OrdersTotal()=5. in the last cycle, full loops were executed, which was 2 loops.


However, according to the MQL4 tutorial, the start function should be executed line by line and only when MetaTrader find the return keyword it will be ready to call start() function again with the most new quotation. That mean, the EA should close my all positions within one cycle, although, the close price may change because of the latest bid and ask price.


the following is my code, please spend a little time to read. and find out the reason of the stupid error of my EA.


Many thanks in advance!


int start()
{
int ticket=0;
double volume=0;
bool close=true;

Print ("we can account the number of times the price changes needed to close 10 positions");

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

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if ( OrderType()==0)
{
ticket=OrderTicket();
volume=OrderLots();
close=OrderClose(ticket,volume,Bid,10,CLR_NONE);
Print ("position order is#",i);
if (close==false)
{
Print ("OrderClose failed with error1#",GetLastError());
}

}
else if ( OrderType()==1)
{
ticket=OrderTicket();
volume=OrderLots();
close=OrderClose(ticket,volume,Ask, 10,CLR_NONE);
Print ("position order is#",i);
if (close==false)
{
Print ("OrderClose failed with error2#",GetLastError());
}
}

else if ( OrderType()>=2)
{
ticket=OrderTicket();
close=OrderDelete(ticket);
Print ("position order is#",i);
if (close==false)
{
Print ("OrderDelete failed with error3#",GetLastError());
}
}

Print ("this is to test how many loops, the EA run", i);

}
return(0);
}

 

Also I found the error code is 4051.

Any helps? Thanks!

 

ERR_INVALID_FUNCTION_PARAMVALUE

4051Invalid function parameter value.

Check your syntaxis again....

 
And also the error 129. But the price setting should be correct. Could you help me to find out?
Reason: