Invalid array access

 

Hi

 

I am getting an error message - IgnoreArray - invalid array access - on this .mqh file, when trying to compile it. Is it possibly related to the changes that came with v600?I can't seem to find enough info on the eror message to help me work out what the problem is. I am hoping to use this code posted by another member to use for reporting purposes. I can get most of the mqh to work without this component.

 Also - I can't find what "int& IgnoreArray[]"  means -  what does the "&" symbol mean? 

 

I have added the whole file if necessary.

thanks 

 

 

int IdentifyTradesToIgnore(double DataArray[],int& IgnoreArray[])
   {
   int      recentindex,i,candidateindex;
   double   MathAvgOnArray,MathStdDevOnArray;
   
   
   // DataArray - identify outliers beyond +/- 8.5 sigma (99.9% CI based on generalized distribution)
   if(ArrayRange(DataArray,0)<3) 
   return(IgnoreArray); // if there are few members of the data array then return without calculation
   
   MathAvgOnArray=MathAvg(DataArray);
   MathStdDevOnArray=MathStdev(DataArray);
   recentindex=0; // reset index position for IgnoreArray searches
   for(i=0;i<ArrayRange(DataArray,0);i++)
      {
      if(DataArray[i]<(MathAvgOnArray-8.5*MathStdDevOnArray) || DataArray[i]>(MathAvgOnArray+8.5*MathStdDevOnArray))
         {
         // check to see if whether or not trade order "i" was previously identified and logged as an excursion trade
         candidateindex=ArrayBsearch(IgnoreArray,i,WHOLE_ARRAY,recentindex,MODE_ASCEND);
         if(IgnoreArray[candidateindex]==i+1)
            {
            recentindex=candidateindex;
            continue; // return to the next outermost loop because we already know the existing i is a documented excursion trade
            }
         if(IgnoreArray[ArrayRange(IgnoreArray,0)-1]==-1)
            {
            IgnoreArray[0]=i+1;
            recentindex=MathMax(recentindex-1,0);  // back-up the recentindex by 1
            continue;
            }
         ArrayResize(IgnoreArray,ArrayRange(IgnoreArray,0)+1);
         IgnoreArray[ArrayRange(IgnoreArray,0)-1]=i+1;
         ArraySort(IgnoreArray,WHOLE_ARRAY,0,MODE_ASCEND);   // sort the array so the most recently appended trade order number is properly indexed
         recentindex=MathMax(recentindex-1,0);  // back-up the recentindex by 1
         }
      }
   return(IgnoreArray);
   }
 

Arrays are always passed by reference. You have to add "&" for the first array also:

 

int IdentifyTradesToIgnore(double& DataArray[],int& IgnoreArray[])

 

 For explanation and examples see documentation: Passing Parameters 

 
drazen64:

Arrays are always passed by reference. You have to add "&" for the first array also:

 

 

 For explanation and examples see documentation: Passing Parameters 


thanks for that info.

 

unfortunately it doesn't seem to fix the issue though. I still get the same error message. 

 

I downloaded your code, but it can't be compiled because there are additional files included.

Add "&" infront of arrays in all your function definitions. 

Below you try to return an array as function result. It can't be done, and besides that type of function result is int, so you can return only one element of the array: return(IgnoreArray[some_idx]);

 

int IdentifyTradesToIgnore(double DataArray[],int& IgnoreArray[])
   {
   int      recentindex,i,candidateindex;
   double   MathAvgOnArray,MathStdDevOnArray;
   
   
   // DataArray - identify outliers beyond +/- 8.5 sigma (99.9% CI based on generalized distribution)
   if(ArrayRange(DataArray,0)<3) 
   return(IgnoreArray); // if there are few members of the data array then return without calculation
   
   MathAvgOnArray=MathAvg(DataArray);
   MathStdDevOnArray=MathStdev(DataArray);
   recentindex=0; // reset index position for IgnoreArray searches
   for(i=0;i<ArrayRange(DataArray,0);i++)
      {
      if(DataArray[i]<(MathAvgOnArray-8.5*MathStdDevOnArray) || DataArray[i]>(MathAvgOnArray+8.5*MathStdDevOnArray))
         {
         // check to see if whether or not trade order "i" was previously identified and logged as an excursion trade
         candidateindex=ArrayBsearch(IgnoreArray,i,WHOLE_ARRAY,recentindex,MODE_ASCEND);
         if(IgnoreArray[candidateindex]==i+1)
            {
            recentindex=candidateindex;
            continue; // return to the next outermost loop because we already know the existing i is a documented excursion trade
            }
         if(IgnoreArray[ArrayRange(IgnoreArray,0)-1]==-1)
            {
            IgnoreArray[0]=i+1;
            recentindex=MathMax(recentindex-1,0);  // back-up the recentindex by 1
            continue;
            }
         ArrayResize(IgnoreArray,ArrayRange(IgnoreArray,0)+1);
         IgnoreArray[ArrayRange(IgnoreArray,0)-1]=i+1;
         ArraySort(IgnoreArray,WHOLE_ARRAY,0,MODE_ASCEND);   // sort the array so the most recently appended trade order number is properly indexed
         recentindex=MathMax(recentindex-1,0);  // back-up the recentindex by 1
         }
      }
   return(IgnoreArray);
   }

 

There is a lots of code. If you don't have experience with MQL4, your best bet would be to hire someone to do it in freelance section

 
simoncs:


thanks for that info.

 

unfortunately it doesn't seem to fix the issue though. I still get the same error message. 

Make your function type void . . . and read this:  https://www.mql5.com/en/forum/140349
 

I am new to Forex trading and I know next to nothing about MQL4 coding but I would like to get this code working.
I will appreciate your help.

The errors from the MQL4 program are:

1. 'close' - invalid array access

2. 'ticket' - variable already defined.

I have made the lines the errors refer to bold and red for easy identification


Below is the code.

// Expert Advisor start function

void OnTick()

{

    double ema21 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, 0);

    double sma50 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);

    double stopLossPrice = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, 0);


    if (ema21 > sma50 && Close > ema21 && OrdersTotal() == 0) {

        int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 0, Ask - stopLossPrice, Ask + (2 * (Ask - stopLossPrice)), "Buy Order", 0, 0, Green);

        if (ticket > 0) {

            Print("Buy order opened successfully");

        } else {

            Print("Error opening buy order: ", GetLastError());

        }

    } else if (ema21 < sma50 && Close < ema21 && OrdersTotal() == 0) {

        int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 0, Bid + stopLossPrice, Bid - (2 * stopLossPrice), "Sell Order", 0, 0, Red);

        if (ticket > 0) {

            Print("Sell order opened successfully");

        } else {

            Print("Error opening sell order: ", GetLastError());

        }

 
Adenekan Ogungbola #:
  1. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  2. Don't double post! You already had another thread open.

              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

Reason: