Skip to main content

Enhancing Trading with API Integration in Amibroker AFL

Amibroker is a powerful tool for developing, testing, and implementing trading systems. Its comprehensive features mean you rarely need another app while crafting strategies. But sometimes, you might need to communicate with external applications from Amibroker. The best method for this is through API calls within the AFL code. This guide will show you how to make external API calls from Amibroker AFL.

Methods for External API Calls in Amibroker

There are two main methods for invoking external APIs in Amibroker, depending on the version you’re using:

InternetOpenURL Function – For Amibroker 6.20 and Above

InternetOpenURL function provides a straightforward way to call APIs or fetch data from any HTTP web resource in just one line of code.

How It Works:

InternetOpenURL(“URL”)

Example:

				
					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 );
}
				
			

In this example, taken from Amibroker’s official documentation, we use InternetOpenURL to retrieve Apple’s sales revenue data from Quandl. The data is then displayed using the printf function. Note that InternetOpenURL in version 6.20 supports only GET requests, but version 6.30 onwards supports POST requests as well.

Using VBScript – Amibroker 3.80 and Above

A legacy method of API calls in Amibroker is using embedded VBScript in AFL. Check out the example below:

				
					_SECTION_BEGIN("AFL API Call using VBScript");

APIOutput = "";

EnableScript("VBScript");

<%

Public Sub callurl(id)

  Dim oXMLHTTP

  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"

  oXMLHTTP.Send

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

End Sub

%>

pb = GetScriptObject();

pb.callurl("1");

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

_SECTION_END();
				
			

The VBScript code enclosed within <% and %> uses XMLHTTP functions to call a dummy REST API. The AFL initializes the VBScript code using GetScriptObject, then invokes callurl with “1” as a parameter. The variable APIOutput stores the API response, exchanging data between VBScript and AFL.

VBScript allows more complex operations that aren’t natively possible in Amibroker.

Related Reading: Creating an Amibroker AFL DLL Plugin: A Step-by-Step Guide

Why Make External API Calls from Amibroker?

Understanding external API calls in Amibroker can open up many opportunities. Here are some key applications:

  • Triggering broker APIs to place orders based on AFL signals
  • Fetching data like revenue or profit and loss figures for AFL strategy logic
  • Receiving live news feeds to display alongside charts
  • Executing strategy logic externally via API calls to another system

2 Comments

Leave a Reply