Download MetaTrader 5

Help me to Find the Most Efficient/Best Code in Variables Declaration [100+ Variables]

To add comments, please log in or register
Publish your application in Cobe Base. Millions of traders will see it!
Musngi
301
Musngi 2016.11.25 18:49 

Help me to Find the Most Efficient/Best code in Variables Declaration [100+ Variables]

I have more than 100 variables declaration inside start(). Here's my example:

int start()
{

  double a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  double b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  double c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  /*

   1
   . 
   . 
   .

   100
  */

  

return 0;
}

 Should I put all variables in one declaration? Like this:

int start()
{
  double a, b, c; // variables 1 to 100+

  a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);


return 0;
}

 Should I declare it global? To avoid excessive declaration in start()

double a, b, c; // global variables 1 to 100+

int start()
{
  a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  /*

   1
   .
   .
   .

   100
  */

 

return 0;
}


 

Carl Schreiber
6590
Carl Schreiber 2016.11.25 19:46  

What about an array?

double arr[100];
..
int start() {
    arr[0] = iRSI(..);
    arr[1] = iRSI(..);
    ...
}
Alain Verleyen
Moderator
30731
Alain Verleyen 2016.11.25 20:16  
Musngi:

Help me to Find the Most Efficient/Best code in Variables Declaration [100+ Variables]

I have more than 100 variables declaration inside start(). Here's my example:

...
Carl Schreiber:

What about an array?

double arr[100];
..
int start() {
    arr[0] = iRSI(..);
    arr[1] = iRSI(..);
    ...
}

That will change nothing if we are talking about execution speed.

eevviill14
60
eevviill14 2016.11.25 20:36  
Musngi:

Help me to Find the Most Efficient/Best code in Variables Declaration [100+ Variables]

I have more than 100 variables declaration inside start(). Here's my example:

... 

 Should I put all variables in one declaration? Like this:

... 

 Should I declare it global? To avoid excessive declaration in start()

... 


 

 Local declaration will use resources of your processor.

Global declaration will use resources of your Random Access Memory.

whroeder1
14504
whroeder1 2016.11.25 20:42  
Musngi:

Help me to Find the Most Efficient/Best code in Variables Declaration [100+ Variables]

 Should I put all variables in one declaration? Like this:

int start()
{
  double a, b, c; // variables 1 to 100+

  a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);


return 0;
}

 Should I declare it global? To avoid excessive declaration in start()

double a, b, c; // global variables 1 to 100+

int start()
{
  a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  /*

   1
   .
   .
   .

   100
  */

 

return 0;
}
  1. Globally declared if they have to be shared between routines.
  2. Statically declared if they have to be remembered between calls.
  3. Don't declare them and then initialize them - that's bad style. Do both at once
    double  a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
    double  b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
    double  c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  4. How are you going to access them?
    if(i == 1) do_something(a);
    if(i == 2) do_something(b);
    Then you might as well as use arrays.
  5. For efficiency, don't do per tick what you can do per bar. If you're waiting for price to reach a level, remember the level and ignore ticks until you reach it (or you reach a invalid condition or new bar.)
Musngi
301
Musngi 2016.11.26 05:22  
Carl Schreiber:

What about an array?

double arr[100];
..
int start() {
    arr[0] = iRSI(..);
    arr[1] = iRSI(..);
    ...
}

No.

I want to declared different variable names for each currency pairs to make it easy to read/maintain. 

Musngi
301
Musngi 2016.11.26 06:14  
whroeder1:
  1. Globally declared if they have to be shared between routines.
  2. Statically declared if they have to be remembered between calls.
  3. Don't declare them and then initialize them - that's bad style. Do both at once
    double  a = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
    double  b = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
    double  c = iRSI("AUDNZD", Period(), 14, PRICE_CLOSE, i);
  4. How are you going to access them?
    if(i == 1) do_something(a);
    if(i == 2) do_something(b);
    Then you might as well as use arrays.
  5. For efficiency, don't do per tick what you can do per bar. If you're waiting for price to reach a level, remember the level and ignore ticks until you reach it (or you reach a invalid condition or new bar.)

Could you please elaborate more on this? 1-5.

I use the variables for counters to use in some calculation then use it to buffers. I don't use this variables to hold the values for longer time. 

Musngi
301
Musngi 2016.11.27 05:53  
My question still not solve. 
whroeder1
14504
whroeder1 2016.11.27 13:40  
Musngi: My question still not solve. 
Because there is no "Best" to such a vague question.
Every post of yours is vague. Most replies are to read the documentation. In response to the rest, you just want more of the same.
https://www.mql5.com/en/forum/161118
https://www.mql5.com/en/forum/160344
https://www.mql5.com/en/forum/160332
https://www.mql5.com/en/forum/160381
<removed by moderator>
Musngi
301
Musngi 2016.11.27 14:22  
whroeder1:
Because there is no "Best" to such a vague question.
Every post of yours is vague. Most replies are to read the documentation. In response to the rest, you just want more of the same.
https://www.mql5.com/en/forum/161118
https://www.mql5.com/en/forum/160344
https://www.mql5.com/en/forum/160332
https://www.mql5.com/en/forum/160381
I think you a troll. Please do not feed the troll. When you respond, you give the troll power. When you ignore the troll, he starves for attention and eventually dies.
My question is very clear. Do you see I give 3 example code to choose? 
To add comments, please log in or register