Download MetaTrader 5
To add comments, please log in or register
Improve your rating. Execute orders in Freelance service!
difenp
305
difenp 2013.12.04 14:43 

Hello everybody. I tried to search for an answer regarding this topic that I am sure has been treated already.

I am trying to save high,low and close of the previous trading day on a csv file to compute some calculations since is not possible to perform portfolio backtesting with mt4.

I wrote a simple script that does the job. Since I have to read 28 pairs on my current broker, I wanted to know if could simply loop through an array of symbols to obtain the results.

I tried to write this code, but I failed to reach what I needed since the script keeps writing the same high, low, close from the chart it has been loaded onto.

//+------------------------------------------------------------------+
//|                                              excel_data_dump.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int arrayLengthIndex = 28;
   string currencyErray[28] = {"USDCHF", "GBPUSD", "EURUSD", "USDJPY", "USDCAD", "AUDUSD", "EURGBP", "EURAUD", "EURCHF", 
                              "EURJPY", "GBPCHF", "CADJPY", "GBPJPY", "AUDNZD", "AUDCAD", "AUDCHF", "AUDJPY", "CHFJPY", "EURNZD", "EURCAD", 
                              "CADCHF", "NZDJPY", "NZDUSD", "GBPAUD", "GBPCAD", "GBPNZD", "NZDCAD", "NZDCHF" };
 
 
   for (int k = 0; k < arrayLengthIndex; k++) {
      
      datetime yesterDayStart = iTime(currencyErray[k], PERIOD_D1, 1);
      datetime yesterDayEnd = iTime(currencyErray[k], PERIOD_D1, 0) - 1;
 
      int barsYesterday = iBarShift(currencyErray[k], PERIOD_M5, yesterDayStart);
      int barsToday = iBarShift(currencyErray[k], PERIOD_M5, yesterDayEnd);
      int totalBars = (barsYesterday - barsToday) + 1;
 
      string fileName = "";
      int fileHandle, fileChunk;

      //fileName = currencyErray[k] + "_" + TimeDay(TimeCurrent()) + "-" + TimeMonth(TimeCurrent()) + "-" + TimeYear(TimeCurrent()) + ".csv";
      fileName = currencyErray[k] + ".csv";
 
      fileHandle = FileOpen(fileName, FILE_CSV|FILE_WRITE, ",");
      if(fileHandle == -1) {
         Alert("An error occured while trying to open file: " + fileName + "");
         return (-1);
      }
 
      fileChunk = FileWrite(fileHandle, "Date,High,Low,Close");
      if (fileChunk < 0) {
         Alert("An error occured while writing on file: " + fileName + " error: " + GetLastError());
         FileClose(fileHandle);
         return (-1);
      }
      int i, j;
      for (i = barsYesterday;  i > (barsYesterday - totalBars); i--) {
         //goes through the data from now to opening of the day
         fileChunk = FileWrite(fileHandle, TimeToStr(Time[i]), High[i], Low[i], Close[i]);
   
         if (fileChunk < 0) {
            Alert("An error occured while writing on file: " + fileName + " error: " + GetLastError());
            FileClose(fileHandle);
            return (-1);
         }
      }
   
      FileClose(fileHandle);
 }
//----
   return(0);
  }
//+------------------------------------------------------------------+

I am wondering if is possible to drop the script on just one chart and read the data from all the symbols listed.

Thank you in advance, Francesco.

whroeder1
13627
whroeder1 2013.12.04 15:07  
zen4x: I am wondering if is possible to drop the script on just one chart and read the data from all the symbols listed.
    fileHandle = FileOpen(fileName, FILE_CSV|FILE_WRITE, ",");
  1. Of course it possible.
  2. Since you are opening the file for writing, when that statement completes, the file is empty. Move the open and closing code OUTSIDE your loop.
  3. Third argument to FileOpen is an int not a string
GreenMoney
379
GreenMoney 2013.12.04 15:21  

WHRoeder:

. . . Third argument to FileOpen is an int not a string

Could you please elaborate? In my previous tests, using ";" did not create any compile-time or run-time errors and seems to work exactly like ';'.

As I noted in my post linked above, the examples in the documentation and the book are inconsistent in the use of a single quotation mark.

difenp
305
difenp 2013.12.05 00:03  
zen4x:

Hello everybody. I tried to search for an answer regarding this topic that I am sure has been treated already.

I am trying to save high,low and close of the previous trading day on a csv file to compute some calculations since is not possible to perform portfolio backtesting with mt4.

I wrote a simple script that does the job. Since I have to read 28 pairs on my current broker, I wanted to know if could simply loop through an array of symbols to obtain the results.

I tried to write this code, but I failed to reach what I needed since the script keeps writing the same high, low, close from the chart it has been loaded onto.

I am wondering if is possible to drop the script on just one chart and read the data from all the symbols listed.

Thank you in advance, Francesco.


Hello Whroeder. Thank you for replying me.

I am not sure If I understood what you asked me to do. At the actual stage I was trying to create 28 files with the pair name to keep separate each price history.

For example "AUDUSD.csv", "EURUSD.csv". I assign the file name when I loop through the array.

Do you mind to make me an example ?

Thanks for the time again.

/
To add comments, please log in or register