# Estimating volatility - Amibroker code



## Gringotts Bank (15 March 2016)

A guy by the name of sergeant007 on wisestocktrader posted this.  Worth sharing.  




/////////// the preferred volatility estimator is Yang-Zhang volatility estimator
// all the estimators compute sigma^2

// the preferred volatility estimator
// for more info, see paper "Drift-independent Volatility Estimation Based on High, Low, Open and Close Prices"
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 ;

}

// the Parkinson volatility estimator
function Parkinson_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_p = 1 / ( n * 4 * log( 2 ) ) * Sum( ( _N_u - _N_d ) ^ 2, n );

    return V_p;
}

// volatility recommended by Rogers AND Satchell (1991) AND Rogers, Satchell, AND Yoon (1994)
function RS_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 );

    return V_rs;
    }

// the traditional close-to-close volatility
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;
}

//example plot 
Plot( RS_vol( 10, Open,High,Low,close), "example", colorRed );


----------



## Newt (15 March 2016)

Thanks GB.  Some related background light reading:

http://atmif.com/papers/range.pdf
http://www.todaysgroep.nl/media/236846/measuring_historic_volatility.pdf


----------



## Gringotts Bank (15 March 2016)

Newt said:


> Thanks GB.  Some related background light reading:
> 
> http://atmif.com/papers/range.pdf
> http://www.todaysgroep.nl/media/236846/measuring_historic_volatility.pdf




Thanks Newt, I didn't realize it was for options.  Now I see the word 'drift'.  Not sure if appropriate for equities.


----------

