Please help to improve the performance of AFL

Hi Friends,

I'm new to AFL, learning the things, I have written a AFL which takes long time in execution .
AFL is running in 1 min timeframe.
error

Code :

dn = DateNum();
newDay = dn != Ref( dn, -1 );

function get_buffer( price, trade_type)
{
// trade_type= 10 OR 20
  buffer_price= Null;
  
  if(price<=70) 
  {
	mul			=int(price);
	round_val	=price-mul;
	if(trade_type==20)
	{
		if(round_val>=0.50){
			buffer_price=mul+1.05;	
		}
		else{
			buffer_price=mul+0.55;	
		}
	}
   }
  // else if ...
  return buffer_price;
}


function getSLPrice(Price, Trade)
{
	result=Null;
	for( i = 1; i < BarCount-1; i++ )
	{
	  if(Trade[i]==20 OR Trade[i]==10) 
		result[i]=get_buffer(price[i],trade[i]);
	  else if(NOT newDay[i])
		result[i]=result[i-1];
	}
	return result;
}

trailingTime=Param("Trailing Minutes", 5, 5,30,1);
timeframe=in1Minute*trailingTime;


sl10min = TimeFrameCompress( High, timeframe );

TimeFrameSet(timeframe);

timeElapsed_t=10;
//SetBarsRequired(800,-2 );
dnt = DateNum();
newDayt = dnt != Ref( dnt, -1 );

High_15min=IIf(newDayt, Null, Ref(High,-1));
Low_15min=IIf(newDayt, Null, Ref(Low, -1));

for( i = 1; i < BarCount; i++ )
{
	if (High_15min[i] !=0)
	{	
		if(High_15min[i-1] <High_15min[i] AND sl10min [i]> High_15min[i])
				High_15min[i]=High_15min[i-1];
	}
	
	if (Low_15min[i] !=0)
	{	
		if(Low_15min[i-1] >Low_15min[i] AND  sl10min [i]< Low_15min[i])
			Low_15min[i]=Low_15min[i-1];
	}
	
}

TimeFrameRestore();

High_15min_1	=TimeFrameExpand(High_15min, timeframe, expandFirst);
Low_15min_1		=TimeFrameExpand(Low_15min, timeframe, expandFirst);

// adding buffer price to traling sl as well. 
TradeType=20;
High_15min_1 	=getSLPrice(High_15min_1, TradeType);
TradeType=10;
Low_15min_1 	=getSLPrice(Low_15min_1, TradeType);

SellTrailingSL	=High_15min_1 ;
BuyTrailingSL	=Low_15min_1 ;

Please help me to improve the performance of AFL.

Thanks

No, it does not take a long time considering that the function is called more than 60000 times. The time per call is LESS than 1 millionth of the second (1 µs)

The only way to do it faster is to use ARRAY processing. Learn how to use it by reading the Users' Guide:
http://www.amibroker.com/guide/h_understandafl.html

The calculations you are doing do NOT need to be done bar by bar. You can achieve the same effect using IIF and ValueWhen functions and speed will be 10x faster at least.

Hi Tomasz,

Thanks for the help.
I'm unable to understand that total bar used 10383 and this function has been called around 60000. how this is happing here.

I will try to code the logic using IIF condition. I have tried initially but unbale to achieve the desired result so coded using for loop .

This is another snippet code from AFL.

SLLine=Null;
for( i = 1; i < BarCount; i++ )
{
	if(Buy_cond2[i] OR Sell_cond2[i])
	{
		 if(Buy_cond2[i]) SLLine[i]=LowestLow[i]; //
		 if(Sell_cond2[i]) SLLine[i]=HighestHigh[i]; //(i-1) get sl price above high
	}
	else if ( NOT newDay[i])
		SLLine[i]=SLLine[i-1];
}

I want to convert the above code using IIF and ValueWhen but I failed :slightly_frowning_face:

SLLine		=ValueWhen(Buy_cond2 OR Sell_cond2, IIf(Buy_cond2,LowestLow, IIf(Sell_cond2, HighestHigh, Null)));

There are many places where I failed writing code using ValueWhen and IIF then used loop, that's why my code is talking more time to execute.

Please correct my last for loop conversion using valuewhen and IIF condition.

Thanks

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

1 Like

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