2004 Jan: eSignal_TRIX.efs

ICE Data Services -

eSignal_TRIX.efs  

File Name: eSignal_TRIX.efs

Description:
Based on Trading In Tempo by Jongseon Kim. This article appeared in the January 2004 issue of Stocks & Commodities.

Formula Parameters:

  • TRIX Length: 5
  • Signal Length: 3
  • Long Entry Signal (GC, B, Disable): GC
  • Long Exit Signal (DC, F): F
  • Short Entry Signal (DC, F, Disable): DC
  • Short Exit Signal (GC, B): B

Notes:

  • GC = Golden Cross
  • DC = Dead Cross
  • F = Fall
  • B = Bounce

Requires Daily, Weekly or Monthly chart interval.
The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
eSignal_TRIX.efs


EFS Code:

/*****************************************************
Provided By : eSignal. (c) Copyright 2003

Indicator: TRIX

Formula Parameters:                     Defaults:
    TRIX Length                         5
    Signal Length                       3
    Long Entry Signal (GC, B, Disable)  GC
    Long Exit Signal (DC, F)            F
    Short Entry Signal (DC, F, Disable) DC
    Short Exit Signal (GC, B)           B

Notes:
    GC = Golden Cross
    DC = Dead Cross
    F = Fall
    B = Bounce
    *Requires Daily, Weekly or Monthly chart interval.
*****************************************************/

function preMain() {
    setStudyTitle("TRIX ");
    setCursorLabelName("TRIX", 0);
    setCursorLabelName("Signal", 1);
    setDefaultBarFgColor(Color.black, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarThickness(2, 0);
    setDefaultBarThickness(2, 1);
    
    var fp1 = new FunctionParameter("nLength",
        FunctionParameter.NUMBER);
    fp1.setName("TRIX Length");
    fp1.setLowerLimit(2);
    fp1.setDefault(5);

    var fp2 = new FunctionParameter("nSLength",
        FunctionParameter.NUMBER);
    fp2.setName("Signal Length");
    fp2.setLowerLimit(2);
    fp2.setDefault(3);

    var fp3 = new FunctionParameter("sLEntry",
        FunctionParameter.STRING);
    fp3.setName("Long Entry Signal");
    fp3.addOption("GC");
    fp3.addOption("B");
    fp3.addOption("Disable");
    fp3.setDefault("GC");

    var fp4 = new FunctionParameter("sLExit",
        FunctionParameter.STRING);
    fp4.setName("Long Exit Signal");
    fp4.addOption("DC");
    fp4.addOption("F");
    fp4.setDefault("F");

    var fp5 = new FunctionParameter("sSEntry",
        FunctionParameter.STRING);
    fp5.setName("Short Entry Signal");
    fp5.addOption("DC");
    fp5.addOption("F");
    fp5.addOption("Disable");
    fp5.setDefault("DC");

    var fp6 = new FunctionParameter("sSExit",
        FunctionParameter.STRING);
    fp6.setName("Short Exit Signal");
    fp6.addOption("GC");
    fp6.addOption("B");
    fp6.setDefault("B");
}

var bEdit = true;
var vMax = null;
var vMin = null;
var nCntr = 0;
var EMA1 = null;
var EMA2 = null;
var EMA3 = null;
var vTRIX = null;
var aTrix = null;
var vSignal = null;
var vSignal1 = null;
var dPercent = 0.0;
var bPrimed = false;
var vPosition = null;
var bEntry = false;
var nSpace = null;


function main(nLength, nSLength, sLEntry, sLExit, sSEntry, sSExit) {
    if (isDWM() == false) {
        drawTextAbsolute(-1, 10, "Formula requires Daily, Weekly or Monthly interval.", 
            Color.red, Color.black, 
            Text.BOLD|Text.RIGHT|Text.RELATIVETOBOTTOM, null, 12, "error");
        return;
    }

    var nState = getBarState();
    nSLength = Math.round(nSLength);

    if (bEdit == true || EMA1 == null) {
        aTrix = new Array(nSLength);
        nLength = Math.round(nLength);
        EMA1 = new MAStudy(nLength, 0, "Close", MAStudy.EXPONENTIAL);
        EMA2 = new MAStudy(nLength, 0, EMA1, MAStudy.MA, MAStudy.EXPONENTIAL);
        EMA3 = new MAStudy(nLength, 0, EMA2, MAStudy.MA, MAStudy.EXPONENTIAL);
        if (nSpace == null) {
            var vInt = getInterval();
            if (vInt == "D") nSpace = 0.001;
            if (vInt == "W") nSpace = 0.003;
            if (vInt == "M") nSpace = 0.02;
        }
        bEdit = false;
    }
    
    if (nState == BARSTATE_NEWBAR && vTRIX != null) {
        nCntr += 1;
        bEntry = false;
        aTrix.pop();           // remove last array element
        aTrix.unshift(vTRIX);  // insert new array element [0]
    }

    var vEMA3 = EMA3.getValue(MAStudy.MA);
    var vEMA3_1 = EMA3.getValue(MAStudy.MA, -1);
    if (vEMA3 == null || vEMA3_1 == null) return;
    
    vTRIX = (vEMA3 - vEMA3_1) / vEMA3_1;
    if (vTRIX == null) return;
    aTrix[0] = vTRIX;

    if (aTrix[nSLength-1] == null) return;
    
    vSignal = EMA(nSLength, aTrix);
    
    // draw signals
    var retData1 = ref(-1);  // retData1[0] = TRIX, retData1[1] = Signal
    var retData2 = ref(-2);  // retData2[0] = TRIX, retData2[1] = Signal
    if (retData1 != null && retData2 != null) {
        var vT_1 = retData1[0];
        var vS_1 = retData1[1];
        var vT_2 = retData2[0];
        var vS_2 = retData2[1];
        // Long
        if (bEntry == false && vPosition == null && sLEntry == "GC") {
            if (vTRIX > vSignal && vT_1 < vS_1) {  // GC - Golden Cross entry
                drawTextRelative(0, vSignal-.001, "Long", Color.lime, Color.black, 
                    Text.BOLD|Text.ONTOP|Text.TOP, null, 11, 
                    "GC_long"+nCntr);
                vPosition = "long";
                bEntry = true;
            } else {
                removeText("GC_long"+nCntr);
                vPosition = null;
            }
        } else if (bEntry == false && vPosition == null && sLEntry == "B") { 
            if (vTRIX > vT_1 && vT_1 < vT_2) {  // B - Bounce entry
                drawTextRelative(0, vTRIX-.001, "Long", Color.lime, Color.black, 
                    Text.BOLD|Text.ONTOP|Text.CENTER, null, 11, 
                    "B_long"+nCntr);
                vPosition = "long";
                bEntry = true;
            } else {
                removeText("B_long"+nCntr);
                vPosition = null;
            }
        } else if (vPosition == "long" && sLExit == "DC") {
            if (vTRIX < vSignal && vT_1 > vS_1) {  // DC - Dead Cross exit
                vPosition = null;
            } else {
                removeText("DC_long"+nCntr);
                vPosition = "long";
            }
        } else if (vPosition == "long" && sLExit == "F") {
            if (vTRIX < vT_1 && vT_1 > vT_2) {  // F - Fall exit
                vPosition = null;
            } else {
                removeText("F_long"+nCntr);
                vPosition == "long";
            }
        }
        // Short
        if (bEntry == false && vPosition == null && sSEntry == "DC") {
            if (vTRIX < vSignal && vT_1 > vS_1) {  // DC - Dead Cross entry
                drawTextRelative(0, vSignal+.001, "Short", Color.red, Color.black, 
                    Text.BOLD|Text.ONTOP|Text.BOTTOM, null, 11, 
                    "DC_short"+nCntr);
                vPosition = "short";
                bEntry = true;
            } else {
                removeText("DC_short"+nCntr);
                vPosition = null;
            }
        } else if (bEntry == false && vPosition == null && sSEntry == "F") { 
            if (vTRIX < vT_1 && vT_1 > vT_2) {  // F - Fall entry
                drawTextRelative(0, vTRIX+.001, "Short", Color.red, Color.black, 
                    Text.BOLD|Text.ONTOP|Text.BOTTOM, null, 11, 
                    "F_short"+nCntr);
                vPosition = "short";
                bEntry = true;
            } else {
                removeText("F_short"+nCntr);
                vPosition = null;
            }
        } else if (vPosition == "short" && sSExit == "GC") {
            if (vTRIX > vSignal && vT_1 < vS_1) {  // GC - Golden Cross exit
                vPosition = null;
            } else {
                removeText("GC_short"+nCntr);
                vPosition = "short";
            }
        } else if (vPosition == "short" && sSExit == "B") {
            if (vTRIX > vT_1 && vT_1 < vT_2) {  // B - Bounce exit
                vPosition = null;
            } else {
                removeText("B_short"+nCntr);
                vPosition == "short";
            }
        }
        if (vPosition == "long") setBarBgColor(Color.green);
        if (vPosition == "short") setBarBgColor(Color.maroon);
    }

    setMaxMin();
    
    return new Array(vTRIX.toFixed(4)*1, vSignal.toFixed(4)*1);
}


/**** Functions ****/

function setMaxMin() {
    if (vMax == null) {
        vMax = vTRIX + nSpace;
    } else {
        vMax = Math.max(vMax, vTRIX+nSpace);
    }
    if (vMin == null) {
        vMin = vTRIX - nSpace;
    } else {
        vMin = Math.min(vMin, vTRIX-nSpace);
    }
    
    setStudyMax(vMax);
    setStudyMin(vMin);
    return;
}

function EMA(nLength, nArray) {
    var nBarState = getBarState();
    var dSum = 0.0;
    var dRef;

    if(nBarState == BARSTATE_ALLBARS || bPrimed == false) {
        dPercent = (2.0 / (nLength + 1.0));
        bPrimed = false;
    }

    if (nBarState == BARSTATE_NEWBAR) {
        vSignal1 = vSignal;
    }

    if(bPrimed == false) {
        for(i = 0; i < nLength; i++) {
            dSum += nArray[i];
        }
        bPrimed = true;
        return (dSum / nLength);
    } else {
        return (((aTrix[0] - vSignal1) * dPercent) + vSignal1);
    }
}