2013 Feb: The Volatility (Regime) Switch Indicator by Ron McEwan

ICE Data Services -

VolatilitySwitch.efs  
EFSLibrary - Discussion Board  

File Name: VolatilitySwitch.efs

Description:
The Volatility (Regime) Switch Indicator by Ron McEwan

Formula Parameters:

VolatilitySwitch.efs

  • Volatility Days Period: 21

Notes:
The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
VolatilitySwitch.efs

VolatilitySwitch.efs

EFS Code:
VolatilitySwitch.efs

/*********************************
Provided By:  
eSignal (Copyright c eSignal), a division of Interactive Data 
Corporation. 2012. All rights reserved. This sample eSignal 
Formula Script (EFS) is for educational purposes only and may be 
modified and saved under a new file name.  eSignal is not responsible
for the functionality once modified.  eSignal reserves the right 
to modify and overwrite this EFS file with each new release.

Description:     
    The Volatility (Regime) Switch Indicator by Ron McEwan

Version:            1.00  12/10/2012

Formula Parameters:                         Default:
Volatility Days Period                      21

Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.

**********************************/

var fpArray = new Array();

function preMain()
{   
    setStudyTitle("Volatility Switch");      
    
    var x = 0;
    
    fpArray[x] = new FunctionParameter("gDays", FunctionParameter.NUMBER);
    with(fpArray[x++])
    {
        setName("Volatility Days Period");
        setLowerLimit(1);        
        setDefault(21);
    }     
}

var bInit = false;
var bVersion = null;

var xClose = null;

function main(gDays)
{
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    
    
    if(!bInit)
    {
        xClose = close();                
        bInit = true;
    } 

    if (xClose.getValue(-gDays*2) == null) return;
    addBand(0.5,PS_DASH,1,Color.red,0);
    
    var vDailyRet_0 = 0;
    var vDailyRet_1 = 0;    
    var nDailyRet = 0;
    
    var nSumDailyRet = 0; //for average
    var nSumDeviation = 0; //for deviation
    
    var i = 0;
    var j = 0;
    
    var nCnt = 0;
    var nAvg = 0;
    var nVol = 0;
    var nVol0 = 0;
    
    for (j=0; j>-gDays; j--)
    {
        //daily return and its average
        nSumDailyRet=0;
        for (i=j; i>j-gDays; i--)
        {
            vDailyRet_0 = xClose.getValue(i);
            vDailyRet_1 = xClose.getValue(i-1);
            nDailyRet = (vDailyRet_0-vDailyRet_1)/((vDailyRet_0+vDailyRet_1)/2);        
            
            nSumDailyRet +=  nDailyRet;
        }            
        nAvg = nSumDailyRet/gDays;       
        
        //21nd hist Volatility (standart deviation)
        nSumDeviation = 0; nVol=0;
        for (i=j; i>j-gDays; i--)
        {
            vDailyRet_0 = xClose.getValue(i);
            vDailyRet_1 = xClose.getValue(i-1);
            nDailyRet = (vDailyRet_0-vDailyRet_1)/((vDailyRet_0+vDailyRet_1)/2);
            
            nSumDeviation += (nAvg - nDailyRet)*(nAvg - nDailyRet);
        }    
        nVol=Math.sqrt(nSumDeviation/gDays);

        //remember 21nd hist Volatility for current bar
        if (j==0) nVol0=nVol;
        //compare 21nd hist Volatility for current and previous bars
        if (nVol<=nVol0) nCnt++;            
    }    
        
    var nVolSwitch = nCnt/gDays;
    return nVolSwitch;
}

function verify() {
    var b = false;
    if (getBuildNumber() < 779) {
        drawTextAbsolute(5, 35, "This study requires version 8.0 or later.", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "error");
        drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "upgrade");
        return b;
    } else {
        b = true;
    }
    return b;
}