Announcement

Collapse
No announcement yet.

Aray help

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

  • Aray help

    Hi

    I am using this function, which works and returns a value, however in the formula output i keep getting a message that

    sprice has no properties at line 160, (sum=sum+sprice[i];

    function summation(sprice,xlength){

    var i=0;
    var sum=0;
    for (i = 0; i <xlength; i++);{
    if (sprice == null);{
    sprice = getValue("Close",0,xlength);}
    if (sprice[i] != null);{
    sum = sum + sprice[i];}
    }
    return (sum);
    }

    Anyone know why i get this the following function is the only function that uses this calculation
    function averagefc(sprice,length){
    if (sprice == 0);{
    sprice = new Array(getValue("Close",0,length+1));
    }
    afcline = (summation(sprice,length))/length;
    return(afcline);
    }

  • #2
    biswar,

    I would guess that the issue is that when getValue() returns only 1 parameter (at the start of processing the chart) that sprice will not be an array. Therefore even your null check on sprice[i] should generate the error you are seeing.

    I am curious, since without knowing what you are trying to do it looks like a possible bug, is there a reason why in the averagefc ()function you do a getValue() asking for length+1 bars, and in the summation() function you only ask for length bars?

    Garth
    Garth

    Comment


    • #3
      array help

      garth

      yes it was possibly a cure for the error i was getting, i thought if the array had a extra value in there it may cure it, which it doesnt make any difference, if i take the +1 out, it doesnt make any difference.

      is there away to stop the error to when it hits the first bar ?

      thanks

      bob

      Comment


      • #4
        biswar,

        There are a few ways to do this, depending on what you need.
        One simple method would be to create a global var to track the number of bars that have been processed, and either 1) exit unless the number of bars is equal to oe greater than the length you need, or 2) Use the variable for the value of length, and when length is equal to 1 you don't treat sprice as an array.


        NOTE: The below code snippets are for examples only

        PHP Code:
        var nNumBars 0// Number of bars processed

        function main(){
        .
        .
        .
              var 
        nState getBarState();
              if (
        nState == BATSTATE_ALLBARS){
                  
        nNumBars 1;
              } else if (
        nState == BARSTATE_NEWBAR){
                  
        nNumBars++;
              } 

               if (
        nNumbers length){
                   return;
               }
        .
        .
        .

        or

        PHP Code:
        function main(){
        .
        .
        .
              var 
        nState getBarState();

               if (
        nState == BATSTATE_ALLBARS){
                  
        nNumBars 1;
              } else if (
        nState == BARSTATE_NEWBAR){
                  
        nNumBars++;
              } 
        .
        .
        .
              if (
        sprice == null){
                 
        sprice getValue("Close",0,nNumBars)
              }
              if (
        nNumBars == 1){
                 if (
        sprice != null){
              }else{
                 if (
        sprice[i] != null);{
              } 

        Hope this helps...

        Garth
        Garth

        Comment


        • #5
          array help

          thanks garth


          i see that this will fix it for new bars coming however, for backtesting purposes, ill have to live with it.

          Comment


          • #6
            I'm not sure I understand. This should work for backtesting or live charts...

            G
            Garth

            Comment


            • #7
              Array help

              Hi

              Yes it did work for backtesting, however due the amount of bars displayed on a tick chart, I am using the formula that requires 90 bars of data first, so it sorta screwed the figures up.

              thanks for you rhelp, i got it working in the end, but decided to scrap it

              Comment

              Working...
              X