Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Last active Sep 14, 2021
Embed
What would you like to do?
import matplotlib.pyplot as plt
from yfapi import YahooFinanceAPI
import datetime
from simulation import GBMMonteCarlo
import pandas as pd
ticker = 'aapl'
api = YahooFinanceAPI()
end = datetime.datetime.today()
start = datetime.datetime(end.year - 2, end.month, end.day)
spdata = api.get_ticker_data(ticker, start, end)
gbmmc = GBMMonteCarlo()
paths = gbmmc.run([ticker], 50, '2023-9-14', data_months=12, plot_results=True)
sim_prices = pd.DataFrame(paths[ticker][0], columns=["sim_open"])
sim_prices["changes"] = sim_prices.sim_open.pct_change()
spdata["changes"] = spdata.Open.pct_change()
max, min = spdata.changes.max()*1.1, spdata.changes.min()*1.1
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.bar([i for i in range(len(spdata) - 1)], spdata["changes"][1:], color="darkgreen")
ax1.set_ylim(min, max)
ax1.set_title("Actual {} Movements (Daily % Change)".format(ticker.upper()))
ax2.bar([i for i in range(len(sim_prices) - 1)], sim_prices["changes"][1:], color="darkred")
ax2.set_title("Simulated {} Movements (Daily % Change)".format(ticker.upper()))
ax2.set_ylim(min, max)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment