Download MetaTrader 5

EA gives "array out of range" error if I place a separate trade

To add comments, please log in or register
aaja
152
aaja  

Hi,

I have an EA made for me by a coder who I've lost contact with.

It works fine until I place a separate trade, whether on the same chart or different chart, whether from another EA or a manual trade, I get the error on the MT4 platform that says: "Array out of range in 'dowgap_2.mq4' (210,27)".

There seems to be some sort of clash between the EA functionality and placing other trades within the MT4 platform, but I'm at a loss. Any ideas?

        }
        
        // Go through orders and check points profit..
        POrder pOrders[];
        int num = pTradeManager.Val.AllTradesAndOrders(Magic,pOrders);
        
        if (!_takeHalfDone && (num > 0))
        {
            // Only expecting one order at posn 0
            POrder pOrder = pOrders[0];
            if (pOrder.Val.Active && (pOrder.Val.PipsProfit() >= TakeHalfPoints))
            {
                // We need to realise a proportion of the profits.
                pOrder.Val.Size = TradeSizeCalculator::NormaliseSize(pOrder.Val.Size * TakeHalfAmount / 100.0);
                
                _log.Write(INFO,"Trade has reached ",LOGLEVEL(TakeHalfPoints)," pnts profit,  closing £",LOGSIZE(pOrder.Val.Size)," of the trade.");
                pTradeManager.Val.CloseTrade( pOrder);
        
                _takeHalfDone = true;
            }
            
            if (_takeHalfDone)
            {
                // The a new trade will have been created after partial close - so we need to fetch it again.
                ArrayFree(pOrders);
            
                num = pTradeManager.Val.AllTradesAndOrders(Magic,pOrders);
                if (num > 0)
                {
                    pOrder = pOrders[0];
                    
                    _log.Write(INFO,"Moving stop of remaining amount to ", LOGLEVEL(TakeHalfStop), " points");
                    pOrder.Val.StopPoints(MathAbs(TakeHalfStop));
                    pTradeManager.Val.AmendTrade(pOrder);
                }
            }
        }
Marco vd Heijden
Moderator
5560
Marco vd Heijden  

"Array out of range in 'dowgap_2.mq4' (210,27)".


Your error is at line 210, position 27.

aaja
152
aaja  
Marco vd Heijden:

"Array out of range in 'dowgap_2.mq4' (210,27)".


Your error is at line 210, position 27.

Line 210 in the MetaEditor is this: POrder pOrder = pOrders[0];

Position 27 is the [0]

What could be wrong/missing there?

honest_knave
Moderator
2326
honest_knave  
aaja:

Line 210 in the MetaEditor is this: POrder pOrder = pOrders[0];

Position 27 is the [0]

What could be wrong/missing there?

You created the array on line 204:

POrder pOrders[];

But it has no size.

So when you try to access the first index with pOrders[0] you are out of range.

This page may help you work with arrays.
Arrays - Variables - MQL4 Tutorial
Arrays - Variables - MQL4 Tutorial
  • book.mql4.com
Arrays - Variables - MQL4 Tutorial
whroeder1
15924
whroeder1  
Don't paste code
Play video
Please edit your post.
For large amounts of code, attach it.
aaja
152
aaja  
honest_knave:

You created the array on line 204:

POrder pOrders[];

But it has no size.

So when you try to access the first index with pOrders[0] you are out of range.

This page may help you work with arrays.
I see. I tried putting a [9] in it's place, not sure if that's the best number for it but it seems to work for now. Thank you, we'll see how it goes.
Marco vd Heijden
Moderator
5560
Marco vd Heijden  

If it gets over 9 it will again cause the out of range error so you either must be sure it does not go over 9 or increase it's size before writing or reading from the non existent location.

So if you want to increase it by one for example:

ArrayResize(pOrders,10,0);
To add comments, please log in or register