Limiting Number of bars a script can use

Hello,

I use intraday Tic data, at the start of a future contract when there is not many bars the AFL runs sweet, but by about halfway through till expiry and the barcount gets high, everything takes a bit longer and amibroker feels very laggy.

I would like to use all the data at end of day when i take a look at the bigger picture, but during the open times i only really look back a week or 2 at max.

Is there a way i can setup a parameter to adjust how many bars an AFL script will look back on the fly? without limiting database storage?

First of all you should take a look at your code and optimize it to make it run faster (e.g. check whether you have (nested) AFL loops in there or whether it does have SetBarsRequired(sbrall) in there). Also check AFL execution speed via Code Check & Profile feature of Formula editor to find out potential "time killers".

-> Performance tuning tips

-> QuickAFL (if not disabled -> see SetBarsRequired) takes care of how many bars are required for indicator calculation.


If you have inevitable process hungry formula (such as one iterating through multiple symbols) then you may execute such AFL code parts in certain intervals/ at certain events only.
http://www.amibroker.com/kb/2015/11/29/how-to-execute-part-of-the-formula-only-when-new-bar-is-added/
http://www.amibroker.com/kb/2015/10/06/how-to-run-certain-piece-of-code-only-once/

2 Likes

Thanks again for a quick reply :slight_smile:
The code runs fairly quickly, on the select 100000 bars it checks code on, but as soon as it starts using all the data bars it slows considerably.
i have read the setbars required and played around with different settings, but nothing improved.

in the amibroker main windows the performance load on AFL is 80ms 40% and data access time 489ms 244%
i am only running 10 symbols in this database
database

drawtime

_SECTION_BEGIN("Tick bids volume trials");
Ticker = Name();

Large = 10;
Giant = 20;
//TimeFrameSet( -1 );
TF = -20;
TF2 = -250;
BuyVolume = IIf( Close >= Aux2 AND (Aux2 != Aux1), Volume, 0 );
SellVolume = IIf( Close <= Aux1 AND (Aux2 != Aux1), Volume, 0 );


//////////////large volume////////////

largesize = Param("large volume size",8,2,50,2);
doublesize = Param("double volume size",5,2,10,1);
large_buy_volume = IIf(buyvolume>=largesize,volume,0);
Large_sell_volume = IIf(sellvolume>=Largesize,volume,0);

large_buy_volumeComp = TimeFrameCompress( large_buy_volume, TF, compressVolume );
large_buy_volumeExp	= TimeFrameExpand( large_buy_volumeComp, TF,  expandFirst );
StaticVarSet( "~large_buy_volume" + Ticker + TF, large_buy_volumeExp, False );

Large_sell_volumeComp = TimeFrameCompress( Large_sell_volume, TF, compressVolume );
Large_sell_volumeExp	= TimeFrameExpand( Large_sell_volumeComp, TF,  expandFirst );
StaticVarSet( "~Large_sell_volume" + Ticker + TF, Large_sell_volumeExp, False );

//////////////////////////////////////////////




////////////Giant Orders/stops/////
g_buy_orders = IIf(buyvolume>=giant,1,0);
g_sell_orders = IIf(sellvolume>=giant,1,0);

g_buy_ordersComp = TimeFrameCompress( g_buy_orders, TF, compressVolume );
g_buy_ordersExp	= TimeFrameExpand( g_buy_ordersComp, TF,  expandFirst );
StaticVarSet( "~gBuyVolume" + Ticker + TF, g_buy_ordersExp, False );

g_sell_ordersComp = TimeFrameCompress( g_sell_orders, TF, compressVolume );
g_sell_ordersExp	= TimeFrameExpand( g_sell_ordersComp, TF,  expandFirst );
StaticVarSet( "~gsellVolume" + Ticker + TF, g_sell_ordersExp, False );

//////////////////Number of buys or sells//////
buynum = IIf(buyvolume>=1,1,0);

buynumComp = TimeFrameCompress( buynum, TF, compressVolume );
buynumExp	= TimeFrameExpand( buynumComp, TF,  expandFirst );
StaticVarSet( "~buynum" + Ticker + TF, buynumExp, False );



Plot(buyvolume,"Buyvolume",colorGreen,styleHistogram,0,0);
Plot(Sellvolume,"Sellvolume",colorRed,styleHistogram,0,0);
_SECTION_END();

Cheers
Jon

There are AFL functions (such as TimeFrameCompress in your code) forcing calculation on all bars. So in upper QuickAFL link there is mentioned

So SetBarsRequired() gives you also ability to DECREASE the requirements of formula. This is done by placing SetBarsRequired at the END of your formula, as any call to SetBarsRequired effectively overwrites previously calculated estimate. So
if you write

x = Cum( 1 );
SetBarsRequired( 1000, 0 ); // use 1000 past bars DESPITE using Cum()

You may force AmiBroker to use only 1000 bars prior first visible even though Cum() by itself would require all bars.

http://www.amibroker.com/kb/2008/07/03/quickafl/

2 Likes

If you want to limit the number of bars that are used (and overwrite the default values) you have to place SetBarsRequired at the end of the code.

2 Likes

Yep , it's understood now, i was reading the function reference in AFL and it only mentioned placing it at the very beginning of a script..
but have been made aware of the reference in QUICK AFL.

thanks all

Cheers
Jon

You did not tell how many bars you have in File->Database Settings, but from the warnings you get (0.5 second to access data) I guess that you are likely to be using 10 (ten) million bars and majority of time spent is in the plugin, not in AmiBroker. Once plugin has control, it effectively blocks AmiBroker until data are delivered.

You should lower the number of bars in Database Settings.

Code check and profile uses upto 100K bars REGARDLESS of database settings and won't give you full picture about how your code would perform if you run it on 10 million bars. Actual chart timing is displayed when you turn appropriate option in Tools->Preferences, "Miscellaneous" tab

3 Likes

Thanks for the replys,

I am running high number of bars in the database.
The fix has been to run 2 different layouts with the daily with a SetBarsRequired(1000000); at the end of the script (gives me 4-5 days of tic data on fdax) that brings the chart draw time down to under 0.3 seconds and is much much better.
then when i want to look at the larger picture just load a different layout that doesn't have the restriction and close the plugin connection it's still laggy but manageable as i'm not trading off it.

Cheers
Jon