Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: MACD indicator display
Author/Uploader: Paul Moore - (email hidden)
Date/Time added: 2006-05-21 19:36:21
Origin:
Keywords: MACD
Level: medium
Flags: indicator

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

Sometimes, it can be difficult to view the MACD histogram bars when the MACD lines are very close together, causing the bars to be very short. This indicator automatically adjusts the scaling of the MACD histogram bars such that they occupy the maximum area in its indicator pane.

Formula:

PeriodFast = Param( "Fast EMA", 12, 2, 200, 1 );
PeriodSlow = Param( "Slow EMA", 26, 2, 200, 1 );
PeriodSignal = Param( "Signal EMA", 9, 2, 200, 1 );
MACDInd = MACD(PeriodFast, PeriodSlow );
SigInd = Signal(PeriodFast, PeriodSlow , PeriodSignal );
HistInd = MACDInd - SigInd ;
_N( macdStr = WriteVal( PeriodFast, 1.0 )+","+WriteVal( PeriodSlow , 1.0 ) );
_N( sigStr = macdStr + ","+WriteVal( PeriodSignal , 1.0 ) );

// Get displayed min and max value of MACD and MACD-H, to rescale it for better
visibility
// BarsDisplayed = BarsSince( Status("barvisible") AND NOT
Ref(Status("barvisible"),-1)  );
BarsDisplayed = IIf( IsEmpty(BarsSince( Status("barvisible") AND NOT
Ref(Status("barvisible"),-1) ) ),
                     BarIndex(),
                     BarsSince( Status("barvisible") AND NOT
Ref(Status("barvisible"),-1) ) );
scMACDMax = LastValue(HHV(Max(MACDInd, sigInd), BarsDisplayed ) );
scMACDMin = LastValue(-HHV(Max(-MACDInd, -sigInd), BarsDisplayed ) );
scaleMACD = Max( abs(scMACDMax), abs(scMACDMin) ); 

// Plot(BarsDisplayed, "bars", colorBlue );
// Plot( HHV(Max(MACDInd, sigInd), BarsDisplayed ), "max", colorGreen );
// Plot( -HHV(Max(-MACDInd, -sigInd), BarsDisplayed ), "min", colorRed );
// Plot( scMACDMax, "max", colorGreen );
// Plot( scMACDMin, "min", colorOrange );

scHistMax = LastValue(HHV(HistInd, BarsDisplayed ) );
scHistMin = LastValue(LLV(HistInd, BarsDisplayed ) );
scaleHist = Max( abs(scHistMax), abs(scHistMin) );

HistColour = colorBlue;
// HistColour = IIf( HistInd > Ref(HistInd,-1), colorBlue, colorRed ); 
Plot( HistInd, "", HistColour, styleHistogram  | styleThick | styleOwnScale , 
      -scaleHist * 1.2, scaleHist * 1.2 );
Plot( MACDInd, "", colorBrightGreen);
Plot( SigInd , "", colorRed);

Plot( scaleMACD * 1.2,"", colorRed , styleNoDraw );
Plot( -scaleMACD * 1.2 ,"", colorRed , styleNoDraw ); 
GraphXSpace = 0;

Comments:

crash
crashandburn59 [at] hotmail.com
2006-05-23 03:54:34
The following simpler code achieves what you set out to do plus it shows if the MACD Histogram is Rising or Falling.

Regards Crash.

r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

mm = MACD(r1, r2);
ms = Signal(r1, r2, r3);
mh = mm-ms;
mc = IIf(mh>Ref(mh,-1),IIf(mh > 0,27,41),IIf(mh > 0,43,32));

Plot(mm,StrFormat(_SECTION_NAME()+"(%g,%g)", r1 ,r2),12,4);
Plot(ms," Signal" + _PARAM_VALUES(),11);
Plot(mh,"MACDHistogram",mc,6|styleNoTitle|32768|4096);
Plot(mh,"MACDHistogram",mc,5|styleNoTitle|32768|4096);
Plot(mm,StrFormat(_SECTION_NAME()+"(%g,%g)", r1 ,r2),12,4);
Plot(ms," Signal" + _PARAM_VALUES(),11);
Plot(mh,"MACDHistogram",mc,6|styleNoTitle|32768|4096);
Plot(mh,"MACDHistogram",mc,5|styleNoTitle|32768|4096);
crash
crashandburn59 [at] hotmail.com
2006-05-23 04:00:39
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

mm = MACD(r1, r2);
ms = Signal(r1, r2, r3);
mh = mm-ms;
mc = IIf(mh>Ref(mh,-1),IIf(mh > 0,27,41),IIf(mh > 0,43,32));

Plot(mm,StrFormat(_SECTION_NAME()+"(%g,%g)", r1 ,r2),12,4);
Plot(ms," Signal" + _PARAM_VALUES(),11);
Plot(mh,"MACDHistogram",mc,6|styleNoTitle|32768|4096);
Plot(mh,"MACDHistogram",mc,5|styleNoTitle|32768|4096);
Paul Moore
paul.moore [at] pandora.be
2006-05-26 08:38:10
Thanks Crash.

I had also initially tried that. It doesn't always work, especially with stock that don't have a long price history. What happens in this case is that the MACD-lines 0-level isn't synchronised with the MACD-H 0-level. Try it with GOOG.

This why I use either the number of bars displayed or the total number of bars in the price history when calculating the scaling factor.

Maybe you can solve it using a simpler method...
Willow
willow2 [at] netspace.net.au
2008-10-14 00:51:01
Good stuff Paul, I've been using the histogram seperate to the macd so I could see it better. Now I can use it both to-gether again. You have also made it easy to change the time-span for I use the histogram in two different time-spans. Much appreciated.
Willow
willow2 [at] netspace.net.au
2008-10-14 00:57:22
Good stuff Paul, I've been using the histogram seperate to the macd so I could see it better. Now I can use it both to-gether again. You have also made it easy to change the time-span for I use the histogram in two different time-spans. Much appreciated.
Paul Bailey
pmbailey4 [at] cox.net
2008-11-27 13:43:43
Paul, I have tried using your formula with no success. I get a syntax error at line Lin 13, Col. 1, Error 32 and have tried to solve per the suggestion with no results. I realize this has been around for a while, however I am a new user am am trying to learn the AFL language and am not haveing much suggest.

Thanks in advance


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM