To backup an Amibroker database in CSV format, run a small AFL exporter as a Scan in the Analysis window: it writes one CSV file per symbol (Ticker, Date, Time, Open, High, Low, Close, Volume) for EOD or intraday data, for one symbol or the entire database in a single click. This is the simplest way to handle an Amibroker export data to CSV job, and the files can be re-imported into Amibroker or opened in Excel and Python.
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 an important step in protecting your data and it helps recovering the data back to Amibroker in case of any system failure/crashes.
Key takeaways
- The AFL below exports every bar to CSV using fopen, fputs and StrFormat inside a Scan.
- Choose EOD or INTRADAY in Parameters; the header and the time column change accordingly.
- Set Apply to = All Symbols (or a watchlist filter) to export the whole database in one go.
- The default folder is C:\AmiExplore\ and it must exist before you press Scan.
- Keep the CSV backup in a second location, such as an external drive or cloud folder.
Amibroker export data to CSV: step by step
In order to Export data to CSV file in Amibroker one has to follow the following steps.
Step 1: Open a new Analysis window
Open Amibroker -> New -> Analysis.
Step 2: Select the AFL code and set the file path
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\ and 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.

Step 3: Choose EOD or INTRADAY in Parameters
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.

Step 4: Select the date range
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.

Step 5: Choose Apply to
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.
Step 6: Set the periodicity
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.

Step 7: Press Scan
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.openalgo.in
_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();
How the AFL works
ParamList()adds the EOD/INTRADAY switch to the Parameters window.fopen()creates one file per symbol, named likeRELIANCE_EOD.csv, in write mode.- A header row is written, then the loop runs through every bar and writes date (and time for intraday) with
StrFormat(). - Prices are written with 4 decimals and volume with none, which keeps file size small without losing precision.
Buy = 0;is required because a Scan needs a Buy variable, even though no signals are generated.
Tips for a reliable Amibroker database backup
- Create the folder from AFL: AmiBroker’s knowledge base article on exporting quotes per symbol suggests calling
fmkdir()beforefopen(), so the export never fails because the folder is missing. - Export a watchlist: set Apply to = Filter and pick a watchlist when you only need part of a large database.
- Export to one file instead: if you want all symbols in a single CSV, AmiBroker has a separate single-file CSV export example.
- Also copy the database folder: an Amibroker database is a folder on disk. Closing Amibroker and copying that folder is the fastest full backup; the CSV export is the portable one that survives version changes and works in other tools.
- Check the numbers: open one CSV after export and compare the last few bars with the chart before you rely on the backup.
How to restore the CSV backup into Amibroker
- Create a new database (File -> New -> Database) with the right base time interval: daily for EOD, 1 minute for intraday.
- Use File -> Import Wizard and select all the CSV files from the backup folder.
- Map the columns as Ticker, Date (YMD), Time (for intraday), Open, High, Low, Close, Volume, and tell the wizard to skip the first line because it is the header.
- Save the format definition so the next restore takes one click.
A clean Amibroker export data to CSV routine also makes your data portable. The same CSV files are useful outside Amibroker too. You can convert the Amibroker database to NinjaTrader format, or load them into Python with pandas for research.
Frequently Asked Questions
How do I export all symbols from Amibroker to CSV?
Run the export AFL in the Analysis window with Apply to set to All Symbols and Range set to All quotes, then press Scan. Each symbol is written to its own CSV file.
Where does Amibroker save the exported CSV files?
In this AFL the files go to C:\AmiExplore\. Change the path inside the fopen() call if you want another folder, and make sure the folder exists.
Can I export intraday data from Amibroker?
Yes. Choose INTRADAY in Parameters and set the periodicity to 1 minute in the Analysis settings. The CSV then includes a Time column.
How do I back up an Amibroker database?
Either copy the whole database folder while Amibroker is closed, or export the quotes to CSV with an AFL Scan. The CSV backup is portable and can be re-imported into any version.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.


Great code
thank you
I cud backup my ami data
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..
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
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..
formula error display
Can this be used for IEOD?
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
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.
im trader
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
@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.
Yeh spot out the problem. Now it is corrected.
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
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;
Hi, Still the same syntax error appears, pls help.
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 ????
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
Nice utility. One query where does it store the csv file?
I am able to resolve on my own. I created the directory named amibackup and also replaced “//” with “\\”
thanks a ton
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
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 );
}
hi
is there any update of the afl for exporting the explorer data to excel/csv…?
looking for that please…
Thanks
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