Service call part of code not executing when running AFL as a Scanner

Hi,

I am making my own service call from AFL. Below is the code which does that. Today and even yesterday for some reason, cover order didn't fire at all. When debugging from a chart, everything is working fine. But when I run this AFL from a scanner keeping 1 recent bar then some times service is not getting called. From the scanner, I am running this AFL as 1 recent bar every 1-second automatically.

AFL is working perfectly fine when plotting BUY, SELL, SHORT, COVER arrays. Only the service call part of AFL is having a problem.

Please help, I am unable to figure out what error I am making.

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

/************** Begin Service Parameters *************/
SERVICE_URI = "http://localhost:50607/api/messages";
SERVICE_STRATEGY_CODE = "GUD01";
/************** End Service Parameters *************/

SymbolName=Name();

if(LastValue(Buy) || LastValue(Ref(Buy, -1))) {
	ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + "BUY";
	if(!Nz(StaticVarGet(ALERT_NAME))) {
		_TRACE("BEGINING TO SEND BUY ORDER FOR " + SymbolName);
		ServiceCall(SymbolName, 1, 1, SERVICE_URI, SERVICE_STRATEGY_CODE);
		StaticVarSet(ALERT_NAME, 1);
		_TRACE("BUY ORDER SENT FOR " + SymbolName);
	}
} else if(LastValue(Sell) || LastValue(Ref(Sell, -1))) {
	ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + "SELL";
	if(!Nz(StaticVarGet(ALERT_NAME))) {
		_TRACE("BEGINING TO SEND SELL ORDER FOR " + SymbolName);
		ServiceCall(SymbolName, 2, 1, SERVICE_URI, SERVICE_STRATEGY_CODE);
		StaticVarSet(ALERT_NAME, 1);
		_TRACE("SELL ORDER SENT FOR " + SymbolName);
	}
} else if(LastValue(Short) || LastValue(Ref(Short, -1)))  {
	ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + "SHORT";
	if(!Nz(StaticVarGet(ALERT_NAME))) {
		_TRACE("BEGINING TO SEND SHORT ORDER FOR " + SymbolName);
		ServiceCall(SymbolName, 3, 2, SERVICE_URI, SERVICE_STRATEGY_CODE);
		StaticVarSet(ALERT_NAME, 1);
		_TRACE("SHORT ORDER SENT FOR " + SymbolName);
	}
} else if(LastValue(Cover) || LastValue(Ref(Cover, -1))) {
	ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + "COVER";
	if(!Nz(StaticVarGet(ALERT_NAME))) {
		_TRACE("BEGINING TO SEND COVER ORDER FOR " + SymbolName);
		ServiceCall(SymbolName, 4, 2, SERVICE_URI, SERVICE_STRATEGY_CODE);
		StaticVarSet(ALERT_NAME, 1);
		_TRACE("COVER ORDER SENT FOR " + SymbolName);
	}
}

I have given a OR condition here, just to make sure nothing should be missed, what if the singal is generated almost at the end of the candle and before the scanner is run then new candle started, so for that reason i have made this work around. I don't know if thats right.

if(LastValue(Buy) || LastValue(Ref(Buy, -1))) {

Thanks and Regards,
Vinay

The AFL code above looks trivial. If the "service call part" does not work you should check the service part (that is NOT shown in the posted code - it is in separate include). How do I debug my formula?

Generally AFL works exactly the same regardless if it is executed as chart, or in scan.

This of course assumes that you are using proper, officially supported data source.

Thanks for the reply.

Regarding Data service provider, I am using reliable data provider Global Data Feeds, That's the recommended one for Amibroker.

Here is the service call code. Nothing problem with the service call code because I am using _TRACE and it is not getting recorded at all in the output window when the problem occurred. At least in the output window, I have to see Begining to Send Sell Order message in the trace window and that is not happening.

function ServiceCall(symbolName, orderType, tradeType, SERVICE_URI, SERVICE_STRATEGY_CODE) {
	
	symbolName = StrReplace(symbolName,"&","%26");
	uri = SERVICE_URI + "?StrategyCode=" + SERVICE_STRATEGY_CODE + 
			"&SymbolName=" + symbolName + "&TradeType=" + tradeType + "&OrderType=" + orderType;
	uri = uri + "&nocache=" + 100 * mtRandom();
	
	_TRACE(uri);
	
	ih = InternetOpenURL( uri ); 
	if( ih ) 
	{ 	
		 while( ( str = InternetReadString( ih ) ) != "" ) 
		 { 
			 printf( "%s", str ); 
		 } 
		 InternetClose( ih ); 
	}
}

One more thing I would like to bring to your notice. The conditions part ealier I used like shown below.

SymbolName=Name();
	TotalBars = BarCount;
	if(Buy[TotalBars - 1] || Buy[TotalBars - 2]) {

After the problem occurred today, I edited like shown below. Do you think if this was the problem ?

SymbolName=Name();

if(LastValue(Buy) || LastValue(Ref(Buy, -1))) {

Thanks,
Vinay

Also one more question. Is there a difference when using a scanner with Range as 1 recent Bar or 1 recent day related to AFL execution. It will only make a difference of the results displayed on a screen and nothing related to AFL execution, correct ?

Yes there is a difference. Recent Day is calendar day. Recent bar represent BARS in whatever periodicity is selected in the Settings. So it can be 1-minute bar for example.
AFL execution may be affected by selected range if QuickAFL is turned on. See http://www.amibroker.com/kb/2008/07/03/quickafl/

1 Like