MQL4 Learning - page 38

 
 

can someone check this code, its something wrong on some part..

when the ea sell and get profit,its keep going on and on non stop sell, same with buy too..what wrong with this code?..can someone fix it..

 
Files:
automan.mq4  10 kb
 

The error code I found is 4051

 

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);

}

 

case 4051: error_string="invalid function parameter value"

Something might not be defined right.

 

solution

I know why

It is because you use the loop on orders the following way

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

Then think about what that does. every time you close an order the OrdersTotal() decreases and on the other side i increases. So no surprise that every time half of the orders only are closed as the interval is reduced on each extremity for every iteration.

the correct loop is

for(int i=OrdersTotal()-1; i>=0;i--)

 
jlpi:
I know why

It is because you use the loop on orders the following way

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

Then think about what that does. every time you close an order the OrdersTotal() decreases and on the other side i increases. So no surprise that every time half of the orders only are closed as the interval is reduced on each extremity for every iteration.

the correct loop is

for(int i=OrdersTotal()-1; i>=0;i--)

Thank you very very much!

yours should work!

I will try now.

 

After I modify the for loop, the previous broblems were solved out. But now, if I try to close 18 positions altogether, only a few positions could be closed at the first price change. For the other positions, they cannot be closed because of Error 129. Error 129 is Invalid price. As you know, my setting of close price is just simply by using Bid or Ask and also the slippageas big as 10. Therefore, it should work. Could someone tell me where I got wrong?

 
ys16:
After I modify the for loop, the previous broblems were solved out. But now, if I try to close 18 positions altogether, only a few positions could be closed at the first price change. For the other positions, they cannot be closed because of Error 129. Error 129 is Invalid price. As you know, my setting of close price is just simply by using Bid or Ask and also the slippageas big as 10. Therefore, it should work. Could someone tell me where I got wrong?

You shoud use RefreshRates() inside your loop, or, even better, use OrderClosePrice() instead of ASK or Bid

Reason: