2009 Jun: Average True Range Trailing Stops, by Sylvain Vervoort

ICE Data Services -


ATR_TrailingStops.efs  EFSLibrary - Discussion Board
  

File Name: ATR_TrailingStops.efs, Modified_ATR.efs, Modified_ATR_TrailingStops.efs


Description:
Average True Range Trailing Stops, by Sylvain Vervoort


Formula Parameters:

ATR_TrailingStops.efs
ATR Period : 5
ATR Multiplication : 3.5
Long or Short : Long
Show Line Trailing Stop : True
Show Labels : True
Show Arrows : True
Display Cursor Labels : True
Line Color : Red

Modified_ATR.efs
ATR Period : 5
Line Color : Red

Modified_ATR_TrailingStops.efs
ATR Period : 5
ATR Multiplication : 3.5
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:
ATR_TrailingStops.efs
Modified_ATR.efs
Modified_ATR_TrailingStops.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:            Average True Range Trailing Stops, by Sylvain VervoortVersion:            1.0  04/09/2009Formula Parameters:                     Default:    ATR Period                          5    ATR Multiplication                  3.5    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();function preMain() {    setPriceStudy(true);    setStudyTitle("ATR Trailing Stops");    setCursorLabelName("ATR Trailing Stop", 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(5);    }    fpArray[x] = new FunctionParameter("nATRMultip", FunctionParameter.NUMBER);    with(fpArray[x++]){        setName("ATR Multiplication");        setLowerLimit(1);                setUpperLimit(10);                setDefault(3.5);    }    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 bInit = false;var bVersion = null;var xATRTrailingStop = null;var xClose = null;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(bInit==false){        setShowCursorLabel(ViewValue);        setDefaultBarFgColor(cColor, 0);                xClose = close();        xATRTrailingStop = efsInternal("ATRTrailingStop", nATRPeriod, nATRMultip, xClose);        bInit=true;    }    if(getCurrentBarIndex() == 0) return;       nClose = xClose.getValue(0);    nClose1 = xClose.getValue(-1);    nATRTS = xATRTrailingStop.getValue(0);    nATRTS1 = xATRTrailingStop.getValue(-1);    if (nATRTS1 == null) return;    if (nClose1 < nATRTS1 && nClose > nATRTS1) {        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);             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()) Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR);                    }    }    if (nClose1 > nATRTS1 && nClose < nATRTS1) {        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()) 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);             Strategy.doShort("Short", Strategy.MARKET, Strategy.NEXTBAR);        }        }    if (bShowTS == false) return;    return xATRTrailingStop.getValue(0);}var xATR = null;var nRef = 0;var bSecondInit = false;function ATRTrailingStop(nATRPeriod, nATRMultip, xSClose){var nClose = 0;var nClose1 = 0;var nLoss = 0;var nRes = 0;var nRef = ref(-1);    if (bSecondInit == false) {        xATR = atr(nATRPeriod);        bSecondInit = true;    }        nClose = xSClose.getValue(0);    nClose1 = xSClose.getValue(-1);    nLoss = nATRMultip * xATR.getValue(0);    if (nLoss == null || nClose1 == null) return;       if (nClose > nRef && nClose1 > nRef) {        nRes = Math.max(nRef, nClose - nLoss);    } else {        if (nClose < nRef && nClose1 < nRef) {            nRes = Math.min(nRef, nClose + nLoss);        } else {            if (nClose > nRef) {                nRes = nClose - nLoss;            } else {                nRes = nClose + nLoss;            }        }    }    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:            Modified Average True Range, by Sylvain VervoortVersion:            1.0  04/09/2009Formula Parameters:                     Default:    ATR Period                          5    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();function preMain() {    setPriceStudy(false);    setStudyTitle("Modified ATR");    setCursorLabelName("Modified ATR", 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(5);    }    fpArray[x] = new FunctionParameter("cColor", FunctionParameter.COLOR);    with(fpArray[x++]){        setName("Line Color");        setDefault(Color.red);    }        }var bInit = false;var bVersion = null;var xATR_Modif = null;function main(nATRPeriod, cColor){var nATR_Modif = 0;    if (bVersion == null) bVersion = verify();    if (bVersion == false) return;       if(bInit==false){        setDefaultBarFgColor(cColor, 0);        xATR_Modif = efsInternal("Calc_ATRMod", nATRPeriod);        bInit=true;    }    nATR_Modif = xATR_Modif.getValue(0);    if (nATR_Modif == null) return;    return nATR_Modif;}var xHigh_Low = null;var xATR_ModifTmp = null;var nRef = 0;var bSecondInit = false;function Calc_ATRMod(nATRPeriod, nATRMultip){var nRes = 0;    if (bSecondInit == false) {        xHigh_Low = efsInternal("Calc_High_Low");        xATR_ModifTmp = efsInternal("Calc_ATRModResult", nATRPeriod, xHigh_Low, sma(nATRPeriod, xHigh_Low))        bSecondInit = true;    }        nRes = xATR_ModifTmp.getValue(0);    if (nRes == null) return;    return nRes;}function Calc_High_Low() {var nRes = high(0) - low(0);    if (nRes == null) return;    return nRes;}var bThirdInit = false;var xClose = null;var xHigh = null;var xLow = null;function Calc_ATRModResult(nATRPeriod, xHigh_Low, xMA_High_Low) {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;    if (bThirdInit == false) {        xClose = close();        xHigh = high();        xLow = low();        bThirdInit = true;    }    var nClose = xClose.getValue(0);    var nClose1 = xClose.getValue(0);    var nHigh = xHigh.getValue(0);    var nHigh1 = xHigh.getValue(0);    var nLow = xLow.getValue(0);    var nLow1 = xLow.getValue(0);    nHigh_Low = xHigh_Low.getValue(0);    nMA_High_Low = xMA_High_Low.getValue(0) * 1.5;    if (nHigh_Low == null || nMA_High_Low == null) return;      if (nHigh_Low < nMA_High_Low) {        nHiLo = nHigh_Low;    } else {        nHiLo = 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) *  ref(-1)) / nATRPeriod;    if (nAtrMod == null) return;    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;}

/*********************************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:            Modified Average True Range Trailing Stops, by Sylvain VervoortVersion:            1.0  04/09/2009Formula Parameters:                     Default:    ATR Period                          5    ATR Multiplication                  3.5    Long or Short                       Long    Show Line Trailing Stop             True    Show Labels                         True    Show Arrows                         True    Display Cursor Labels               True    Line Color                          RedNotes:    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("Modified ATR Trailing Stops");    setCursorLabelName("Modified ATR TS", 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(5);    }    fpArray[x] = new FunctionParameter("nATRMultip", FunctionParameter.NUMBER);    with(fpArray[x++]){        setName("ATR Multiplication");        setLowerLimit(1);                setUpperLimit(10);                setDefault(3.5);    }    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 bInit = false;var bVersion = null;var xATRTrailingStop = null;var xClose = null;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(bInit==false){        setShowCursorLabel(ViewValue);        setDefaultBarFgColor(cColor, 0);        xClose = close();                xATRTrailingStop = efsInternal("ModifiedATRTrailingStop", nATRPeriod, nATRMultip, xClose);        bInit=true;    }        if(getCurrentBarIndex() == 0) return;        nClose = xClose.getValue(0);    nClose1 = xClose.getValue(-1);    nATRTS = xATRTrailingStop.getValue(0);    nATRTS1 = xATRTrailingStop.getValue(-1);    if (nATRTS1 == null) return;    if (nClose1 < nATRTS1 && nClose > nATRTS1) {        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);             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()) Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR);                    }    }    if (nClose1 > nATRTS1 && nClose < nATRTS1) {        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()) 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);             Strategy.doShort("Short", Strategy.MARKET, Strategy.NEXTBAR);        }        }    if (bShowTS == false) return;    return xATRTrailingStop.getValue(0);}var xHigh_Low = null;var xATR_Modif = null;var nRef = 0;var bSecondInit = false;function ModifiedATRTrailingStop(nATRPeriod, nATRMultip, xSClose){var nBarState = getBarState();var nClose = 0;var nClose1 = 0;var nLoss = 0;var nRes = 0;var nRef = ref(-1);    if (bSecondInit == false) {        xHigh_Low = efsInternal("Calc_High_Low");        xATR_Modif = efsInternal("Calc_ATRMod", nATRPeriod, nATRMultip, xSClose, xHigh_Low, sma(nATRPeriod, xHigh_Low))        bSecondInit = true;    }        nClose = xSClose.getValue(0);    nClose1 = xSClose.getValue(-1);    nLoss = nATRMultip * xATR_Modif.getValue(0);    if (nLoss == null) return;       if (nClose > nRef && nClose1 > nRef) {        nRes = Math.max(nRef, nClose - nLoss);    } else {        if (nClose < nRef && nClose1 < nRef) {            nRes = Math.min(nRef, nClose + nLoss);        } else {            if (nClose > nRef) {                nRes = nClose - nLoss;            } else {                nRes = nClose + nLoss;            }        }    }    return nRes;}function Calc_High_Low() {var nRes = high(0) - low(0);    if (nRes == null) return;    return nRes;}var bThirdInit = false;var xHigh = null;var xLow = null;function Calc_ATRMod(nATRPeriod, nATRMultip, xTClose, xHigh_Low, xMA_High_Low) {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;    if (bThirdInit == false) {        xHigh = high();        xLow = low();        bThirdInit = true;    }    var nClose = xTClose.getValue(0);    var nClose1 = xTClose.getValue(0);    var nHigh = xHigh.getValue(0);    var nHigh1 = xHigh.getValue(0);    var nLow = xLow.getValue(0);    var nLow1 = xLow.getValue(0);    nHigh_Low = xHigh_Low.getValue(0);    nMA_High_Low = xMA_High_Low.getValue(0) * 1.5;    if (nHigh_Low == null || nMA_High_Low == null) return;      if (nHigh_Low < nMA_High_Low) {        nHiLo = nHigh_Low;    } else {        nHiLo = 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) *  ref(-1)) / nATRPeriod;    if (nAtrMod == null) return;    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;}