Last active
May 24, 2024 13:40
-
-
Save scubamut/deb34b9541decf9894e5532652cf8677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fintools import endpoints | |
# end_points prices (in this case, monthly) | |
end_points = endpoints(period='M', trading_days=prices_d.index) | |
prices_m = prices_d.loc[end_points] | |
prices_m[:2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from datetime import datetime, date | |
import yfinance as yf | |
assets = ['AAPL', # Apple | |
'KO', # Coca-Cola | |
'DIS', # Disney | |
'XOM', # Exxon Mobil | |
'JPM', # JPMorgan Chase | |
'MCD', # McDonald's | |
'WMT'] # Walmart | |
# download historical data from yfinance | |
hist_data = {} | |
for asset in assets: | |
data = yf.download(asset, start='2015-01-01', end='2017-12-31') | |
hist_data[asset] = data['Adj Close'] | |
hist_data = pd.concat(hist_data, axis=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# S&P500 Historical Data | |
import yfinance as yf | |
from datetime import datetime | |
import pytz | |
import pandas as pd | |
start = str(datetime(1900, 1, 1, 0, 0, 0, 0, pytz.utc).date()) | |
end = str(datetime.today().date()) | |
df = yf.download( ['^GSPC']) | |
df_d = df['Close'].to_frame() | |
df_daily = df_d.copy() | |
df_10 = df_d.resample('10B').first().copy() | |
df_21 = df_d.resample('21B').first().copy() | |
df_m = df_d.resample('M').last().copy() | |
df_sm = df_d.resample('SM').last().copy() | |
df_ms = df_d.resample('M').first().copy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#***************************************************************** | |
# Load historical data | |
#****************************************************************** | |
import pandas as pd | |
from datetime import datetime | |
import pytz | |
import yfinance as yf | |
cash_proxy = 'CASHX' | |
risk_free = 'BIL' | |
rs_lookback = 12 | |
risk_lookback = 12 | |
n_top = 7 | |
# these are the ETFs used by Systematic Investor (2005 - today) | |
symbols = ['SPY','EFA','EWJ','EEM','IYR','RWX','IEF','TLT','DBC','GLD'] | |
# get data | |
tickers = symbols.copy() | |
if cash_proxy != 'CASHX' : | |
tickers = list(set(tickers + [cash_proxy])) | |
if isinstance(risk_free, str) : | |
tickers = list(set(tickers + [risk_free])) | |
data_path = '/home/scubamut/MEGAsync/10_DATA/' | |
start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc) | |
end = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc) | |
# end = datetime.today().replace(tzinfo=timezone.utc) | |
# data as a DatFrame of DataFrames, one for each symbol | |
data = yf.download(tickers, start, end).copy() | |
data1 = data.copy() | |
data.columns = data.columns.swaplevel(1,0) | |
# for one symbol | |
# print(data.SPY[:2]) | |
# if required | |
inception_dates = pd.DataFrame([data[ticker].first_valid_index().date() for ticker in tickers], index=tickers, columns=['inception']) | |
# print (inception_dates) | |
# daily prices | |
prices_d = data1['Adj Close'].copy().dropna() | |
# month-end prices | |
prices_m = prices_d.resample('M').last() | |
prices_m[:2] | |
# Alias Description | |
# -------------------- | |
# B business day frequency | |
# C custom business day frequency | |
# D calendar day frequency | |
# W weekly frequency | |
# M month end frequency | |
# SM semi-month end frequency (15th and end of month) | |
# BM business month end frequency | |
# CBM custom business month end frequency | |
# MS month start frequency | |
# SMS semi-month start frequency (1st and 15th) | |
# BMS business month start frequency | |
# CBMS custom business month start frequency | |
# Q quarter end frequency | |
# BQ business quarter end frequency | |
# QS quarter start frequency | |
# BQS business quarter start frequency | |
# A,Y year end frequency | |
# BA,BY business year end frequency | |
# AS,YS year start frequency | |
# BAS,BYS business year start frequency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yfinance as yf | |
import pandas as pd | |
from datetime import datetime | |
# NOTE: RETURNS MATCH WITH PV | |
from fintools import * | |
start = "1990-01-01" | |
end = datetime.today().strftime('%Y-%m-%d') | |
symbols = ['SPY','XLY','XLK','XLI','XLB','XLE','XLP','XLV','XLU','XLF']) | |
daily_prices = yf.download(symbols, start, end)['Adj Close'] | |
symbol = 'SPY' | |
df = monthly_return_table(daily_prices[symbol]) | |
df = pd.DataFrame(df.values, index=df.index, columns=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep', | |
'Oct','Nov','Dec','Annual Return']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment