Australian (ASX) Stock Market Forum

Document - Looping In AmiBroker AFL

GreatPig

Pigs In Space
Joined
9 July 2004
Posts
2,368
Reactions
14
Attached is a document I've written covering the use of loops in AmiBroker's AFL.

It starts off discussing arrays and array indexing, then progresses to looping, the different types of loop constructs, the switch & case construct (new in version 4.91.0 Beta), and finally a few real examples.

It's aimed at AmiBroker users who already have a basic knowledge of programming AFL but would like to learn how to use loops.

Feedback from both experienced programmers and beginners welcome, to let me know if I've made any mistakes or have anything wrong, and whether it's logical and easy to follow for someone who's not already familiar with the topic. I've tried to make it progress step by step, but may have unwittingly made some of the steps giant leaps. :D

The document may be freely distributed.

Cheers,
GP
 

Attachments

  • Looping in AmiBroker AFL.pdf
    280.4 KB · Views: 1,303
I haven't gone through it in detail yet, but looks to be A grade stuff GP. Thanks! I'm sure many will benefit from your work. When playing with systems, I've often found myself wanting to use recursion but never really looked into how. Seems Ami supports it with fairly standard syntax.

Very tempted to make the switch back to Amibroker from Metastock 9. The longer I use metastock, the more I find myself longing for Ami's simplicity and power. Does Ami work with TradeSim?
 
Thanks. Note though that looping is not the same as recursion. AmiBroker doesn't support recursion (last I saw mention of it).

And no idea about AmiBroker with Tradesim. Never used Tradesim.

Cheers,
GP
 
Thanks GP, i'll have a look at it later today, if it passes my test it will be idiot proof!:D

Cheers,
 
Hi GP,

Got a programming question for you. How do I know what type of variable AFL will assume, eg.

If I use x=null, that could be intrepreted as a single variable being null, or it could be the entire array whose contents are all null. Similarly for y=true or z=0. How does AFL decide which type of variable to define for you. In all cases, it would seem to me that it could make a case to go either way.

Performance wise, it would be much better if one had to declare the type of variable, rather than for AFL to assume its an array when it should be a variable.

Bingk6
 
Bingk6,

It could be either, but it doesn't really matter.

I don't know how AmiBroker handles this internally, whether it assumes one or the other until later or parses the whole script first before deciding (I remember reading something about a preprocessor, so it may well be done that way). There could be efficiency issues if it initialises a whole array when ultimately it will just be a single number, but unless you're running something that needs ultra-high performance, then it shouldn't really matter either. Filling an array of a few thousand numbers with the same value doesn't take much time in compiled assembly code (which is what it is when the script interpreter does it). On the other hand, maybe the script interpreter optimises and just remembers that it needs to initialise the variable but doesn't actually do anything until it's later used.

GP
 
I've now uploaded a new version of this document to the AmiBroker Yahoo forum files section, to correct a couple of minor errors. Specifically, a couple of the DO loop examples were missing semicolons after the while condition.

I won't put the new version here as well, as I think one public copy is enough and I have the ability there to delete old versions myself when updating the file.

Cheers,
GP
 
If you'd like, send me a PM with the link to your updated file and I'll replace the file in your original post.
 
I've now uploaded a new version of this document to the AmiBroker Yahoo forum files section, to correct a couple of minor errors. Specifically, a couple of the DO loop examples were missing semicolons after the while condition.

I won't put the new version here as well, as I think one public copy is enough and I have the ability there to delete old versions myself when updating the file.

Cheers,
GP

Hi GreatPig,

Thanks for the doc. I'm on the yahoo group as well, can you tell me what the name of the thread is so I can find the revised doc?

I'm new to programming and this looks like the sort of thing I'm after (having some issues with scaling in/out and guessing this will clear things up for me).

Cheers
FKOS
 
Hi,

Has anyone successfully used Guppy's Count Back Line stop loss looping formula?

Great Pig has outlined Guppy's CBL stoploss code (page 20) & Guppy's CBL trailing stoploss code (page 21) in the pdf at the start of this thread. I have tried it but it does not seem to be working, for me.

Question:
1. Do I need to add any other code apart from my Buy & Sell conditions?
2. Do I need to use both formula's (ie page 20's code & page 21's code together) for Guppy's CBL trailing stoploss to work?

Apologies if these are crazy questions. I'm new to Amibroker. Can write arrays but need time to develop some looping skills.

Lastly many thanks to Great Pig for the time & effort in putting this document together. It is the first I have found with a mention of Guppy's CBL - much appreciated. :)

With thanks

Tradezy
 
Why do people almost always post ".... it doesn't work...." but don't exactly mention WHAT it is that is not working. Frustrating.
 
Why do people almost always post ".... it doesn't work...." but don't exactly mention WHAT it is that is not working. Frustrating.

Hi Trash,

I'll elaborate....

I am using the following code:

SetOption ("Maxopenpositions", 5);
SetPositionsize (20, spsPercentofEquity);
Moving1 = MA (C,20);
Moving2 = MA (C, 50);
Buy = Cross (Moving1, Moving2);
Sell = Cross (Moving2, Moving1);

I then add on the Guppy code from page 20.

Issue 1:
The results are identical whether or not I use the Guppy code or not. So it appears the Guppy CBL stop loss is not being activated. This is confirmed by looking at a chart.

Issue 2:
In addition to the code on page 20 I add on the Guppy CBL trailing stop loss code on page 21. Again the results are identical.So it appears the Guppy Initial CBL and the trail CBL are not being activated.

I noticed that there could be an amendment to the code updated on the Yahoo Groups Forum. Having trouble finding it there. In any case is appears to be for Do Loop examples.

I have faith the code works. It's my lack of looping skills letting me down.

I hope this explains why I am asking the 2 questions in the previous post.

Assistance appreciated.

With thanks,

Tradezy
 
So you have this in your code.

Code:
function Cbl(bars) {
}
cblArr = Null; if (bars > 0)
{
for (i = 1; i < BarCount; i++) {
steps = bars - 1;
mostLow = Low[i];
for (j = i-1; j >= 0 && steps > 0; j--) // Loop backwards over all ...
{
if (Low[j] < mostLow) {
mostLow = Low[j];
steps--; }
}
cblArr[i] = mostLow; }
}
return cblArr;

All this has done has defined a function called CBL()

So next you need to set up an array say:

GuppyCBL = CBL( 3 );

From there you can work with the data. Sorry i don't know anything about Guppy and CBL.
 
Hi Trash,

I'll elaborate....

I am using the following code:

SetOption ("Maxopenpositions", 5);
SetPositionsize (20, spsPercentofEquity);
Moving1 = MA (C,20);
Moving2 = MA (C, 50);
Buy = Cross (Moving1, Moving2);
Sell = Cross (Moving2, Moving1);

I then add on the Guppy code from page 20.

Issue 1:
The results are identical whether or not I use the Guppy code or not. So it appears the Guppy CBL stop loss is not being activated. This is confirmed by looking at a chart.

Issue 2:
In addition to the code on page 20 I add on the Guppy CBL trailing stop loss code on page 21. Again the results are identical.So it appears the Guppy Initial CBL and the trail CBL are not being activated.

I noticed that there could be an amendment to the code updated on the Yahoo Groups Forum. Having trouble finding it there. In any case is appears to be for Do Loop examples.

I have faith the code works. It's my lack of looping skills letting me down.

I hope this explains why I am asking the 2 questions in the previous post.

Assistance appreciated.

With thanks,

Tradezy

Your upper code has no CBL or whatever at all. Where is the complete one? I mean do you expect others reading your mind? Or having some magical eyes looking on your screen from hundreds or thousands of miles away? How do you call your CBL within your whole code? No one knows. Here is the news: Wheel of fortune is out and canceled in year 2014. No one watches it anymore because it is boring and tiresome.
 
Instead of writing long essays you could have saved everyone and yourself massive of time by including a reproducible code showing how your cbl is used in there.

So by guessing if you just add a function without calling it then it just sits outside on the bench but is not playing on the field where the action takes place or in short it is just wasting space.
 
Hi Tradezy,

Gee you're copping a flogging in this thread. I've never coded in Amibroker before, but I know how to code in C and C++. Tradezy do you have much experience in programming? Do you understand what each statement does in your code above?

Usually in a standard structual programming language like c, you have code that is structured like this:

Guppy1(int bars)
{
...
}

Guppy2(int bars)
{
...
}

Main Program()
{
SetOption ("Maxopenpositions", 5);
SetPositionsize (20, spsPercentofEquity);
Moving1 = MA (C,20);
Moving2 = MA (C, 50);
Buy = Cross (Moving1, Moving2);
Sell = Cross (Moving2, Moving1);

for (int i=0; i<50; i++)
{
Guppy1(i);
Guppy2(2*i);
}
}

Notice the main program calls functions Guppy1() and Guppy2().

When the program runs, the Main Program is where execution starts and you call functions/procedures such as Guppy1() and Guppy2(). We write functions/procedures so that we don't have to write repetitive code or better put, we want to write re-usable code. Just say we wanted to call function Guppy1() fifty times. Can you see the advantage of writing a function? Can you see why if you don't call the function Guppy1(), nothing in that function code will be executed?

Tradezy I want you to have a look at the Amibroker code on page 20 for function CBL and ask yourself the following questions:

What does cblArr = Null actually do? and how could I achieve the same result using a for loop?
What happens if bars = 0 or -1?
Why does the first for loop start at 1 and not 0? and what would happen if it started at 0?
Why do we use BarCount and not Bars in first for loop? What is the difference between BarCount and bars? Are they the same thing? :D
What happens if we remove the 2nd for loop and it's enclosed code altogether?
What happens if bars > BarCount?

Tradezy, don't fall into the trap of thinking you know what you are doing? Experiment with the code so that it is right in your mind. You say you are having problems with looping skills, so why not post some looping code in the forum with some output. If you don't solve the little things first, then you will go through the whole Amibroker process thinking you know what you are doing when you should be knowing you know what you're doing. May I also suggest that you read in maybe one of Howard Bandy's books how code is structured in terms of calling modules/functions/procedures.

Cheers,

Andrew.
 
Top