This video covers how you can export data into a text file using Amibroker’s AFL code.
We share the code, the exported file and how it all works in Amibroker analysis tab. Amibroker .afl code on how to export data, in particular the open high low and close of the most recent candle of a list of specific symbols outlined in the code.
This coding video for Amibroker afl code will share:
- How to loop symbols
- Seek the recent daily candle
- Create a text file
- Write to a text file
- Run an analysis which outputs the text file to your specified folder
Amibroker Code to Create a Text File
The below code is the code from the video above and will export a text file of recent candles of a few indices listed in the code.
To adjust it to your needs, be sure to change the FOLDERPATH to your preferred folder where your text file needs to go.
You can also change the symbols to the symbols you want data for. The symbols listed start with $DAX and ends at $XJO
Please note this code isn’t professionally written so if you feel you know more about afl code you may wish to edit the code to suit your needs.
Code - Edit your file path
fileName = “C:\\ FOLDERPATH \\OHLC_data.txt”;
fh = fopen(fileName, “w”);
if (fh)
{
fputs(“Symbol,Date,Open,High,Low,Close\n”, fh);
// Define the symbols you want to export data for
Symbols = “$DAX,$FT100,$DJI,$N225,$XJO”;
SetForeign(Symbols);
for (symbolIndex = 0; symbolIndex < StrCount(Symbols, “,”) + 1; symbolIndex++)
{
Symbol = StrExtract(Symbols, symbolIndex);
SetForeign(Symbol);
lastIndex = (BarCount – 1);
line = StrFormat(“%s, %.2f, %.2f, %.2f, %.2f\n”, Symbol, O, H, L, C);
fputs(line, fh);
RestorePriceArrays();
}
fclose(fh);
printf(“Most recent day OHLC data exported to %s successfully.”, fileName);
}
else
{
printf(“Error: Unable to open file for writing.”);
}
AFL Amibroker Resources
Here are a range of Amibroker afl resources to help you understand how it works, what different functions are and access to the community.