Avoid syntax error in EnableScript

I am getting error30 when I try to parse JSON responses from Binance.
Screen Shot 2022-09-21 at 10.46.10 am

Full code here...

SECTION_BEGIN("Binance Signed API");
/*
Define the time, apikey, the BASE_URL, the url path (endpoint) and querystring.
Wallet endpoints for the query string are found at 
https://binance-docs.github.io/apidocs/spot/en/#wallet-endpoints

*/
time =InternetOpenURL("https://testnet.binance.vision/api/v3/time");
if( time )
{
     while( ( str = InternetReadString( time ) ) != "" )
     {
		extracted = StrMid(str,14,13);
         printf( "%s", extracted);
     }
     InternetClose( time );
}
apikey="API_KEY_HERE";
BASE_URL = "https://api1.binance.com";
url_path = "/sapi/v1/accountSnapshot";
query_string = "type=SPOT&recvWindow=5000&timestamp=" + extracted;

/*
Write a file "instring.txt" which contains the query string. 
Note that for signed APIs, at least timestamp must always be written to file but it is good to specify a recvwindow
also, after which the request should no longer be valid (in case of a delay getting the order to the broker)
Use shellexecute to hash the input of that file with the secret key and outoput that to file "outsig.txt" 
*/
fh = fopen( "C:\\Program Files\\AmiBroker\\Brokers\\Binance\\instring.txt", "w");
if( fh )
{
   fputs(query_string, fh );
   fclose( fh );
} 
ShellExecute("openssl", " dgst -sha256 -hmac API_SECRET_HERE -out outsig.txt  instring.txt", "C:\\Program Files\\AmiBroker\\Brokers\\Binance", 1);

/*
Pause to allow the script to complete writing "outsig.txt" 
*/
ThreadSleep(100);

/*
initialise signature variable. Define signature as the output from the file "outsig.txt".
Then remove "HMAC-SHA2-256(instring.txt)= " from that string using the StrReplace command
Define the URL by concatenating the base url, the url path (endpoint), "?", the query string, then 
"&signature=" and append it with your signature. Set header using the API key, then open the url, read
and print string to output.
*/
signature = "";
fh = fopen("C:\\Program Files\\AmiBroker\\Brokers\\Binance\\outsig.txt", "r");
if( fh )
{
   while( ! feof( fh ) )
   {
    signature = fgets( fh ); 
    signature = StrReplace(signature,"HMAC-SHA2-256(instring.txt)= ","");
    url = BASE_URL + url_path + "?" + query_string + "&signature=" + signature;
      
    InternetSetHeaders("X-MBX-APIKEY: " + apikey);
    io = InternetOpenURL(url);
    str = InternetReadString(io);
    printf( "%s", str );
	InternetClose( io );
	}
}
fclose(fh);
_SECTION_END();

_SECTION_BEGIN("Parse Binance JSON Response");
EnableScript("jscript");
ThreadSleep(100);
<%
	string_holding_json = str;
 
	obj = JSON.parse( string_holding_json );

	totalAssetOfBtc = obj.totalAssetOfBtc;
%>

scr = GetScriptObject(); 
printf( "JSON Total assets of BTC = %s", scr);

Am I doing this incorrectly?

In the obj1.something, does the something refer to a JSON heading in what is returned from the API?

I’ll play around with that when I get back from work this evening.

Thanks.

Just read how a JSON string is converted to an object. The hierarchy of JSON will be traversed using the dot-notation.
If Arrays are there, then you will use [].
It requires reading and go slow but steady.

ThreadSleep(100);

EnableScript("jscript");
<%
  function parse_js( json_string )
  {
	return JSON.parse( json_string );
  }
%>

function ParseJSON( json_string )
{
   script = GetScriptObject();
   return script.parse_js( json_string );
}

// In AFL, use the ParseJSON() afl function and send it the string

obj1 = ParseJSON( str );     // str from InternetReadString()
print( "%s", obj1.something1 );
print( "%s", obj1.something2 );  // Traverse the object as required.

There was typo in previous post

1 Like

You are getting an error because you are trying to use DEBUGGER. Debugger is able to debug ONLY AFL, not Javascript. Use "Syntax check" and you won't get an error.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.