2016 Oct: Which Trend Indicator Wins? by Markos Katsanos

ICE Data Services -

R2.efs, ADX.efs, ER.efs, VHF.efs  

EFSLibrary - Discussion Board  

File Name: R2.efs, ADX.efs, ER.efs, VHF.efs

Description:
Which Trend Indicator Wins? by Markos Katsanos

Formula Parameters:

R2.efs

  • Length: 18
  • R2 Smooth: 3
  • Trend: 0.42
  • Max R2: 0.85
  • Lag: 10
  • LR Crit: 10
  • MA Length: 50

ADX.efs

  • Length: 14
  • Trend: 30
  • Max ADX: 42
  • Lag: 12
  • Crit: 22
  • Mult: 1.8
  • MA Length: 50

ER.efs

  • Length: 24
  • ER Smooth: 3
  • Trend: 0.36
  • Max ER: 0.42
  • Lag: 3
  • Mult: 2.5
  • Crit: 0.26
  • MA Length: 50

VHF.efs

  • Length: 14
  • Trend: 30
  • Max VHF: 42
  • Lag: 12
  • Crit: 22
  • Mult: 1.8
  • MA Length: 50

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

Download File:
R2.efs
ADX.efs
ER.efs
VHF.efs

R2.efs, ADX.efs, ER.efs, VHF.efs

EFS Code:

R2.efs

/*********************************
Provided By:  
eSignal (Copyright c eSignal), a division of Interactive Data 
Corporation. 2016. 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:        
    Which Trend Indicator Wins? by Markos Katsanos

Version:            1.00  08/10/2016

Formula Parameters:                     Default:
Length                                  18
R2 Smooth                               3
Trend                                   0.42
Max R2                                  0.85
Lag                                     10
LR Crit                                 10
MA Length                               50



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(){
    setPriceStudy(false);
    setDefaultBarFgColor(Color.RGB(0,148,255),0);
    
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(18);
        setName("Length");
    }

    fpArray[x] = new FunctionParameter("Smooth", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(3);
        setName("R2 Smooth");
    }

    fpArray[x] = new FunctionParameter("Trend", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.42);
        setName("Trend");
    }

    fpArray[x] = new FunctionParameter("R2Max", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.85);
        setName("Max R2");
    }

    fpArray[x] = new FunctionParameter("Lag", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(10);
        setName("Lag");
    }

    fpArray[x] = new FunctionParameter("LRcrit", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(10);
        setName("LR Crit");
    }

    fpArray[x] = new FunctionParameter("MALength", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(50);
        setName("MA Length");
    }
}

var bInit = false;
var bVersion = null;
var xClose = null;
var xR2Lin = null;
var xSlope = null;
var xR2 = null;
var xSMA = null;

function main(Length, Smooth, Trend, R2Max, Lag, LRcrit, MALength){
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;
    
    if (getCurrentBarCount() <= (Length + Smooth + Lag)) return;
    
    if (getBarState() == BARSTATE_ALLBARS){
        xClose = null;
        xR2Lin = null;
        xSlope = null;
        xR2 = null;
        xSMA = null;
        bInit = false;
    }
    
    if (!bInit){
        xClose = close();
        xR2Lin = efsInternal("calc_R2", xClose, Length);
        if (xR2Lin.getValue(0) != null){
            xSlope = getSeries(xR2Lin, 0);
            xR2 = getSeries(xR2Lin,1);
        }
        xR2 = sma(Smooth, xR2);
        xSMA = sma(MALength, xClose);
        addBand(Trend, PS_DASH, 1, Color.RGB(195,195,195),0);
        bInit = true;
    }
 
    var nSlope = xSlope.getValue(0) * 100;
    var nR2 = xR2.getValue(0);

    if (getCurrentBarIndex() != 0){
        nR2_1 = xR2.getValue(-1);
        nR2_lag = xR2.getValue(-Lag);
        nClose = xClose.getValue(0);
        nClose_1 = xClose.getValue(-1);
        nSMA = xSMA.getValue(0);
        nSMA_1 = xSMA.getValue(-1);


        if (Strategy.isLong() && nClose_1 > nSMA_1 && nClose < nSMA)
            Strategy.doSell("MA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        else if (Strategy.isShort() && nClose_1 < nSMA_1 && nClose > nSMA)
            Strategy.doCover("XMA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);

        if ((!Strategy.isInTrade()) && nR2_1 < Trend && nR2 >= Trend && nR2 < R2Max && nR2 > nR2_lag){
            if (nClose > nSMA && nSlope > LRcrit)
                Strategy.doLong("STRONGTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            else if (nClose < nSMA && nSlope <= LRcrit)
                Strategy.doShort("STRONGDOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        }

        if (Strategy.isLong()) setBarBgColor(Color.RGB(0,60,0));
        else if (Strategy.isShort()) setBarBgColor(Color.RGB(170,46,46));
    }
    return nR2;
}

function calc_R2(xClose, Length){
    var X = 0, Y = 0, XY = 0, X2 = 0, Y2 = 0;
    
    for (var i = 0; i < Length; i++){
        var xVal = xClose.getValue(-i);
        X += (i + 1);
        Y += xVal;
        XY += ((i + 1) * xVal);
        X2 += ((i + 1) * (i + 1));
        Y2 += (xVal * xVal);
    }

    var xAvg = X / Length;
    var yAvg = Y / Length;
    var aSum1 = 0, aSum2 = 0;

    for (var i = 0; i < Length; i++){
        aSum1 += (i-xAvg) * (xClose.getValue(-i)-yAvg); 
        aSum2 += (i-xAvg) * (i-xAvg);
    }

    var nSlope = aSum1 / aSum2;
    var R2 = Math.pow((Length * XY - X * Y) / Math.sqrt((Length * X2 - X * X) * (Length * Y2 - Y*Y)),2);
    return new Array(nSlope, R2);
}

function verify(){
    var b = false;
    if (getBuildNumber() < 779){
        
        drawTextAbsolute(5, 35, "This study requires version 10.6 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;
}

ADX.efs

/*********************************
Provided By:  
eSignal (Copyright c eSignal), a division of Interactive Data 
Corporation. 2016. 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:        
    Which Trend Indicator Wins? by Markos Katsanos

Version:            1.00  08/10/2016

Formula Parameters:                     Default:
Length                                  14
Trend                                   30
Max ADX                                 42
Lag                                     12
Crit                                    22
Mult                                    1.8
MA Length                               50



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(){
    setPriceStudy(false);
    setDefaultBarFgColor(Color.RGB(0,148,255),0);
    
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(14);
        setName("Length");
    }

    fpArray[x] = new FunctionParameter("Trend", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(100);  
        setDefault(30);
        setName("Trend");
    }

    fpArray[x] = new FunctionParameter("AdxMax", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(100);  
        setDefault(42);
        setName("Max ADX");
    }

    fpArray[x] = new FunctionParameter("Lag", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(12);
        setName("Lag");
    }

    fpArray[x] = new FunctionParameter("Crit", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(100);  
        setDefault(22);
        setName("Crit");
    }

    fpArray[x] = new FunctionParameter("Mult", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.01);
        setUpperLimit(100);  
        setDefault(1.8);
        setName("Mult");
    }

    fpArray[x] = new FunctionParameter("MALength", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(50);
        setName("MA Length");
    }
}

var bInit = false;
var bVersion = null;
var xClose = null;
var xADX = null;
var xSMA = null;
var xLADX = null;

function main(Length, Trend, AdxMax, Lag, Crit, Mult, MALength){
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;
    
    if (getCurrentBarCount() <= Length) return;
    
    if (getBarState() == BARSTATE_ALLBARS){
        xClose = null;
        xADX = null;
        xSMA = null;
        xLADX = null;
        bInit = false;
    }
    
    if (!bInit){
        xClose = close();
        xADX = adx(Length,Length);
        xLADX = lowerDonchian(Lag, xADX);
        xSMA = sma(MALength, xClose);
        addBand(Trend, PS_DASH, 1, Color.RGB(195,195,195),1);
        bInit = true;
    }

    var nADX = xADX.getValue(0);

    if (getCurrentBarIndex() != 0){
        var nSMA = xSMA.getValue(0);
        var nSMA_1 = xSMA.getValue(-1);
        var nADX_1 = xADX.getValue(-1);
        var nClose = xClose.getValue(0);
        var nClose_1 = xClose.getValue(-1);
        var nLADX = xLADX.getValue(0);
        
        if (Strategy.isLong() && nClose_1 > nSMA_1 && nClose < nSMA)
            Strategy.doSell("MA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        else if (Strategy.isShort() && nClose_1 < nSMA_1 && nClose > nSMA)
            Strategy.doCover("XMA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        
        if (!Strategy.isInTrade()){
            if (nADX_1 < Trend && nADX > Trend && nADX < AdxMax){
                if (nClose > nSMA)
                    Strategy.doLong("STRONGTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
                else if (nClose < nSMA)
                    Strategy.doShort("STRONGDOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            }
            else if (nADX > (Mult * nLADX) && nADX_1 < Crit && nADX > Crit){
                if (nClose > nSMA)
                    Strategy.doLong("TREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
                else if (nClose < nSMA)
                    Strategy.doShort("DOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            }
        }
    
        if (Strategy.isLong()) setBarBgColor(Color.RGB(0,60,0));
        else if (Strategy.isShort()) setBarBgColor(Color.RGB(170,46,46))
    }

    return xADX.getValue(0);
}
        
function verify(){
    var b = false;
    if (getBuildNumber() < 779){
        
        drawTextAbsolute(5, 35, "This study requires version 10.6 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;
}

ER.efs

/*********************************
Provided By:  
eSignal (Copyright c eSignal), a division of Interactive Data 
Corporation. 2016. 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:        
    Which Trend Indicator Wins? by Markos Katsanos

Version:            1.00  08/10/2016

Formula Parameters:                     Default:
Length                                  24
ER Smooth                               3
Trend                                   0.36
Max ER                                  0.42
Lag                                     3
Mult                                    2.5
Crit                                    0.26
MA Length                               50



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(){
    setPriceStudy(false);
    setDefaultBarFgColor(Color.RGB(0,148,255),0);
    
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(24);
        setName("Length");
    }

    fpArray[x] = new FunctionParameter("Smooth", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(3);
        setName("ER Smooth");
    }

    fpArray[x] = new FunctionParameter("Trend", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.36);
        setName("Trend");
    }

    fpArray[x] = new FunctionParameter("ERMax", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.42);
        setName("Max ER");
    }

    fpArray[x] = new FunctionParameter("Lag", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(3);
        setName("Lag");
    }

    fpArray[x] = new FunctionParameter("Mult", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.01);
        setUpperLimit(100);  
        setDefault(2.5);
        setName("Mult");
    }

    fpArray[x] = new FunctionParameter("Crit", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.26);
        setName("Crit");
    }

    fpArray[x] = new FunctionParameter("MALength", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(50);
        setName("MA Length");
    }
}

var bInit = false;
var bVersion = null;
var xClose = null;
var xER = null;
var xSMA = null;
var xLER = null;

function main(Length, Smooth, Trend, ERMax, Lag, Mult, Crit, MALength){
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;
    
    if (getCurrentBarCount() <= (Length + Smooth + Lag)) return;
    
    if (getBarState() == BARSTATE_ALLBARS){
        xClose = null;
        xER = null;
        xSMA = null;
        xLER = null;
        bInit = false;
    }
    
    if (!bInit){
        xClose = close();
        xER = efsInternal("calc_ER",xClose,Length);
        xER = sma(Smooth, xER);
        xLER = lowerDonchian(Lag, xER);
        xSMA = sma(MALength, xClose);
        addBand(Trend, PS_DASH, 1, Color.RGB(195,195,195),1);
        bInit = true;
    }

    var nER = xER.getValue(0);

    if (getCurrentBarIndex() != 0){
        var nER_1 = xER.getValue(-1);
        var nER_lag = xER.getValue(-Lag);
        var nLER = xLER.getValue(0);
        var nSMA = xSMA.getValue(0);
        var nSMA_1 = xSMA.getValue(-1);
        var nClose = xClose.getValue(0);
        var nClose_1 = xClose.getValue(-1);
        
        if (Strategy.isLong() && nClose_1 > nSMA_1 && nClose < nSMA)
            Strategy.doSell("MA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        else if (Strategy.isShort() && nClose_1 < nSMA_1 && nClose > nSMA)
            Strategy.doCover("XMA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        
        if (!Strategy.isInTrade()){
            if (nER_1 < Trend && nER > Trend && nER < ERMax && nER > nER_lag){
                if (nClose > nSMA)
                    Strategy.doLong("STRONGTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
                else if (nClose < nSMA)
                    Strategy.doShort("STRONGDOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            }
            else if (nER > (Mult * nLER) && nER_1 < Crit && nER > Crit){
                if (nClose > nSMA)
                    Strategy.doLong("TREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
                else if (nClose > nSMA)
                    Strategy.doShort("DOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            }
        }
    
        if (Strategy.isLong()) setBarBgColor(Color.RGB(0,60,0));
        else if (Strategy.isShort()) setBarBgColor(Color.RGB(170,46,46));  
    }

    return nER;
}

function calc_ER(Close,Length){
    var nNetCh = Math.abs(Close.getValue(0) - Close.getValue(-Length));
    var nSum = 0;
    for (var i = 0; i < Length; i++){
        nSum += Math.abs(Close.getValue(-i) - Close.getValue(-i-1));
    }

    if (nSum == 0) return;
    ER = nNetCh / nSum;
    
    return ER;
}

function verify(){
    var b = false;
    if (getBuildNumber() < 779){
        
        drawTextAbsolute(5, 35, "This study requires version 10.6 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;
}

VHF.efs

/*********************************
Provided By:  
eSignal (Copyright c eSignal), a division of Interactive Data 
Corporation. 2016. 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:        
    Which Trend Indicator Wins? by Markos Katsanos

Version:            1.00  08/10/2016

Formula Parameters:                     Default:
Length                                  14
Trend                                   30
Max VHF                                 42
Lag                                     12
Crit                                    22
Mult                                    1.8
MA Length                               50



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(){
    setPriceStudy(false);
    setDefaultBarFgColor(Color.RGB(0,148,255),0);
    
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(36);
        setName("Length");
    }

    fpArray[x] = new FunctionParameter("Trend", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.38);
        setName("Trend");
    }

    fpArray[x] = new FunctionParameter("VHFMax", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setUpperLimit(1);  
        setDefault(0.45);
        setName("Max VHF");
    }

    fpArray[x] = new FunctionParameter("Lag", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(10);
        setName("Lag");
    }

    fpArray[x] = new FunctionParameter("Crit", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1);  
        setDefault(0.24);
        setName("Crit");
    }

    fpArray[x] = new FunctionParameter("Mult", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.01);
        setUpperLimit(100);  
        setDefault(1.5);
        setName("Mult");
    }

    fpArray[x] = new FunctionParameter("MALength", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(50);
        setName("MA Length");
    }
}

var bInit = false;
var bVersion = null;
var xClose = null;
var xVHF = null;
var xSMA = null;
var xLVHF = null;

function main(Length, Trend, VHFMax, Lag, Crit, Mult, MALength){
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;
    
    if (getCurrentBarCount() <= Length) return;
    
    if (getBarState() == BARSTATE_ALLBARS){
        xClose = null;
        xVHF = null;
        xSMA = null;
        xLVHF = null;
        xHighest = null;
        xLowest = null;
        bInit = false;
    }
    
    if (!bInit){
        xClose = close();
        xVHF = efsInternal("calc_VHF",xClose,Length);
        xLVHF = lowerDonchian(Lag, xVHF);
        xSMA = sma(MALength, xClose);
        addBand(0.35, PS_DASH, 1, Color.RGB(195,195,195),1);
        bInit = true;
    }

    nVHF = xVHF.getValue(0);

    if (getCurrentBarIndex() != 0){
        var nSMA = xSMA.getValue(0);
        var nSMA_1 = xSMA.getValue(-1);
        var nVHF_1 = xVHF.getValue(-1);
        var nVHF_lag = xVHF.getValue(-Lag);
        var nClose = xClose.getValue(0);
        var nClose_1 = xClose.getValue(-1);
        var nLVHF = xLVHF.getValue(0);
        
        if (Strategy.isLong() && nClose_1 > nSMA_1 && nClose < nSMA)
            Strategy.doSell("MA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        else if (Strategy.isShort() && nClose_1 < nSMA_1 && nClose > nSMA)
            Strategy.doCover("XMA", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
        
        if (!Strategy.isInTrade()){
            if (nVHF_1 < Trend && nVHF > Trend && nVHF < VHFMax && nVHF > nVHF_lag){
                if (nClose > nSMA)
                    Strategy.doLong("STRONGTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
                else if (nClose < nSMA)
                    Strategy.doShort("STRONGDOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            }
            else if (nVHF > (Mult * nLVHF) && nVHF_1 < Crit && nVHF > Crit){
                if (nClose > nSMA)
                    Strategy.doLong("TREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
                else if (nClose < nSMA)
                    Strategy.doShort("DOWNTREND", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
            }
        }
    
        if (Strategy.isLong()) setBarBgColor(Color.RGB(0,60,0));
        else if (Strategy.isShort()) setBarBgColor(Color.RGB(170,46,46))
    }

    return nVHF;
}

var xHighest = null;
var xLowest = null;

function calc_VHF(Close,Length){
    
    if (getBarState() == BARSTATE_ALLBARS){
        xHighest = upperDonchian(Length,Close);
        xLowest = lowerDonchian(Length,Close);
    }
    var nHighest = xHighest.getValue(0);
    var nLowest = xLowest.getValue(0);
    
    if (nHighest == null || nLowest == null) return;
    
    var nSum = 0;
    
    for (var i = 0; i < Length; i++){
        nSum += Math.abs(Close.getValue(-i) - Close.getValue(-i-1));
    }

    if (nSum == 0) return;
    VHF = (nHighest - nLowest) / nSum;
    
    return VHF;
}

function verify(){
    var b = false;
    if (getBuildNumber() < 779){
        
        drawTextAbsolute(5, 35, "This study requires version 10.6 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;
}