Extract Historical Data TO CSV

Created at 05 Oct 2012, 10:59
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
RO

romiko

Joined 26.09.2012

Extract Historical Data TO CSV
05 Oct 2012, 10:59


 

Hi,

 

Before my Algorithm runs, I would like it to extract historical data to a text file, is this possible? See this script below for MetaTrader, I want the same in CAlgo, basically, the code will extract data on a weekly basis.

 

Output desired (included one line, but i want historical data):

 

DATE,TIME,CLOSE,StochK,StochD,WilliamsR
20030707,0000,1.37370000,7.1743119266e+001,7.2390220187e+001,-6.2189054726e-00120030708,0000,1.36870000,7.5140977444e+001,7.3307139273e+001,-1.2500000000e+001

Code in MT that needs to work in CAlgo

externstring IndExportFileName = "mt5export.csv";
externint  trainSize = 2000;

MqlRates srcArr[];
double StochKArr[], StochDArr[], WilliamsRArr[];

voidOnStart()
  {
//---
   ArraySetAsSeries(srcArr, true);   
   ArraySetAsSeries(StochKArr, true);   
   ArraySetAsSeries(StochDArr, true);   
   ArraySetAsSeries(WilliamsRArr, true);
         
   int copied = CopyRates(Symbol(), Period(), 0, trainSize, srcArr);
   
   if (copied!=trainSize) { Print("Not enough data for " + Symbol()); return; }
   
   int hStochastic = iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, STO_LOWHIGH);
   int hWilliamsR = iWPR(Symbol(), Period(), 21);
   
   
   CopyBuffer(hStochastic, 0, 0, trainSize, StochKArr);
   CopyBuffer(hStochastic, 1, 0, trainSize, StochDArr);
   CopyBuffer(hWilliamsR, 0, 0, trainSize, WilliamsRArr);
    
   int hFile = FileOpen(IndExportFileName, FILE_CSV | FILE_ANSI | FILE_WRITE | FILE_REWRITE, ",", CP_ACP);
   
   FileWriteString(hFile, "DATE,TIME,CLOSE,StochK,StochD,WilliamsR\n");
   
   Print("Exporting indicator data to " + IndExportFileName);
   
   for (int i=trainSize-1; i>=0; i--)
      {
         string candleDate = TimeToString(srcArr[i].time, TIME_DATE);
         StringReplace(candleDate,".","");
         string candleTime = TimeToString(srcArr[i].time, TIME_MINUTES);
         StringReplace(candleTime,":","");
         FileWrite(hFile, candleDate, candleTime, DoubleToString(srcArr[i].close), 
                                                 DoubleToString(StochKArr[i], -10),
                                                 DoubleToString(StochDArr[i], -10),
                                                 DoubleToString(WilliamsRArr[i], -10)
                                                 );
      }
      
   FileClose(hFile);   
     
   Print("Indicator data exported."); 
  }
//+------------------------------------------------------------------+

 

 


@romiko
Replies

admin
05 Oct 2012, 11:18

Hello,

 

In order to get historical data into a file you need to create a cBot that writes to a file and run it on back-testing mode.  

If you need to do this on a weekly basis choose the date for the back-testing to be a week and run the cBot once a week to append to the text file or create a new file.

See this post for a reference if you need help writing to a file: /forum/cbot-support/8

After you run this cBot and get the text file with the historical data you can run your other algorithm which presumably uses this historical data.

 


@admin

romiko
05 Oct 2012, 12:44

Awesome, thank you! This solution will work for me :)


@romiko