MACD with Python

This is not trading advice.

MACD indicator stands for Moving Average Convergence Divergence. As the name suggests, indicator is evaluating two moving averages and the relation between them.

Definiition of MACD from investopedia https://www.investopedia.com/terms/m/macd.asp:

$$MACD = FastEMA - SlowEMA$$$$SignalLine = 9 period EMA from MACD$$

FastEMA = 12 period EMA from closing price
SlowEMA = 26 period EMA from closing price

MACD is using Exponencially weighted Moving Averages (EMA). Typical indicator setup is MACD(12,26,9) as shown above.

Bullish bias - when both lines are above zero.
Bearish bias - when both lines are below zero.

The crossover might signify the reversal of a trend.

Buy signal is generated when MACD line crosses SignalLine from below. Buy signal is considered to be stronger when crossover occurs below zero line. Buying with bearish bias expecting trend reversal.

Sell signal is generated when MACD line crosses SignalLine from above. Sell signal is considered to be stronger when crossover occurs above zero line. Selling with bullish bias expecting trend reversal.

Also the further the crossover occurs from zero line, the stronger the signal.

In addition to this we have also the histogram (difference between MACD line and SignalLine) that serves as an early warning for crossovers. When bars are getting shorter, it signals that crossover signal might come soon.

We can also use MACD to spot divergences between price action and the indicator, divergences are more difficult to detect programatically though.

Standard signals

Below are shown standard MACD signals - buy when MACD line crosses above signal line and sell signals when MACD crosses below signal line.

Modified buy/sell signalling - long only below zero line, short only above zero line

when MACD line crosses above signal line - we consider it as a buy signal. But we will impose further conditions. We will buy only when bulish MACD crossover occurs below zero line.

Further EXPERIMENTAL conditions:

Going long when we are in short term bullish uptrend - price is above short term EMA. Observation, if EMA is 9 or higher, we might lose important signals, so we shorten EMA to like 5 or 7. We might also experiment with extra override for crossovers that are too high and too low above/below zero line.

Sell setup is analogical.

By imposing further conditions we are getting less (and looks like more reliable) signals that might work well for long term investing/swing trading.

Sources:

https://github.com/Crypto-toolbox/pandas-technical-indicators
https://www.quantopian.com/posts/technical-analysis-indicators-without-talib-code
https://www.investopedia.com/terms/m/macd.asp

https://www.youtube.com/watch?v=qyU9IM3M_Kw&ab_channel=EXFINSISExpertFinancialAnalysis
https://www.youtube.com/watch?v=E3KP1WyLITY&ab_channel=Trading212
https://www.youtube.com/watch?v=kz_NJERCgm8&t=493s&ab_channel=ComputerScience

https://www.youtube.com/watch?v=RzGIdibpo8Q&ab_channel=TRADINGRUSH
https://www.youtube.com/watch?v=nmffSjdZbWQ&ab_channel=TRADINGRUSH

https://stackoverflow.com/questions/31810461/python-matplotlib-vertically-aligned-plots-in-matplotlib

https://www.investopedia.com/terms/m/macd.asp
https://www.youtube.com/watch?v=n72tM2HLv34&ab_channel=TheSecretMindset