Announcement

Collapse
No announcement yet.

How do I get the prior bar value of a custom indicator?

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

  • How do I get the prior bar value of a custom indicator?

    Hi all,

    I know this should be very obvious, but I can't seem to figure out how to get the value of my custom indicator for prior bars.

    I can get bar values and built in study values, just not my indicator value.

    Can some please give me a clue. Thanks so much!!!


    Jerry
    "Make it a great day"
    [email protected]

  • #2
    Solution...

    If you are "returning" the value to the cursor window (with a return statement at the end of your code), then you can use the "Ref" function to retrieve previous data. Instructions below..

    If not, then you have to write code to record the LAST value of your indicator. This is the only other solution.

    -------------[ Ref instructions ]---------------------------------------
    ref( nParams )


    ยท nParams: Offsets indicating which historical indicator data points you wish to retrieve.

    This is a special function that allows you to retrieve prior values for a built-in study or studies.

    Examples:

    /*** 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);
    //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);
    }

    Example2:

    /*** 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);
    //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;
    }
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment

    Working...
    X