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