Export Data from Amibroker to csv

#1
Hi all,

I was trying to search an option which will export the data from AmiBroker to a folder where the data will be stored in seperate csv sheets. for eg. Abb will be stored in a sheet where the data will be there from the date specified in the AA window to the date specified in AA window. and same for Acc and same for ambuja and on and on.

1. The range of date is specified in AutoAnalysis Window.
2. The stocks to be exported are defined in the filter (Group).

Now after a good search I found the code from AmiBroker HELP section and I tried. It worked. The only problem I'm facing is that, it is not taking the date range specified in the AutoAnalysis window. By default it is taking the first day of my database To tilldate. Below is the code I found

( First make sure that you have made the " SaveData " directory in C drive. )


fh = fopen( "c:\\SaveData\\"+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;

Can anyone please throw some light that how the range of date will start working ?? Thanks in Advance.

Thanks & Regards
Avinash Raste
 

asnavale

Well-Known Member
#2
Hi all,

I was trying to search an option which will export the data from AmiBroker to a folder where the data will be stored in seperate csv sheets. for eg. Abb will be stored in a sheet where the data will be there from the date specified in the AA window to the date specified in AA window. and same for Acc and same for ambuja and on and on.

1. The range of date is specified in AutoAnalysis Window.
2. The stocks to be exported are defined in the filter (Group).

Now after a good search I found the code from AmiBroker HELP section and I tried. It worked. The only problem I'm facing is that, it is not taking the date range specified in the AutoAnalysis window. By default it is taking the first day of my database To tilldate. Below is the code I found

( First make sure that you have made the " SaveData " directory in C drive. )


fh = fopen( "c:\\SaveData\\"+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;

Can anyone please throw some light that how the range of date will start working ?? Thanks in Advance.

Thanks & Regards
Avinash Raste
Hi Avinash,

The code you have quoted can not give you data between required range. It is coded to give the data output from start date of the stock in the database till end date. This is hard coded in the for(...) loop:

for(i=0; i<BarCount; i++);

This statement takes the data from bar No. 0 that is first bar in the database upto BarCount which is the last Bar in the Database. The dates specified in the Analysis window have no effect on the code. To get the data exported for the date range you should put an if condition to see that the data fetched from the data base belongs to the date range you want. If the data is for a period before your start date it should be rejected. Similarly if the data is for a date after the range it also should be rejected.

This condition should be the first statement after the for(...) statement. Let us say you want to export data from 1st Jan 2004 to 31st Dec 2008. You should test the date retrieved from database against the start date of 01-01-2004. If the date fetched is equal to or greater than this value then check for end date i.e. 31-12-2008. If it is less than or equal to this date then only export the data. Otherwise skip to next data line and repeat the same process. I think you can write these two if(...) conditions and modify the AFL to get what you want. In case you can not do it please post your reply I will give you the code.

-Anant
 
#4
Hi Anant,

very goodevening.

As I said, I tried to put it in code language ( AFL ) but could not succeeded though I understood the logic very well as I'm not a good programer. I have to admit that I could not make it as logic was not a problem but there was definitely a problem with syntex. I could not put it syntetically right. It will be a great help if you can make it work.

Thanks & Regards

Avinash Raste
 
#5
Hello to all...here is a small afl for backup your data or Export Data from Amibroker to csv....

_SECTION_BEGIN("back up of ami file");
//This program is written to keep the back up of ami broker file.
//hope you will enjoy
//thank you......OPTIMIZER


Filter=1;
AddColumn(O,"Open");
AddColumn(H,"High");
AddColumn(L,"LOW");
AddColumn(C,"Close");
AddColumn(V,"Volume");
_SECTION_END();


how to use it...

click this afl...when a chart open > click auto analyser >select all symbols & all quotations (may be it done by default...is not then you do this ) >click explore..............>click file-export....name a file and save...

thanks....
 

asnavale

Well-Known Member
#7
Hi Anant,

very goodevening.

As I said, I tried to put it in code language ( AFL ) but could not succeeded though I understood the logic very well as I'm not a good programer. I have to admit that I could not make it as logic was not a problem but there was definitely a problem with syntex. I could not put it syntetically right. It will be a great help if you can make it work.

Thanks & Regards

Avinash Raste
Hi Avinash,

I just saw your post. I will be posting the required AFL shortly.

-Anant
 

asnavale

Well-Known Member
#8
Hello to all...here is a small afl for backup your data or Export Data from Amibroker to csv....

_SECTION_BEGIN("back up of ami file");
//This program is written to keep the back up of ami broker file.
//hope you will enjoy
//thank you......OPTIMIZER


Filter=1;
AddColumn(O,"Open");
AddColumn(H,"High");
AddColumn(L,"LOW");
AddColumn(C,"Close");
AddColumn(V,"Volume");
_SECTION_END();


how to use it...

click this afl...when a chart open > click auto analyser >select all symbols & all quotations (may be it done by default...is not then you do this ) >click explore..............>click file-export....name a file and save...

thanks....
Hi Kundu,

The code you have given saves all data for all stocks in a single file. What Avinash wants is putting data of each stock in a separate file. This can be done through an AFL and not exploration. For each stock to be saved separately, your code has to be run on each stock. Imagine running the AFL some 4000-5000 times to account for all the traded stocks on both exchanges.

-Anant
 

asnavale

Well-Known Member
#9
Hi Avinash,

Here is the AFL for exporting Data to CSV file for a selected range of dates. This AFL has to be run in Analysis Window in Explore mode. You can select the dates for which the data is to be exported in the Analysis Window. Click on Parameters button and select the Start Date and End Date from the calendar. Please note that the AFL works properly only if the following conditions are fulfilled:

1. The Directory 'C:\SaveData' must exist.
2. The start Date must be earlier to End Date.
3. The Start Date and End Date must have data. They should not be holidays.
4. The stock should have data for the range of dates selected.

For example, if a stock is listed from 1-1-2008 and you try to export data from an earlier date, such as 1-5-2007, it will not work properly. Similarly, if a stock has data only upto, say 31-12-2008, and you try to export data upto 30-6-2009 this also will not work properly.

You can change the code to modify the conditions and work properly even if you give dates which are not acceptable as per the above rules. This I leave to you to try out. In case you are unable to do it just post a message here and I will post the modification required.

The AFL is given below. You can Block all the Blue colored text below and copy & paste into AFL editor and save in AmiBroker Formulas/Custom folder by giving a name to the file. I have given the name 'DataEporter.afl".

//**************** Data Exporter AFL ***********************


// Set the Starting and End Dates
// Make sure these dates are not holidays.

StartDate = ParamDate("Starting Date", "31-12-2007");
EndDate = ParamDate("Start Date", "31-12-2008");

//Find the corresponding Bar Numbers

StartBar = Max(0, LastValue(ValueWhen(DateNum() == StartDate, BarIndex(),1)));
EndBar = Min(BarCount - 1, LastValue(ValueWhen(DateNum() == EndDate, BarIndex(),1)));

Filter = 1; // This allows all required data to be included

// Before running the AFL Make sure that C:\SaveData directory exists

fh = fopen( "c:\\SaveData\\"+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 = StartBar; i <= EndBar; 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 );
}

// The following lines are redundant but are required for the Exploration to work.
// These lines will just output the data being written to the file.

SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "Symbol", 77);
AddColumn(DateTime(), "Date", formatDateTime);
AddColumn(O, "Open", 6.2);
AddColumn(H, "High", 6.2);
AddColumn(L, "Low", 6.2);
AddColumn(C, "Colse", 6.2);
AddColumn(V, "Volume", 10.0);


//******************* END OF AFL ********************

For any questions please post your queries in this thread.

-Anant
 
Last edited:
#10
Dear Anant,

First of all thank you very much for the reply and A very very Happy Diwali and a healthy, wealthy & prosperous new year.

I am extreamly sorry to reply late as I was out of the station. I tried the code you have given in the post. I have the data since 1st Dec. 2008 and the problem is, whatever date I choose, it gives me the data of Dec month i.e. first month of my database. 1st to 31st Dec., 2008.

I do not know what is the problem. I will be greatful if you can solve this problem.

Thanks & Regards

Avinash Raste
 

Similar threads