import yfinance as yf
import datetime as dt

# Download historical data
ticker = "MSFT"
start = dt.datetime.today() - dt.timedelta(1825)
end = dt.datetime.today()
ohlcv = yf.download(ticker, start, end)

# Calculate MA
def MACD(DF, a, b, c):
    df = ohlcv.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
    
df = MACD(ohlcv, 12, 26, 9)

df.iloc[:, [4, 8, 9]].plot()
