[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Jscript help



PureBytes Links

Trading Reference Links

Hello,

The problem is that you access the array elements OUTSIDE the
upper bound.

> k = Vol.length;
> Sumvol = 0;
> 
> for( i = 0; i < k ; i++ )
> {
> Sumvol = Sumvol + Vol[ k - i ];
> }

At the very beginning the "i" variable is 0 while k variable is equal to the
length (size) of volume array.

So you access Vol[ k - i ], but i = 0 so Vol[ k ], k = Vol.length, so Vol[ Vol.length ]
This is one array element TOO FAR.
It is so because arrays in Jscript have zero-based indexes
from 0 to length - 1.

so your loop should look like this
for( i = 0; i < k ; i++ )
{
Sumvol = Sumvol + Vol[ k - i - 1 ];
}


Best regards,
Tomasz Janeczko
===============
AmiBroker - the comprehensive share manager.
http://www.amibroker.com


----- Original Message ----- 
From: <yellayi@xxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Saturday, November 03, 2001 3:24 AM
Subject: [amibroker] Jscript help


> I am trying to calculate the sum of daily volume in a descending 
> order and I get a run time error with the following code.
> 
> EnableScript("jscript");
> <%
> Vol = VBArray( AFL( "Volume" ) ).toArray();
> 
> // perform the loop that calculates Sum of Volume
> 
> k = Vol.length;
> Sumvol = 0;
> 
> for( i = 0; i < k ; i++ )
> {
> Sumvol = Sumvol + Vol[ k - i ];
> }
> 
> AFL.Var("Sumvol") = Sumvol;
> %>
> 
> Filter = C>0;
> NumColumns=2;
> Column0=Close;
> Column1 = Sumvol;
> 
> If I change the expression in the for loop to 
> Sumvol = Sumvol + Vol[ i ];
> 
> it works. However, that is not what I want. Any help is appreciated.
> 
> Also the following for loop gives an error , I don't know why?
> for( i = k; i > k -5 ; i--) -- doesn't seem like i--
> 
> 
> thanks
> 
> mohan
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 
> 
>