Custom Ticksize Function

Hi I'm using AB for the Indonesian Stock Market.

Here the stock tick differs depending on the price of the stock.

I've program a function to accommodate the tick size for different price stock. Here's my function :


function CustomTickSize(share_price)
{
	
	if (share_price <= 500)
	 {
		tick_size = 1;
	 
	 }

	if (share_price > 500 AND share_price <= 5000) 
	{
		tick_size =  5;
	
	}
	
	if (share_price > 5000)
	{
		tick_size = 25;
	}
	
	
	
	return tick_size;
}

and to use my custom function I tried use it to via my backtest script I tried this code :

ticksize = CustomTickSize(C[barindex()]);

It says Error 9 : Array Subscript has to be a number.

Can someone please advice. thanks.

BarIndex() returns array but not number.

function CustomTickSize(share_price)
{
	tick_size = (share_price <= 500) + 
				(share_price > 500 AND share_price <= 5000) * 5 +
				(share_price > 5000) * 25;	
	return tick_size;
}

TickSize = CustomTickSize(C);// array

Mandatory reading:

https://www.amibroker.com/guide/h_understandafl.html

https://www.amibroker.com/guide/a_mistakes.html

2 Likes

Thanks for the elegant reply and solution fxshrat. Much appreciated.

Hi, if you want to do it with even less code (and a bit faster) you can use:
(for this special example)

function CustomTickSize(share_price)
{
	return 5^( (share_price > 500) + (share_price > 5000) ) ;
}

Yes, it will work in that specific case but not if ticksize changes to something like this

function CustomTickSize(share_price)
{
	tick_size = (share_price <= 500) * 2 + 
				(share_price > 500 AND share_price <= 5000) * 5 +
				(share_price > 5000) * 20;	
	return tick_size;
}

Or adding further ticksize ranges

function CustomTickSize(share_price)
{
	tick_size = (share_price <= 500) * 2 + 
				(share_price > 500 AND share_price <= 1000) * 5 +
				(share_price > 1000 AND share_price <= 5000) * 10 +
				(share_price > 5000) * 25;	
	return tick_size;
}

So first version is quickly adjustable.

Thanks for the reply guys. Appreciate it but when I tried to use the function :

ticksize = CustomTickSize(C);

Somehow the exit price did not change to the desired price level (conform to the tick size) when I tried to backtest my script.

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