Export Data from Amibroker to csv

#12
Dear gvdevan,

Thank you very much for replying the query. I actually did already, what you have suggested. It works fine. The only problem I am facing is, it gives all the scrips in one single sheet. :). And I want is -

1. I have specified the filter in AA window which has all the F & O stocks.
2. I want only those stocks to be exported.
3. I want each stock to be exported in a seperate sheet with the date range, specified in AA window. Hope I am able to make you understand. Please ask me if I am not very clear. Thanks once again for replying. See if you can find out the solution. I'm trying.

Thanks & Regards
Avinash Raste
 

Vipul_84

Well-Known Member
#13
Nice AFL by Mr. Anant Navale
Thanks for your AFL coding.... :thumb:
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
 
#14
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

Thank you for supplying the above code. Could you help cut out the error message I get on line 16
when compiling please ? -

fh = fopen( "c:\\SaveData"+Name()+".csv", "w");
Error 31. Syntax error, unexpected MEMBER, expecting ')' or','

Many thanks
 
#15
Anant,
when I open a csv file it has ticker, open, high, low, close, and volume columns.

Is there any way to create the csv files without the ticker column.

Thanks fillup
 
#17
What about One Minute Data Export to CSV?
Hello and thanks for afl. The above AFL is for exporting only EOD data or daily data. what about exporting one minute data ? I had an afl of same kind which exported one minute data very nicely from any one date to another. But it suddenly stopped working. I wonder if any expert here can help me abot this afl to export oneminute data to csv from amibroker.
 

Similar threads