2015 Apr (Feb): Kiss & Touch With The Modified True Range by Chris Lindgren

ICE Data Services -

 

ModifiedTrueRange.efs  

EFSLibrary - Discussion Board  

 

File Name: ModifiedTrueRange.efs

 

Description:
Kiss & Touch With The Modified True Range by Chris Lindgren

 

Formula Parameters:


ModifiedTrueRange.efs

  • Period: 20
  • Grouping Offset: 1
  • Goal: 9.99

Notes:

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

 

Download File:
ModifiedTrueRange.efs

ModifiedTrueRange.efs



EFS Code:

ModifiedTrueRange.efs

/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2015) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release. 

Description:        
    Kiss & Touch With The Modified True Range by Chris Lindgren
    
Version:            1.00  02/06/2015

Formula Parameters:                     Default:
Period                                  20 
Grouping Offset                         1
Goal                                    9.99

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("ModifiedTrueRange");
    
    setCursorLabelName("GoalLevel", 0);
    setCursorLabelName("MTR", 1);
    
    setDefaultBarFgColor(Color.yellow, 0);
    setDefaultBarFgColor(Color.grey, 1);

    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_HISTOGRAM, 1);

    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(3, 1);

    var x = 0;

    fpArray[x] = new FunctionParameter("fpPeriod", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("Period");
        setLowerLimit(1);
        setDefault(20);
    }

    fpArray[x] = new FunctionParameter("fpGroupOffset", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("Grouping Offset");
        setLowerLimit(1); 
        setDefault(1);
    }

    fpArray[x] = new FunctionParameter("fpGoal", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("Goal");
        setLowerLimit(0);
        setDefault(9.99);
    } 
}

var bInit = false;
var bVersion = null;

var xClose = null;
var xLowest = null;
var xHighest = null;
var xRanges = null;

var xMTR = null;
var xRange = null;

var xHitGoal = null;
var xAvgMTR = null;
var xMTRStdDev = null;

var aLabels = [];
var nButtonSpace = 5;

function main(fpPeriod, fpGroupOffset, fpGoal){
    
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;

    if (!bInit){
    	xClose = close();
    	xLowest = lowest(fpGroupOffset, low());
        xHighest = highest(fpGroupOffset, high());
        xRanges = efsInternal("Calc_Ranges", xClose, xLowest, xHighest, fpGroupOffset);
        
        xMTR = getSeries(xRanges, 0);
        xRange = getSeries(xRanges, 1);
        
        xHitGoal = efsInternal("Calc_HitGoal", xMTR, fpGoal);
        
        xAvgMTR = sma(fpPeriod, xMTR);
        xMTRStdDev = efsInternal("Calc_StdDev", xRange, fpPeriod);
        
        bInit = true; 
    }
  
    var nMTR = xMTR.getValue(0);
    if (nMTR == null)
        return;
    setBarFgColor((nMTR >= fpGoal) ? Color.green : Color.red, 1);
        
    var GoalCount = null;
    for (var i = 0; i < fpPeriod; i++){
        var nHitGoal = xHitGoal.getValue(-i);
        if (nHitGoal == null){
            GoalCount = "n/a"
            break;
        }
        GoalCount += nHitGoal;
    }
        
    var GoalPct = (GoalCount == "n/a") ? "n/a" : "%" + Math.round(GoalCount / fpPeriod * 100);

    var AvgMTR = xAvgMTR.getValue(0);
    var MTRStdDev = xMTRStdDev.getValue(0);
    
    AvgMTR = (AvgMTR == null) ? "n/a" : "$" + AvgMTR.toFixed(2);
    MTRStdDev = (MTRStdDev == null) ? "n/a" : "$" + MTRStdDev.toFixed(2);
        
    if (isLastBarOnChart()){
        var x = 10;
        var y = 25;
            
        aLabels = [];
        aLabels.push((fpGroupOffset > 1) ? 
        ["** Special Grouping is set is for " + fpGroupOffset + " bars **", 285, Color.RGB(255,155,0)] : null);
        aLabels.push(["Period Length " + fpPeriod, 135, Color.lime]);
        aLabels.push(["Goal $" + fpGoal.toFixed(2), 105, Color.lime]);
        aLabels.push(["Goal Count " + GoalCount, 115, Color.lime]);
        aLabels.push(["Percent Goal Achieved " + GoalPct, 180, Color.lime]);
        aLabels.push(["AverageMTR(" + fpPeriod + ") = " + AvgMTR, 215, Color.lightgrey]);
        aLabels.push(["MTR Std Dev(" + fpPeriod + ") = " + MTRStdDev, 215, Color.lightgrey]);
    
        aLabels.forEach(
            function(aValue, nIndex){
                if (aValue != null){
                    drawTextPixel(x, y, aValue[0], Color.navy, aValue[2],
                    Text.BOLD|Text.FRAME|Text.CENTER, "Arial", 8, aValue[0], aValue[1]);
                    x = x + aValue[1] + nButtonSpace;
                }
                if (nIndex == 4){
                    x = 10;
                    y = 45;
                }
            } 
        )
    } 
    
    return [fpGoal, nMTR]; 
}

function Calc_Ranges(xClose, xLowest, xHighest, nGroupOffset){
    
    nPrevClose = xClose.getValue(-nGroupOffset);
    nLowest = xLowest.getValue(0);
    nHighest = xHighest.getValue(0);

    if (nPrevClose == null || nLowest == null || nHighest == null) 
        return;

    var nRangeLow = Math.abs(nPrevClose - nLowest);
    var nRangeHigh = Math.abs(nPrevClose - nHighest);
    
    var nMTR = Math.max(nRangeLow, nRangeHigh);
    
    var nRangeLow1 = (nPrevClose - nLowest);
    var nRangeHigh1 = (nPrevClose - nHighest);
    
    var nRange = Math.abs(nRangeLow1) >= Math.abs(nRangeHigh1) ? nRangeLow1 : nRangeHigh1;
    
    return [nMTR, nRange];
}

function Calc_StdDev(xSource, nPeriod){
    
    var nSumX = 0; 
    var nSumX2 = 0; 

    for (i = 0; i < nPeriod; i++){ 
        var nSource = xSource.getValue(-i);
        if (nSource == null)
            return;
        nSumX += nSource; 
        nSumX2 += (nSource * nSource); 
    } 

    var nSMA = (nSumX / nPeriod); 
    var nStdDev = Math.sqrt((nSumX2 / nPeriod) - (nSMA * nSMA)); 

    return nStdDev; 
}

function Calc_HitGoal(xMTR, nGoal){
    
    var nMTR = xMTR.getValue(0);
        
    if (nMTR == null)
        return;
    
    var nHitGoal = (nMTR >= nGoal) ? 1 : 0;
   
    return nHitGoal;
}

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