Limiting Calculation of composite to 1 day only

I am trying to limit number of bars for which i calculate a composite to make it faster for live trading. I use staticvaradd for composite, but it calculates the composite for the whole of data. To make my exploration faster i want to do it just for current day.

if ( Status( "action" ) == actionExplore )
{
if (Status("stocknum")==0)
{
	StaticVarRemove("~Total*");
	for (n=0;(symbol=StrExtract(List,n))!="";n++)
	{
		SetForeign(symbol);
		StaticVarAdd("~Totalcount",1);
		RestorePriceArrays();
	} 
}
}

Use SetBarsRequired() at the end of your code to limit the number of bars that are used.

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.

Read here:

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

1 Like

I tried with SetBarsRequired(), but did not see any improvement in time. Basically i want it to do calculation of past 1 5min bar, nothing else. So i tried using SetBarsRequired(1,0).

I see Milosz answering, so until then, do you really need a static Array or a single numeric Value (Scalar) in the first place.
StaticVarAdd is thread-safe and atomic in nature but you can write your semaphore if needed and its copy paste ready from the Manual.

1 Like

A quote from the above document:

Since AddToComposite creates artificial stock data it is desirable that it works the same regardless of how many ‘visible’ bars there are or how many bars are needed by other parts of the formula. For this reason internally AddToComposite does this:

SetBarsRequired( sbrAll, sbrAll );

which effectivelly means “use all available bars” for the formula. AddToComposite function simply tells the AFL engine to use all available bars (from the very first to the very last) regardless of how formula looks like. This is to ensure that AddToComposite updates ALL bars of the composite.

Summing up - if you use AddToComposite() you can not control the number of bars that are used, but you can if you Static Vars instead. I've been creating several composites using Static Variables which I use during my session and speed improvement is massive (like night and day - comparing to AddToComposite).

... but you need to tick this option in AA settings:

1

.... or use Indicator (chart window) for creating your composites and be able to benefit from using limited number of bars.

1 Like

Short answer is: don't do that.

Donald Knuth said once "Premature optimization is the root of all evil" and later "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. "

This is classic example. You are focusing on wrong part of the code.

You will get zero gain from trying what you are trying to do. Updating array using StaticVarAdd is order of magnitude faster than the rest of your code (SetForeign).

Instead all of the trickery you are doing, using list, extracting items, all those loops, REMOVE ALL code.

Do NOT call SetForeign inside loop in the first symbol.

Analysis window would go thru all symbols in multiple threads FASTER than you can do.

Just follow the manual. Correct code and fastest is already in the users guide: http://www.amibroker.com/guide/afl/staticvaradd.html

if( status("stocknum") == 0 ) 
{ 
// remove any earier composite values 
   StaticVarRemove("~Total*");
// and NOTHING else, you just don't need the rest of your code
} 

StaticVarAdd("~Totalcount",1); // that is ALL what is needed

SetForeign is seriously abused in many codes posted in various places. As I wrote, you don't need it for composites.

4 Likes

A lot of facts stated by you in the post :slight_smile:
OP should clarify which is now a pattern in many topics by OPs so no point highlighting.

After scratching my head, I thought, maybe those symbols run on "stocknum == 0" can obviously get atomic adds irrespective of the order of the watchlist but I assumed OP is using Symbols that are Not in the watchlist on which Exploration is run.

Yes, the most important thing in this case and the whole thread (as Tomasz wrote) is the fact, that StaticVarAdd() as a multithreading safe addition, can be used directly in Automatic Analysis' multi-threading environment (and for this reason it is the best and the fastest option when creating composites) and I also should have mentioned that - it is my fault.

But on the other hand, the OP didn't write anything about the true reason of using his code - what was he trying to achieve and how was he going to use it. His code (in it's current form) only counts the number of symbols in some String Variable - but that can be done without any loop or StaticVarAdd - simply using StrCount(). If he wanted to know how many symbols there are in a watchlist(s), that can be even done without any coding at all (i.e. in Filter settings). If he wanted to create a useful composite there should be some condition(s) (as shown in the StaticVarAdd documentation). Besides I don't understand how someone can use StaticVarAdd without referring to it's description in AB documentation (with a ready to use proper code) ... It's the first thing that should have been done.

Because @akshay.gupta55 code is not complete and I had no idea about the wider context in which it will be used (I was also busy yesterday), I decided not to comment it in any way and only respond to his main request regarding limiting the number of bars that are used when creating a composite, because I know from my experience, that in some cases it also helps a lot.

BTW some users on this forum have one thing in common - "the path of least resistance". They create a new thread, provide no details, don't do the necessary research before, paste some (sometimes random) code and watch how the discussion between other users unfolds. An ideal situation is when two (or more) experienced users "clash with each other" and compete who is going to provide a better code or explanation :wink:

1 Like

Maybe my mistake i have not mentioned the whole usecase here. So i am calculating a composite at stocknum==0 and then after that composite is used for taking signals on symbols and finally i use it for sending live orders. That's why i used stocknum==0 and setforeign, since i want exploration code to run once.

if ( Status( "action" ) == actionExplore )
{
if (Status("stocknum")==0)
{
	StaticVarRemove("~Total*");
	for (n=0;(symbol=StrExtract(List,n))!="";n++)
	{
		SetForeign(symbol);
		StaticVarAdd("~Totalcount",1);
		RestorePriceArrays();
	} 

Totalcount=StaticVarGet("~Totalcount");
buy=totalcount>20;
sell=totalcount<10;

}
}

Are you just trying to count the number of Symbols in the variable List?
@Milosz already posted using StrCount(), doesn't that serve the purpose ?

No counting symbols is just an example i posted, i m trying to calculate a composite from the watchlist which requires me to go through each and every symbol

1 Like

As @Milosz wrote you are making it difficult to help you. We are not here to do guessing games. If you want help you have to provide ALL information. The "example" of counting symbols is useless because a) it can be done without composites (StrCount) and b) it does not show actual problem

It is quite possible that composites are NOT needed AT ALL in your case. Composites are useful if they are calculated ONCE and used many times in many different places, not calculated every time.
If things are calculated every time the code is run, you usually don't need composites.

But again - this is guessing game. We are not here for playing games. If you want help you have to describe in great detail what you want. Details matter. It is ALL in the details.

As @Milosz wrote our responses are way longer than your "questions".

Use advice given here: How to ask a good question

2 Likes