Divergence Trading: Moving Average Convergence-Divergence Strategy
Introduction
The Moving Average Crossover Strategy was covered in a previous article, which served as an intro to some of the different strategies used in Quantitative Analysis. In this blog post, we will be looking at a slightly more complex strategy in Python named the Moving Average Convergence Divergence strategy.
Getting Started
Install the required Libraries Before diving in, make sure to install the necessary libraries. We’ll be using pandas
for data manipulation, yfinance
for getting the historical stock data and matplotlib
for the data visualization
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np
Fetching Historical Stock Data
Define a function to get historical stock data using the yfinance
library
def get_stock_data(symbol, start_date, end_date):
stock_data = yf.download(symbol, start=start_date, end=end_date)
return stock_data
Moving Average Convergence Divergence Strategy
This strategy creates buy/sell signals based on the convergence and divergence of two Exponential Moving Averages (EMAs).