How to manually import OHLC data via AFL script?


If I have some new OHLC data that I would like to import manually by calling via the AFL script, how can I do it? I looked at GetRTData and GetRTDataForeign but they work only with real-time streaming sources (already set up in AB). I’m trying to see if I can make my code work more flexibility with other data sources that sends information via CSV.

Alternatively, I tried using fgets but I hit into a problem because I do not know how to concatenate a new element at the end of that array. Take for e.g., a ticker has 100 bars and I wish to import the 101st bar in AFL, is it possible to ‘expand’ the array by 1 bar?

If u already have historical data then the below code will help,after loading click explore.
_SECTION_BEGIN(“back up of ami file”);
Filter = 1;

//AddColumn(OI,“Open Interest”);

I believe AddColumn merely adds that array to be displayed in the Output (aka. Results window). It does not expand the Open array by one more element.

To use a Python example, to append an element to a list, one calls mylist.append(12). In R, it is the function c(). I’m looking for an equivalent in Amibroker.

Alternatively, as per my first idea, is there a way to call an AFL function to import new OHLC information?


If you have data coming in CSV format, can you not just use the Import ASCII / Import Wizard? I know it works for End of Day data just fine. If you are trying to import some other (shorter) time frame, you would probably be better off getting a proper streaming quote service.

I don’t know of any AFL script to do import.

Hope this Helps.

I don’t think it’s possible from within an AFL formula itself, but it can be done as below.

1 Like

Thanks for your suggestions, Snoopy. I currently use Reuters Datalink to download EOD data into Amibroker and am using Import Metastock data (which is probably similar to the Import Wizard). As a programmer (Python and R), my natural instinct is to look for a programmatic solution to automate any boring repetitive tasks that I have to do everyday.

After posting my question, I did further searching and realised that in the new AB v6.2 (I’m still using v6.0), there is a new BATCH feature which supposedly can be called via AFL to load new data (look for header ‘Triggering batch run programmatically from AFL level’). Will be checking that out and reporting back if successful.

Thanks Helix, I clean forgot about the OLE! Will check it out too as it seems like a good alternative option (since it’s already available in my v6.0).

There are lots of misunderstandings / misconceptions here.

First and foremost, you need to understand that AmiBroker is MULTITHREADED application.
And that each AFL formula runs in DIFFERENT thread. The thread that is handling data is ALSO different than formula execution thread. Therefore any ‘import’ action triggered from AFL level would NOT be immediately reflected in current formula array but only possibly in NEXT execution.

Having said that, import can easily be triggered from AFL, without batch

AB = CreateObject("Broker.Application");
AB.Import( 0, "filename", "format.definition"); // see the footer note 1)

Batch can also be used but that is not the point.

The thing is that doing so to feed real time data is BAD idea in first place.

The ONLY correct way to implement streaming in EFFICIENT way is to write a proper data plugin using AmiBroker Development Kit (downloadable from

Note 1: 3rd argument in the Import call is format definition file:


Thanks for clearing up the misunderstanding / misconceptions, Tomasz!

Please allow me to share this csv import script. Especially those users who maintain multiple databases as doing it in the import wizard is risky i.e. importing wrong data into the database. Cleaning the database is pain. This script will ensure correct data is imported. You just need to change the directory of the import csv file and the import format(this is very important!) . Copy this code and save it to .js file in Amibroker/Scripts directory. You can run this script from the Amibroker Tool. (Please backup your database before you try it!)

	/* This Java Script will import EOD in CSV format as defined in custom2.format.
	   It will get the EOD data from Amibroker_Ouput_file folder and verify the current 
	   database. */
	/* Create AmiBroker app object */
	var AmiBroker = new ActiveXObject( "Broker.Application" );
	/* OLE Import */
	/* AmiBroker.Import(0,filename, "custom2.format"); */
	/* AmiBroker.SaveDatabase(); */
	/* Check is the current database is Data */

	if (AmiBroker.DatabasePath == "C:\\Program Files (x86)\\AmiBroker\\Data" ) {
		WScript.echo( "Updating US data in progress" );
		AmiBroker.Import(0,"C:\\Amibroker_Output_file\\US_YH_EOD.csv", "custom2.format");
		WScript.echo( "Updating US data completed" );
	/* Check is the current database is SGX Data */
	if (AmiBroker.DatabasePath == "C:\\Program Files (x86)\\AmiBroker\\SGX_Data") {
		WScript.echo( "Updating SGX data in progress" );
		AmiBroker.Import(0,"C:\\Amibroker_Output_file\\SGX_EOD.csv", "custom2.format");
		WScript.echo( "Updating SGX data completed" );


As a programmer (Python and R), my natural instinct is to look for a programmatic solution to automate any boring repetitive tasks that I have to do everyday

For eod import via a script refer to

For eod import via python refer to this

Thanks Peter and zbin! A lot more things for me to play around with!

Dear Peter,

For fail safe execution can a echo be passed “Wrong Database Error”, as also an auto abort.

This script will not cause import error. I have 2 databases and 2 csv files. What I am at one database, I run the script. The script will check which database I am at and import the correct csv file. It will do likewise when I am at the other database when I run the import script.