Three Colors MACD Indicator

I hv been trying some of AFL code for MACD and made some modification by read it online and others made indicator.

MACD part 1 - Two Colors

MACD1

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

mm = MACD(r1, r2);
ms = Signal(r1,r2,r3);
Hist = mm-ms ;

MACUP = Hist > Ref(Hist,-1);
MACDN = Hist < Ref(Hist,-1);

BarColor = IIf(MACUP,colorGreen,IIf(MACDN,colorRed,colorBlue));

Plot( Hist, "MACD Histogram", ParamColor("Histogram color", colorBlack ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
Plot( Hist, "MACD Histogram", BarColor , styleHistogram | styleThick);
Plot( Hist, "MACD Histogram", BarColor , styleHistogram | styleThick);
Plot(0, "0", colorBlueGrey );

________________________________________________________________
MACD part 2 - Three Colors

MACD2

r1 = Param( "Fast avg", 7, 2, 200, 1 );
r2 = Param( "Slow avg", 14, 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,29,16),IIf(mh > 0,16,32));

Plot(mh,"MACDHistogram",mc,6|styleHistogram|4|32768|4096);

_________________________________________________________________
And now come down to my question, i would like to make others model.

MACD%20finish

What is AFL style code used by MACD above??

I try using stylecandle but there is wick and only 1 side not Up n down like MACD should be.

Or maybe stylestaircase combine with stylecloud?

Any one knows? or can help?

Thank you

Use code tags, its a mandatory forum rule.

Just increase the width of the plot

Plot(mh, "MACDHistogram", mc, 6|styleHistogram|4|32768|4096, Null, Null, 0, 0, 5);

Default 1 is now 5, last argument.

image

1 Like

Another thing is you need to check the Flags you are passing,

6 is wrong, and it isn't a true 2^n that bit flags are meant to be.

styleThick may also not be required and you can adjust width to 6 or 7

Use code tags, its a mandatory forum rule.

Thanks travick for the information and the reminder. Also thanks for the upgrade code, it's really what i want the MACD would be.

Plot(mh, "MACDHistogram", mc, 6|styleHistogram|4|32768|4096, Null, Null, 0, 0, 5);
//Default 1 is now 5, last argument.

So its mean line width that change from 1 to 5, and what i curious also about your

", Null, Null, 0, 0,"

is it to arrange the positioning on chart? right or wrong?

Or may be i overlook something on Plot ()?

That is the function definition of Plot,

Plot( array, name, color/barcolor, style = styleLine, minvalue = {empty}, maxvalue = {empty}, XShift = 0, Zorder = 0, width = 1 )

We're leaving default values for arguments that we aren't altering. minvalue, maxvalue etc
https://www.amibroker.com/guide/afl/plot.html

1 Like

Yeah agree, with the line width "thick" option wouldn't be necessary anymore. It's really help me learning about AFL coding.

I'm still newbie and learn by looking on others and try to understanding the function, plot and logic behind it.

Thank you.

1 Like

Wow Thank your for the link bro.

Using this code:

Plot(mh, "MACDHistogram", mc, 7|styleHistogram|32768|4096, Null, Null, 0, 0, 7);

Working like a charm. Most Appreciate it.

Capture

@miko, when plotting histograms, I frequently use a negative value for the "width" parameter as per the documentation:

width - defines pixel or percent width of given plot. The default is 1 pixel. Positive values specify PIXEL width, negative values specify width in percent of current bar width. So for example -20 will give you dynamic width that is 20% of the bar width. Example: Plot( C, "Close", colorBlack, styleBar, Null, Null, 0, 1, -20 /* line width as percent of bar */ )

IMO, It will look better when zooming the charts.

Moreover, I suggest to get rid of the hardcoded values and replace them with the predefined constants that are a lot easier to read.

What styles are applied using the following line? (What color is 32?)

Plot(C, "Close", 32, 2 | 4096, Null, Null, 0, 0, -80);

This seems to be a lot more easier to understand:

Plot( C, "Close", colorRed, styleHistogram | styleNoLabel, Null, Null, 0, 0, -80 );

Styles (and colors) constants are documented here.

4 Likes

@beppe Thanks for your post, I was actually getting to that, but was playing around with the resolution before coming back here :slight_smile:

1 Like

The purpose modifying model is non other then make it easy to see rather then always zooming or even using magnifier software :sweat_smile:

@beppe For get rid of the hardcoded values, I'm agree with you 100% for using predefined constants.

Like i said before i read it online and looking at others made indicators. Sadly the model base indicator that i used using that hardcoded values. it headache time to time for newbie like me. But it actually helping me a lot in the end, made me easier to understand what's logic behind their coding. :star_struck: Coz they made me read a lot. :rofl:

So i made some change on coding thank to @travick and now complete it by @beppe, I appreciate it.

Plot(mh, "MACDHistogram", mc, 7|styleHistogram | styleNoLabel, Null, Null, 0, 0,-80);

Result:
Capture

@miko2 why did you leave the hardcoded 7?

Plot(mh, "MACDHistogram", mc, 7|styleHistogram | styleNoLabel, Null, Null, 0, 0,-80);

7 is the result of  1 | 2 | 4  and is as such it means  styleLine | styleHistogram | styleThick  so it is like you specified styleHistogram twice.

In this case, since you are using the binary-OR ( | ) operator, the repeated styleHistogram (a 2 flag) is harmless.

At the same time, the 1 flag (styleLine) is ignored because, as indicated in the document linked, some combinations do not make sense:

Not all flag combinations make sense, for example (64+1) (candlestick + line) will result in candlestick chart (style=64)

Also, the 4 flag corresponding styleThick is practically redundant; you are using an explicit value for the width.

Hardcoded numbers will quickly lead to confusing expressions: using only the constants will significantly improve readability and reduce potential mistakes.

2 Likes

@beppe After i read your replay and try to delete those "7", i just realized that my chart changing. It is indeed the MACD double on style.

Yes hardcoded numbers really made me confuse.