problem with [i]

 

hi

in my EA, I try to print my two variables but it's not working:

**********************************************************

double m_10 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
double m_20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + 0);


for(int i=1; i<500 ; i--)
{
m_10[i] = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + i);
m_20[i] = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + i);
Print(m_10[i]+ " " + m_20[i]);

}

**********************************************************

this is the message error :

'[' assignment expected

Help please!!

Thanks in advance.

 

search forum for "arrays" and learn how to use them. For example: https://book.mql4.com/variables/arrays


you use "[]" on a variable... do you know why you do?


these are concepts you should learn completely otherwise problems will continue to appear all the time.


yes... people can just key in the code and show you but come on - this is basic programming syntax which you need to master.


The mql4 book has so much to offer - why not take advantage of what it can give to you?


Will not regret it !


Best...

;)

 

Hi

I used [] because I don't want to write 500 lines, and I thought it was the good method ( I used it in a indicator but now I'm in a EA) !

I was wrong and I don't know who to do

that's why I try to find another method

thank in advance

 

double m_10 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
double m_20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + 0);

should be changed to:

double m_10[501];

double m_20[501];

 

Hi Phy and thank you

When I replace double m_10 by double m_10[501]

the message error is : '0' variable expected ( for this line : double m_10 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);)

so I do this :

double m_10[500] ;

double m_20[500];

m_10 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
m_20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + 0);


for(int i=1; i<500 ; i--)
{
m_10[i] = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + i);
m_20[i] = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + i);
// Print(m_10[i]+ " " + m_20[i]);

Alert(m_10[i]+ " " + m_20[i]);

}

But the result of the Alert is 0 and O

 

Hi Natsirte,

There are 2 mistakes as far as I can see but without testing it I can't tell, give it a try though.

First replace i-- for i++, since you want your i value to increase not to decrease.

Also keep in mind that an array is used if you want to access an specific position data stored by it, otherwise you can perfectly write it as follows:

for(int i=1; i<500 ; i++)
{
m_10 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + i);
m_20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + i);
Print("m_10 value is: ", m_10, " and m_20 value is: ", m_20);

}

Also, why do you shift it constantly? why Current + i?

Good luck.

 

Hi

great thanks for your help

I'll try it this evening! :-)

 
natsirte:

Hi

great thanks for your help

I'll try it this evening! :-)

 

double m_10[500] ;
double m_20[500];
m_10[0] = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
m_20[0] = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + 0);

for(int i=1; i<500 ; i--)
{
m_10[i] = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, Current + i);
m_20[i] = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, Current + i);
// Print(m_10[i]+ " " + m_20[i]);
Alert(m_10[i]+ " " + m_20[i]);

}

Thanks to everybody for this help!

 

You changed [501] to [500].

Your loop now writes outside the bounds of the array.

Reason: