How to do External API Calls from Amibroker AFL?

Amibroker is a self-sufficient app to build, backtest and deploy your trading systems. It’s quite versatile in terms of its features and you don’t have to go to another app when you are developing strategies in Amibroker. However, there may be times when you need to fetch/send data from/to an external application to Amibroker. And the best way to do that would be to call an API from within the AFL code. In this article, we’ll learn how to do external API calls from Amibroker AFL.

Two Ways to do External API calls from Amibroker

Broadly, there are two ways using which you can invoke external APIs from Amibroker, and it depends on the version of Amibroker you are using:

Using InternetOpenURL Function – Amibroker version 6.20 or above

This is a handy way to invoke any API in just one line of code. And not just APIs, this function can fetch data from any HTTP web resource.




ih = InternetOpenURL( "https://www.quandl.com/api/v3/datasets/SEC/AAPL_SALESREVENUENET_Q.csv?api_key=" );
printf( "AAPL Revenue: " );
if( ih )
while( ( str = InternetReadString( ih ) ) != "" )
printf( "%s", str );
InternetClose( ih );

The above example is copied from Amibroker’s official documentation. This simple AFL code is using InternetOpenURL function to fetch the sales revenue data of the stock AAPL from Quandl using their REST API. The fetched data is then printed using the printf function.

The InternetOpenURL function supports only GET requests in Amibroker version 6.20. But from the 6.30 version onwards it supports POST requests as well.

Using VBScript – Amibroker version 3.80 or above

This is a legacy way of calling external API from AFL code. Here you can use embedded VBScript in the AFL to invoke the API. See the example below:

_SECTION_BEGIN("AFL API Call using VBScript");

APIOutput = "";



Public Sub callurl(id)


  Dim oStream

  Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP")

  url = "https://dummy.restapiexample.com/api/v1/employee/"+id

  oXMLHTTP.Open "GET", url, False

  oXMLHTTP.setRequestHeader "Cache-Control", "no-cache"

  oXMLHTTP.setRequestHeader "Pragma", "no-cache"


  AFL.Var("APIOutput") = oXMLHTTP.responseText

End Sub


pb = GetScriptObject();


printf("\nAPI Output:"+APIOutput);


The code enclosed within <% and %> is the VBScript code where we are using XMLHTTP functions to invoke a dummy REST API.

The GetScriptObject function in the AFL initializes the VBScript code. Then callurl function is invoked with a parameter “1”.

APIOutput is the variable that is exchanged between VBScript and AFL to store the API output.

Using VBScript you can invoke external APIs like the example above and also do several other things which are not possible natively in Amibroker.

Also Read: How to create an Amibroker AFL DLL Plugin? Step by Step Tutorial

Usecases for calling External APIs from Amibroker

Hope you’ve understood how to call external API calls from Amibroker. Now let’s look at some of the broad use cases for doing so:

  • Invoke broker APIs to punch the orders when a signal is received from AFL
  • External API fetch some data to use in the AFL strategy logic. Example: fundamental data such as revenue or P&L
  • API calls to fetch live news feed that can be displayed along with the chart
  • Execute strategy logic outside Amibroker using API calls to an external system.

Related Posts

Leave a Reply

Your email address will not be published.