Implementing MACD in Python
Implementing MACD in Python
Towards Data
Science
Sharing concepts,
Implementing MACD in Python
ideas, and codes. MACD is a popularly used technical indicator in trading stocks,
Follow currencies, cryptocurrencies, etc.
Luke Posey Follow
Mar 30 · 5 min read
255
MACD is popularly used in analyzing charts for stocks, currencies, crypto, and other assets…Credit: Unsplash
Basics of MACD
MACD is used and discussed in many different trading circles. Moving Average
Convergence Divergence (MACD) is a trend following indicator. MACD can be
calculated very simply by subtracting the 26 period EMA from the 12 period
EMA. We previously discussed EMAs in our article here. MACD can be used
and interpreted in a handful of different ways to give the trader potential
value and insight into their trading decisions.
Useful Strategies
In a bullish crossover, just like in Moving Averages, a buy signal occurs when
MACD crosses above the signal line. A bearish signal occurs when MACD
crosses below the signal line. If a crossover occurs with a high sloping MACD,
this can be a sign of an overbought or oversold condition, depending on if the
crossover is bullish or bearish respectively. MACD is a great indicator for
understanding if movement in the price is strong or weak. A weak movement
is likely to correct and a strong movement is likely to continue.
Python Implementation
We start as we always do by picking a stock and gathering the data. As usual,
we’ll do our analysis on AMD and use the IEX API to grab the data. IEX has
always been a highly reliable source of data for me, but you can use Quandl or
whatever other source of data you prefer.
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
import pyEX as p
ticker = 'AMD'
timeframe = '6m'
df = p.chartDF(ticker, timeframe)
df = df[['close']]
df.reset_index(level=0, inplace=True)
df.columns=['ds','y']
macd = exp1-exp2
exp3 = macd.ewm(span=9, adjust=False).mean()
This allows us to plot the MACD vs the signal line. See if you can spot the
bullish and bearish crossovers!
Check the graph below. Were you correct? Remember, a bullish crossover
happens when the MACD crosses above the signal line and a bearish crossover
happens when the MACD crosses below the signal line.
The above example was a simple way to use MACD to study crossovers. Next,
let’s study strength and examine overbought or oversold conditions.
macd = exp1-exp2
Blue line represents the AMD stock price, orange line represents the MACD
We can blow this MACD line up a bit by plotting it separate from the stock
price and see the steep slopes more clearly.
Let’s recall our discussion of overbought and oversold from earlier. We can see
the MACD stays pretty flat over time. But there are certain times where the
MACD curve is steeper than others. These are instances of overbought or
oversold conditions. We represent our oversold conditions with green circles
and overbought with red circles. You can see that soon after the MACD shows
an overbought or oversold condition the momentum slowed and the stock
price reacted accordingly.
MACD - Wikipedia
The average series is also a derivative estimate, with an additional low-pass
lter in tandem for further smoothing…
en.wikipedia.org
www.investopedia.com
255 claps
More from Towards Data Science More from Towards Data Science More from Towards Data Science
10 Simple hacks to speed up your Why you’re not a job-ready data What 70% of Data Science Learners
Data Analysis in Python scientist (yet) Do Wrong
Responses
Write a response…