Announcement

Collapse
No announcement yet.

Referencing previous values

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

  • Referencing previous values

    Hi, I have maybe a simple question. In Tradestation and other such packages, I can reference the values of variables in my calculations/studies from previous days via something like:

    variable_x(10) // Tradestation
    // variable_x's value 10 days ago in same formula
    PREV // Metastock
    // Value of formula from previous bar

    I cannot seem to find a way to do this with eSignal formula script.
    A simple example should demonstrate what I mean:


    ///////////////////////////////////
    // Exponential Moving Average study
    ///////////////////////////////////

    function preMain() {
    setPriceStudy(true);
    }

    var ema = 0.0;
    var ema_avg = 0.0;

    function main(nInputLength) {

    // ema is calculated here

    // now we calculate the ema average
    ema_avg = (ema(0) + ema(1) + ema(2) + ema(3)) / 4;
    return ema_avg;

    }

    At the end, I want to return the average of the value of ema within the same formula of the _past_ four days. How can I do this without calculating all four averages every single bar?

    Christian

  • #2
    Atlas,

    I'm not clear which of these two you are trying to do, so I will answer for both:

    1) If you are trying to reference old variables (vs old returns) there is no way to do that as such, but variable defined outside of main() and PreMain() keep there values though each iteration of main().

    var foo = 1;

    main(){

    foo = foo +1;

    }

    Foo will increment by 1 for each interation of main (ie: each bar in the chart).

    2) If you want to look at past returns from the formula, you can either keep the returns in a variable define outside of main(), or use ref(). ref() refences the last returns as:

    ref(offset, # of bars);

    So if I wanted to read the last 10 bars of returns my formula did:

    var Last10 = ref(0,-10);

    or if I wanted to go back 5 bars and read the previous 3.

    var lookback = ref(-5, -3); // go back 5 and read previous 3

    If for some reason I wanted them in reverse order I could do:

    var lookback - ref (-8, 3); // go back 8 and read forward 3

    Hope this answers the question.

    Garth

    Edited by - gspiker on 10/19/2002 09:00:54
    Garth

    Comment

    Working...
    X