Download MetaTrader 5

"Array out of range" problem

To add comments, please log in or register
Leo 2016.07.18 06:02 


I am developing a very simple EA which retrieves the open positions every time it starts (on the OnInit() event handler) .

It should, supposedly, evaluate positions on every tick (on the OnTick() event handler ) to decide if the position must be closed.

By now, I realized it is better to have the opportunity of closing manually the  position(currently my EA opens just one position at once).

So I realized that by inserting an "extern" boolean variable I could solve this problem : whenever I want to close, without waiting for the EA

decision, I go to the EA properties and set the variable to true ....

But whenever I do, I see the EA restarts ....this is no problem , the real problem is that , in the array of open positions (i't a static array

of custom structures. with just one element , made like this :


 struct TypePosition{

    int      Type;

   int      Ticket;   

   double   TakeProfit;

   double   StopLoss;

   double   Price;

   datetime OpenTime;

   double   Lots;


TypePosition Position [NUMMAXPOS];


where NUMMAXPOS  is a #define  and it's =1 )


on the OnTick() event handler, when I check that the extern flag for closing manually is true, and try to launch the OrderClose () function on the

Position[ NUMMAXPOS - NumCurrentPos]     (where NumCurrentPos is 1, as I verified by printing ,  and NUMMAXPOS is 1 as specified)

I get a "Array out of range" error....  But Position[ NUMMAXPOS - NumCurrentPos]  , that is, Position[ 0]  is actually existing, and even if there's

some error or mistaken variables in the OrderClose () call, I would expect a totally different kind of error  .....

This really baffles me , does anyone know why this happens ?

I see on the OnTick() call right before the error , the value of NumCurrentPosition is what I expect to be (1) , so closing Position[0] without problems, but 

instead it doesn' .

Could it be a flaw in MQL4 which is solved by a special fix I don't know about ? 

whroeder1 2016.07.18 14:00  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. It's not "a flaw in MQL4" It's your broken code, which you don't post. There are no mind reader's here, but I'd guess you change NumCurrentPos somewhere.
To add comments, please log in or register