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.
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: 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. Syntax: InternetOpenURL(“URL”’) Example: 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. 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: 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 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: Two Ways to do External API calls from Amibroker
Using InternetOpenURL Function – Amibroker version 6.20 or above
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 );
}
Using VBScript – Amibroker version 3.80 or above
_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();
Usecases for calling External APIs from Amibroker