#
#
# MACD: Moving Average Convergence Divergence

# It is a trend following momentum indicator which is calculated 
# by taking the difference of two moving averages of an asset price (typically 12 period MA and 26 period MA).   
#
#
#
import pandas_datareader.data as pdr
import datetime

# Download historical data for required stocks
ticker = "TSLA"
ohlcv = pdr.get_data_yahoo(ticker, datetime.date.today() - datetime.timedelta(1825),datetime.date.today())


def MACD(DF, a, b, c):

    df = DF.copy()
    df["MA_Fast"] = df['Adj Close'].ewm(span=a, min_periods=a).mean()
    df["MA_Slow"] = df['Adj Close'].ewm(span=b, min_periods=b).mean()
    df["MACD"] = df['MA_Fast'] - df['MA_Slow']
    df["Signal"] = df['MACD'].ewm(span=c, min_periods=c).mean()
    df.dropna(inplace=True)
    return df



macd_df = MACD(ohlcv, 12, 26, 9)[['MACD', 'MA_Fast', 'MA_Slow']]
macd_df.plot()