2017 Jan: Mean-Reversion Swing Trading by Ken Calhoun

ICE Data Services -

Mean_Reversion_Swing.efs  

EFSLibrary - Discussion Board  

File Name: Mean_Reversion_Swing.efs

Description:
Mean-Reversion Swing Trading by Ken Calhoun

Formula Parameters:

Mean_Reversion_Swing.efs

  • Slope: 1
  • Trigger: 0.5

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

Download File:
Mean_Reversion_Swing.efs

Mean_Reversion_Swing.efs

EFS Code:

/*********************************
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:        
    Mean-Reversion Swing Trading by Ken Calhoun

Version:            1.00  11/09/2016

Formula Parameters:                     Default:
Slope                                   1
Trigger                                 0.5



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(true);
    setStudyTitle("MR Swing");
    
    var x = 0;
    fpArray[x] = new FunctionParameter("Slope", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.01);
        setUpperLimit(100);  
        setDefault(1);
        setName("Slope");
    }
    fpArray[x] = new FunctionParameter("Trigger", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.000001);
        setDefault(0.5);
        setName("Trigger");
    }
}

var bInit = false;
var bVersion = null;
var xDHigh = null;
var xHigh = null;
var xDLow = null;
var xLow = null;
var xDDateTime = null;
var bInSwing = false;
var bSwFinished = false;
var nSwBarsBack = 0;
var nMeanRev = 0;
var nMeanHigh = 0;
var nSwHigh = 0;
var bMRFound = false;
var dMeanRevD = null;
var bFindBO = false;

function main(Slope, Trigger){
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;

    if (getBarState() == BARSTATE_ALLBARS){
        bInSwing = false;
        bSwFinished = false;
        nSwBarsBack = 0;
        nMeanRev = 0;
        nMeanHigh = 0;
        nSwHigh = 0;
        bMRFound = false;
        dMeanRevD = null;
        bFindBO = false;
        bInit = false;
    }
    
    if (!bInit){
        xDHigh = high(inv("D"));
        xDLow = low(inv("D"));
        xDDateTime = rawtime(inv("D"));
        xLow = low();
        xHigh = high();
    }

    nDHigh = xDHigh.getValue(0);
    var slopeFinished = false;
    var i = 0;
    var swingLen = 0;

    while (!slopeFinished){
        if (xDHigh.getValue(i) > xDHigh.getValue(i-1)){
            swingLen++;
            i--;
        }
        else slopeFinished = true;
    }

    if (bInSwing && swingLen == 0){
        nSwBarsBack = 0;
        bSwFinished = true;
        bInSwing = false;
    }

    if (bSwFinished && nMeanRev != 0){
        nSwBarsBack++;
        if (xDLow.getValue(-1) < nMeanRev && day(0) != day(-1)){
            drawLineRelative(-1, nMeanRev, -nSwBarsBack, nSwHigh, PS_SOLID, 3, Color.red, 
                            ("Mean rev. " + (rawtime(-nSwBarsBack))));
            dMeanRevD = xDDateTime.getValue(-1);
            nMeanHigh = nSwHigh;
            nSwBarsBack = 0;
            bSwFinished = false;
            bMRFound = false;
        }
    }

    if (!bSwFinished && !bInSwing && !bMRFound  && nMeanRev != 0 && xDDateTime.getValue(-1) == dMeanRevD){
        if (xHigh.getValue(0) >= nMeanRev + Trigger){
            drawTextRelative(0, BelowBar1, "\u00E9", Color.green, null, 
                            Text.PRESET|Text.CENTER, "Wingdings", 10, "Entry"+rawtime(0));
            drawTextRelative(0, BelowBar2, "MR", Color.green, null, 
                            Text.PRESET|Text.CENTER|Text.BOLD, "Arial", 8, "Entry text"+rawtime(0));
            bMRFound = true;
            bFindBO = true;
            nMeanRev = 0;
        }
    }

    if (bFindBO &&  xHigh.getValue(0) >= nMeanHigh){
        drawTextRelative(0, AboveBar1, "\u00E9", Color.green, null, 
                            Text.PRESET|Text.CENTER, "Wingdings", 10, "2nd Entry"+rawtime(0));
        drawTextRelative(0, AboveBar2, "BO", Color.green, null, 
                            Text.PRESET|Text.CENTER|Text.BOLD, "Arial", 8, "2nd Entry text"+rawtime(0));
        bFindBO = false;
    }
        
    if (swingLen >=4){
        var nSStart = getFirstBarIndexOfDay(xDDateTime.getValue(-(swingLen)));

        if(nSStart != null){
            if (((nDHigh - xDHigh.getValue(-(swingLen)))/(xDLow.getValue(0) - xDLow.getValue(-(swingLen)))) > Slope){
                nSwHigh = nDHigh;
                var nSwLow = xDLow.getValue(-(swingLen));
                nMeanRev = (nSwHigh + nSwLow)/2;
                drawLineRelative(0, nDHigh, (nSStart - getCurrentBarIndex()), xDLow.getValue(-(swingLen)), 
                                PS_SOLID, 3, Color.green, "Uptrend" + xDDateTime.getValue(-(swingLen)));
                bInSwing = true;
                bSwFinished = false;
                nSwBarsBack = 0;
            }
            else{
                bInSwing = false;
                bSwFinished = true;
            }
        }
    }    
}

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;
}