Advanced MACD Crossover AFL

Today, you’ll learn how to code MACD crossover AFL in Amibroker to improve your trading.


  • What is the MACD indicator?
  • Simple Crossover
  • Above or below the Zero Line
  • Optimize the Settings

Many traders use MACD as a trend-following or momentum indicator to find high probability trades.

In this MACD crossover AFL, I will show you how to code it in Amibroker from the simplest code to a more complex one to show the features of Amibroker.

I have shared the AFL codes below to illustrate how to use it.

Later on, you’ll learn how to optimize your MACD settings to use the best settings.

Professional traders do that daily or weekly to improve their trading system.

Of course, you have to test it with a sample and out of sample data to verify your optimization.

In this MACD buy sell signal AFL, you can backtest it in Amibroker to see if it is still reliable.

You need to have different datasets to avoid curve fitting.

Well, you might have a trading system that follows the price of the stock too near to give wrong trading signals.

In this Amibroker MACD crossover AFL, you can have the right trading signals to improve your win rate.

Okay, in this post, I will simply teach you how to code this AFL to show different ways you can use Amibroker. This tutorial is not about optimization or backtesting, but I will touch on it a little bit to introduce to you the concept.

Let’s first talk about a simple crossover and then move to more in-depth tutorials to add other indicators to improve our probability.

Sometimes, you need to have other confirmation to increase our confidence in trading.

I can’t teach you everything about this AFL, yet you can read below to learn more about the MACD indicator.

You can modify everything I teach here to improve it.

When I started stock trading, I was buying and selling stocks by looking at the chart manually, but now, I use Amibroker to improve my trading.

What is the MACD indicator?

MACD stands for “Moving Average Convergence Divergence.” The default MACD indicator is simply the difference between the 12-period EMA and 26-period EMA. (Source)

EMA means “Exponential Moving Average.” The 9-day EMA of MACD is the signal line.

Most traders buy when the MACD crosses above the signal line. However, they use different techniques to reduce the lagging signal of the moving average.

Of course, you can use the zero line to determine the trend. For example, if the MACD crosses over from positive to negative, most traders view it as a bearish trend. (Source)

Okay, you can also say it is bullish in reverse to analyze the trend.

Well, I use the indicator for my divergence trading to find possible reversals. For example, if the price has a lower low and the MACD indicator has higher highs, you’ll know that there is a high chance that the price will move higher.

To avoid false signals, some traders buy if MACD crosses over the signal line and MACD is below the zero-line. Also, do not use the indicator in a sideways market in stock trading to prevent whipsaws.

MACD is a momentum indicator, and it can not give accurate signals in consolidating prices.

I don’t really use the crossover in the stock market because moving averages are lagging indicators.

The cross will always happen after the move because there are delays in the signals.

Well, I have been profitable with anticipating my trades. I have written a post about this indicator to show my readers how to use it effectively.

You can search this website about it to use MACD in breakouts.

Well, I recommend that you read the post to learn an excellent strategy.

However, today we will not talk about early entries to show you this MACD crossover AFL.

Okay, let’s talk about the code below to start right away.

Simple Crossover

A MACD crossover is a MACD indicator signal that occurs when MACD crosses the zero(0) line. It is a bull signal if a crossover is below the line or a bearish signal if the crossover is coming from above. The signal is not an immediate signal but a warning that a reversal or change in momentum is about to come.

You can use this code in Amibroker to have a simple buy and sell signals.

Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

Plot( Close, "Price", colorBlack, styleCandle );

PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );

GraphXSpace = 5;

You will have many losses if you use this simple code because the second and third signals are all wrong.

The reason is I did not include trend signals in the AFL code to see if there is a trend.

If you remember what I told you above, MACD is a trend indicator, and you can’t use it in a consolidating market.

Well, I have proven my point to persuade you to avoid more losses.

I hope you learn something today for me to help you.

In the early days of trading, I lost a lot of money in the stock market because I did not know how to use the MACD indicator.

Okay, I do not want that to happen to you to save from the misinformation out there.

You can see that there are too many signals in the chart, and there are wrong signals.

Well, to increase the probability of your trades, you should confirm it with other indicators.

Regardless of the trend, The MACD crossover AFL above will trigger a buy and sell signal whenever both MACD and signal meet.

You have to use confluence in stock trading to reduce your losses.

In the next tutorial below, I show you how to use the zero-line to improve your success rate.

Above or below the Zero Line

Buy = Cross( MACD(), Signal() ) AND MACD()>0 AND Signal()>0;
Sell = Cross( Signal(), MACD() ) AND MACD()<0 AND Signal()<0;

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

Plot( Close, "Price", colorBlack, styleCandle );

PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );

GraphXSpace = 5;

Now, we have more accurate signals in the chart to short the market correctly.

Well, this is obviously a strong bearish stock in the market, and there is no buy signal because of its direction.

If you look closely, the triggers are below zero of the MACD indicator to show a strong downtrend.

Well, you can see in the stock chart to believe me. However,  these tools are not 100% accurate because the market can trick you more often.

In my experience, I can’t predict the win rate of this MACD indicator, but I can reduce the risk by using an excellent risk-reward ratio.

You can search our website in the search bar to learn more.

I use 1:2 risk-reward ratio to increase my capital even if the win rate is low.

Okay, let’s go back to our topic to get going.

When you add another confirmation, you increase the probability to increase your win rate.

However, you should limit your indicators up to three to reduce the complexity.

You’ll be confused when your charts are cluttered.

Some times, an unorganized screen can’t help you decide the direction of the price to trade correctly.

When I started trading, I was using more than five indicators daily to get different signals.

To make the story short, I did not make any money because I became more confused.

I had a hard time making my decisions, and I made a lot of bad trades.

Well, I use less than three indicators in trading today to simplify my stock charts.

Wrap Up

The MACD crossover AFL can create many bad signals when you do not add a confirmation signal to increase your losses.

Use this AFL in a trending stock in the market to avoid several losses. You should modify these codes in the Amibroker editor to see if you can improve it.

Also, I hope you learned something today in this post to improve your trading decisions.

Amibroker can help you daily to find good trading set-ups. If you want to become a professional trader, you have to invest in this tool.

If you like this post, you can share it in your favorite social networks to help other traders.

Related Articles