2007 Jan: Exiting After Profitable Closes (HighestHighTrend.efs)

ICE Data Services -

HighestHighTrend.efs  

EFSLibrary - Discussion Board  

File Name: HighestHighTrend.efs

Description:
This study is based on the January 2007 Trading System Lab article, Exiting After Profitable Closes, by Volker Knapp.

Formula Parameters:

  • Number of days for Trend Entry: 20
  • Number of days for Counter Trend Entry: 10
  • Number of days for Trend Exit: 20
  • Number of days for Counter Trend Exit: 8
  • Fixed Stop Percent: 0.10

Notes:
This system is a long only system compatible for Back Testing and real time analysis. The related article is copyrighted material. If you are not a subscriber of Active Trader Magazine, please visit www.activetradermag.com.

Download File:
HighestHighTrend.efs


EFS Code:

/***************************************
Provided By : eSignal (c) Copyright 2006
Description:  Exiting after profitable closes 
              by Volker Knapp

Version 1.0  11/13/2006

Notes:
* Jan 2007 Issue of Active Trader Magazine
* Study requires version 8.0 or later.
* Long only system, compatible for Back Testing
    and real time.
* Parameters optimized for Daily interval.


Formula Parameters:                     Default:
Number of days for Trend Entry          20
Number of days for Counter Trend Entry  10
Number of days for Trend Exit           20
Number of days for Counter Trend Exit   8
Fixed Stop Percent                      0.10
*****************************************************************/

function preMain() {
    setPriceStudy(true);
    setStudyTitle("Highest High Trend System ");
    setShowTitleParameters(false);    
    setCursorLabelName("HH", 0);
    setCursorLabelName("LL", 1);
    setCursorLabelName("Stop", 2);
    setDefaultBarThickness(2, 0);
    setDefaultBarThickness(2, 1);
    setDefaultBarThickness(2, 2);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.blue, 1);
    setDefaultBarFgColor(Color.red, 2);
    setPlotType(PLOTTYPE_FLATLINES, 2);

    var fp1 = new FunctionParameter("nTdays", FunctionParameter.NUMBER);
        fp1.setName("Number of days for Trend Entry");
        fp1.setLowerLimit(1);
        fp1.setDefault(20);
    var fp2 = new FunctionParameter("nCTdays", FunctionParameter.NUMBER);
        fp2.setName("Number of days for Counter Trend Entry");
        fp2.setLowerLimit(1);
        fp2.setDefault(10);
    var fp3 = new FunctionParameter("nTexit", FunctionParameter.NUMBER);
        fp3.setName("Number of days for Trend Exit");
        fp3.setLowerLimit(1);
        fp3.setDefault(20);    
    var fp4 = new FunctionParameter("nCTexit", FunctionParameter.NUMBER);
        fp4.setName("Number of days for Counter Trend Exit");
        fp4.setLowerLimit(1);
        fp4.setDefault(8);    
    var fp5 = new FunctionParameter("nStopPrct", FunctionParameter.NUMBER);
        fp5.setName("Fixed Stop Percent");
        fp5.setLowerLimit(0);
        fp5.setUpperLimit(1);
        fp5.setDefault(0.10);    
}

// Global Variables
var bVersion  = null;    // Version flag
var bInit     = false;   // Initialization flag

var nCount = 0;
var xHH_Trend = null;
var xLL_CTrend = null;
var vPosition = 0;  // 0 = flat, 1 = Trend long, 2 = Counter Trend Long
var nEntry = null;  // entry price
var nStop = null;   // stop price
var bExitTrade = false;
var bBT = true; // back test flag

function main(nTdays, nCTdays, nTexit, nCTexit, nStopPrct) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    

    var nState = getBarState();
    
    //Initialization
    if (bInit == false) {
        xHH_Trend = upperDonchian(nTdays, high());
        xLL_CTrend = lowerDonchian(nCTdays, low());
        bInit = true;
    }
    
    var nH_0 = high(0);
    var nL_0 = low(0);
    var nC_1 = close(-1);
    var nOpen = open(0);
    var nHH = xHH_Trend.getValue(0);
    var nLL = xLL_CTrend.getValue(0);
    var nHH_1 = xHH_Trend.getValue(-1);
    var nLL_1 = xLL_CTrend.getValue(-1);    
    if (nHH_1 == null || nLL_1 == null) return;

    // Record Keeping
    if (nState == BARSTATE_NEWBAR) {
        bExitTrade = false;
        if (vPosition == 0) {
            nCount = 0;
            nEntry = null;
            nStop = null;
        }
        if (getCurrentBarIndex() == 0) bBT = false;
    }
    
    // Exit Conditions
    if (vPosition != 0 && nState == BARSTATE_NEWBAR) {
        var sText = "";
        /*var sText = "Unprofitable Close";
        if (nCount >= 1 && nC_1 < nEntry) {
            // not a profitable close
            // exit position
            vPosition = 0;
            bExitTrade = true;
        }*/
        nCount++;
        // Trend Exit
        if (vPosition == 1 && nCount >= nTexit) {
            sText = "Counter Trend Exit";
            bExitTrade = true;
        }
        // Counter Trend Exit
        if (vPosition == 2 && nCount >= nCTexit) {
            sText = "Counter Trend Exit";
            bExitTrade = true;
        }
        
        if (bExitTrade == true) {
            vPosition = 0;
            drawShape(Shape.DIAMOND, AboveBar1, Color.red, "xtop"+rawtime(0));
            drawShape(Shape.DIAMOND, BelowBar1, Color.red, "xbot"+rawtime(0));
            Alert.playSound("train.wav");
            if (bBT) {
                Strategy.doSell(sText, Strategy.MARKET, Strategy.THISBAR);
            }
        }
    }
    // Fixed %Stop Exit
    if (bExitTrade == false && vPosition != 0) {
        if (nL_0 <= nStop) {
            vPosition = 0;
            bExitTrade = true;
            drawShape(Shape.DIAMOND, AboveBar1, Color.red, "xtop"+rawtime(0));
            drawShape(Shape.DIAMOND, BelowBar1, Color.red, "xbot"+rawtime(0));
            Alert.playSound("train.wav");
            if (bBT) {
                Strategy.doSell("Stop Exit", Strategy.STOP, Strategy.THISBAR, 
                    Strategy.DEFAULT, Math.min(nOpen, nStop));
            }
        }
    }   
    
    
    
    // Entry Conditions
    if (vPosition == 0 && bExitTrade == false) {
        // Trend Entry
        if (nH_0 > nHH_1) {
            vPosition = 1;
            nEntry = Math.max(nOpen, nHH_1);
            nStop = nEntry * (1 - nStopPrct);  // fixed stop
            drawShape(Shape.UPARROW, AboveBar1, Color.green, rawtime(0));
            Alert.playSound("ding.wav");
            if (bBT) {
                Strategy.doLong("Trend Signal", Strategy.STOP, 
                    Strategy.THISBAR, Strategy.DEFAULT, nEntry);
            }
        } else if (nL_0 < nLL_1) { // Counter Trend Entry
            vPosition = 2;
            nEntry = Math.min(nOpen, nLL_1);
            nStop = nEntry * (1 - nStopPrct);  // fixed stop
            drawShape(Shape.UPARROW, AboveBar1, Color.green, rawtime(0));
            Alert.playSound("ding.wav");
            if (bBT) {
                Strategy.doLong("Counter Trend Signal", Strategy.LIMIT, 
                    Strategy.THISBAR, Strategy.DEFAULT, nEntry);
            }
        }
    }
    
    return new Array(nHH, nLL, nStop);
}


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