RefUsage2.efs
File Name: RefUsage2.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 multiple values or indicators to the chart.
Formula Parameters:
NA
Notes:
The formula uses two basic moving average studies 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:
RefUsage2.efs
EFS Code:
/*** syntax: ref(nRelativeOffset, nNumBars) *** These examples are based on formulas that return multiple values. For example: return new Array(vValue1, vValue2); See RefUsage.efs for examples of formulas that return a single values. For example: return vValue; var myRef = ref(-1) Returns an array of the previous bar's values: myRef[0] = vMA1 from -1 bar ago. myRef[1] = vMA2 from -1 bar ago. var myRef = ref(-2, 2) Returns a two dimentional array of values for bars -2 to -1 bars ago: myRef[0][0] = vMA1 from -2 bars ago. myRef[0][1] = vMA2 from -2 bars ago. myRef[1][0] = vMA1 from -1 bar ago. myRef[1][1] = vMA2 from -1 bar ago. var myRef = ref(-1, -2) Returns a two dimentional array of values for bars -1 to -2 bars ago: myRef[0][0] = vMA1 from -1 bar ago. myRef[0][1] = vMA2 from -1 bar ago. myRef[1][0] = vMA1 from -2 bars ago. myRef[1][1] = vMA2 from -2 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); var study2 = new MAStudy(20, 0, "Close", MAStudy.Simple); function preMain() { setPriceStudy(true); } var BarCntr = 0; function main() { var vMA1 = study1.getValue(MAStudy.MA); var vMA2 = study2.getValue(MAStudy.MA); if (getBarState() == BARSTATE_NEWBAR) { BarCntr += 1; } /*** BarCntr logic ***/ if (BarCntr > 20) { // We're using 20 because our longest MAStudy requires a minimum of 20 bars. var myRef = ref(-1); if (myRef == null) return; //var myRef = ref(-2, 2); //var myRef = ref(-1, -2); var myValue1 = myRef[0]; var myValue2 = myRef[1]; } // Open the Formula Output Window from the tools menu to view the values of myRef. if (BarCntr > 20) { debugPrintln("Bar Index: " + getCurrentBarIndex() + " myValue1= " + myValue1 + " myValue2= " + myValue2); } return new Array(vMA1, vMA2); }