RefUsage.efs

ICE Data Services -

RefUsage.efs    

File Name: RefUsage.efs

Description:
The function, ref(), is used to refer to previously returned values of a formula's indicator. This example formula demonstrates the usage of ref() for formulas that return only one indicator to the chart.

Formula Parameters:

NA

Notes:
The formula uses a basic moving average study and outputs information to the Formula Output Window. To view the results, open the output window before applying the formula to a chart from the "Tools" menu.

Download File:
RefUsage.efs

EFS Code:

/***  syntax: ref(nRelativeOffset, nNumBars)   ***

    These examples are based on formulas that return a single value.  For example:
        return vValue;
        
    See RefUsage2.efs for examples of formulas that returns an array of values. For example:
        return new Array(vValue1, vValue2);
    
    var myRef = ref(-1)     Returns the previous bar's value.
    var myRef = ref(-3, 3)  Returns values of bars -3 to -1 bars ago.
    var myRef = ref(-1, -3) Returns values of bars -1 to -3 bars ago.
    
    Note:   VERY IMPORTANT!!
    
            When using ref() it is very important that you incorporate some logic into your
            code that will ensure that ref() returns a valid result.  See the "BarCntr" logic 
            below.  If you don't use the BarCntr logic or some other technique, you will get
            unfavorable results.

 **********************************************/

var study1 = new MAStudy(10, 0, "Close", MAStudy.Simple);
 
function preMain() {
    setPriceStudy(true);
}

var BarCntr = 0;
function main() {
    var vMA1 = study1.getValue(MAStudy.MA);
    
    if (getBarState() == BARSTATE_NEWBAR) {
        BarCntr += 1;
    }

/***  BarCntr logic  ***/
    if (BarCntr > 10) {     // We're using 10 because our MAStudy requires a minimum of 10 bars.
    var myRef = ref(-1);
    if (myRef == null) return;
    //var myRef = ref(-3, 3);
    //var myRef = ref(-1, -3);
    }


    // Open the Formula Output Window from the tools menu to view the values of myRef.
    if (BarCntr > 10) {
        debugPrintln("Bar Index: " + getCurrentBarIndex() + "    myRef= " + myRef);
    }
    
    return vMA1;
}