Announcement

Collapse
No announcement yet.

Is this possible?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is this possible?

    Is it possible to alter this efs function to set the trailing stops based on the open and close values instead of the high and low values?

    /***********************************************
    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 Vervoort


    Version: 1.0 04/09/2009


    Formula 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(1);
    }
    fpArray[x] = new FunctionParameter("nATRMultip", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("ATR Multiplication");
    setLowerLimit(1);
    setUpperLimit(10);
    setDefault(1);
    }
    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() == 1) 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.BOL D|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.BOL D|Text.LEFT,
    null, 13, "upgrade");
    return b;
    } else {
    b = true;

    }
    return b;
    }
Working...
X