Created
June 21, 2024 07:34
-
-
Save ChadThackray/3db2b9fd63f91aa23060630c66036175 to your computer and use it in GitHub Desktop.
Portfolio allocation backtesting in Python from scratch
This file contains hidden or 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
# Licensed under the MIT License. See comment below for full licence information. | |
import yfinance as yf | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
weightings1 = {"SPY":"100"} | |
weightings2 = {"SPY":"95","BTC-USD":"5"} | |
members = ["BTC-USD","SPY"] | |
def PortfolioCalc(weightings, data, name): | |
data[name] = sum([ int(weightings[x])*data[x]/100 for x in list(weightings.keys()) ]) | |
return data | |
basedata = yf.Ticker(members[0]).history(period="max").reset_index()[["Date","Open"]] | |
basedata["Date"] = pd.to_datetime(basedata["Date"]) | |
basedata = basedata.rename(columns = {"Open":members[0]}) | |
if (len(members)>1): | |
for x in range(1,len(members)): | |
newdata = yf.Ticker(members[x]).history(period="max").reset_index()[["Date","Open"]] | |
newdata["Date"] = pd.to_datetime(newdata["Date"]) | |
newdata = newdata.rename(columns = {"Open":members[x]}) | |
basedata = pd.merge(basedata, newdata, on="Date") | |
basedata = basedata[ basedata["Date"] > "2016-01-01"] | |
print(basedata) | |
for x in members: | |
basedata[x] = basedata[x]/(basedata[x].iloc[0]) | |
basedata = PortfolioCalc(weightings1, basedata, "crypto1") | |
basedata = PortfolioCalc(weightings2, basedata, "crypto2") | |
#for x in members: | |
#plt.semilogy(basedata["Date"], basedata[x], label=x) | |
plt.style.use("dark_background") | |
plt.plot(basedata["Date"], basedata["crypto1"], label = "100% s&p500") | |
plt.plot(basedata["Date"], basedata["crypto2"], label = "95% s&p500, 5% BTC") | |
plt.legend(loc="upper left") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copyright 2021 Chad Thackray
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.