Replacing addtocomposite with staticvaradd

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

Count = 0; 

for( i = 5; i <= 30; i++ ) 
   TimeFrameSet( i * in1Minute ); 
   r=  RSI(14);
   r = TimeFrameExpand( r, i * in1Minute );


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" );

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:

// example from the manual 
if( status("stocknum") == 0 )
     // remove any earier composite values

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.


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