MQL4 - automated forex trading   /  

Forum

problem with [i]

Back to topics list To post a new topic, please log in or register

avatar
34
natsirte 2008.06.08 22:27 

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.

article

Comfortable Scalping

The article describes the method of creating a tool for comfortable scalping. However, such an approach to trade opening can be applied in any trading.


avatar
396
ukt 2008.06.08 22:53 

search forum for "arrays" and learn how to use them. For example: http://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...

;)




avatar
34
natsirte 2008.06.08 23:34 

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


avatar
2462
phy 2008.06.09 01:24 

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];


avatar
34
natsirte 2008.06.09 03:05 

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


avatar
37
forexfordinner 2008.06.09 06:50 

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.


avatar
34
natsirte 2008.06.09 16:35 

Hi

great thanks for your help

I'll try it this evening! :-)


avatar
34
natsirte 2008.06.09 22:59 
natsirte wrote:

Hi

great thanks for your help

I'll try it this evening! :-)


avatar
34
natsirte 2008.06.09 23:01 

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!


avatar
2462
phy 2008.06.10 19:40 

You changed [501] to [500].

Your loop now writes outside the bounds of the array.

Back to topics list  

To add comments, please log in or register