Announcement

Collapse
No announcement yet.

Working with arrays

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

  • Working with arrays

    Hello,

    I have a problem with the programming of arrays.
    For example I would like to compare the current stochastic value
    with the value one bar before. If the current value is greater, the
    line shall be green, if it is smaller, it shall be red.

    Unfortunately, the result isn't correct since it doesn't always
    show the right color. This probably lies to it because the array is
    filled with new values too early.

    Who can tell me how I get a correct result?
    Must I use an array or is there also a simpler solution?

    Thanks
    Guido


    Here is my example code:

    // --------------------------------------------
    // $ColorStoch.efs
    // --------------------------------------------

    var study1 = new StochStudy(7, 3, 3);
    arStoch_7 = new Array(2);

    function preMain() {
    setPriceStudy(false);
    setStudyTitle("StochTest");
    setDefaultBarFgColor(Color.blue);
    setDefaultBarThickness(2);
    }

    function main() {
    var vStoch_7 = study1.getValue(StochStudy.FAST);
    var vTempStoch_7;

    vTempStoch_7 = arStoch_7.unshift(vStoch_7); // get one element

    if(arStoch_7[0] >= arStoch_7[1]) {
    setBarFgColor(Color.green, 0);
    } else if(arStoch_7[0] < arStoch_7[1]) {
    setBarFgColor(Color.red, 0);
    }

    vTempStoch_7 = arStoch_7.pop(); // delete one element

    return vStoch_7;
    }

    // --------------------------------------------

  • #2
    Hi,

    Try doing this:

    var vStoch_7 = study1.getValue(StochStudy.FAST,0,-2);

    Right now you are only getting one parameter (the most recent) from the study.

    FYI, usually if it this simple a case I will just define two variables, one inside and one outside of main().

    var LastFastStoch = null;

    main() {
    var FastStoch;

    Then get the variable etc... and right before I return:

    if (LastFastStoch == null){
    LastFastStoch = FastStoch;
    return;
    } else {
    if (LastFastStoch > FastStoch){
    setBarFgColor(Color.green, 0);
    } else {
    setBarFgColor(Color.red, 0);
    }
    LastFastStoch = FastStoch;
    return (FastStoch);
    }
    Last edited by gspiker; 12-03-2002, 12:35 PM.
    Garth

    Comment

    Working...
    X