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

Re: [amibroker] Learning to Loop



PureBytes Links

Trading Reference Links

Pavel, on Sunday, July 23, 2006 10:52 PM, Steve Dugas gave a detailed description of looping to Allen.   Maybe this description will help you. Ron D 

====================================================================

Hello,

I am finding learning how to code 'predefined' variables (i.e. those 
listed in the User Guide) with good success, however I am finding 
looping hard to get a handle on. Is there a tutorial available 
somewhere that might be of assistance?

Thanks,

Pavel.
=====================================================================

Hi Dingo,

I have no problem doing anything along the lines of powerscan and writing code.Ami is excellent and intuitive as far as that...

The problem for a complete non programmer is when i get to "looping",the manuals tend to leave out thebasic steps....its little things like the "i" and "{ " and " = = " and "buy=0" that are difficult to figure out.I get the intended logic,its just tought to build on having no foundation...

Appreciate it

Allan

=======================================================================================
Hi Allen - Here are the basics of a simple loop, hope it is helpful to someone...

In "regular" AFL, all elements of the data arrays are automatically processed at once. So for example

Close = 0;

will automatically initialize the entire Close array ( all dates ) by setting all of its elements to zero ( or more precisely, a working copy of Close, since actual database is not overwritten ). In general, it is quicker and easier to use this regular AFL - loops are really only necessary when you need to access individual array elements for some reason. A simple loop "template" would be

for ( beginning counter value; test for condition(s) to break out of loop; increment counter )
{
    code to do whatever you want to current array element
}

Now we will turn the template into a real loop.  "i" is simply a variable that accepts a numeric value and acts as a counter. We will increment it with each pass through the loop to keep track of where we are. The letter "i" is commonly used for this because, I beleive, it stands for "integer", but we can actually use any variable name we want, such as "count", etc

for( count = 0; count < BarCount; count++ )
{
    Close[count] = 0;
}

This also initializes the count array to zero, but one element at a time. As you can see, using regular AFL is easier   8 - )   Now we will "pick apart" the loop a bit...

for ( count = 0

Since we want to start with the first array element ( element # 0 ), we initialize the counter at 0. The loop reads this code just once at the beginning to see where to start, it will not be rechecked after the loop starts running.

count < Barcount

BarCount is the number of data bars for any given ticker. Lets say it is 1000, so the actual elements of the Close array would be numbered 0 - 999.
"count < Barcount" tests to see if it is time to stop looping yet. It means "continue doing loops while count is less than Barcount." Since we have just set "count" to 0, it is less than 1000 so the loop will be entered.

{
    Close[count] = 0;
}

This is the "body" of the loop - it contains the instructions to be performed on each pass through the loop. Here we are just initializing a single element of the Close array to 0. On this pass, the variable "count" is set to 0, so Count[0]  ( first element ) will be set to 0.

count++ ) // back up to the header now

This is just a short way of writing "count = count + 1". We are just incrementing the count by one. When we are incrementing by 1, we are allowed to use this "shorthand". If we were incrementing by anything else, like 2 or 3, we would have to write "count = count + 2", etc. So next, count is incremented from 0 to 1.

Now, the first pass is done, and subsequent passes will continue repeating the same above steps until the end of the array is reached -
1. Is count still < Barcount? If so, do another pass through loop, if not, time to exit the loop.
2. If doing another pass, set Close[count] ( next element ) to 0
3. increment counter again so next elent is accessed on next pass

If this sounds too simplstic or condescending, I apologize. Or, if you have any questions, feel free to ask...

Steve
Content-Description: "AVG certification"
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.16.8/621 - Release Date: 1/9/2007 1:37 PM