2009 May: Fixed Percentage Stop Method, by Sylvain Vervoort

ICE Data Services -

FixedPctTrailingStop.efs  
EFSLibrary - Discussion Board  

File Name:

  • FixedPctTrailingStop.efs
  • FixedPctTrailingStop_Strategy.efs

Description:
Fixed-Percentage Trailing Stop

Formula Parameters:

FixedPctTrailingStop.efs

  • Trailing Loss : 14
  • Long or Short : Long
  • Show Line Trailing Stop : True
  • Show Labels : True
  • Show Arrows : True
  • Display Cursor Labels : True

FixedPctTrailingStop_Strategy.efs

  • Trailing Loss : 14
  • Long or Short : Long
  • Show Line Trailing Stop : True
  • Show Labels : True
  • Show Arrows : True
  • Display Cursor Labels : True

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

Download File:
FixedPctTrailingStop.efs
FixedPctTrailingStop_Strategy.efs



EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2009. 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:        
    Fixed-Percentage Trailing Stop 

Version:            1.0  03/16/2009

Formula Parameters:                     Default:
    Trailing Loss                       14
    Long or Short                       Long
    Show Line Trailing Stop             True
    Show Labels                         True
    Show Arrows                         True
    Display Cursor Labels               True

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("Fixed-Percentage Trailing Stop");
    setCursorLabelName("Trailing Stop", 0);
    setShowTitleParameters(false);

    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_LINE, 0); 
    setDefaultBarThickness(2, 0);

    askForInput();
    var x=0;
    fpArray[x] = new FunctionParameter("nTrailingLoss", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("Trailing Loss");
        setLowerLimit(0);        
        setUpperLimit(100);        
        setDefault(14);
    }

    fpArray[x] = new FunctionParameter("sStrategy", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("Long or Short");
        addOption("Long");
        addOption("Short");        
        setDefault("Long");
    }

    fpArray[x] = new FunctionParameter("bShowTS", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Show Line Trailing Stop");
        addOption("true");
        addOption("false");        
        setDefault("true");
    }

    fpArray[x] = new FunctionParameter("bShowL", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Show Labels");
        addOption("true");
        addOption("false");        
        setDefault("true");
    }

    fpArray[x] = new FunctionParameter("bShowArrows", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Show Arrows");
        addOption("true");
        addOption("false");        
        setDefault("true");
    }    

    fpArray[x] = new FunctionParameter("ViewValue", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Display Cursor Labels");
        setDefault(true);
    }        
}

var bInit = false;
var bVersion = null;
var nRef = 0;
var xS_R = null;

function main(sStrategy, nTrailingLoss, bShowTS, bShowL, bShowArrows, ViewValue){
var nClose = close(0);
var nClose1 = close(-1);
var nX_R = 0;
var nX_R1 = 0;
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   

    if(bInit==false){
        setShowCursorLabel(ViewValue);
        xS_R = efsInternal("TrailingStop", nTrailingLoss);
        bInit=true;
    }

    nX_R = xS_R.getValue(0);
    nX_R1 = xS_R.getValue(-1);
    if (nX_R1 == null) return;

    if (nClose1 < nX_R1 && nClose > nX_R) {
        if (bShowArrows) drawShape( Shape.UPARROW, BelowBar2, Color.green);
        if (sStrategy == "Long") {
            if (bShowL) drawTextRelative(-3, low(0),  " LONG", Color.white, Color.green, Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
        } else {
            if (bShowL) drawTextRelative(-3, high(0),  " EXIT", Color.white, Color.green, Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
        }
    }
    if (nClose1 > nX_R1 && nClose < nX_R) {
        if (bShowArrows) drawShape( Shape.DOWNARROW, AboveBar2, Color.red);
        if (sStrategy == "Long") {
            if (bShowL) drawTextRelative(-3, high(0),  " EXIT", Color.white, Color.red, Text.FRAME | Text.TOP, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
        } else {
            if (bShowL) drawTextRelative(-3, high(0),  "SHORT", Color.white, Color.red, Text.FRAME , "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
        }    
    }

    if (bShowTS == false) return;
    return xS_R.getValue(0);
}

function TrailingStop(nTrailingLoss){
var Close = close(0);
var loss = Close * nTrailingLoss / 100;
var nRes = 0;
var nRef = ref(-1);
    if (nRef == null) nRef = Close;
    if (Close > nRef && close(-1) > nRef) {
        nRes = Math.max(nRef, Close - loss)
    } else {
        if (Close < nRef && close(-1) < nRef) {
            nRes = Math.min(nRef, Close + loss)
        } else {
            if (Close > nRef) {
                nRes = Close - loss;
            } else {
                nRes = Close + loss;
            }
        }
    }
    return nRes;
}

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

 

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2009. 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:        
    Fixed-Percentage Trailing Stop Strategy

Version:            1.0  03/16/2009

Formula Parameters:                     Default:
    Trailing Loss                       14
    Long or Short                       Long
    Show Line Trailing Stop             True
    Show Labels                         True
    Show Arrows                         True
    Display Cursor Labels               True

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("Fixed-Percentage Trailing Stop");
    setCursorLabelName("Trailing Stop", 0);
    setShowTitleParameters(false);

    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_LINE, 0); 
    setDefaultBarThickness(2, 0);

    askForInput();
    var x=0;
    fpArray[x] = new FunctionParameter("nTrailingLoss", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("Trailing Loss");
        setLowerLimit(0);        
        setUpperLimit(100);        
        setDefault(14);
    }

    fpArray[x] = new FunctionParameter("sStrategy", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("Long or Short");
        addOption("Long");
        addOption("Short");        
        setDefault("Long");
    }

    fpArray[x] = new FunctionParameter("bShowTS", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Show Line Trailing Stop");
        addOption("true");
        addOption("false");        
        setDefault("true");
    }

    fpArray[x] = new FunctionParameter("bShowL", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Show Labels");
        addOption("true");
        addOption("false");        
        setDefault("true");
    }

    fpArray[x] = new FunctionParameter("bShowArrows", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Show Arrows");
        addOption("true");
        addOption("false");        
        setDefault("true");
    }    

    fpArray[x] = new FunctionParameter("ViewValue", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Display Cursor Labels");
        setDefault(true);
    }        
}

var bInit = false;
var bVersion = null;
var nRef = 0;
var xS_R = null;

function main(sStrategy, nTrailingLoss, bShowTS, bShowL, bShowArrows, ViewValue){
var nClose = close(0);
var nClose1 = close(-1);
var nX_R = 0;
var nX_R1 = 0;
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   

    if(bInit==false){
        setShowCursorLabel(ViewValue);
        xS_R = efsInternal("TrailingStop", nTrailingLoss);
        bInit=true;
    }

    if (getCurrentBarIndex() == 0) return;

    nX_R = xS_R.getValue(0);
    nX_R1 = xS_R.getValue(-1);
    if (nX_R1 == null) return;

    if (nClose1 < nX_R1 && nClose > nX_R) {
        if (bShowArrows) drawShape( Shape.UPARROW, BelowBar2, Color.green);
        if (sStrategy == "Long") {
            if (bShowL) drawTextRelative(-3, low(0),  " LONG", Color.white, Color.green, Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            Strategy.doLong("Long", Strategy.MARKET, Strategy.THISBAR);
        } else {
            if (bShowL) drawTextRelative(-3, high(0),  " EXIT", Color.white, Color.green, Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            if (Strategy.isShort()) Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.THISBAR);
        }
    }
    if (nClose1 > nX_R1 && nClose < nX_R) {
        if (bShowArrows) drawShape( Shape.DOWNARROW, AboveBar2, Color.red);
        if (sStrategy == "Long") {
            if (bShowL) drawTextRelative(-3, high(0),  " EXIT", Color.white, Color.red, Text.FRAME | Text.TOP, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            if (Strategy.isLong()) Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.THISBAR);
        } else {
            if (bShowL) drawTextRelative(-3, high(0),  "SHORT", Color.white, Color.red, Text.FRAME , "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            Strategy.doShort("Short", Strategy.MARKET, Strategy.THISBAR);
        }    
    }

    if (bShowTS == false) return;
    return xS_R.getValue(0);
}

function TrailingStop(nTrailingLoss){
var Close = close(0);
var loss = Close * nTrailingLoss / 100;
var nRes = 0;
var nRef = ref(-1);
    if (nRef == null) nRef = Close;
    if (Close > nRef && close(-1) > nRef) {
        nRes = Math.max(nRef, Close - loss)
    } else {
        if (Close < nRef && close(-1) < nRef) {
            nRes = Math.min(nRef, Close + loss)
        } else {
            if (Close > nRef) {
                nRes = Close - loss;
            } else {
                nRes = Close + loss;
            }
        }
    }
    return nRes;
}

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