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