@Sallre,

yes, I figured that I have misunderstood.

Here are two StaticVar versions for per bar sum using TimeFrame functions.

Side note, I could never really get **TimeFrameMode**(…) to work for base time interval being tick and doing calculations for lower granularity such as 20R (and other range bars intervals) or 200T (and other tick intervals) etc… So for those ones the upper ATC version could be better to sum up something per bar, IMO. Maybe Tomasz has a working Static Var version example for non-timebased intervals.

Anyway, here are two sum-up-per-bar versions for time based intervals using StaticVars…

**First one, non-loop version:**

```
// Apply via Scan
if ( Status( "action" ) == actionScan ) {
Buy = 0;
array = OI;
tmfrm = 60*60;// choose Interval here
expandmode = expandPoint;
TimeFrameSet( tmfrm );
bi = BarIndex();
TimeFrameRestore();
bi = Nz(TimeFrameExpand( bi, tmfrm, expandmode ));
newbar = Ref( bi > 0, -1 );
sumOI = SumSince( newbar, array ) + ValueWhen( newbar, array );
// store OI Sum to static var in BASE TIME INTERVAL
StaticVarSet( "SumOfOI_" + Name(), sumOI );
// store Interval
StaticVarSet( "SumOfOI_" + Name() + "_Interval", tmfrm );
}
```

For plotting you may use

```
OIsum = StaticVarGet( "SumOfOI_" + Name() );
staticint = Nz(StaticVarGet( "SumOfOI_" + Name() + "_Interval" ));
if( Interval() > staticint )
Error( "Set to an interval being of higher or equal granularity than " + staticint/60 + "-Min" );
else
Plot(OIsum, "Sum of OI - " + staticint/ 60 + "-Min", colorRed, styleHistogram, Null, Null, 0, 0, -60 );
//StaticVarRemove( "SumOfOI_" + Name() + "*" );
```

**2nd one, loop version (slower):**

```
// Apply via Scan
if ( Status( "action" ) == actionScan ) {
Buy = 0;
array = OI;
expandmode = expandPoint;
for( i = 5; i <= 60; i += 5 ) {
tmfrm = 60 * i;
TimeFrameSet( tmfrm );
bi = BarIndex();
TimeFrameRestore();
bi = Nz(TimeFrameExpand( bi, tmfrm, expandmode ));
newbar = Ref( bi > 0, -1 );
sumOI = SumSince( newbar, array ) + ValueWhen( newbar, array );
// store OI Sum to static var in BASE TIME INTERVAL
StaticVarSet( "SumOfOI_" + Name() + tmfrm, sumOI );
// store Interval
StaticVarSet( "SumOfOI_" + Name() + "_Interval_" + tmfrm, tmfrm );
}
}
```

For plotting you may use

```
OIsum = StaticVarGet( "SumOfOI_" + Name() + Interval() );
staticint = Nz(StaticVarGet( "SumOfOI_" + Name() + "_Interval_" + Interval() ));
if( Interval() > staticint )
Error( "Set to an interval being of higher or equal granularity than " + staticint/60 + "-Min" );
else
Plot(OIsum, "Sum of OI - " + staticint/ 60 + "-Min", colorRed, styleHistogram, Null, Null, 0, 0, -60 );
//StaticVarRemove( "SumOfOI_" + Name() + "*" );
```