# MACD & RSI - AFL code required Help please!

#### kesk

##### Member
I am trying daytrading and loosing an awful lot of money. Using Amibroker for intraday chart and Yahoo data feed. I am doing only Mini Nifty using nifty cash values. That could be one of the reason.

What i have read about MACD and RSI is like this: When the signal crossover happens in MACD, you either buy or sell. Also to note down the RSI is either in 70 or 30 range. But what i have observed is, when MACD curve is lower, before the signal crossover, then it maximises the profit. When the signal is touching the line, probably we stand to gain lower points.

Can any one give me an AFL code which generates a signal when the following 2 conditions are met:

1. MACD is in its peak and the RSI is at 80
2. MACD is in its most trough and the RSI is at 20.

If a buy/sell signal is given when these 2 conditions are met, it might generate moderate revenues without loosing moeny.

I tried to write the codes myself, but always have failed. I would request anyone who has experience in writing AFL code, to post it here. I would be thankful for their generosity. Maybe it would be helpful for others too.

kesk

#### kesk

##### Member
I had written a formula approximating the functions that i require: And here is the result of the formula.

Here is the code:
----------------------------------------------------------------------
/* Condition Sell:
1.MACD is in +ve territory.
2. RSI is in 80
*/

Cond1=MACD()<0;
cond2=RSI(80);
Cond3=C<Ref(L,-1);

Sell = Cond1 AND Cond2 AND Cond3;

1.MACD is in -ve territory.
2. RSI is in 20
*/

Cond4=MACD()>0;
cond5=RSI(20);
Cond6=C>Ref(H,-1);

Buy = Cond4 AND Cond5 AND Cond6;

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

PlotShapes(shapeDownArrow * Sell,colorRed,0,H);
---------------------------------------------------

Straight away there is a problem in the code:
1. The RSI is set at 80 and 20. But it should be between a range like 0<20 and 80<100...i dont know how to set this.
2. The MACD value is checked whether it is +ve or -ve only...not the highest and lowest.

Any idea how to implement these two checks.

I would welcome any kind of replies.

Thanks.

kesk

#### rrdesai_goa

##### Member
Hi

Have u tried the Zero-lag MACD which gives the signal a fair bit sooner so that u can get in the trade earlier and vice versa??

rgrds

#### shivipahwa

##### New Member
Seniors,
Can someone help me in developing sound alert when a buy or sell signal is generated in this indicator and it filters the name of the stock in which that particular buy or sell signal has come. I would be really thankfull . For any clarifications my email id is [email protected]
_SECTION_BEGIN("RSI Divergence");
//------------------------------------------------------------------------------
//
// Formula Name: RSI Divergence
// E-mail:
// Origin:
// Keywords:
// Level: medium
// Flags: indicator
// Formula URL: http://www.amibroker.com/library/formula.php?id=603
// Details URL: http://www.amibroker.com/library/detail.php?id=603
//
//------------------------------------------------------------------------------
//
// + scanner
//
//------------------------------------------------------------------------------

/*---------------------------------------------------
RSI Divergence
Aron Pipa, March, 20 , 2006
--------------------------------------------------------*/

GraphXSpace=7;
n=Param("% Reverse ",20,0,100,1);

Var = Zig(RSI(), n);
t= Trough(RSI(), n, 1);
p= Peak(RSI(), n, 1);
x[0] =Var[0];
price[0] = C[0];
j=0;

// bearish divergence
for ( i=0; i<BarCount; i++)
{
if(Var == p)
{

j++;
x[j] =Var;
price[j] =C;
if(x[j] <x[j-1] && price[j-1]< price[j])
Sell =1;
}
}

// bullish divergence
for ( i=0; i<BarCount; i++)
{
if(Var == t)
{
j++;
x[j] =Var;
price[j] =C;
if(x[j] >x[j-1] && price[j]<price[j-1])
}
}

Plot(Var, "", 39);
PlotShapes ( IIf(Sell, shapeSmallCircle, shapeNone), colorRed, 0 , Var,0);
PlotShapes( IIf(Buy, shapeSmallCircle, shapeNone), colorBrightGreen, 0, Var,0);

Title ="RSI Divergence" ;
_SECTION_END();

Regards

#### molthi

##### Member
i had written a formula approximating the functions that i require: And here is the result of the formula.

here is the code:
----------------------------------------------------------------------
/* condition sell:
1.macd is in +ve territory.
2. Rsi is in 80
*/

cond1=macd()<0;
cond2=rsi(80);
cond3=c<ref(l,-1);

sell = cond1 and cond2 and cond3;

1.macd is in -ve territory.
2. Rsi is in 20
*/

cond4=macd()>0;
cond5=rsi(20);
cond6=c>ref(h,-1);

buy = cond4 and cond5 and cond6;

plot( c, "close", paramcolor("color", colorblack ), stylenotitle | paramstyle("style") | getpricestyle() );

plotshapes(shapedownarrow * sell,colorred,0,h);
---------------------------------------------------

straight away there is a problem in the code:
1. The rsi is set at 80 and 20. But it should be between a range like 0<20 and 80<100...i dont know how to set this.
2. The macd value is checked whether it is +ve or -ve only...not the highest and lowest.

Any idea how to implement these two checks.

I would welcome any kind of replies.

Thanks.

Kesk

Replace the highlighted lines with.

RSI(14)>80;
RSI(14)<20;

##### Well-Known Member
I am trying daytrading and loosing an awful lot of money. Using Amibroker for intraday chart and Yahoo data feed. I am doing only Mini Nifty using nifty cash values. That could be one of the reason.

What i have read about MACD and RSI is like this: When the signal crossover happens in MACD, you either buy or sell. Also to note down the RSI is either in 70 or 30 range. But what i have observed is, when MACD curve is lower, before the signal crossover, then it maximises the profit. When the signal is touching the line, probably we stand to gain lower points.
MACD is basically plotting the difference between 2 ema. if u take macd(12,26), it means that u are looking at the gap between the 2 ema and using that to make a trading decision. EMA cross eachother and u'll notice macd crossed the zero line as well. To add to the confusion, you are adding RSI as a confirmation.

Lets look at the above chart. We have ema-26 plotted in blue and ema-12 plotted in red. The gap between the 2 lines is the macd (red line). You take a 9 average of this line plot that as the signal (blue).

What u can notice that both the ema are pointing up and they never crossed or gave u any visual indication that marker's trend has turned bearish. Reduction in the gap means the bulls are taking profit and fresh bulls are buying. Once the profit taking is over, prices will shoot up. But MACD gave u a sell signal 2 times and rsi confirmed it both times. Does it mean the indicators are not trust worthy... ?

Its how you read the indicator that dictates your profit. Above the 0 line, macd is bullish and you should bee looking only for long trades. Any sell signal u get is the exit signal for long. Any buy signal is a signal for you to add positions or take new ones. The angle of the macd show the momentum. A flat macd means market is flattening out. Any trade taken will have a higher probability of hitting your SL. when the macd is rising above the 0 line, thats ur signal to go long. The angle of the rise is the indication of the amount of money u should put to risk.

In a flat market, macd crossing the signal is a good signal for trading both ways. In a trending market, take only those signals that is trend following.

RSI is a very powerful tool. One can trade on rsi alone if u study it well...

#### pkgmtnl

##### Well-Known Member

can u put the syntax for Angle of MACD? Both way Buy side and Sell side.
In one post Asnavale ji has indicated for it, but on SELL side its not giving correct signal.

It was :-
V1= MACD(r1, r2);
V2 = Ref(MACD(r1, r2), -1);
PI = 22/7;

Cond13 = atan(V1-V2) > PI/6;

SELL
Cond14 = atan(V2 -V1) > PI/6;

Can u give ur syntax for Angle (Upwards and Downwards).

Thnx

Last edited:

#### asnavale

##### Well-Known Member

can u put the syntax for Angle of MACD? Both way Buy side and Sell side.
In one post Asnavale ji has indicated for it, but on SELL side its not giving correct signal.

It was :-
V1= MACD(r1, r2);
V2 = Ref(MACD(r1, r2), -1);
PI = 22/7;

Cond13 = atan(V1-V2) > PI/6;

SELL
Cond14 = atan(V2 -V1) > PI/6;

Can u give ur syntax for Angle (Upwards and Downwards).

Thnx
Hi pkg,

for Sell make the cond like this:

Cond14 = atan((V2 - V1) < -PI/6;

-Anant

#### swot9

##### Member
Dear Anant Ji,

Is it possible to get this correct AFL for backtest??

Regards,

Paresh