2009 Jul: Trailing Resistance & Support Stops, by Sylvain Vervoort

ICE Data Services -

SVE_TRENDS_Trail.efs  
EFSLibrary - Discussion Board  

File Name: SVE_TRENDS_Trail.efs

Description:
Trailing Resistance & Support Stops, by Sylvain Vervoort

Formula Parameters:

  • ATR Period : 10
  • ATR Multiplication : 2.8
  • Long or Short : Long
  • Show Line Trailing Stop : True
  • Show Labels : True
  • Show Arrows : True
  • Display Cursor Labels : True
  • Line Color : Red

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

Download File:
SVE_TRENDS_Trail.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:        
    Trailing Resistance & Support Stops, by Sylvain Vervoort

Version:            1.0  05/07/2009

Formula Parameters:                     Default:
    ATR Period                          10
    ATR Multiplication                  2.8
    Long or Short                       Long
    Show Line Trailing Stop             True
    Show Labels                         True
    Show Arrows                         True
    Display Cursor Labels               True
    Line Color                          Red

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();
var bInit = false;

function preMain() {
    setPriceStudy(true);
    setStudyTitle("R&S Trailing Stops");
    setCursorLabelName("R&S Trailing Stops", 0);
    setShowTitleParameters(false);
    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_LINE, 0); 
    setDefaultBarThickness(2, 0);
    askForInput();
    var x=0;
    fpArray[x] = new FunctionParameter("nATRPeriod", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("ATR Period");
        setLowerLimit(1);        
        setUpperLimit(100);        
        setDefault(10);
    }
    fpArray[x] = new FunctionParameter("nATRMultip", FunctionParameter.NUMBER);
    with(fpArray[x++]){
        setName("ATR Multiplication");
        setLowerLimit(1);        
        setUpperLimit(10);        
        setDefault(2.8);
    }
    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);
    }        
    fpArray[x] = new FunctionParameter("sStrategy", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("Long or Short");
        addOption("Long");
        addOption("Short");        
        setDefault("Long");
    }
    fpArray[x] = new FunctionParameter("cColor", FunctionParameter.COLOR);
    with(fpArray[x++]){
        setName("Line Color");
        setDefault(Color.red);
    }    
}

var bVersion = null;
var xSVE_TRENDS_Trail = null;
var xClose = null;
var bTrade = true;

function main(nATRPeriod, nATRMultip, sStrategy, bShowTS, bShowL, bShowArrows, ViewValue, cColor){
var nClose = 0;
var nClose1 = 0;
var nATRTS = 0;
var nATRTS1 = 0;

    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    if (getCurrentBarCount() <= nATRPeriod + 5) return;
    if(bInit==false){
        setShowCursorLabel(ViewValue);
        setDefaultBarFgColor(cColor, 0);
        xClose = close();        
        xSVE_TRENDS_Trail = efsInternal("SVE_TRENDS_Trail", nATRPeriod, nATRMultip, xClose);
        bInit=true;
    }
    
    if(getCurrentBarIndex() == 0) {
        bTrade = false;
        return;
    }

    nClose = xClose.getValue(0);
    nClose1 = xClose.getValue(-1);
    nATRTS = xSVE_TRENDS_Trail.getValue(0);
    nATRTS1 = xSVE_TRENDS_Trail.getValue(-1);
    if (nATRTS == null) return;

    if (nClose1 < nATRTS1 && nClose > nATRTS) {
        if (bShowArrows) drawShape( Shape.UPARROW, BelowBar1, Color.green);
        if (sStrategy == "Long") {
            if (bShowL) drawTextRelative(0, BelowBar2, " LONG", Color.white, Color.green, Text.PRESET|Text.CENTER|Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            if (bTrade) Strategy.doLong("Long", Strategy.MARKET, Strategy.NEXTBAR);
        } else {
            if (bShowL) drawTextRelative(0, BelowBar2,  " EXIT", Color.white, Color.green, Text.PRESET|Text.CENTER|Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            if (Strategy.isShort() && bTrade) Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR);            
        }
    }
    if (nClose1 > nATRTS1 && nClose < nATRTS) {
        if (bShowArrows) drawShape( Shape.DOWNARROW, AboveBar1, Color.red);
        if (sStrategy == "Long") {
            if (bShowL) drawTextRelative(0, AboveBar2,  " EXIT", Color.white, Color.red, Text.PRESET|Text.CENTER|Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            if (Strategy.isLong() && bTrade) Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR);
        } else {
            if (bShowL) drawTextRelative(0, AboveBar2,  "SHORT", Color.white, Color.red, Text.PRESET|Text.CENTER|Text.FRAME , "Arial Black", 10, "b"+(getCurrentBarCount()), -5); 
            if (bTrade) Strategy.doShort("Short", Strategy.MARKET, Strategy.NEXTBAR);
        }    
    }
    if (bShowTS == false) return;
    return nATRTS;
}

var xHigh = null;
var xLow = null;
var xATR_Modif = null;
var bSecondInit = false;
var Support = 0;
var Support_1 = 0;

function SVE_TRENDS_Trail(nATRPeriod, nATRMultip, xSClose){
var nClose = 0;
var nLoss = 0;
var Resistance = 0;
var Trends = 0;
var nRef = ref(-1);
    if (bSecondInit == false) {
        xHigh = high();
        xLow = low();
        xATR_Modif = efsInternal("Calc_ATRMod", nATRPeriod, nATRMultip, xSClose, xHigh, xLow)
        bSecondInit = true;
    }  

    nClose = xSClose.getValue(0);
    if (xATR_Modif.getValue(0) == null) return;
    nLoss = nATRMultip * xATR_Modif.getValue(0);
    Resistance = nClose + nLoss;
    
    if(getBarState()==BARSTATE_NEWBAR){
        Support_1 = Support;
    }

    if (xLow.getValue(0)  >= xLow.getValue(-2) && 
        xLow.getValue(-1) >= xLow.getValue(-2) && 
        xLow.getValue(-3) >= xLow.getValue(-2) && 
        xLow.getValue(-4) >= xLow.getValue(-2)) {
        Support = xLow.getValue(-2);
    } else {
        if (xLow.getValue(0) > xHigh.getValue(-1) * 1.0013) {
            Support = xHigh.getValue(-1) * 0.9945;
        } else {   
            if (xLow.getValue(0) > nRef * 1.1) {
                Support = Support_1 * 1.05;
            } else {   
                Support = Support_1;
            }    
        }        
    }

    if (xHigh.getValue(0) > nRef && xHigh.getValue(-1) > nRef) {
        Trends = Math.max(nRef, Support);
    } else {    
        if (xHigh.getValue(0) < nRef && xHigh.getValue(-1) < nRef) {
            Trends = Math.min(nRef, Resistance);
        } else {    
            if (xHigh.getValue(0) >= nRef) {
                Trends = Support;
            } else {    
                Trends = Resistance;
            }    
        }        
    }    
    return Trends;
}

function Calc_High_Low() {
var nRes = high(0) - low(0);
    if (nRes == null) return;
    return nRes;
}

var bThirdInit = false;
var xHigh_Low = null;
var xMA_High_Low = null;

function Calc_ATRMod(nATRPeriod, nATRMultip, xTClose, xHigh, xLow) {
var nHiLo = 0;
var nHref = 0;
var nLref = 0;
var ndiff1 = 0;
var ndiff2 = 0;
var nHigh_Low = 0;
var nMA_High_Low = 0;
var nAtrMod = 0;
var nRef = ref(-1);
    if (bThirdInit == false) {
        xHigh_Low = efsInternal("Calc_High_Low");
        xMA_High_Low = sma(nATRPeriod, xHigh_Low)
        bThirdInit = true;
    }    
    if (xMA_High_Low.getValue(-1) == null) return;
    var nClose = xTClose.getValue(0);
    var nClose1 = xTClose.getValue(-1);
    var nHigh = xHigh.getValue(0);
    var nHigh1 = xHigh.getValue(-1);
    var nLow = xLow.getValue(0);
    var nLow1 = xLow.getValue(-1);
    nHigh_Low = xHigh_Low.getValue(0);
    nMA_High_Low = xMA_High_Low.getValue(0);
  
    if (nHigh_Low < 1.5 * nMA_High_Low) {
        nHiLo = nHigh_Low;
    } else {
        nHiLo = 1.5 * nMA_High_Low;
    }

    if (nLow <= nHigh1) {
        nHref = nHigh - nClose1;
    } else {
        nHref = (nHigh - nClose1) - (nLow - nHigh1) / 2;
    }

    if (nHigh >= nLow1) {
        nLref = nClose1 - nLow;
    } else {
        nLref = (nClose1 - nLow) - (nLow1 - nHigh) / 2;
    }

    ndiff1 = Math.max(nHiLo, nHref);
    ndiff2 = Math.max(ndiff1, nLref);
    nAtrMod = (ndiff2 + (nATRPeriod - 1) *  nRef) / nATRPeriod;
    return nAtrMod;
}

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