2

This mql4 script is not exporting to CSV-file when attached to a chart. Why?

static datetime check;
//+------------------------------------------------------------------+
//|   scripts/Export.mq4                  pre  Build 562-            |
//|   MQL4/Scripts/Export.mq4             post Build 562+            |
//+------------------------------------------------------------------+
void start() {                // New-MQL4 post Build 562+: void OnStart(){}
   if ( check != Time[0] ) {
        check  = Time[0];
        WriteCSV(); 
      } 
}

//+------------------------------------------------------------------+
//| WriteCSV                                                         |
//+------------------------------------------------------------------+
void WriteCSV() {
   int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
                          FILE_CSV|FILE_READ|FILE_WRITE, ','
                          );

   if ( handle > 0 ) {
        FileSeek( handle, 0, SEEK_END ); 

        FileWrite( handle,
                   TimeToStr( TimeCurrent(), TIME_DATE ),
                   iOpen ( Symbol(), Period(), 1 ),
                   iHigh ( Symbol(), Period(), 1 ),
                   iLow  ( Symbol(), Period(), 1 ),
                   iClose( Symbol(), Period(), 1 )
                   );

        FileClose( handle );
      }
}
//+------------------------------------------------------------------+
user3666197
  • 1
  • 6
  • 50
  • 92
ssn
  • 439
  • 5
  • 14

3 Answers3

2

This looks like a old thread but I just wanted to throw this in for what it's worth. There are multiple things that concern me here.

  1. your first command in the script:

    int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
                           FILE_CSV|FILE_READ|FILE_WRITE, ','
                           );
    

    should open the file for write, if it doesn't exist it will create it. So, if the file isn't located anywhere, then it's not being created. as stated by user3666197. Make sure your running MT4 as Administrator.

  2. you may want to include a loop when retrieving any data with the 'i' prefix. iClose, iOpen, iHigh, iLow - because all of the data your requesting may not be available. MT4 will need to pull from the server in which case the number of bars returned will be 0.

iClose() Returned value Close price value for the bar of specified symbol with timeframe and shift. If local history is empty (not loaded), function returns 0.

To check errors, one has to call the GetLastError() function. So if you've found the file and it is being created but no bars or just a few bars are being saved this would be why.

Just thought I'd throw that in there in case it became an issue for you down the road.

Good luck

user3666197
  • 1
  • 6
  • 50
  • 92
0

There is chances the directory you are trying to export to does not have permission for MT4 and therefore does not allow access.

pipable
  • 26
  • 3
0

A Filename may collide with O/S naming restrictions:

Test a constant O/S-safe string first

int handle = FileOpen( "FileOpenTest_FILENAME_FEASIBLE.CSV",
                        FILE_CSV | FILE_READ | FILE_WRITE
                        );

and last but not least, check, if your Broker's instrument naming does not introduce a collision with O/S. Have seen these practices that have stopped MQL4 code, that was in production state for years.

Symbol (re)naming alike "EURUSD.." on the Broker side may suprisingly block your otherwise standard fileIO operations - first hand experience on Windows Server 2008 DataCentre hosting, where the O/S suddenly started to silently refuse to open filenames with similar Symbol() injected artifacts once the Broker company Marketing Dept. has introduced a new naming convention for the FOREX instruments for various Account/Product types.

A fileIO operations may collide with user effective rights:

Check your user effective rights for locations, where MT4 permits fileIO-s to take place:

  1. ~<_TerminalDIR_>\MQL4\Files
  2. ~<_TerminalDIR_>\tester\files

( valid as of 2014-10-01, Build 670 )

user3666197
  • 1
  • 6
  • 50
  • 92