HTTP REST API Call from AFL Algo

Hi,

I am very new to Amibroker and AFL language.
I am trying to call a REST GET API from my algo written in AFL, but it ends up opening the endpoint in new browser tabs for every call.
I am trying something like this -
AlertIf( True, "EXEC https://http.cat/100", "Hello World", 2 );

Can some please help me with this?

Thanks in advance.

1 Like

Use InternetOpenURL() function http://www.amibroker.com/f?internetopenurl

See also other threads on this forum:
https://forum.amibroker.com/search?q=internetopenurl

2 Likes

@myselfshiv, here is an example you can test with a working (fake) server:

Version (6.28); // tested version
// Fake Online REST API for Testing and Prototyping powered by JSON Server and lowdb 
URL = "https://jsonplaceholder.typicode.com/";

_TRACE("Getting 100 posts");
ih = InternetOpenURL( URL + "posts/");
if( ih )
{
     while( ( str = InternetReadString( ih ) ) != "" )
     {
         _TRACEF( "%s", str );
     }
     InternetClose( ih );
}

_TRACE("Getting 10 users");
ih = InternetOpenURL( URL + "users/");
if( ih )
{
     while( ( str = InternetReadString( ih ) ) != "" )
     {
         _TRACEF( "%s", str );
     }
     InternetClose( ih );
}
10 Likes

Thanks @Tomasz and @beppe

I was trying your suggestion and I am getting syntax errors. Do I need a specific version of Amibroker to be able to use the InternetOpenURL() ?
OR do I need to include/import any package to make this work?

Context - I am from Java background, trying my hands at AFL :expressionless:

In my example, I used version 6.28.0 Beta 64-bit, but the official documentation says that:

  • InternetOpenURL()
  • InternetReadString()
  • InternetClose()

were officially introduced in AmiBroker version 6.20.

No includes or imports are needed: ALL the AFL documented functions are directly callable from your formulas.

2 Likes

Is there any where I can download a trial verision for 6.20 or above?

No, the trial version is only 6.00. Any higher version requires license (purchase) .

ok thanks, this helps

Would it be possible to use InternetOpenUrl() to do the following please:

  • First AB finds buy signal
  • for each buy signal, query the REST API service for data such as market depth, VWAP etc
  • if buy depth >10 and VWAP <5, then send buy signal to the API to execute order

@C_M using InternetOpenURL() to query some data from a REST server is entirelye possible as demonstrated by the code samples I posted previously.

The data you'll receive will very probably be in JSON format, so you need to parse it and extract the relevant information for your formula. Then you can do whatever you want with such data!

There is one important caveat: be sure that your REST server will provide headers with "no-cache" otherwise you'll see stale data.
I did some test using a very basic nodejs/express and found that the AmiBroker function will not force a refresh of data for repeated requests of the same resources, but will simply get them from the cache. On the express server, I had to use a middleware function to avoid the issue.

5 Likes

@beppe I'm not 100% sure if this is applicable in this case, but I do a lot of web scraping / web data extraction (using also AmiBroker Internet functions for that) and at the beginning I had similar problems with stale/cached data in case of repetitive queries, but these problems were solved when I changed Temporary Internet Files settings - "Check for newer versions of stored pages" to Every time I visit the webpage

obraz

If you haven't done that already, you can check if this helps.

InternetOpenURL() is a WinINet function and WinINet is a part of Operating System also used by IE:

Best :slight_smile:

6 Likes

If remote REST server does not deliver fresh content, you can force it by adding extra dummy parameter with random value. This effectively disables any caching.

ih = InternetOpenURL( URL + "&dummy=" + 100 * mtRandom() );
5 Likes

In 6.30 you can use "no cache" (INTERNET_FLAG_RELOAD) flag, see: http://www.amibroker.com/devlog/wp-content/uploads/2018/11/readme6300.html

Version( 6.30 ); // required version

INTERNET_FLAG_RELOAD = 0x80000000;
INTERNET_FLAG_NO_COOKIES = 0x00080000;

// Example don't use cached content and don't use cookies
ih = InternetOpenURL( "http://www.google.com", INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_COOKIES );

InternetClose( ih );
6 Likes

HI, My broker requires checksum(SHA-256 OF api_key + request_token + api_secret) to further POST (REST API) request for issuing session token in the following format:-
curl https://api.kite.trade/session/token
-H "X-Kite-Version: 3"
-d "api_key=xxx"
-d "request_token=yyy"
-d "checksum=zzz"
I have value of xxx and yyy. Please suggests how to go about it in AFL. Thank you !Login Flow REST API