Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Backup your Amibroker EOD/Intraday Database in CSV Format

1 min read

This Amibroker tutorial helps you to export your entire Amibroker EOD/Intraday Database in CSV Format with just a click of a button. Backing up a database helps to prevent data loss in case of unexpected events such as hardware failure, system crashes, or malware attacks.

Backing up an Amibroker database is a crucial step in protecting your data and it helps recovering the data back to Amibroker in case of any system failure/crashes.

Inorder to Export data to CSV file in Amibroker one has to follow the following steps

1)Open Amibroker -> New -> Analysis
2)Select the AFL Code in the formula Section (AFL Code given below) and the file path in the AFL code

the default file path is set to C:\AmiExplore\ all the exported CSV files will be extracted automatically to this path. So ensure the path exists before you proceed further. In case you want a different export file path then change the file path accordingly in the Downloaded AFL code

Amibroker – New Analysis Window

3)Goto the Parameter Settings and Select EOD / INTRADAY from the dropdown. Selection mostly depends upon what kind of data you want to backup. If you want to backup daily data then select EOD option. If in case you want to backup intraday data then select INTRADAY Option

Amibroker Parameter Settings Window

4)Select All Quotes to Extract all the quotes in the CSV format. If you want to export only a specific range of dates, you can select the “Range” option set to From to Dates as shown below

Selecting From-To Dates Option in Amibroker New Analysis

5)You can select “Apply to” option set to Current only to extract the current symbol or you alternatively can select All Symbols to extract all the symbols in one shot.

6)Goto Backtesting Settings and Set the Required Timeframe to Export the data. For Daily Charts data export select the Daily timeframe. For Exporting Intraday Data select 1-minute timeframe as shown below.

Amibroker Backtesting Settings

6)Press the Scan button. Bingo now you can check the exported CSV data in the above-mentioned file path.

Amibroker AFL code to Export data in CSV Format

//Coded by Rajandran R 
//Website - www.marketcalls.in / www.algomojo.com



_SECTION_BEGIN("Amibroker Export Intraday/EOD data");

exportdata = ParamList("Data Type","EOD|INTRADAY");
header = "";

fh = fopen( "c://AmiExplore//"+Name()+"_"+exportdata+".csv", "w");

if(exportdata=="EOD")
{
header="Ticker,Date,Open,High,Low,Close,Volume";
}
if(exportdata=="INTRADAY")
{
header="Ticker,Date,Time,Open,High,Low,Close,Volume";
}

if( fh )
{

fputs( header, fh );
y = Year();
m = Month();
d = Day();
r = Hour();
e = Minute();
n = Second();

for( i = 0; i < BarCount; i++ )
{
fputs( "\n"+Name() + "," , fh );
ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );

if(exportdata=="INTRADAY")
{
ts = StrFormat("%02.0f:%02.0f:%02.0f,",
r[ i ],e[ i ],n[ i ] );
fputs( ts, fh );
}
qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f",
O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
fputs( qs, fh );
}

fclose( fh );
}

Buy = 0;


_SECTION_END();
Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Sending Smart Futures/Spot – Signals to Two-Legged Options Execution…

Thought of making an open source AFL code demanded by most of the option traders to control the cost, to control the risk, and...
Rajandran R
12 min read

Send Smart Options Execution Orders from Futures or Spot…

This tutorial provides instructions on how to utilize simple buy and sell trading signals in Spot/Future charts to place option orders (including ATM, ITM,...
Rajandran R
19 min read

Backup your Amibroker EOD/Intraday Database in CSV Format

This Amibroker tutorial helps you to export your entire Amibroker EOD/Intraday Database in CSV Format with just a click of a button. Backing up...
Rajandran R
1 min read

23 Replies to “Backup your Amibroker EOD/Intraday Database in CSV Format”

  1. Hi.. Thanks for the code.. Assume that My hard disk’s C drive crashed due to virus and I can’t able to retrieve any past datas.. So, I’ll format the disk and will create a new database which will accept only .txt format..

    My question is,Is it possible to create Ami database which will accept both .csv and .txt format..? Since, our backup is in .csv format I raised this Question..

    Thank you..

    1. Yeh Govindharaj… It supports both .txt and .csv format… you just simply edit the afl code and change it to ur required format and get the backup….
      Now use import wizard and import ascii funtionality to import those backup data into ur new amidatabase

  2. Hi.. I just replaced .csv with .txt in the first line of the code and got backup in .txt format.. Sorry for the previous post..

    But, how to use these datas in a new Database..?

    Thank you..

    1. yes it can used for IEOD you need to edit the code according to your required output format. Small knowledge of C/C++ is good enough to edit the code

  3. sir idon’t know how to Backup you Amibroker EOD Database in CSV Format can you help to do this pleas sir
    i have team wever.

  4. Dear Rajendran,
    While i am trying to take backup the database in amibroker using the above afl it shows an syntax error as below..

    fh = fopen( “
    —————-^

    Error 31.
    Syntax error, unexpected $end, expecting ‘)’ or ‘,’

    Kindly help in this regard.
    Regards,
    Swetharanyan

    1. @Swetharanyan

      Copy the AFL code from here directly to your notepad and from notepad then copy to your amibroker code editor. Direct copying from
      website sometimes creates Double quotation problem.

  5. sir,
    i am also getting syntax error
    While i am trying to take backup the database in amibroker using the above afl it shows an syntax error as below..

    fh = fopen( “
    —————-^

    Error 31.
    Syntax error, unexpected $end, expecting ‘)’ or ‘,’
    i have adopted the solution u suggested but , its showing error

    1. Hi Sameer,
      try this
      fh = fopen( “c:\\AmiBackup\\”+Name()+”.csv”, “w”);
      if( fh )
      {
      fputs( “Ticker,Date,Open,High,Low,Close,Volume \n”, fh );
      y = Year();
      m = Month();
      d = Day();
      //r = Hour();
      //e = Minute();
      //n = Second();
      for( i = 0; i < BarCount; i++ )
      {
      fputs( Name() + "," , fh );
      ds = StrFormat("%02.0f-%02.0f-%02.0f,",
      y[ i ], m[ i ], d[ i ] );
      fputs( ds, fh );
      //ts = StrFormat("%02.0f:%02.0f:%02.0f,",
      //r[ i ],e[ i ],n[ i ] );
      //fputs( ts, fh );
      qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f\n",
      O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
      fputs( qs, fh );
      }
      fclose( fh );
      }
      Buy = 0;

  6. hi
    still the same syntax error apears
    i had already done copy to note pad and then copy to farmula
    still not working any solution to save data in amibroker ????

  7. solved the problem of syntax error..
    the inverted commas should be replaced
    for example – fh = fopen( “c:AmiBackup”+Name()+”.csv”, “w”);

    to

    fh = fopen(“C:AmiBackup”+Name()+”.csv”, “w”);

    this will solve the problem. although you will have to change all the commas in the code.. not just in this line

    1. I am able to resolve on my own. I created the directory named amibackup and also replaced “//” with “\\”
      thanks a ton

  8. Hi Rajendran,
    I am having a bit of a problem with this code. Firstly, I was trying to export intraday data. So I removed the comments to include time. I am afraid the data is coming in one continuous line and there are no line breaks. The time is also not coming correctly they are all coming as 00:00:00.
    When I see the data in the quote editor and of course in Ami the data is displayed correctly as bars.
    My final code is pasted below for ready reference.
    Please help…
    TIA
    Praveen.

    Code===========================
    fh = fopen( “c://AmiBackup//”+Name()+”.csv”, “w”);
    if( fh )
    {
    fputs( “Ticker,Date,Open,High,Low,Close,Volume”, fh );
    y = Year();
    m = Month();
    d = Day();
    r = Hour();
    e = Minute();
    n = Second();
    for( i = 0; i < BarCount; i++ )
    {
    fputs( Name() + "," , fh );
    ds = StrFormat("%02.0f-%02.0f-%02.0f,",y[ i ], m[ i ], d[ i ] );
    fputs( ds, fh );

    ts = StrFormat("%02.0f:%02.0f:%02.0f,",
    r[ i ],e[ i ],n[ i ] );
    fputs( ts, fh );

    qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f",O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
    fputs( qs, fh );
    }

    fclose( fh );
    }

    Buy = 0;
    Code ends=====================================
    Sample output data==============================
    Ticker,Date,Open,High,Low,Close,VolumeNIFTY-I,2012-01-02,00:00:00,4637.7998,4656.0000,4592.3999,4648.1001,277565NIFTY-I,2012-01-03,00:00:00,4680.5498,4794.3999,4680.5498,4783.2002,362152NIFTY-I,2012-01-04,00:00:00,4775.2002,4799.0000,4737.6001,4760.2002,353400NIFTY-I,2012-01-05,00:00:00,4759.0000,4791.8501,4740.1001,4758.1001,271152NIFTY-I,2012-01-06,00:00:00,4745.0000,4821.0000,4692.6001,4771.9502,450646NIFTY-I,2012-01-09,00:00:00,4748.3501,4778.0000,4705.1499,4751.7998,321543NIFTY-I,2012-01-10,00:00:00,4780.0000,4876.2998,4779.5000,4870.2998,392413NIFTY-I,2012-01-11,00:00:00,4805.0000,4889.0000,4805.0000,4872.3501,266376

    1. Thanx Rajendran,
      I figured out the issue… Guess, the line feeds are missing.. Thats the reason for the data coming continuous.
      As regards the time stamp issue, it needs to be set in the settings to one minute and then it comes out just fine.
      Posting it here so that others may also benefit…
      HIH,
      Praveen.
      Corrected Code===========================
      fh = fopen( “c://AmiBackup//”+Name()+”.csv”, “w”);
      if( fh )
      {
      fputs( “Ticker,Date,Open,High,Low,Close,Volume\n”, fh );
      y = Year();
      m = Month();
      d = Day();
      r = Hour();
      e = Minute();
      n = Second();
      for( i = 0; i < BarCount; i++ )
      {
      fputs( Name() + "," , fh );
      ds = StrFormat("%02.0f-%02.0f-%02.0f,",y[ i ], m[ i ], d[ i ] );
      fputs( ds, fh );

      ts = StrFormat("%02.0f:%02.0f:%02.0f,",
      r[ i ],e[ i ],n[ i ] );
      fputs( ts, fh );

      qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f\n",O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
      fputs( qs, fh );
      }

      fclose( fh );
      }

  9. hi

    is there any update of the afl for exporting the explorer data to excel/csv…?

    looking for that please…

    Thanks

  10. Hi there, It serves the purpose but exactly what I am looking for… I have one code if you please help me with this. My one custom requirement in the below-mentioned code is to get date in ddmmyyyy format and not in dd-mm-yyyy. Admin please help…

    // create folder for exporting purposes
    fmkdir( “C:\\DataExport\\” );

    // open file for writing
    // file name depends on currently processed ticker
    fh = fopen( “c:\\DataExport\\” + Name() + “.txt”, “w” );

    // proceed if file handle is correct
    if ( fh )
    {
    dt = DateTime();

    // write header line
    fputs( “Ticker,Date/Time,Open,High,Low,Close,Volume\n”, fh );

    // iterate through all the bars

    for ( i = 0; i < BarCount; i++ )
    {
    // write ticker name
    fputs( Name() + "," , fh );

    // write date/time information
    fputs( DateTimeToStr( dt[ i ] ) + ",", fh );

    //write quotations and go to the next line
    qs = StrFormat( "%g,%g,%g,%g,%g\n", O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
    fputs( qs, fh );

    }
    // close file handle
    fclose( fh );
    }

    // line required by SCAN option

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More