Download MetaTrader 5
To add comments, please log in or register
Follow the news. View our company's official channel!
mar
1145
mar 2013.12.16 15:06 

Hello Foum,

I use different timeframes in an indicator to make some calculations. At the moment I use all timeframes by creating two arrays like these:

string Timeframe[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1"};
int TimeframeNo[] = {PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1};

But now I would like to use extern variables to determine which timeframes should be used like this:

bool M1  = true;
bool M5  = true;
bool M15 = true;
bool M30 = true;
bool H1  = true;
bool H4  = true;
bool D1  = true;
bool W1  = true;
But how can I create an array with external variables??
Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2013.12.16 15:11  
mar:

Hello Foum,

I use different timeframes in an indicator to make some calculations. At the moment I use all timeframes by creating two arrays like these:

But now I would like to use extern variables to determine which timeframes should be used like this:

But how can I create an array with external variables??
Populate the arrays dependant on the settings of the extern variables . . .
mar
1145
mar 2013.12.16 15:17  
But how? I only know how to populate an array the way I did it in the example. But when the timeframes are optional, I have to check with if (M1==true) .... if (W1==true) and everytime it is true, the variable must be added to the array, am I right? But how can I add something to an array?
Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2013.12.16 15:22  
mar:
But how? I only know how to populate an array the way I did it in the example. But when the timeframes are optional, I have to check with if (M1==true) .... if (W1==true) and everytime it is true, the variable must be added to the array, am I right? But how can I add something to an array?

Yes, correct.

As an example . . .

string Timeframe[];



if(M1)
   {
   ArrayResize(TimeFrame, ArraySize(TimeFrame) + 1)
   TimeFrame(ArraySize(TimeFrame) - 1) = "M1";
   }
   
if(M5)
   {
   ArrayResize(TimeFrame, ArraySize(TimeFrame) + 1)
   TimeFrame(ArraySize(TimeFrame) - 1) = "M5";
   }
   
// etc.   
mar
1145
mar 2013.12.16 15:28  
Great! Thanks, Raptor!!
mar
1145
mar 2013.12.16 15:36  
A last question... I just read about the function of init() and deinit(). Can I populate the array in the init()?
Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2013.12.16 15:52  
mar:
A last question... I just read about the function of init() and deinit(). Can I populate the array in the init()?
Yes.
mar
1145
mar 2013.12.16 15:53  

I did this in the init():

if (M1)
   {
      ArrayResize(Timeframe, ArraySize(Timeframe)+1);
      Timeframe(ArraySize(Timeframe)-1)="M1";
      ArrayResize(TimeframeNo, ArraySize(TimeframeNo)+1);
      TimeframeNo(ArraySize(TimeframeNo)-1)=PERIOD_M1;
   }

And string Timeframe[]; is defined as global. Same with int TimeframeNo[];


Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2013.12.16 15:59  
mar:

I did this in the init():

And string Timeframe[]; is defined as global. Same with int TimeframeNo[];


Typo in my code . . . it was meant as an example, not for you to copy & paste . . .

if(M1)
   {
   ArrayResize(TimeFrame, ArraySize(TimeFrame) + 1)
   TimeFrame[ArraySize(TimeFrame) - 1] = "M1";
   }
   
if(M5)
   {
   ArrayResize(TimeFrame, ArraySize(TimeFrame) + 1)
   TimeFrame[ArraySize(TimeFrame) - 1] = "M5";
   }



mar
1145
mar 2013.12.16 16:09  
Yes, I see... :)
/
To add comments, please log in or register