In [ ]:
import sys

sys.path.insert(0, ".")
sys.path.append("../")  # import py files from parent folders/..*

MACD¶

In [ ]:
# Importing Required Libraries
import yfinance as yf
import cufflinks as cf

cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)


# Define the Ticker Symbol
ticker_symbol = "AAPL"

# Get the data on this ticker
ticker_data = yf.Ticker(ticker_symbol)

# Get the historical prices for the specified period
ticker_df = ticker_data.history(period="1d", start="2022-1-1", end="2023-6-15")

# Calculate MACD
# Short term EMA
short_EMA = ticker_df.Close.ewm(span=12, adjust=False).mean()

# Long term EMA
long_EMA = ticker_df.Close.ewm(span=26, adjust=False).mean()

# Calculate MACD line
MACD = short_EMA - long_EMA

# Calculate Signal Line
signal = MACD.ewm(span=9, adjust=False).mean()

# Add MACD and signal line to the data frame
ticker_df["MACD"] = MACD
ticker_df["Signal Line"] = signal

# Plot the chart
ticker_df.iplot(
    title="Moving Averages of " + ticker_symbol,
    xTitle="Date",
    yTitle="Price",
    theme="pearl",
)