VBScript is unable to make webservice call

Hi,

I am not able to make a call to API service from AFL using VBScript. Same HTTP post call I was able to successfully make it using PostMan, but not from AFL. Its not even throwing any error. Also, I checked the response and I got some text which was in Chinese which I couldn't understand. Please help.

Is there any other better way to make HTTP Post calls using Amibroker. I am using Amibroker 6.2.

Below is the source code called ServiceLib.afl, which I have placed in the include folder.

_SECTION_BEGIN("Service Library 1.0");

EnableScript("VBScript");

SERVICE_URI = ParamStr("Service URI","http://localhost:50607/api/messages");
SERVICE_AUTHORIZATION_CODE = ParamStr("Service Authorization Code","OMSUX-001");
SERVICE_AUTHORIZATION_KEY = ParamStr("Service Authorization Key","7ca134b4-4905-442b-a3f2-9616d315dc1f");
SERVICE_STRATEGY_CODE = ParamStr("Service Strategy Code","GUD01");

<% 

Public Sub ServiceCall(SymbolName, OrderType)


strJSON = "{""AuthorizationCode"":""" & AFL("SERVICE_AUTHORIZATION_CODE") & """,""AuthorizationKey"":""" & AFL("SERVICE_AUTHORIZATION_KEY") & """," & _
					 """StrategyCode"":""" & AFL("SERVICE_STRATEGY_CODE") & """,""SymbolName"":""" & SymbolName & """,""OrderType"":" & OrderType & "}"

'MsgBox strJSON, vbOK, "ServiceCall"
Dim objXmlHttpMain

set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP") 
on error resume next 
objXmlHttpMain.open "POST", AFL("SERVICE_URI"), False
objXmlHttpMain.setRequestHeader "Content-Type", "application/json"

objXmlHttpMain.send strJSONToSend

'MsgBox objXmlHttpMain.responseBody

set objXmlHttpMain = nothing

If Err.Number <> 0 Then
  MsgBox  "Error: " & Err.Description
  Err.Clear
End If


'MsgBox strJSON, vbOK, "ServiceCall"


End Sub 

%> 

function SendOrder(OrderType)
{
	local type, SymbolName;
	
	type="";
	SymbolName=Name();
	
	switch(OrderType)
	{
		case 1:
			type="Buy";
			break;
		case 2:
			type="Sell";
			break;
		case 3:
			type="Short";
			break;
		case 4:
			type="Cover";
			break;
	}
	vbScriptObject = GetScriptObject(); 
		vbScriptObject.ServiceCall(SymbolName, OrderType);
	/*	
	if (LastValue(ValueWhen(Ref(Buy,-1),BarIndex())==BarIndex()) AND StaticVarGet(SymbolName+GetChartID()+type+"Alert")==0 )
	{
		vbScriptObject = GetScriptObject(); 
		vbScriptObject.ServiceCall(SymbolName, OrderType);
		StaticVarSet(SymbolName + GetChartID() + type+"AlertBar", LastValue(TimeNum()));
	}
	if (LastValue(TimeNum()) == StaticVarGet(SymbolName+GetChartID()+type+"AlertBar"))
		StaticVarSet(SymbolName+GetChartID()+type+"Alert",1); //alert was triggered, no more alerts on this bar
	else
		StaticVarSet(SymbolName+GetChartID()+type+"Alert",0); // new bar formed, and alerts can be trigered.
*/
}
function SendBuyOrder() {
	SendOrder(1);
}
function SendSellOrder() {
	SendOrder(2);
}
function SendShortOrder() {
	SendOrder(3);
}
function SendCoverOrder() {
	SendOrder(4);
}

_SECTION_END();

This is how I am calling in my main AFL file at the end.

_SECTION_BEGIN("SEND ORDERS");
SEND_ORDERS = ParamToggle("Send Live Orders?", "NO|YES");

#include_once "Formulas\Include\ServiceLib.afl";

if(SEND_ORDERS) {
	
	if(LastValue(Buy)) {
		SendBuyOrder();
	} else if(LastValue(Sell)) {
		SendSellOrder();
	} else if(LastValue(Short)) {
		SendShortOrder();
	} else if(LastValue(Cover)) {
		SendCoverOrder();
	}
}
_SECTION_END();

Yes there is: RFC: InternetPostRequest

Hi Tomasz,

Can it be done in 6.2 or 6.0? I knew about InternetPostRequest. HOw to achieve in older versions of Amibroker? Is it possible to use any other technique for older versions?

6.30 upgrade in general is definitely worth it and 6.30.4 is the minimum for that function.

As an alternative, you can try the CreateObject() way using either XMLHTTP or ServerXMLHTTP MS objects ie. whichever suits your need.
This should work in older versions although i haven't tested myself.

I am trying to avoid using CreateObject. Is it possible to use Amibroker Development Kit and build a function using Curl++ for making HTTP calls and use that in AFL? Also, can you point me to an article on how to build a simple plugin using ADK (I did go through the documentation, but didn't find info on how to call the plugin from AFL)? I have already downloaded ADK from the downloads section.

My answer now is just loosely based on what i undesrstand mostly from TJs replies.

I dont know why you would go the DLL way, as even those AFL function calls will be locked by critical section.
What is the reason to avoid CreateObject? Anyway, TJ mentioned another way by runnning a local server also that you can use instead.

go through it properly, and also study the sample codes. There are many details explained directly in the sample codes contextually that are not in the general doc.

Maybe you can explain, but i have no idea as to what performance gain you will be targeting with the plugin when something is already implemented with so many checks as a built-in fn.

Just upgrade to 6.30 that is simple answer and solution.

2 Likes