Two dimensional Array not assigning values !

 

Hi guys,

I have a problem with a 2d array, i have never worked with 2d arrays before, so its kinda  expected. I read a lot, both forum and google, but found nothing . 

I have a 2d array  to store ticket numbers for past orders, the orders are divided by series, this is why i have a second dimension.

 so 

int tkt[][7];

this is how i declared it on global scope, and after that, when an order is sent, i do that :

         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,sl,tp,"Initial Order",base_magic,0,clrGreen);
         if(ticket>0)
         {
         size++;
         ArrayResize(tkt,size);
         hedge_number[base_magic-1]=0;
         distance[base_magic-1]=hedge_distance*point;
         tkt[base_magic-1,0]=ticket;
         base_magic++;
         next[base_magic-1]=1;
        Print("ticket : ",ticket);
        Print("tkt ",tkt[base_magic-1,0]);
         }

i use different magic number for different series of orders, some kind of sorting system. So when i want to use the ticket ,i think i stored, it does nothing(if used in code, because i cannot select orders this way, when it hasnt assigned a value) or prints "0", in the same time when i print the ticket itself, everything is normal. So i guess, i have some problem assigning  values to it. 

I hope you can help me take care of this !

Thanks in advance

 Best Regards,

Stan

 
Stanislav Ivanov:

Hi guys,

I have a problem with a 2d array, i have never worked with 2d arrays before, so its kinda  expected. I read a lot, both forum and google, but found nothing . 

I have a 2d array  to store ticket numbers for past orders, the orders are divided by series, this is why i have a second dimension.

 so 

this is how i declared it on global scope, and after that, when an order is sent, i do that :

i use different magic number for different series of orders, some kind of sorting system. So when i want to use the ticket ,i think i stored, it does nothing(if used in code, because i cannot select orders this way, when it hasnt assigned a value) or prints "0", in the same time when i print the ticket itself, everything is normal. So i guess, i have some problem assigning  values to it. 

I hope you can help me take care of this !

Thanks in advance

 Best Regards,

Stan

What error did you got? I guess that the values in your first [] is empty so you need to specify something there and that might help you. Try it.

 
jaffer wilson:

What error did you got? I guess that the values in your first [] is empty so you need to specify something there and that might help you. Try it.

Thanks
 
Stanislav Ivanov:

Hi guys,

I have a problem with a 2d array, i have never worked with 2d arrays before, so its kinda  expected. I read a lot, both forum and google, but found nothing . 

I have a 2d array  to store ticket numbers for past orders, the orders are divided by series, this is why i have a second dimension.

 so 

this is how i declared it on global scope, and after that, when an order is sent, i do that :

i use different magic number for different series of orders, some kind of sorting system. So when i want to use the ticket ,i think i stored, it does nothing(if used in code, because i cannot select orders this way, when it hasnt assigned a value) or prints "0", in the same time when i print the ticket itself, everything is normal. So i guess, i have some problem assigning  values to it. 

I hope you can help me take care of this !

Thanks in advance

 Best Regards,

Stan

A matrix is the completely wrong data structure for what you are trying to do. Back in the day before meta trader had structs you had to use them as a hacky workaround. Nowadays it is poor form to use a matrix in place of an array of structs. 


First off, if you really do need a matrix (you should never need a matrix unless you are computing linear algebra) then you must define its size and call it correctly. 

int matrix[3][3] = {
   {1,2,3},
   {4,5,6},
   {7,8,9}
};
int num = matrix[1][2];

This is a much better replacement for a matrix. 

struct TicketMagic
{
   int ticket;
   int magic;
};

TicketMagic tm_array[7];

tm_array[i].ticket = OrderTicket();
tm_array[i].magic = OrderMagicNumber();
 
nicholi shen:

A matrix is the completely wrong data structure for what you are trying to do. Back in the day before meta trader had structs you had to use them as a hacky workaround. Nowadays it is poor form to use a matrix in place of an array of structs. 


First off, if you really do need a matrix (you should never need a matrix unless you are computing linear algebra) then you must define its size and call it correctly. 

This is a much better replacement for a matrix. 

What i am trying to achieve is different from what you are showing. The EA has a hedge functions, a trade can have up to 6 hedge trades, they all will have the same magic number(different series of trades will have different magic numbers). I want to store tickets for the magic number, but also for the current trade of the line.

Example :

1 st(initial) trade is 0 index in the second dimension

2nd trade/ 1st hedge is index 1 in the second dimension

and so on to index 6, total dimension size is 7(0 is initial order, 1-6 hedge)

the first dimension is the magic of the series.

So i want to access ticket not only by magic , but by the order number in the series !

 
Stanislav Ivanov:

What i am trying to achieve is different from what you are showing. The EA has a hedge functions, a trade can have up to 6 hedge trades, they all will have the same magic number(different series of trades will have different magic numbers). I want to store tickets for the magic number, but also for the current trade of the line.

Example :

1 st(initial) trade is 0 index in the second dimension

2nd trade/ 1st hedge is index 1 in the second dimension

and so on to index 6, total dimension size is 7(0 is initial order, 1-6 hedge)

the first dimension is the magic of the series.

So i want to access ticket not only by magic , but by the order number in the series !

Yes, that's how structs work. My example was to be taken contextually not literally. Perhaps this is a better example?

struct TicketMagic{ int ticket, magic; string name;};
void OnStart()
{
   TicketMagic tm_array[] = {
      {1234, 4321, "order 1"},
      {2345, 5432, "order 2"},
      {3456, 6543, "order 3"}
   };
   
   for(int i=0; i<ArraySize(tm_array); i++)
      printf("%s: ticket=%d, magic=%d", 
         tm_array[i].name,
         tm_array[i].ticket, 
         tm_array[i].magic
      );
}


//FOO EURUSD,H1: order 1: ticket=1234, magic=4321
//FOO EURUSD,H1: order 2: ticket=2345, magic=5432
//FOO EURUSD,H1: order 3: ticket=3456, magic=6543
 
nicholi shen:

Yes, that's how structs work. My example was to be taken contextually not literally. Perhaps this is a better example?

yes, thanks 

 
nicholi shen:

Yes, that's how structs work. My example was to be taken contextually not literally. Perhaps this is a better example?



Hi Nicholi, thank you very much for your suggested solution that you gave here. I had more or less the same kind of request than what Stanislav stated above.  Your solution worked like a charm in my situation as well.  You also introduced me to "structures", something I did not even knew existed.

I found out as I researched "structures" further that they have "construtors" and "destructors".   So, because I store information pertaining to my orders and how different orders relate to one another, in the "struct" I use, it is important that my struct variable (hope its the right description) and especially its contents does not get destroyed/ deleted/ re-initialized while the EA is running.

So my question is actually:  What happens with the information saved in my struct  went the following happens:

  1. I change time frame of the chart on which my EA is running,
  2. I open the "properties" window of my EA and close the "properties" box without making any changes,
  3. I open the "properties" window of my EA, changes some of the "Inputs" and close the "properties" box,
  4. If my MT4 platform loses its connection to the broker's server for a while and then re-connects.

So, I just want to know what can happen that will cause my array struct to loose the values that are stored in it when the above happens while the EA is running.

Thank you,

Christoff

 
mor707:

Hi Nicholi, thank you very much for your suggested solution that you gave here. I had more or less the same kind of request than what Stanislav stated above.  Your solution worked like a charm in my situation as well.  You also introduced me to "structures", something I did not even knew existed.

I found out as I researched "structures" further that they have "construtors" and "destructors".   So, because I store information pertaining to my orders and how different orders relate to one another, in the "struct" I use, it is important that my struct variable (hope its the right description) and especially its contents does not get destroyed/ deleted/ re-initialized while the EA is running.

So my question is actually:  What happens with the information saved in my struct  went the following happens:

  1. I change time frame of the chart on which my EA is running,
  2. I open the "properties" window of my EA and close the "properties" box without making any changes,
  3. I open the "properties" window of my EA, changes some of the "Inputs" and close the "properties" box,
  4. If my MT4 platform loses its connection to the broker's server for a while and then re-connects.

So, I just want to know what can happen that will cause my array struct to loose the values that are stored in it when the above happens while the EA is running.

Thank you,

Christoff

On points 1 and 3 the EA will reinitialize and the info will be lost unless you save it as Global Variables. On pt 2 nothing will happen, and regarding pt4 i think he EA wont be reinitialized, therefore changes wont be made

 
mor707:

Hi Nicholi, thank you very much for your suggested solution that you gave here. I had more or less the same kind of request than what Stanislav stated above.  Your solution worked like a charm in my situation as well.  You also introduced me to "structures", something I did not even knew existed.

I found out as I researched "structures" further that they have "construtors" and "destructors".   So, because I store information pertaining to my orders and how different orders relate to one another, in the "struct" I use, it is important that my struct variable (hope its the right description) and especially its contents does not get destroyed/ deleted/ re-initialized while the EA is running.

So my question is actually:  What happens with the information saved in my struct  went the following happens:

  1. I change time frame of the chart on which my EA is running,
  2. I open the "properties" window of my EA and close the "properties" box without making any changes,
  3. I open the "properties" window of my EA, changes some of the "Inputs" and close the "properties" box,
  4. If my MT4 platform loses its connection to the broker's server for a while and then re-connects.

So, I just want to know what can happen that will cause my array struct to loose the values that are stored in it when the above happens while the EA is running.

Thank you,

Christoff

Any time your EA is removed due to any condition, including but not limited to re-initialization, all data structures are also destroyed. The only way to prevent this is to use an object and implement logic to serialize its data and save it to a file so when the EA comes back up it will be able to load in the previously saved state.   

 
nicholi shen:

The only way to prevent this is to use an object and implement logic to serialize its data and save it to a file

While this is indeed the proper way there is another - saving into a chart object. It can keep data through timeframe change and terminal (and PC) restart, it will save some disk read and write compared to file write, but it will lose data if terminal is not properly exited.

Reason: