ref()

ICE Data Services -

ref()
Previous  Top  Next

ref( barIndex )

The ref() function allows you to retrieve prior values for a built-in study (or studies). If multiple return values are being used then the ref() function will return its results in an array.

Parameters

barIndex
offset indicating which historical indicator data points to retrieve

Usage
var study1    = null;
var study2    = null;
var bInit      = false;
var BarCntr   = 0;

function preMain() { 
   setPriceStudy(true); 
} 
function main() { 
var nMA1;
var nMA2;
var myRef;
var myValue1, myValue2;
   if ( bInit==false ) {
      study1 = sma( 10 );
      study2 = sma( 20 );
      bInit=true;
   }
   nMA1 = study1.getValue(0);
   nMA2 = study2.getValue(0); 
   if (getBarState() == BARSTATE_NEWBAR) { 
      BarCntr += 1; 
   } 
   /*** BarCntr logic ***/     // We're using 20 because our longest sma() requires a minimum of 20 bars. 
   if (BarCntr > 20) {           //myRef will now contain the two MA values from the prior bar.       //they will be in an array so we need to treat them as such       myRef = ref(-1);        if ( myRef != null ) {          myValue1 = myRef[0];           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( nMA1, nMA2 );  }