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

[amibroker] Always INITIALIZE array variables before accessing individual elements of the array


  • To: <amibroker@xxxxxxxxxxxxxxx>
  • Subject: [amibroker] Always INITIALIZE array variables before accessing individual elements of the array
  • From: "Tomasz Janeczko" <amibroker@xxxxxx>
  • Date: Mon, 13 Oct 2003 02:47:47 -0700

PureBytes Links

Trading Reference Links

Hello,

Some people forget to INITIALIZE variables in their code before accessing
individual elements of the arrays.

Example:

Sell[ 10 ] = 1;  

This statement assigns value of 1 to 10th element of the array.
But what about all the remaining elements ?
Answer: they are left uninitialized (i.e. the contents is random).

Example 2:

Buy = Cross( MACD(), Signal() );

for( i = 1; i < BarCount; i++ )
{
  if( Buy[ i - 1 ] == 1 ) Sell[ i ] = True;
}

The code above writes 'True' to Sell array ONLY for one bar following
buy signal. All the other elements of Sell array are RANDOM.

Therefore you should either
1. write to ALL array elements in your code
or
2. initialize variable before accessing array elements

Example 2 corrected according to method 1 should look like this:

Buy = Cross( MACD(), Signal() );

for( i = 1; i < BarCount; i++ )
{
  if( Buy[ i - 1 ] == 1 ) 
       Sell[ i ] = True;
  else
       Sell[ i ] = False;
}

Example 2 corrected according to method 2 should look like this:

Buy = Cross( MACD(), Signal() );

Sell = 0; // INITIALIZE the variable !!!!!!!!!!!

for( i = 1; i < BarCount; i++ )
{
  if( Buy[ i - 1 ] == 1 ) 
       Sell[ i ] = True;
}

So again: always initialize variable by assigning some value to it before
accessing individual array elements (if your code does not ensure that all elements are written)

Best regards,
Tomasz Janeczko
amibroker.com

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Rent DVDs from home.
Over 14,500 titles. Free Shipping
& No Late Fees. Try Netflix for FREE!
http://us.click.yahoo.com/ArdFIC/hP.FAA/3jkFAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

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