This is a mobile optimized page that loads fast, if you want to load the real page, click this text.

Document - Looping In AmiBroker AFL

Ok tradezy, here is an indicator you can overlay on your price chart.

Code:
_SECTION_BEGIN( "Guppy CBL" );
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-- )
            {
                if ( Low[j] < mostLow )
                {
                    mostLow = Low[j];
                    steps--;
                }
            }

            cblArr[i] = mostLow;
        }
    }

    return cblArr;
}
_SECTION_END();


_SECTION_BEGIN( "Guppy Stop" );
function TrailingStop( data )
{
    stop[0] = data[0];

    for ( i = 1; i < BarCount; i++ )
    {
        if ( Close[i] >= stop[i-1] )
            stop[i] = Max( data[i], stop[i-1] );
        else
            stop[i] = data[i];
    }

    return stop;
}

cblStop = TrailingStop( Cbl( 3 ) );
Plot( cblStop, "cblStop", colorBlue, styleLine );
_SECTION_END();

I hope this helps you understand what was required.
 
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

Metastock achieves self-referencing (recursive) with PREV whereas AmiBroker uses AMA() or AMA2(). Here is a message from TJ which discusses both approaches:

"PREV in MS is needed because MS does NOT have looping.

In AB, there are loop constructs so there is no need for PREV
because loops give faster and more general way of solving coding
problems.

Since loops are more general they allow easy translation of every
case where MS needs PREV.

Statment like this:
Z =(A*B)+(C*PREV);

Using loop looks as follows:

z = 0; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = Z[ i - 1 ]; // PREV is previous value of Z.
Z[ i ] = A[ i ] * B[ i ] + C[ i ] * prev;
}

As you can see the statement is identical with the exception that AFL looping code uses [ ] operator to access individual array elements.

Good thing is that AFL looping code is BarCount-times faster than MS code. Since 5 years of EOD data is 1300 bars, AFL looping code is 1300 times faster than MS in that case.

It is also possible to use AMA/AMA2 instead of PREV:

Z = AMA2( A, B, C );

which is shorter but AMA is less general than looping code.

Best regards,
Tomasz Janeczko
amibroker.com"
 

Sorry GP. I did not realize that your message was from 2007.
 
Sorry GP. I did not realize that your message was from 2007.

It doesn't matter whether it is 2007 or now it stands as wrong in both cases.
Here's another quote.


 
Hello Everyone,

Thank you all for responding so far.

To be clear I am an absolute Amibroker looping beginner.!!!!... This is why I am posting these questions. I've tried reading the Amibroker manual & Howard Bandy's Introduction to Amibroker and I am still struggling to grasp looping.

As requested, below is the code I am using to get the Guppy trailing stop. The looping section of the code is from page 20 & 21 of the Great Pig's "Looping in Amibroker AFL" document. I have added on my own Buy & Sell conditions.

Unfortunately the Guppy Count Back Line is not triggering when I run my backtest.

Could someone with programing experience please correct the code, so that the Guppy trailing stop triggers when I run a backtest? :1zhelp:

With thanks

Tradezy


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--) 
{ 
if (Low[j] < mostLow) 
{
mostLow = Low[j]; 
steps--; 
} 
} 
cblArr[i] = mostLow; 
} 
} 
return cblArr; 
} 

function TrailingStop(data) 
{
stop[0] = data[0]; 
for (i = 1; i < BarCount; i++) 
{
if (Close[i] >= stop[i-1]) 
stop[i] = Max(data[i], stop[i-1]); 
else 
stop[i] = data[i]; 
} 
return stop; 
}

SetOption ("Maxopenpositions", 5);
SetPositionsize (20, spsPercentofEquity);
Moving1 = MA (C,20);
Moving2 = MA (C,50);
cblStop = TrailingStop (Cbl(3));
Buy = Cross (Moving1, Moving2);
Sell = Cross (cblStop, C);
 
Simply plot cblStop then you will see that there won't be a cross. So your rules have to be different based on Ref( cblStop, -1).
 
Thanks Trash, That really helps!

The original Guppy Count Back line trailing sell is only meant to recalculate when a new high is formed.

The loop provided by GP does not seem to cater for this.

Is there any way to change the looping code to make this happen?

With thanks,

Tradezy

PS Here is the code below for anyone interested.

Note the last line has been amended, thanks to the suggestion by Trash.


CODE]function Cbl(bars)
{
cblArr = Null;
if (bars > 0)
{
for (i = 1; i < BarCount; i++)
{
steps = bars - 1;
mostLow = Low;
for (j = i-1; j >= 0 && steps > 0; j--)
{
if (Low[j] < mostLow)
{
mostLow = Low[j];
steps--;
}
}
cblArr = mostLow;
}
}
return cblArr;
}

function TrailingStop(data)
{
stop[0] = data[0];
for (i = 1; i < BarCount; i++)
{
if (Close >= stop[i-1])
stop = Max(data, stop[i-1]);
else
stop = data;
}
return stop;
}

SetOption ("Maxopenpositions", 5);
SetPositionsize (20, spsPercentofEquity);
Moving1 = MA (C,20);
Moving2 = MA (C,50);
cblStop = TrailingStop (Cbl(3));
Buy = Cross (Moving1, Moving2);
Sell = C < Ref (cblStop, -1);[/CODE]
 


Here is a link for a Guppy Countback plugin. I have not used it but give it a shot.

http://www.marketcalls.in/amibroker...-trailing-stop-loss-plugin-for-amibroker.html
 
Hi Colion,

Thanks for the link for the Guppy Countback plugin. It looks like an interesting website.

Thank you,

Tradezy.
 

I am a Newbie, so I am reading your file 8 years after you posted it. i just wanted to say thanks it is great! I wish someone would write an entire book on the programming aspect of AmiBroker (Howard bandy's books a good but deal mostly with trading systems).

Great job and thanks for generously sharing.

Larry
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more...