Skip to main content

Volatility Estimator for Amibroker (AFL)

sergeant007 over 10 years ago Amibroker (AFL)

  • Rating:
    4 / 5 (Votes 7)
  • Tags:
    oscillator, amibroker, function

This AFL calculates various volatility estimators (including Yang-Zhang volatility estimator). For detailed algorithm, see this site: http://atmif.com/papers/range.pdf

Indicator / Formula

Copy & Paste Friendly
/////////// 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;
}

7 comments

6. halfman
over 10 years ago

Thanks for a great indicator. It’s working. Add plot to draw this indicator. For ex : Yang_Zhang_vol( n, _open, _high, _low, _close ) -- > Plot(Yang_zhang_vol (C, O, H, L, C), "Yang Zhang", colorBlack, styleOwnScale);

n is a free paramater. You can use closing price, or other prices you like. I’m still trying to find out how to use this indicator.

Leave Comment

Please login here to leave a comment.