How to combine two necessary buy signals into one AFL code?

#1
hi all,

I'm new to this community and also to AFL, so please bear with me if I ask any stupid questions or simple things that you all take for granted.

I'm trying to start out with a few simple codes. I have made the following:

Buy=Cross(MACD(),Signal());
Buy=EMA( Close, 20 ) > EMA( Close, 50 );
Buy=StochK( 14, 3 ) > StochD( 14, 3, 3 );
Sell=Cross(Signal(), MACD());
Sell=EMA( Close, 20 ) < EMA(Close, 50 );
Sell=StochK(14, 3 ) < StochD(14, 3, 3 );
Filter=Buy OR Sell;
AddColumn( IIf( Buy, 66, 83 ), "Signal", formatChar );
AddColumn( MACD(), "MACD", 1.4 );
AddColumn( Signal(), "Signal", 1.4 );
AddColumn( StochK(), "StochK", 1.4 );
AddColumn( StochD(), "StochD", 1.4 );

This reflects the significance I attach to the EMA and the STS. However, when I Filter the results, a ticker gets inserted by Amibroker when it fulfills ONE of above buy/sell conditions. This is not what I want. I would like Amibroker to only filter the stocks for me that fulfill ALL of above Buy or sell conditions and only then show this ticker on the report screen.

How do I incorporate this into the code??

Thanks very very much for your assistance.
 

rvlv

Active Member
#2
Hi friend
you are doing a big mistake of bypassing syntax rules.
see cross syntax requirements are, Cross, bracket opens,itemm1,comma,item2,bracketcloses,then ;
if you ignored anyone of those requirements,you end up confusiong the computer.
please spare it.

For example,
Cross
CROSS- crossover check Trading system toolbox

please Learn to read amibroker help topics
read just once,but read it well,keep a notes.
Quality=doing a thing right in the first time.
When you go to market,would you buy rotten tomatoes?
the first item goes above second item,the sequence is set like that.


SYNTAX Cross( ARRAY1, ARRAY2 )
RETURNS ARRAY
FUNCTION Gives a "1" or true on the day that ARRAY1 crosses above ARRAY2. Otherwise the result is "0".

scenario change
To find out when ARRAY1 crosses below ARRAY2,
use the formula cross(ARRAY2, ARRAY1)
to check when closing price crosses above EMA(CLOSE,9),
USE BELOW THING
EXAMPLE

cross( close, ema(close,9) )

iF YOU LIKE TO combine multiple buy signals, just use
Buy = BUY1+BUY2+BUY3;
BUY1=CONDITION FOR MACD,BUY2=CONDITION FOR RSI ETC

hope that helps
rvlv
 
#3
thanks for the message, well i looked into it and now i have another problem.

When I use the following Afl code i get a good report back:
Cond1=Cross( MACD ( 12 , 26 ) , Signal ( 12, 26, 9 ) );
Cond2=Cross( StochK ( 15 , 3 ) , StochD ( 15, 3, 3 ) );
Cond3=Cross( Signal ( 12, 26, 9 ) , MACD ( 12, 25 ));
Cond4=Cross( StochD ( 15, 3, 3 ) , StochK ( 15 , 3 ) );
Buy=Cond1+Cond2;
Sell=Cond3+Cond4;
Filter=Buy;
AddColumn( Close, "Close", 1.2 );
AddColumn( MACD ( 12, 26 ) , "MACD", 1.2 );
AddColumn( Signal ( 12, 26, 9 ) , "Signal", 1.2 );
AddColumn( StochK ( 15 , 3 ) , "K%", 1.3 );
AddColumn( StochD ( 15 , 3 , 3 ) , "D%", 1.3 );

However when I add a few requirements that e.g. the stochK is below 80 I get results which include also stocks with StochK higher than 80! How come? I just asked the formula not to include these stocks but it is doing it anyway...??

Here's the formula i use:
Cond1=Cross( MACD ( 12 , 26 ) , Signal ( 12, 26, 9 ) );
Cond2=Cross( StochK ( 15 , 3 ) , StochD ( 15, 3, 3 ) );
Cond3=StochK ( 15 , 3 ) < 80;
Cond4=StochD ( 15 , 3 , 3 ) < 80;
Cond5=Cross( Signal ( 12, 26, 9 ) , MACD ( 12, 25 ));
Cond6=Cross( StochD ( 15, 3, 3 ) , StochK ( 15 , 3 ) );
Buy=Cond1+Cond2+Cond3+Cond4;
Sell=Cond5+Cond6;
Filter=Buy;
Filter=Volume > 1.3 * EMA( Volume, 40 );
Filter=Close > 1;
AddColumn( Close, "Close", 1.2 );
AddColumn( MACD ( 12, 26 ) , "MACD", 1.2 );
AddColumn( Signal ( 12, 26, 9 ) , "Signal", 1.2 );
AddColumn( StochK ( 15 , 3 ) , "K%", 1.3 );
AddColumn( StochD ( 15 , 3 , 3 ) , "D%", 1.3 );

I tried to find a way around it by inserting the stochK and stochD < 80 requirement as filters instead of buy conditions. Here's what I came up with:

Cond1=Cross( MACD ( 12 , 26 ) , Signal ( 12, 26, 9 ) );
Cond2=Cross( StochK ( 15 , 3 ) , StochD ( 15, 3, 3 ) );
Cond3=Cross( Signal ( 12, 26, 9 ) , MACD ( 12, 25 ));
Cond4=Cross( StochD ( 15, 3, 3 ) , StochK ( 15 , 3 ) );
Buy=Cond1+Cond2;
Sell=Cond3+Cond4;
Filter=Volume > 1.3 * EMA( Volume, 40 );
Filter=Close > 1;
Filter=StochK ( 15, 3 ) < 80;
Filter=StochD ( 15, 3, 3 ) < 80;
AddColumn( Close, "Close", 1.2 );
AddColumn( MACD ( 12, 26 ) , "MACD", 1.2 );
AddColumn( Signal ( 12, 26, 9 ) , "Signal", 1.2 );
AddColumn( StochK ( 15 , 3 ) , "K%", 1.3 );
AddColumn( StochD ( 15 , 3 , 3 ) , "D%", 1.3 );

But still the same problem, it returns the same results...

Can someone explain to me why that is? What am I doing wrong?? There's no syntax errors because I check them before exporting...
 

asnavale

Well-Known Member
#5
thanks for the message, well i looked into it and now i have another problem.

When I use the following Afl code i get a good report back:
Cond1=Cross( MACD ( 12 , 26 ) , Signal ( 12, 26, 9 ) );
Cond2=Cross( StochK ( 15 , 3 ) , StochD ( 15, 3, 3 ) );
Cond3=Cross( Signal ( 12, 26, 9 ) , MACD ( 12, 25 ));
Cond4=Cross( StochD ( 15, 3, 3 ) , StochK ( 15 , 3 ) );
Buy=Cond1+Cond2;
Sell=Cond3+Cond4;
Filter=Buy;

....

....

.....

I tried to find a way around it by inserting the stochK and stochD < 80 requirement as filters instead of buy conditions. Here's what I came up with:

Cond1=Cross( MACD ( 12 , 26 ) , Signal ( 12, 26, 9 ) );
Cond2=Cross( StochK ( 15 , 3 ) , StochD ( 15, 3, 3 ) );
Cond3=Cross( Signal ( 12, 26, 9 ) , MACD ( 12, 25 ));
Cond4=Cross( StochD ( 15, 3, 3 ) , StochK ( 15 , 3 ) );
Buy=Cond1+Cond2;
Sell=Cond3+Cond4;
Filter=Volume > 1.3 * EMA( Volume, 40 );
Filter=Close > 1;
Filter=StochK ( 15, 3 ) < 80;
Filter=StochD ( 15, 3, 3 ) < 80;
AddColumn( Close, "Close", 1.2 );
AddColumn( MACD ( 12, 26 ) , "MACD", 1.2 );
AddColumn( Signal ( 12, 26, 9 ) , "Signal", 1.2 );
AddColumn( StochK ( 15 , 3 ) , "K%", 1.3 );
AddColumn( StochD ( 15 , 3 , 3 ) , "D%", 1.3 );

But still the same problem, it returns the same results...

Can someone explain to me why that is? What am I doing wrong?? There's no syntax errors because I check them before exporting...
Hi Deepblue,

Just remember one thing while writing the AFL.

IF YOU WANT MORE THAN ONE CONDITIONS TO BE SATISFIED SIMULTANEOUSLY FOR GETTING A SIGNAL USE THE LOGICAL OPERATOR AND. IF YOU WANT ONE OR SOME OF THE CONDITIONS TO BE SATISFIED
USE THE LOGICAL OPERATOR OR.

In your case above, Write each condition as Cond1, Cond2, etc. and finally combine them for a BUY or SELL condition as:

BUY = Cond1 AND Cond2 AND Cond3 AND .... AND CondN;

Therefore, combine your Stoch condition also with AND:

BUY = Cond1 AND Cond2 AND StochK < 80 AND StochD < 80;

You can write similar code for SELL.


-Anant
 

Similar threads