Using the code below, a security with exactly 20 bars of data produces SD output for 20 periods as all zero, with 18 periods only SD3 calculates, but it takes 10 periods for SD2 and SD1 to calculate. This happens also on securities with > 200 bars. What am I missing here?

function C_2_C( n, _close )
{
_ret = log( _close ) - log( Ref( _close, -1 ) );
_avg = 1 / n * Sum( _ret, n );
_vol = 1 / ( n - 1 ) * Sum( ( _ret - _avg ) ^ 2, n );
return _vol;
}
function Yang_Zhang_Vol( n, _open, _high, _low, _close )
{
_N_o = log( _open ) - log( Ref( _close, -1 ) ); // normalized open
_N_u = log( _high ) - log( _open ); // normalized high
_N_d = log( _low ) - log( _open ); // normalized low
_N_c = log( _close ) - log( _open ); // normalized close
V_rs = 1 / n * Sum( _N_u * ( _N_u - _N_c ) + _N_d * ( _N_d - _N_c ), n ); // RS volatility estimator
_N_o_avg = 1 / n * Sum( _N_o, n );
V_o = 1 / ( n - 1 ) * Sum( ( _N_o - _N_o_avg ) ^ 2, n );
_N_c_avg = 1 / n * Sum( _N_c, n );
V_c = 1 / ( n - 1 ) * Sum( ( _N_c - _N_c_avg ) ^ 2, n );
k = 0.34 / ( 1.34 + ( n + 1 ) / ( n - 1 ) );
V_yang_zhang = V_o + k * V_c + ( 1 - k ) * V_rs;
return V_yang_zhang;
}
Periods = Min( LastValue(BarCount-1), 200 );
SD1 = sqrt( Yang_Zhang_Vol( Periods, O, H, L, C ) ) * sqrt( 252 );
SD2 = sqrt(C_2_C(Periods,C)) * sqrt(252);
SD3 = StDev(log(C/Ref(C,-1)),Periods) * sqrt(252);

Understood. Unfortunately, AMA would not work as a replacement because of the way the fraction is applied (on total sum not per bar), so I just made Periods = Min( round((BarCount-1)/2), 200 ).

I'm running into this problem again, but this time with the built-in StdDev (log(c/Ref(C,-1)). Is there any way to identify the absolute minimum number of bars necessary for a successful calculation result?

For instance, if I have 1488 bars of data, then no length in StdDev lower than roughly 1488 / 2.4 will work.

Are you sure your data is good? Typically I can calculate StDev(array,N) as long as I have N+2 bars of data available. I suggest using an exploration to output your close and StDev values for every bar to see if you can find any anomalies.

Data is fine. I haven't had a chance to attack it again, but I recall that N+2 has worked in previous StdDev instances, so not sure why it would be different this time around. But there is a MA in the StdDev, so doesn't that imply you need at least N / 2 + 1 minimum bars?

I'm also seeing differences in the number of total bars between Barcount and BarIndex().

I'm also seeing differences in the number of total bars between Barcount and BarIndex().

@TraderX, this is nomal. BarIndex() returns zero-based bar number, so if you have for examplle 100 bars (BarCount == 100) and BarIndex array indexes will be 0, 1, 2, ..., 99