Replacing addtocomposite with staticvaradd

Following is my code which calculates composite rsi from different timeframes:

Count = 0; 
result=0;

for( i = 5; i <= 30; i++ ) 
{ 
   TimeFrameSet( i * in1Minute ); 
   r=  RSI(14);
   TimeFrameRestore(); 
   r = TimeFrameExpand( r, i * in1Minute );
   result=result+r;   
   Count++; 
} 

comp_r=AddToComposite(result/count,"~comp_r","x");

Basically i wanted to make my code more faster and replace addtocomposite with staticvaradd. If i just replace the addtocomposite line with staticvaradd("comp_r",result/count), it does not work. I am basically running this in backtest for 5 yr data with 1min bar.

Can anyone guide in making the above code more efficient ?
@Tomasz Please guide in changing addtocomposite with staticvaradd.

StaticVarAdd( "comp_r",result/count, True, True );

Retrieving the result

comp = StaticVarGet( "comp_r" );
3 Likes

You have just made it to persist. Making persist will not change much, except static variable will be saved.

The admin has marked my post being the solution.
Have you seen StaticvarGet in my post?

Full code of using StaticVarAdd instead of AddToComposite is actually included in the manual:

http://www.amibroker.com/guide/afl/staticvaradd.html

// example from the manual 
// http://www.amibroker.com/guide/afl/staticvaradd.html
if( status("stocknum") == 0 )
{
     // remove any earier composite values
   StaticVarRemove("~Composite");
}

StaticVarAdd( "~Composite", MACD() > Signal() );
Buy = 0;

The thing is that you should REMOVE previous data if you want to re-run such formula. Otherwise it will add it over and over again. AddToComposite internally by default deleted old data on first symbol (with default atcFlagDeleteValues flag). So you need to do the same with StaticVarRemove.

And you need to use of course version 6.20 or higher.

3 Likes

Thanks @codejunkie and @Tomasz. Everything works fine. I was wondering if we have to use it in Live Auto trading , will the implementation change ?

1 Like