Australian (ASX) Stock Market Forum

Amibroker FAQ

Hi friends,

I am just trying to write a few simple Amibroker formulas to get a feel of how AFL works.

I was trying to scan signals for the following logic:
2 higher highs and 2 higher lows
one inside bar
close > 20 ma > 50 ma

for some reason, i am getting no buy signals.
when i replaced 'close > 20 ma > 50 ma' with ma crossover, I was getting a few signals.
I think it has got something to do with 'close > 20 ma > 50 ma' , but I can't figure out what it is.
Can someone please help me with this???
Thank You.



Code:
lowlogic = Ref(Low,-1)>Ref(Low,-2);
highlogic = Ref(High,-1)>Ref(High,-2);
logic1 = Close> lastValue(EMA(C,20))>lastValue(EMA(C,50));
iblow = Low>Ref(Low,-1);
ibhigh = High<Ref(High,-1);
Buy = lowlogic AND highlogic AND logic1 AND iblow AND ibhigh;
 
Hi friends,

I am just trying to write a few simple Amibroker formulas to get a feel of how AFL works.

I was trying to scan signals for the following logic:
2 higher highs and 2 higher lows
one inside bar
close > 20 ma > 50 ma

Code:
//Exploration 

Lowlogic = Ref(L,-1) > Ref(L,-2);
Highlogic = Ref(H,-1) > Ref(H,-2);
Logic1 = C > EMA(C,20) & C > EMA(C,50); 
Iblow = L > Ref(L,-1);
Ibhigh = H < Ref(H,-1);
Buy = Lowlogic & Highlogic & Logic1 & Iblow & Ibhigh;

Filter = Buy;
AddColumn(IIf(Buy, C, 0),"Buy", 1.3);
 
Hi friends,

I am just trying to write a few simple Amibroker formulas to get a feel of how AFL works.

I was trying to scan signals for the following logic:
2 higher highs and 2 higher lows
one inside bar
close > 20 ma > 50 ma

for some reason, i am getting no buy signals.
when i replaced 'close > 20 ma > 50 ma' with ma crossover, I was getting a few signals.
I think it has got something to do with 'close > 20 ma > 50 ma' , but I can't figure out what it is.
Can someone please help me with this???
Thank You.



Code:
lowlogic = Ref(Low,-1)>Ref(Low,-2);
highlogic = Ref(High,-1)>Ref(High,-2);
logic1 = Close> lastValue(EMA(C,20))>lastValue(EMA(C,50));
iblow = Low>Ref(Low,-1);
ibhigh = High<Ref(High,-1);
Buy = lowlogic AND highlogic AND logic1 AND iblow AND ibhigh;

It's wrong. You have to include AND operator.

Code:
logic1 = Close > EMA(C,20) AND Close > EMA(C,50);
 
Taking Wysiwygs code sample and if you just wanna explore buy signals that occur on most recent bar then all that is needed code wise is adding Status( "lastbarinrange" )

Code:
//Exploration 

Lowlogic = Ref(L,-1) > Ref(L,-2);
Highlogic = Ref(H,-1) > Ref(H,-2);
Logic1 = C > EMA(C,20) & C > EMA(C,50); 
Iblow = L > Ref(L,-1);
Ibhigh = H < Ref(H,-1);
Buy = Lowlogic & Highlogic & Logic1 & Iblow & Ibhigh;

Filter = Buy AND Status( "lastbarinrange" );
AddColumn(C,"Close@Buy", 1.3);

BTW the only difference between Exploration and Scan is that Exploration is advanced scan.
 
If you wanna do conditions like this one

Code:
Lowlogic = L > Ref(L, -1) AND Ref(L,-1) > Ref(L,-2)  AND Ref(L,-2) > Ref(L,-3);

Then it is shorter to use

Code:
Lowlogic = Sum(L > Ref( L, -1 ), 3 ) == 3;
 
If you wanna do conditions like this one

Code:
Lowlogic = L > Ref(L, -1) AND Ref(L,-1) > Ref(L,-2)  AND Ref(L,-2) > Ref(L,-3);

Then it is shorter to use

Code:
Lowlogic = Sum(L > Ref( L, -1 ), 3 ) == 3;

Proof

Code:
Plot( C, "Price", colorDefault, styleCandle );


Lowlogic = L > Ref( L, -1 ) AND Ref(L,-1) > Ref(L,-2)  AND Ref(L,-2) > Ref(L,-3);
PlotShapes( Lowlogic * shapeSmallCircle, colorRed, layer = 0, y = L, -10 );


Lowlogic = Sum(L > Ref( L, -1 ), 3 ) == 3;
PlotShapes(Lowlogic * shapeSmallCircle, colorYellow, layer, y, -20 );
 
Hi friends,

Still struggling to understand how AFL works. (Sorry if this is a silly question)
--------------------------------------------------------
myhigh = HHV(Close,20);
mymacd = MACD(12,26) > 0;
myrsi = RSI(14) > 60;
Buy = myhigh AND mymacd AND myrsi;
--------------------------------------------------------

After writing the AFL, i scanned for buy signals for a given stock. But, when I scanned, I noticed that I was getting false signals.

Capture2.PNG

As you can notice, one of my conditions is highest value of close of 20 days. So, if close of 25th June was higher than close of 26th June, why did it give me the signal on 26th June??? I am not sure where I am going wrong.

Thanks :)
 
Hi friends,

Still struggling to understand how AFL works. (Sorry if this is a silly question)
--------------------------------------------------------
myhigh = HHV(Close,20);
mymacd = MACD(12,26) > 0;
myrsi = RSI(14) > 60;
Buy = myhigh AND mymacd AND myrsi;
--------------------------------------------------------

After writing the AFL, i scanned for buy signals for a given stock. But, when I scanned, I noticed that I was getting false signals.

View attachment 63495

As you can notice, one of my conditions is highest value of close of 20 days. So, if close of 25th June was higher than close of 26th June, why did it give me the signal on 26th June??? I am not sure where I am going wrong.

Thanks :)



Hi,

I think I understood my mistake. When I tried

myhigh = Close> Ref(HHV(Close,20),-1);
instead of
myhigh = HHV(Close,20);

it started working.

As per what I understand, we need a condition for buy. If I just say hhv(close,20) it is not a condition, hence Amibroker won't execute it.
 
Hi,

I think I understood my mistake. When I tried

myhigh = Close> Ref(HHV(Close,20),-1);
instead of
myhigh = HHV(Close,20);

it started working.

As per what I understand, we need a condition for buy. If I just say hhv(close,20) it is not a condition, hence Amibroker won't execute it.

Yes, trade signal arrays expect conditions returning true(1) or false(0).

If you are not sure you may use _Trace or new one in 6.00 _TraceF to see what is output of a variable.

Code:
period = 20; // number of averaging periods 
m = MA( Close, period ); // simple moving average
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
Short = Cover = 0;

dt = DateTime();
for( i = 0; i < BarCount; i++ )
	_TRACE( "Buy sig:" + Buy[i] + ", " + DateTimeToStr( dt[i] ) + " --- Sell sig:" + Sell[i] + ", " + DateTimeToStr( dt[i] ));

To get to know the type of a variable you may use typeof()

i.e

Code:
printf( typeof( Buy ) );
 
I wish to trade a watchlist which varies in time.

Is there a AFL function which would allow me to exclude a symbol from the defined watchlist from a certain date and on the other hand to only recognise a symbol after a certain date.

For instance, Watchlist1 contains the following members:

AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH

I aim to back test this trading system from 1 January 2012 to 30 June 2015

The first scenario

How to exclude symbol CCC from trading AFTER 1 February 2014

The second scenario

How to exclude symbol EEE from trading BEFORE 1 November 2014
 
I wish to trade a watchlist which varies in time.

Is there a AFL function which would allow me to exclude a symbol from the defined watchlist from a certain date and on the other hand to only recognise a symbol after a certain date.

For instance, Watchlist1 contains the following members:

AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH

I aim to back test this trading system from 1 January 2012 to 30 June 2015

The first scenario

How to exclude symbol CCC from trading AFTER 1 February 2014

The second scenario

How to exclude symbol EEE from trading BEFORE 1 November 2014


Code:
switch( Name() ) {
    case "CCC": datecond = dn <= 1140201;  break;
    case "EEE": datecond = dn >= 1141101;  break;
    default: datecond = true; break;
}


period = 20; 
m = MA( Close, period );
Buy = Cross( Close, m ) AND datecond; 
Sell = Cross( m, Close )  AND datecond;
Short = Cover = 0;
 
Code:
switch( Name() ) {
    case "CCC": datecond = dn <= 1140201;  break;
    case "EEE": datecond = dn >= 1141101;  break;
    default: datecond = true; break;
}


period = 20; 
m = MA( Close, period );
Buy = Cross( Close, m ) AND datecond; 
Sell = Cross( m, Close )  AND datecond;
Short = Cover = 0;

Variable dn stands for

Code:
 dn = DateNum();
 
Sorry, I know this is probably very straight forward but for some reason I am having a problem.

With the following code:
Code:
SetTradeDelays( 1, 1, 1, 1 );

Buy = Close > MA( Close, 50 );

Sell = (MA(Close,50)>C);

If I am exploring a date range, say from 14/01/15, and the close on 14/01/15 is already above MA(Close,50) then I am expecting that I will buy at the opening on 15/01/15.

This is not what I am getting. The buy is only being generated in the future after a sequence which firstly has a close below MA(Close,50) say on 15/02/15. The buy is generated the next time the close goes above MA(Close,50), say 16/03/15 - buy taking place on 17/03/15 at the open price.

As I said I am hoping to achieve an immediate buy from the date of my backtest, as my condition is already met and not wait until a future date.

I presume the answer is extremely obvious, but ......
 
I used this code and got 4 buys next day after test begin date. I assume you meant sell at crossing of price close and MA50.?

Code:
Buy = Close > MA( Close, 50 );
Sell = Cross(MA(Close,50), C);
 
Sorry, I know this is probably very straight forward but for some reason I am having a problem.

With the following code:
Code:
SetTradeDelays( 1, 1, 1, 1 );

Buy = Close > MA( Close, 50 );

Sell = (MA(Close,50)>C);

If I am exploring a date range, say from 14/01/15, and the close on 14/01/15 is already above MA(Close,50) then I am expecting that I will buy at the opening on 15/01/15.

This is not what I am getting. The buy is only being generated in the future after a sequence which firstly has a close below MA(Close,50) say on 15/02/15. The buy is generated the next time the close goes above MA(Close,50), say 16/03/15 - buy taking place on 17/03/15 at the open price.

As I said I am hoping to achieve an immediate buy from the date of my backtest, as my condition is already met and not wait until a future date.

I presume the answer is extremely obvious, but ......

So what is with your problem few posts ago?
Is this supposed to be a code factory where you just have to grab your stuff??
 
I used this code and got 4 buys next day after test begin date. I assume you meant sell at crossing of price close and MA50.?

Code:
Buy = Close > MA( Close, 50 );
Sell = Cross(MA(Close,50), C);

Thanks for your consideration. I have sorted my issue. I had also included the following code to declutter my chart of superfluous buy signals:

Code:
buy = ExRem( buy, sell );

Once this was removed it works as designed. Oh dear:banghead:

So what is with your problem few posts ago?
Is this supposed to be a code factory where you just have to grab your stuff??

Hi Trash - yes thanks for your assistance before - your code worked.

As to code factory - I always try to get an answer from searching forums - but in absence of getting an outcome I do post for assistance. The last post was in some sense pretty obvious, except it wasn't when I posted.

Your input into these forums is always well informed and appreciated:xyxthumbs. Forgive someone who is challenged, but tries his hardest.
 
Hi Trash - yes thanks for your assistance before - your code worked.

As to code factory - I always try to get an answer from searching forums - but in absence of getting an outcome I do post for assistance. The last post was in some sense pretty obvious, except it wasn't when I posted.

Your input into these forums is always well informed and appreciated:xyxthumbs. Forgive someone who is challenged, but tries his hardest.

I was surprised because there was no feedback in regards to that one but just a move on to next "problem". So it is not about asking for help. But providing help is not a fool's job. So finding or getting some free code is not like finding a candy after every step taken in wonderland. So at least a "Thanks for taking the time. It works (or still not working)" is a minimum (always and no matter who is the "fool"). Perhaps you live in a different world but in my world things work differently.

So watch out or you may end up like James Woods https://www.youtube.com/watch?v=BmvLexamrmk

*end of the rant*
 
Top