Please help with FileWrite() FileWriteArray()

 

Hi,

I searching in web and cant understand how it works...

I have EA that at the end should write collected info in file (txt or csv) NOT BIN

I'm collecting data in array

struct dati
  {
   int            Tiki; //
   int            Trend;  //
   int            Sk;  //
   double         Profit;  //
  };
dati array_vesture[];
for (int i=1;i<ArrSk;i++)
{
   array_vesture[ArrSk].Tiki = sTiki;
   array_vesture[ArrSk].Trend = sTrend;
   array_vesture[ArrSk].Sk = array_vesture[ArrSk].Sk + 1;
   array_vesture[ArrSk].Profit = array_vesture[ArrSk].Profit + sProfit;
}

ArrSk - usually not larger than 2000

void OnDeinit(const int reason)
  {
   string fails = "strategija2.txt";
   int file_handle;
   string   InpDirectoryName="Data"; // directory name

   ResetLastError();
   file_handle=FileOpen(InpDirectoryName+"//"+fails, FILE_SHARE_WRITE|FILE_CSV,";");
   if(file_handle!=INVALID_HANDLE)
   {
   int test;
   for (int i=1;i<=ArrSk-1;i++)
      {
         Print("arr test "+array_vesture[i].Tiki+","+
            array_vesture[i].Trend+","+
            array_vesture[i].Sk+","+
            array_vesture[i].Profit);
            
         test = FileWrite(file_handle,array_vesture[i].Tiki,
            array_vesture[i].Trend,
            array_vesture[i].Sk,
            array_vesture[i].Profit);
            Print("test "+test);				// RETURNS 0 ?????????????
      }   
   }
   else
      Print("Failed to open the file, error ",GetLastError());
  }

When I using command Print() it outputs my data as i wanted

but when I using command FileWrite() it gives my empty file.. WHY??? I also tried different FileOpen types..

file_handle=FileOpen(InpDirectoryName+"//"+fails, FILE_SHARE_WRITE|FILE_CSV,";");

file_handle=FileOpen(InpDirectoryName+"//"+fails, FILE_WRITE|FILE_TXT);

file_handle=FileOpen(InpDirectoryName+"//"+fails, FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_CSV,";");

and all combinations I can think of .. with no luck :( Also tried to use command FileWriteArray() in different combinations also with no luck :((


Few lines from journal....

2014.02.14 20:01:02.381 2013.01.07 23:59 KyaDataMiner EURUSD,M1: test 0

2014.02.14 20:01:02.381 2013.01.07 23:59 KyaDataMiner EURUSD,M1: arr test 41005112,1,0,5.8

2014.02.14 20:01:02.381 2013.01.07 23:59 KyaDataMiner EURUSD,M1: test 0

2014.02.14 20:01:02.381 2013.01.07 23:59 KyaDataMiner EURUSD,M1: arr test 41005112,0,0,0.1

2014.02.14 20:01:02.381 2013.01.07 23:59 KyaDataMiner EURUSD,M1: test 0

2014.02.14 20:01:02.381 2013.01.07 23:59 KyaDataMiner EURUSD,M1: arr test 05112222,1,0,5.2

I want output TXT or CSV file look like this:

41005112,1,0,5.8

41005112,0,0,0.1

05112222,1,0,5.2

Sorry for my English, and this is my first post.. :) I hope somebody can explain me where I make mistake.. :)) Thank You all very much!


 

Welcome to mql4.com forum,

Your English is just fine, don't worry. Which build are you using ?

   for(int i=1;i<ArrSk;i++)
     {
      array_vesture[i].Tiki=i;
      array_vesture[i].Trend=i*2;
      array_vesture[i].Sk=1;
      array_vesture[i].Profit=Ask;
     }

and

   file_handle=FileOpen(InpDirectoryName+"//"+fails, FILE_WRITE|FILE_CSV,";");

All is ok for me :

2014.02.14 21:44:09.638 FileWriteBug EURUSD,Daily: test 15
2014.02.14 21:44:07.752 FileWriteBug EURUSD,Daily: arr test 4,8,1,1.36985
2014.02.14 21:44:06.233 FileWriteBug EURUSD,Daily: test 15
2014.02.14 21:43:29.065 FileWriteBug EURUSD,Daily: arr test 3,6,1,1.36985
2014.02.14 21:43:27.561 FileWriteBug EURUSD,Daily: test 15
2014.02.14 21:43:25.464 FileWriteBug EURUSD,Daily: arr test 2,4,1,1.36985
2014.02.14 21:43:23.807 FileWriteBug EURUSD,Daily: test 15


Files:
 

try it:

#define PIPE_TYPE_MESSAGE                       4
#define PIPE_READMODE_MESSAGE           2
#define PIPE_WAIT                                       0
#define PIPE_ACCESS_DUPLEX              3
#define PIPE_UNLIMITED_INSTANCES        255
#define NMPWAIT_USE_DEFAULT_WAIT        0
#define INVALID_HANDLE_VALUE            -1
#define ERROR_PIPE_CONNECTED            535
#define FILE_SHARE_READ         1
#define FILE_SHARE_WRITE        2
#define GENERIC_WRITE           1073741824
#define CREATE_ALWAYS           2


#import "kernel32.dll"
  int ReadFile(int hPipe, int& inBuffer[],int NumberOfBytesToRead, int& bytesRead[], int lpOverlapped);
        int WriteFile(int hPipe, string sBuffer, int NumberOfBytesToWrite, int& bytesWritten[], int lpOverlapped);
        int CreateFileW(string name,int desiredAccess,int SharedMode,int security,int creation,int flags,int templateFile);
#import
 string pathName="D:\\Forex\\Dados\\"+paridade; //paridade like Um.csv
                           string FullName;
                           string Buffer_Old = "vazio";
            string Buffer_New = NULL;
int ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
                int fWrite= CreateFileW(FullName, GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, 0);
                if (fWrite == INVALID_HANDLE_VALUE)
                   {
                      Print("Unable to open " + FullName + " for writing");
                   }else{
                      int BytesWritten[1] = {0};
                      
                      //Buffer_Old = "Teste de texto";
                      int szData = StringLen(Buffer_Old);
                      WriteFile(fWrite, Buffer_Old, 2*szData, BytesWritten, NULL);
                      CloseHandle(fWrite);
                      Print(StringSpliter(Pairs_Time_Infos[in],"@",1) + " was saved.");
                   }//Fecha o Else do if (!IsValidFileHandle(fWrite))
 

Thank you! :))

I m using version 4.00 build 604 (7. feb. 2014) DF Markets Meta Trader 4..

Testing your version (FileWriteBug.mq4 (2.26 KB) ).. file is empty... :(((

 
Dadisx:

Thank you! :))

I m using version 4.00 build 604 (7. feb. 2014) DF Markets Meta Trader 4..

Testing your version (FileWriteBug.mq4 (2.26 KB) ).. file is empty... :(((

I am using build 606, here is the file.
Files:
 
bisewski:

try it:


Sorry Im not so advanced user :( I get many errors from this script and Im not sure how to fix them :( errors like

'GENERIC_WRITE' - undeclared identifier FileWrite111.mq4 37 51

'CREATE_ALWAYS' - undeclared identifier FileWrite111.mq4 37 80

'CreateFileW' - function must have a body FileWrite111.mq4 37 29

'INVALID_HANDLE_VALUE' - undeclared identifier FileWrite111.mq4 38 31

 
angevoyageur:
I am using build 606, here is the file.


FileWriteBug.mq4 does not work for me at all :( file is emty and in journal :

2014.02.14 21:11:06.183 EURUSD,M1: 59958 tick events (2441 bars, 60958 bar states) processed within 16 ms (total time 2839 ms)

2014.02.14 21:11:06.153 FileWriteBug test started

2014.02.14 21:11:03.335 TestGenerator: spread set to 30

2014.02.14 21:11:03.333 Expert FileWriteBug EURUSD,M1: loaded successfully

noting even Print function results.. :(

Probably need to upgrade to 606 version .. :/

 
Dadisx:


FileWriteBug.mq4 does not work for me at all :( file is emty and in journal :

2014.02.14 21:11:06.183 EURUSD,M1: 59958 tick events (2441 bars, 60958 bar states) processed within 16 ms (total time 2839 ms)

2014.02.14 21:11:06.153 FileWriteBug test started

2014.02.14 21:11:03.335 TestGenerator: spread set to 30

2014.02.14 21:11:03.333 Expert FileWriteBug EURUSD,M1: loaded successfully

noting even Print function results.. :(

Probably need to upgrade to 606 version .. :/

It's a script not an EA. And I see now that you are talking about the Strategy Tester.

I am running it on a live chart, script can't be backtested.

 

sorry, now is ok, see the code again...

 
bisewski:

sorry, now is ok, see the code again...


No luck

string paridade = "aaa.csv";

string pathName="d:\\Forex\\Dados\\"+paridade; //paridade like Um.csv

does not make even folder on d:\ :(

 
angevoyageur:

It's a script not an EA. And I see now that you are talking about the Strategy Tester.

I am running it on a live chart, script can't be backtested.



Sorry I m confused.. Is it possible to use EA on back testing (strategy tester), collect info and after that write info in file?