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

[amibroker] Re: How to count from arrays?



PureBytes Links

Trading Reference Links

Since I can't see the rest of your code I'm not sure why you wanted 
this to be in a loop but if all you are interested in is the number 
of 1's and 0's there are in some array looking backwards for n ( in 
this case 10 ) bars then this can be done without a loop by for 
example ...

// Generate an array with 1's and 0's

OnesZeros = IIf(C > Ref(C, -1), 1, 0);

Ones = Sum(OnesZeros, 10);
Zeros = Sum(OnesZeros == 0, 10);

Filter = 1;
AddColumn(OnesZeros, "1 / 0");
AddColumn(Ones, "1's");
AddColumn(Zeros, "0's");

--- In amibroker@xxxxxxxxxxxxxxx, John Nelson <trader@xxx> wrote:
>
> 
> All,
> 
> I'm counting the number of 1's and 0's from arrays which are the 
results 
> of our well-known set of indicators. The 1's and 0's actually 
represent 
> peaks and valleys and I'd like an indication of how many of them 
there 
> are within a specific period starting from the current bar and 
going back 
> myPeriod bars. I've noticed something strange though.
> 
> If I try to count in this manner....
> 
> for ( i = 0; i < myPeriod; i++ ) {
> 	if ( myarray[i] == 1 ) 
> 		total1 = total1 + 1;
> 	else
> 		total2 = total2 + 1;
> 	}
> 
> I stumble across a runtime error that admonishes me for having a 
subscript 
> that is not between 0 ... (BarCount-1). However the following 
snippet of 
> code avoids the runtime error...
>                                                                    
                              
> for ( i = 0; i < BarCount && i < myPeriod; i++ ) 
{                                                                    
                        
>         if ( myarray[i] == 
1 )                                                                  
                              
>                 total1 = total1 + 
1;                                                                   
                       
>         
else                                                                 
                                                 
>                 total2 = total2 + 
1;                                                                   
                       
>         }
> 
> Now this is really strange since I know "myPeriod" is far less 
than the
> BarCount. Could it be that AFL operations aren't striclty linear 
in that
> the results of my array operations preceeding the for loop are not 
yet
> complete? Could it be that I don't understand how array subscripts 
work?
> 
> So what is the proper way to count the number of 1's and 0's in an 
array 
> starting from the most recent bar and working back?
> 
> -- John
>







Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/