Filling a 2D Array

 

I am trying to generate a 2D array of high, low, open, close prices and I am totally lost as to how to fill the array.

double   PriceArray[1000][4];

int init()
  {
   {
   for(int i=1;i<1000;i++)
   PriceArray[i][0] = iHigh(NULL,240,i);
   PriceArray[i][1] = iLow(NULL,240,i);
   PriceArray[i][2] = iOpen(NULL,240,i);
   PriceArray[i][3] = iClose(NULL,240,i);
   } 
   Print(PriceArray[1][0]);
   Print(PriceArray[1][1]);
   Print(PriceArray[1][2]);
   Print(PriceArray[1][3]);

 return(0);
  }

This doesn't look a great way to fill the array. Also for some reason the last print value is zero (i.e the iClose value for the first candle is zero - the first 3 values are correct.).

Is there a better way to do this?

thanks

 
sd59:

I am trying to generate a 2D array of high, low, open, close prices and I am totally lost as to how to fill the array.

This doesn't look a great way to fill the array. Also for some reason the last print value is zero (i.e the iClose value for the first candle is zero - the first 3 values are correct.).

Is there a better way to do this?

thanks


I sorted the zero value problem.

Would still be grateful for comments on whether I have used best approach to create the 2D array.

thanks

 
Why do you want to fill the array one time? Why aren't you just using ACR
int      tfPeriods[]={  0, PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30,
                           PERIOD_H1, PERIOD_H4, PERIOD_D1,  PERIOD_W1     };
string   tfAsText[]={   "n/a",   "M1",      "M5",      "M15",      "M30",
                                 "H1",      "H4",      "D1",       "W1"    };
   #define ACR_TIME     0  // Array Copy Rates
   #define ACR_OPEN     1
   #define ACR_LOW      2
   #define ACR_HIGH     3
   #define ACR_CLOSE    4
// #define ACR_VOLUME   5
      #define ACR_COUNT    6
int      MyArrayCopyRates(double& acr[][ACR_COUNT], string s="", int p=0){
   if(s == "") s=Symbol();
   if(p == 0)  p=Period();
   for(int iTf = 1; p > tfPeriods[iTf]; iTf++){}
   for(int res= -1; res <= 0;){
      //{https://www.mql5.com/en/forum/129734/page2 says ArrayCopyRates is a nonreent-
      // rant, non-thread-safe call. Therefor you must put a mutex around the
      //}call in case of multiple EAs on multiple charts.
      GetTradeContext();
         res = ArrayCopyRates(acr, s, p);
         int gle   = GetLastError();
      RelTradeContext();
      if(res > 0) break;
      if (gle != ERR_HISTORY_WILL_UPDATED){
         DisableTrading("No "+s+"/"+tfAsText[iTf]+" history: " + gle);
         return(res);                                                         }
      Comment("Updating history for "+s+"/"+tfAsText[iTf]+" chart");
        LogMe("Updating history for "+s+"/"+tfAsText[iTf]+" chart");
      Sleep(15000);  SetNeedToRefresh();
   }  // for
   if(acr[0][ACR_TIME] != 0.) return(res);                  // Verify data
   DisableTrading( "No "+s+"/"+tfAsText[iTf]+" history: " + GetLastError() );
   return(0);
}
:
double   m1_acr[][ACR_COUNT];
void     OninitMasar(){ MyArrayCopyRates(m1_acr, market_pair, PERIOD_M1);  }
:
   lastLE   [iMasar] = m1_acr[iLEc][ACR_TIME];
 

Not sure I understand your first question - why fill it more than once?

You ask a good question about using ACR function and now that you have brought it to my attention I will use it, thanks.

What do these functions do in your code?: GetTradeContext(); & RelTradeContext();

 
sd59:

Not sure I understand your first question - why fill it more than once?

What do these functions do in your code?: GetTradeContext(); & RelTradeContext();

  1. after 4 hours your array is outdated when a new H4 bar forms. You're not expecting your array to update itself like ACR does.
  2. Mutex like the comment says. From my code
 

Yes, my code above was very basic because i was struggling to find decent examples of how to fill a 2D array in a loop. i understand about updating with new candles.

I am not a programmer and have no idea what a 'mutex' is!

thanks for your help anyway.

 
sd59: I am not a programmer and have no idea what a 'mutex' is!
Do you know what Google is? mutex
 
err.yeh still didn't help
 
sd59:
err.yeh still didn't help

It just means coding to prevent one process changing data values another related process requires not to be changed in order to succeed.. or something like that, lol

 
sd59:
err.yeh still didn't help

From the wiki article . . .

In computer science, mutual exclusion refers to the requirement of ensuring that no two processes or threads (henceforth referred to only as processes) are in their critical section at the same time. Here, a critical section refers to a period of time when the process accesses a shared resource, such as shared memory.

 
RaptorUK:

From the wiki article . . .


thanks