Help noob with arrays

To add comments, please log in or register
Robert Browne
127
Robert Browne  

So I'm making my first effort into using arrays. 


I'm simply trying to store close price at certain times to retrieve later in my code. Is this code going to achieve the desired result or have I ballzed up completely.

int OnInit()
  {
   double EU[6] = {iClose("EURUSD",PERIOD_H1,2), iClose("EURUSD",PERIOD_H1,8), iClose("EURUSD",PERIOD_H1,25), iClose("EURUSD",PERIOD_H1,43), iClose("EURUSD",PERIOD_H1,73), iClose("EURUSD",PERIOD_H1,121)};
   double EG[6] = {iClose("EURGBP",PERIOD_H1,2), iClose("EURGBP",PERIOD_H1,8), iClose("EURGBP",PERIOD_H1,25), iClose("EURGBP",PERIOD_H1,43), iClose("EURGBP",PERIOD_H1,73), iClose("EURGBP",PERIOD_H1,121)};
   double EA[6] = {iClose("EURAUD",PERIOD_H1,2), iClose("EURAUD",PERIOD_H1,8), iClose("EURAUD",PERIOD_H1,25), iClose("EURAUD",PERIOD_H1,43), iClose("EURAUD",PERIOD_H1,73), iClose("EURAUD",PERIOD_H1,121)};
   double EN[6] = {iClose("EURNZD",PERIOD_H1,2), iClose("EURNZD",PERIOD_H1,8), iClose("EURNZD",PERIOD_H1,25), iClose("EURNZD",PERIOD_H1,43), iClose("EURNZD",PERIOD_H1,73), iClose("EURNZD",PERIOD_H1,121)};
   double EC[6] = {iClose("EURCAD",PERIOD_H1,2), iClose("EURCAD",PERIOD_H1,8), iClose("EURCAD",PERIOD_H1,25), iClose("EURCAD",PERIOD_H1,43), iClose("EURCAD",PERIOD_H1,73), iClose("EURCAD",PERIOD_H1,121)};
   double EH[6] = {iClose("EURCHF",PERIOD_H1,2), iClose("EURCHF",PERIOD_H1,8), iClose("EURCHF",PERIOD_H1,25), iClose("EURCHF",PERIOD_H1,43), iClose("EURCHF",PERIOD_H1,73), iClose("EURCHF",PERIOD_H1,121)};
   double EJ[6] = {iClose("EURJPY",PERIOD_H1,2), iClose("EURJPY",PERIOD_H1,8), iClose("EURJPY",PERIOD_H1,25), iClose("EURJPY",PERIOD_H1,43), iClose("EURJPY",PERIOD_H1,73), iClose("EURJPY",PERIOD_H1,121)};

   double GU[6] = {iClose("GBPUSD",PERIOD_H1,2), iClose("GBPUSD",PERIOD_H1,8), iClose("GBPUSD",PERIOD_H1,25), iClose("GBPUSD",PERIOD_H1,43), iClose("GBPUSD",PERIOD_H1,73), iClose("GBPUSD",PERIOD_H1,121)};
   double GA[6] = {iClose("GBPAUD",PERIOD_H1,2), iClose("GBPAUD",PERIOD_H1,8), iClose("GBPAUD",PERIOD_H1,25), iClose("GBPAUD",PERIOD_H1,43), iClose("GBPAUD",PERIOD_H1,73), iClose("GBPAUD",PERIOD_H1,121)};
   double GN[6] = {iClose("GBPNZD",PERIOD_H1,2), iClose("GBPNZD",PERIOD_H1,8), iClose("GBPNZD",PERIOD_H1,25), iClose("GBPNZD",PERIOD_H1,43), iClose("GBPNZD",PERIOD_H1,73), iClose("GBPNZD",PERIOD_H1,121)};
   double GC[6] = {iClose("GBPCAD",PERIOD_H1,2), iClose("GBPCAD",PERIOD_H1,8), iClose("GBPCAD",PERIOD_H1,25), iClose("GBPCAD",PERIOD_H1,43), iClose("GBPCAD",PERIOD_H1,73), iClose("GBPCAD",PERIOD_H1,121)};
   double GH[6] = {iClose("GBPCHF",PERIOD_H1,2), iClose("GBPCHF",PERIOD_H1,8), iClose("GBPCHF",PERIOD_H1,25), iClose("GBPCHF",PERIOD_H1,43), iClose("GBPCHF",PERIOD_H1,73), iClose("GBPCHF",PERIOD_H1,121)};
   double GJ[6] = {iClose("GBPJPY",PERIOD_H1,2), iClose("GBPJPY",PERIOD_H1,8), iClose("GBPJPY",PERIOD_H1,25), iClose("GBPJPY",PERIOD_H1,43), iClose("GBPJPY",PERIOD_H1,73), iClose("GBPJPY",PERIOD_H1,121)};
   
   double AU[6] = {iClose("AUDUSD",PERIOD_H1,2), iClose("AUDUSD",PERIOD_H1,8), iClose("AUDUSD",PERIOD_H1,25), iClose("AUDUSD",PERIOD_H1,43), iClose("AUDUSD",PERIOD_H1,73), iClose("AUDUSD",PERIOD_H1,121)};
   double AN[6] = {iClose("AUDNZD",PERIOD_H1,2), iClose("AUDNZD",PERIOD_H1,8), iClose("AUDNZD",PERIOD_H1,25), iClose("AUDNZD",PERIOD_H1,43), iClose("AUDNZD",PERIOD_H1,73), iClose("AUDNZD",PERIOD_H1,121)};
   double AC[6] = {iClose("AUDCAD",PERIOD_H1,2), iClose("AUDCAD",PERIOD_H1,8), iClose("AUDCAD",PERIOD_H1,25), iClose("AUDCAD",PERIOD_H1,43), iClose("AUDCAD",PERIOD_H1,73), iClose("AUDCAD",PERIOD_H1,121)};
   double AH[6] = {iClose("AUDCHF",PERIOD_H1,2), iClose("AUDCHF",PERIOD_H1,8), iClose("AUDCHF",PERIOD_H1,25), iClose("AUDCHF",PERIOD_H1,43), iClose("AUDCHF",PERIOD_H1,73), iClose("AUDCHF",PERIOD_H1,121)};
   double AJ[6] = {iClose("AUDJPY",PERIOD_H1,2), iClose("AUDJPY",PERIOD_H1,8), iClose("AUDJPY",PERIOD_H1,25), iClose("AUDJPY",PERIOD_H1,43), iClose("AUDJPY",PERIOD_H1,73), iClose("AUDJPY",PERIOD_H1,121)};
   
   double NU[6] = {iClose("NZDUSD",PERIOD_H1,2), iClose("NZDUSD",PERIOD_H1,8), iClose("NZDUSD",PERIOD_H1,25), iClose("NZDUSD",PERIOD_H1,43), iClose("NZDUSD",PERIOD_H1,73), iClose("NZDUSD",PERIOD_H1,121)};
   double NC[6] = {iClose("NZDCAD",PERIOD_H1,2), iClose("NZDCAD",PERIOD_H1,8), iClose("NZDCAD",PERIOD_H1,25), iClose("NZDCAD",PERIOD_H1,43), iClose("NZDCAD",PERIOD_H1,73), iClose("NZDCAD",PERIOD_H1,121)};
   double NH[6] = {iClose("NZDCHF",PERIOD_H1,2), iClose("NZDCHF",PERIOD_H1,8), iClose("NZDCHF",PERIOD_H1,25), iClose("NZDCHF",PERIOD_H1,43), iClose("NZDCHF",PERIOD_H1,73), iClose("NZDCHF",PERIOD_H1,121)};
   double NJ[6] = {iClose("NZDJPY",PERIOD_H1,2), iClose("NZDJPY",PERIOD_H1,8), iClose("NZDJPY",PERIOD_H1,25), iClose("NZDJPY",PERIOD_H1,43), iClose("NZDJPY",PERIOD_H1,73), iClose("NZDJPY",PERIOD_H1,121)};
   
   double UC[6] = {iClose("USDCAD",PERIOD_H1,2), iClose("USDCAD",PERIOD_H1,8), iClose("USDCAD",PERIOD_H1,25), iClose("USDCAD",PERIOD_H1,43), iClose("USDCAD",PERIOD_H1,73), iClose("USDCAD",PERIOD_H1,121)};
   double UH[6] = {iClose("USDCHF",PERIOD_H1,2), iClose("USDCHF",PERIOD_H1,8), iClose("USDCHF",PERIOD_H1,25), iClose("USDCHF",PERIOD_H1,43), iClose("USDCHF",PERIOD_H1,73), iClose("USDCHF",PERIOD_H1,121)};
   double UJ[6] = {iClose("USDJPY",PERIOD_H1,2), iClose("USDJPY",PERIOD_H1,8), iClose("USDJPY",PERIOD_H1,25), iClose("USDJPY",PERIOD_H1,43), iClose("USDJPY",PERIOD_H1,73), iClose("USDJPY",PERIOD_H1,121)};
   
   double CH[6] = {iClose("CADCHF",PERIOD_H1,2), iClose("CADCHF",PERIOD_H1,8), iClose("CADCHF",PERIOD_H1,25), iClose("CADCHF",PERIOD_H1,43), iClose("CADCHF",PERIOD_H1,73), iClose("CADCHF",PERIOD_H1,121)};
   double CJ[6] = {iClose("CADJPY",PERIOD_H1,2), iClose("CADJPY",PERIOD_H1,8), iClose("CADJPY",PERIOD_H1,25), iClose("CADJPY",PERIOD_H1,43), iClose("CADJPY",PERIOD_H1,73), iClose("CADJPY",PERIOD_H1,121)};

   double HJ[6] = {iClose("CHFJPY",PERIOD_H1,2), iClose("CHFJPY",PERIOD_H1,8), iClose("CHFJPY",PERIOD_H1,25), iClose("CHFJPY",PERIOD_H1,43), iClose("CHFJPY",PERIOD_H1,73), iClose("CHFJPY",PERIOD_H1,121)};

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
int OnTick()
  {
   static int currenthour;

   int thishour=TimeHour(TimeCurrent());

   if(currenthour!=thishour)
     {
      currenthour=thishour;
      RunOnNewHour(thishour);
     }
//+------------------------------------------------------------------+
void RunOnNewHour(int hour)
  {
   switch(hour)
     {
      case  3:
         ResetSessionArrays(1,13,25,44,73,121);
         break;
      case  8:
         ResetSessionArrays(1,12,25,42,73,121);
         break;
      case 15:
         ResetSessionArrays(1,13,25,44,73,121);
         break;
      case 21:
         ResetSessionArrays(1,14,25,43,73,121);
         break;
     }
   }
//+------------------------------------------------------------------+
void ResetSessionArrays(int a, int b, int c, int d, int e, int f)
  {
   EU[6] = {iClose("EURUSD",PERIOD_H1,a), iClose("EURUSD",PERIOD_H1,b), iClose("EURUSD",PERIOD_H1,c), iClose("EURUSD",PERIOD_H1,d), iClose("EURUSD",PERIOD_H1,e), iClose("EURUSD",PERIOD_H1,f)};
   EG[6] = {iClose("EURGBP",PERIOD_H1,a), iClose("EURGBP",PERIOD_H1,b), iClose("EURGBP",PERIOD_H1,c), iClose("EURGBP",PERIOD_H1,d), iClose("EURGBP",PERIOD_H1,e), iClose("EURGBP",PERIOD_H1,f)};
   EA[6] = {iClose("EURAUD",PERIOD_H1,a), iClose("EURAUD",PERIOD_H1,b), iClose("EURAUD",PERIOD_H1,c), iClose("EURAUD",PERIOD_H1,d), iClose("EURAUD",PERIOD_H1,e), iClose("EURAUD",PERIOD_H1,f)};
   EN[6] = {iClose("EURNZD",PERIOD_H1,a), iClose("EURNZD",PERIOD_H1,b), iClose("EURNZD",PERIOD_H1,c), iClose("EURNZD",PERIOD_H1,d), iClose("EURNZD",PERIOD_H1,e), iClose("EURNZD",PERIOD_H1,f)};
   EC[6] = {iClose("EURCAD",PERIOD_H1,a), iClose("EURCAD",PERIOD_H1,b), iClose("EURCAD",PERIOD_H1,c), iClose("EURCAD",PERIOD_H1,d), iClose("EURCAD",PERIOD_H1,e), iClose("EURCAD",PERIOD_H1,f)};
   EH[6] = {iClose("EURCHF",PERIOD_H1,a), iClose("EURCHF",PERIOD_H1,b), iClose("EURCHF",PERIOD_H1,c), iClose("EURCHF",PERIOD_H1,d), iClose("EURCHF",PERIOD_H1,e), iClose("EURCHF",PERIOD_H1,f)};
   EJ[6] = {iClose("EURJPY",PERIOD_H1,a), iClose("EURJPY",PERIOD_H1,b), iClose("EURJPY",PERIOD_H1,c), iClose("EURJPY",PERIOD_H1,d), iClose("EURJPY",PERIOD_H1,e), iClose("EURJPY",PERIOD_H1,f)};

   GU[6] = {iClose("GBPUSD",PERIOD_H1,a), iClose("GBPUSD",PERIOD_H1,b), iClose("GBPUSD",PERIOD_H1,c), iClose("GBPUSD",PERIOD_H1,d), iClose("GBPUSD",PERIOD_H1,e), iClose("GBPUSD",PERIOD_H1,f)};
   GA[6] = {iClose("GBPAUD",PERIOD_H1,a), iClose("GBPAUD",PERIOD_H1,b), iClose("GBPAUD",PERIOD_H1,c), iClose("GBPAUD",PERIOD_H1,d), iClose("GBPAUD",PERIOD_H1,e), iClose("GBPAUD",PERIOD_H1,f)};
   GN[6] = {iClose("GBPNZD",PERIOD_H1,a), iClose("GBPNZD",PERIOD_H1,b), iClose("GBPNZD",PERIOD_H1,c), iClose("GBPNZD",PERIOD_H1,d), iClose("GBPNZD",PERIOD_H1,e), iClose("GBPNZD",PERIOD_H1,f)};
   GC[6] = {iClose("GBPCAD",PERIOD_H1,a), iClose("GBPCAD",PERIOD_H1,b), iClose("GBPCAD",PERIOD_H1,c), iClose("GBPCAD",PERIOD_H1,d), iClose("GBPCAD",PERIOD_H1,e), iClose("GBPCAD",PERIOD_H1,f)};
   GH[6] = {iClose("GBPCHF",PERIOD_H1,a), iClose("GBPCHF",PERIOD_H1,b), iClose("GBPCHF",PERIOD_H1,c), iClose("GBPCHF",PERIOD_H1,d), iClose("GBPCHF",PERIOD_H1,e), iClose("GBPCHF",PERIOD_H1,f)};
   GJ[6] = {iClose("GBPJPY",PERIOD_H1,a), iClose("GBPJPY",PERIOD_H1,b), iClose("GBPJPY",PERIOD_H1,c), iClose("GBPJPY",PERIOD_H1,d), iClose("GBPJPY",PERIOD_H1,e), iClose("GBPJPY",PERIOD_H1,f)};
   
   AU[6] = {iClose("AUDUSD",PERIOD_H1,a), iClose("AUDUSD",PERIOD_H1,b), iClose("AUDUSD",PERIOD_H1,c), iClose("AUDUSD",PERIOD_H1,d), iClose("AUDUSD",PERIOD_H1,e), iClose("AUDUSD",PERIOD_H1,f)};
   AN[6] = {iClose("AUDNZD",PERIOD_H1,a), iClose("AUDNZD",PERIOD_H1,b), iClose("AUDNZD",PERIOD_H1,c), iClose("AUDNZD",PERIOD_H1,d), iClose("AUDNZD",PERIOD_H1,e), iClose("AUDNZD",PERIOD_H1,f)};
   AC[6] = {iClose("AUDCAD",PERIOD_H1,a), iClose("AUDCAD",PERIOD_H1,b), iClose("AUDCAD",PERIOD_H1,c), iClose("AUDCAD",PERIOD_H1,d), iClose("AUDCAD",PERIOD_H1,e), iClose("AUDCAD",PERIOD_H1,f)};
   AH[6] = {iClose("AUDCHF",PERIOD_H1,a), iClose("AUDCHF",PERIOD_H1,b), iClose("AUDCHF",PERIOD_H1,c), iClose("AUDCHF",PERIOD_H1,d), iClose("AUDCHF",PERIOD_H1,e), iClose("AUDCHF",PERIOD_H1,f)};
   AJ[6] = {iClose("AUDJPY",PERIOD_H1,a), iClose("AUDJPY",PERIOD_H1,b), iClose("AUDJPY",PERIOD_H1,c), iClose("AUDJPY",PERIOD_H1,d), iClose("AUDJPY",PERIOD_H1,e), iClose("AUDJPY",PERIOD_H1,f)};
   
   NU[6] = {iClose("NZDUSD",PERIOD_H1,a), iClose("NZDUSD",PERIOD_H1,b), iClose("NZDUSD",PERIOD_H1,c), iClose("NZDUSD",PERIOD_H1,d), iClose("NZDUSD",PERIOD_H1,e), iClose("NZDUSD",PERIOD_H1,f)};
   NC[6] = {iClose("NZDCAD",PERIOD_H1,a), iClose("NZDCAD",PERIOD_H1,b), iClose("NZDCAD",PERIOD_H1,c), iClose("NZDCAD",PERIOD_H1,d), iClose("NZDCAD",PERIOD_H1,e), iClose("NZDCAD",PERIOD_H1,f)};
   NH[6] = {iClose("NZDCHF",PERIOD_H1,a), iClose("NZDCHF",PERIOD_H1,b), iClose("NZDCHF",PERIOD_H1,c), iClose("NZDCHF",PERIOD_H1,d), iClose("NZDCHF",PERIOD_H1,e), iClose("NZDCHF",PERIOD_H1,f)};
   NJ[6] = {iClose("NZDJPY",PERIOD_H1,a), iClose("NZDJPY",PERIOD_H1,b), iClose("NZDJPY",PERIOD_H1,c), iClose("NZDJPY",PERIOD_H1,d), iClose("NZDJPY",PERIOD_H1,e), iClose("NZDJPY",PERIOD_H1,f)};
   
   UC[6] = {iClose("USDCAD",PERIOD_H1,a), iClose("USDCAD",PERIOD_H1,b), iClose("USDCAD",PERIOD_H1,c), iClose("USDCAD",PERIOD_H1,d), iClose("USDCAD",PERIOD_H1,e), iClose("USDCAD",PERIOD_H1,f)};
   UH[6] = {iClose("USDCHF",PERIOD_H1,a), iClose("USDCHF",PERIOD_H1,b), iClose("USDCHF",PERIOD_H1,c), iClose("USDCHF",PERIOD_H1,d), iClose("USDCHF",PERIOD_H1,e), iClose("USDCHF",PERIOD_H1,f)};
   UJ[6] = {iClose("USDJPY",PERIOD_H1,a), iClose("USDJPY",PERIOD_H1,b), iClose("USDJPY",PERIOD_H1,c), iClose("USDJPY",PERIOD_H1,d), iClose("USDJPY",PERIOD_H1,e), iClose("USDJPY",PERIOD_H1,f)};
   
   CH[6] = {iClose("CADCHF",PERIOD_H1,a), iClose("CADCHF",PERIOD_H1,b), iClose("CADCHF",PERIOD_H1,c), iClose("CADCHF",PERIOD_H1,d), iClose("CADCHF",PERIOD_H1,e), iClose("CADCHF",PERIOD_H1,f)};
   CJ[6] = {iClose("CADJPY",PERIOD_H1,a), iClose("CADJPY",PERIOD_H1,b), iClose("CADJPY",PERIOD_H1,c), iClose("CADJPY",PERIOD_H1,d), iClose("CADJPY",PERIOD_H1,e), iClose("CADJPY",PERIOD_H1,f)};

   HJ[6] = {iClose("CHFJPY",PERIOD_H1,a), iClose("CHFJPY",PERIOD_H1,b), iClose("CHFJPY",PERIOD_H1,c), iClose("CHFJPY",PERIOD_H1,d), iClose("CHFJPY",PERIOD_H1,e), iClose("CHFJPY",PERIOD_H1,f)};
  }
//+------------------------------------------------------------------+
kypa
1079
kypa  

You should probably declare the arrays as global without values and then copy one by one the values in OnInit or OnTick, EG[0] = iClose("EURGBP",PERIOD_H1,2); EG[1] = iClose("EURGBP",PERIOD_H1,8); and so on.

Also things declared in OnInit won't be available in OnTick.

William Roeder
20228
William Roeder  
  1. Arr[i] = aValue; not arr[n] = { value, value, value...};
  2. type Arr[n] = { const1, const2, ...}; or simply type Arr = { const1, const2, ...};  can not use non-constant values to initialize a new array.

  3. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.

Robert Browne
127
Robert Browne  
Thanks all. I'll work on a replacement code and display for comment
nicholi shen
2392
nicholi shen  
Robert Browne:
Thanks all. I'll work on a replacement code and display for comment

DRY :)

To add comments, please log in or register