• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 10:47am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 10:47am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

MQL4 Array - Problem 2 replies

Saving array data to file ? 4 replies

Two Dimensional Array vs One Dimensional Array 13 replies

Resizing the 2nd dimension of an array 0 replies

Calculate Stochastic Formula on a array (non indicator buffer array) 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: How to get 2-dimension array to file and than get back it [MQL4]
Cancel

How to get 2-dimension array to file and than get back it [MQL4]

  • Post #1
  • Quote
  • First Post: Nov 3, 2011 9:01pm Nov 3, 2011 9:01pm
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
Hi!
could somebody help me and explain how can i get 2-dimension array (i.e. double array[x][y] where x=100, y=30) to inside a file and than how can i get back the numbers from this file to an array in otehr EA? 1-dimension is much easier!!!
I found this article: http://articles.mql4.com/493 but i don't know how can I read the file with numbers from this file in my secound EA...
Additionally i'd like to save big precision of my numbers and i was pondering to use FileWriteDouble() funcion (in binary).
Could somebody help me - i appreciate it a lot!
thank You!
  • Post #2
  • Quote
  • Nov 3, 2011 10:19pm Nov 3, 2011 10:19pm
  •  CodeMeister
  • Joined Sep 2009 | Status: Making Code While Making Pips | 1,672 Posts
I doubt if I or many other on this forum could improve on the code in the linked article. I didn't review it in depth, so there may be a small detail I missed. But as a starting point, it is good enough.

I don't think you have a very good understanding of numbers and how computer's handle numbers. In MT4, the most precision that you need is 5 decimal places. The MQL4 double variable type has 16 decimal digits of precision, the same as most other modern language double types. In any case, the best format for storing and exchanging data is CSV and that is a string representation of the number. Be sure not to truncate any digits and you will be alright.
 
 
  • Post #3
  • Quote
  • Nov 4, 2011 6:31am Nov 4, 2011 6:31am
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
Is the array in MT4 to start with?

If so it would be easy with FileWriteArray( .... and then FileReadArray(
If you think I'm mad, I must be mad
 
 
  • Post #4
  • Quote
  • Nov 4, 2011 7:35am Nov 4, 2011 7:35am
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
Quoting futurespec
Disliked
Is the array in MT4 to start with?

If so it would be easy with FileWriteArray( .... and then FileReadArray(
Ignored
I'm not sure what u mean in first question. :/ The array is created in one EA in MQL4/MT4 and i want to use it in another EA.

Yep, i was thinking about FileWriteArray but its only for 1-dimension array. And i need save variable from 2-dimension array.
And question is how can i do this quite easy?

thanks a lot for answers!
 
 
  • Post #5
  • Quote
  • Nov 4, 2011 7:48am Nov 4, 2011 7:48am
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
something like......

mTradesHist = "YourFileName.bin";

int mHist = FileOpen(mTradesHist, FILE_BIN|FILE_WRITE);

if(mHist > 0)
{
FileWriteArray(mHist, mTrade, 0, 3000);
FileClose(mHist);
}

Where mTradesHist is the filename of the file to save.

Then....

int mHist = FileOpen(mTradesHist, FILE_BIN|FILE_READ);
if(mHist > 0)
{
int j = FileReadArray(mHist, mTrade, 0, 3000);
FileClose(mHist);
}


(Assuming a 100 * 30 2 dim array)
mTrade being name of your array.
If you think I'm mad, I must be mad
 
 
  • Post #6
  • Quote
  • Nov 4, 2011 9:03am Nov 4, 2011 9:03am
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
ok, thanks a lot!!!
if i understand correct if i have array like:
double array[100][30]
i have to do next array like:
array_one_dim[3000]
copy from first array to next like this:

for(i=0;i<100;i++)
for(j=0;j<30;j++)
array_one_dim[i*j] = array[i][j]


and than i use your code.

Is it correct?

thank You very much!
 
 
  • Post #7
  • Quote
  • Nov 4, 2011 9:07am Nov 4, 2011 9:07am
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
Could do that or else put it into a 2 dim array again.

As I understand it it reads/writes in sequence the number of elements that you ask for (3000) so could be [3000] or [100][30] or [50][60] etc

HTH
If you think I'm mad, I must be mad
 
 
  • Post #8
  • Quote
  • Nov 4, 2011 9:41am Nov 4, 2011 9:41am
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
ohh, again i'm not sure what you exactly mean - sorry!
the problem is:
i want to do an indicator where i do an array in 2 dim. Its important - i need 2 dim because its much more easier.
Next, i want to save this array in a file because i want to use it in other program (EA).
It's very important for me to my EA read this variables from file and save it to exacly the same array what was in idicator - it mean in 2 dim.
the way of process is something like this:
creat 2 dim array -> save it to file -> read this from next EA -> use it in the same sequence

It mean if i have array in indicator like array[3][4] i want have the same array in my EA -> array[3][4]

and thats way i have meny problems with that ;(
 
 
  • Post #9
  • Quote
  • Nov 4, 2011 9:48am Nov 4, 2011 9:48am
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
Ok, arrays are not my strong point but....

write the array[3][4] to save 12 elements.
next in new EA read the array into your newarray[3][4]

element 1 goes to new array[0][0] (arrays count from '0')
element 2 goes to new array[0][1]
element 3 goes to new array[0][2]
element 4 goes to new array[1][0]
element 5 goes to new array[1][1] etc, etc

Your new array could be different size but you would need to be extremely careful what elements you put where.

does that help?
If you think I'm mad, I must be mad
 
 
  • Post #10
  • Quote
  • Nov 4, 2011 10:24am Nov 4, 2011 10:24am
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
Yes, You do
Thank You very much. I think i will be able to do that now
i will check it when i come back to home but i'm going to do a function what will change 2 dim array to 1 dim and the other way (from 1 to 2 dim). I think it will be not so hard to do and i will keep the same size in two EA.
It will be something like that:
array[3][4] -> array[12] -> save it to a file -> in next EA read the variable form the file -> save it to array[12] -> change to array[3][4]
 
 
  • Post #11
  • Quote
  • Nov 4, 2011 10:29am Nov 4, 2011 10:29am
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
Ok, NP.

Do not follow the logic of why you would want to do that but if it works for you then that is great.

If you are on Skype and need more help then if I can would be happy to try. Just PM me your Skype
If you think I'm mad, I must be mad
 
 
  • Post #12
  • Quote
  • Nov 4, 2011 12:22pm Nov 4, 2011 12:22pm
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
thank You very much my friend
I will try do everythink alone now and i will send u answer what will i do exactly and if it works!
 
 
  • Post #13
  • Quote
  • Nov 4, 2011 3:46pm Nov 4, 2011 3:46pm
  •  michx
  • | Joined Jul 2010 | Status: Member | 23 Posts
Oky dokY,
everything is very O.K!
Thank You very much!!!

if somebody will have a similar problem he can go the same way what i did .
I solved my problem (based on Yours suggest) by wrote 2 functions:
Inserted Code
         int save_to_file(double array[][], int x, int y, string file_name /*.bin*/)
            {     
            int i,j;
            double array_copy[];
            ArrayResize(array_copy,x*y);
         
            for(i=0;i<x;i++)
               for(j=0;j<y;j++)
                  array_copy[i*y+j] = array[i][j];

            int handle = FileOpen(file_name, FILE_BIN | FILE_WRITE);
               if(handle<1)
                  {
                  Print("Nie można otworzyć pliku file_name, błąd otwarcia pliku = ",
                  GetLastError());
                  return(-1);
                  }
               else
                  {
                  FileWriteArray(handle, array_copy, 0, x*y);               
                  }
            FileClose(handle);
         
            return(handle);    
            }
         
         
         
         int load_from_file(double &array[][], int x, int y, string file_name /*.bin*/)
            {       
            int i,j;
            double array_copy[];
            ArrayResize(array_copy,x*y);
            
            int handle = FileOpen(file_name, FILE_BIN | FILE_READ);
            if(handle<1)
               {
               Print("Nie można otworzyć pliku file_name, błąd otwarcia pliku = ",
               GetLastError());
               return(-1);
               }
            else
               {
               FileReadArray(handle, array_copy, 0, x*y);
               }
            FileClose(handle);
                 
            for(i=0;i<x;i++)
               for(j=0;j<y;j++)
                  array[i][j] = array_copy[i*y+j];
        
            return(handle);
            }

It works!
 
 
  • Post #14
  • Quote
  • Nov 4, 2011 3:53pm Nov 4, 2011 3:53pm
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
NP
Pleased to have shed a little light :-)

Now all I have to do is find my own lightbulb!
If you think I'm mad, I must be mad
 
 
  • Post #15
  • Quote
  • Last Post: Nov 7, 2011 1:47am Nov 7, 2011 1:47am
  •  bandung
  • Joined Apr 2010 | Status: Member | 642 Posts
Quoting michx
Disliked
Oky dokY,
everything is very O.K!
Thank You very much!!!

if somebody will have a similar problem he can go the same way what i did .
I solved my problem (based on Yours suggest) by wrote 2 functions:
[code]
int save_to_file(double array[][], int x, int y, string file_name /*.bin*/)
{
int i,j;
double array_copy[];
ArrayResize(array_copy,x*y);

for(i=0;i<x;i++)
for(j=0;j<y;j++)
array_copy[i*y+j] = array[i][j];...
Ignored
hi buddy,
you dont need to do those i,j loop...

just get a size of your 2 dimensions array, eg: arr[10][2] --> size is 20
then call

Inserted Code
FileWriteArray(handle, YourArray, 0, size);

code example:
PHP Code
 
//--global vars
double gdaIndiData     [/*LookBack*/][5/*starttime,endtime,vah,val,poc*/];
double gdaIndiDataSaved[/*LookBack*/][5/*starttime,endtime,vah,val,poc*/];
string gsIndiDataFN;
bool   gbDataNotFound = false;
//--

void saveData()
{
   
int handle = FileOpen(gsIndiDataFN, FILE_BIN|FILE_WRITE);
   
int size   = LookBack*5;
   if(
handle>0)
   {
      
FileWriteArray(handle, gdaIndiData, 0, size); 
      
FileClose(handle);
   } 

}

void readData()
{
   
   
int     size   = LookBack*5
         
, handle = FileOpen(gsIndiDataFN, FILE_BIN|FILE_READ);
   
   if(
handle>0)
   {   
      
ArrayResize(gdaIndiDataSaved, LookBack);
      
FileReadArray(handle, gdaIndiDataSaved, 0, size);
      
FileClose(handle);
   } 
   else 
   { 
gbDataNotFound = true; }   
} 
...
 
 
  • Platform Tech
  • /
  • How to get 2-dimension array to file and than get back it [MQL4]
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023