Populating arrays

can anyone help me with a coding problem I want to populate an array using a for loop but I’m just not seeing it

For instance say I have an array:

my_array [20][4]

and I have 20 variables named a through t
how would I put them into the elements of the third column using a for loop ?

Hi,

Haven’t tested this but something along the lines of :-


string my_array [20][4];

for (int i = 0; i<20;i++){
   my_array[i][0] = CharToStr(i + 97);
}

Also not quite sure what you mean by third column?

Hi kenny, well when I put data in an array I imagine it as a grid so I can remember which elements contain which data, so I think of my_array[20][4] as a grid of 20 rows and 4 columns

so in other words i wanted to put my 20 variables into:
my_array[0,2]
my_array[1,2]
my_array[2,2]
my_array[3,2]
my_array[4,2] etc

Hi SDC,

The above code should load the letters a through t into the array for you.

nice one, thanks kenny im using that array because I’m trying to modify a function from coders guru to monitor multiple lines to see of they crossed instead of just the two he wrote it for but i was looking at it some more and his code goes like this:
call to the function is: int isCrossed = Crossed (shortEma,longEma);

int Crossed (double line1 , double line2)

{
static int last_direction = 0;
static int current_direction = 0;

if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}

I was thinking, as he initializes current_direction and last_direction to 0 would that not give false results the first time it is run ?

for instance if the new direction is up, current_direction=1
so in the next line:

if(current_direction !=last_direction) //changed

would it not therefore indicate the lines crossed the first time it is run even if they didnt as last_direction would still be at zero from when it was initialized ?

Hi SDC,

Had to twist my mind around this but I think I have the picture.

You are quite correct…now the challenge is to fix the problem:D

How about this ? I think the extra line would cause a value of 3 to be returned to the function call if the last direction was still at zero so it would not open a buy or a sell order unless the return was a 1 or a 2
The second time around the fucnction would behave normaly and change last direction to a 1 or a 2 if the lines crossed.

int Crossed (double line1 , double line2)

{
static int last_direction = 0;
static int current_direction = 0;

if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(last_direction = 0) current_direction = 3; // fix for invalid first iteration
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}

Edit: actually that wouldnt work either because then the second time around it would be comparing the current_direction with last_direction at 3 so it would see that as a line cross because they are not equal hmmm this is not an easy problem to solve

Hi SDC,

I think that will still give you a false as you will simply fall foul of the 3 != last_direction…but try this…

if(last_direction = 0) {
   last_direction = current_direction;
   return(0)
}

yep that would do it !!
I wonder how many people have used that function in their EA’s without realizing that error is in it, the function was from that Coders Guru tutorial on the mql4.com site, its dated 2005 !!

SDC, when I am just comparing two MA’s at a time, I use the iMACD function and put my MA values in that. It saves space and is easier to manage for me.

If the MACD is a positive value, the fast MA is above the slow. If it is a negative value, the fast MA is below. If it went from neg to positive or vice versa, it crossed.

LethalXxXDose

i ended up doing it a different way because i decided i need to work with multiple lines so I did it by using an array to hold all the values then comparing the relevent array elements