Twitter introduced the risk-return map of the portfolio that combines SPXL and TMF.
Save the code to create the graph here.
Obtain the TMF, SPXL, VTI csv files from the following site in advance (the period is MAX). (Actually, I should just apply the code to get ...)
TMF: https://www.nasdaq.com/market-activity/funds-and-etfs/tmf/historical SPXL: https://www.nasdaq.com/market-activity/funds-and-etfs/spxl/historical VTI: https://www.nasdaq.com/market-activity/funds-and-etfs/vti/historical
Save the files with the file names "TMF.csv", "SPXL.csv", and "VTI.csv", respectively.
#Invite frends to the party...
import numpy as np
import pandas as pd
from datetime import datetime
import seaborn as sns
sns.set()
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
def make_df(etfs):
df = pd.DataFrame()
for etf in etfs:
csvfile = etf +'.csv'
csv_data = pd.read_csv(csvfile)
csv_data.Date = pd.to_datetime(csv_data.Date)
csv_data = csv_data.set_index('Date')
csv_data= csv_data.rename(columns={' Close/Last': etf})
df[etf] = csv_data[etf]
df = df.sort_index().dropna()
return df
def make_port_df(df):
#Return calculation
rtn = (df / df.shift(1) -1).fillna(0)
#Create portfolio data for each ratio
for w in [9, 8, 7, 6, 5, 4, 3, 2, 1]:
rtn['SPXL'+str(w)+' : TMF'+str(10-w)] =rtn['SPXL']*(w/10) + rtn['TMF']*(1-w/10)
rtn += 1
df = rtn.cumprod()
return df
def rr_map(df):
#Monthly return calculation
dfm = df.resample('M').ffill()
m_rtn = (dfm / dfm.shift(1) -1).dropna()
#Risk calculation
std = (m_rtn.std())*(12**0.5)
#Annual return calculation
cum = m_rtn+1
a_rtn = (cum.product())**(12/len(m_rtn))-1
#Risk-return map creation
plt.figure(figsize=(6, 6))
for asset in list(a_rtn.index):
plt.scatter(x = std[asset], y = a_rtn[asset],label = asset)
plt.xlabel('Risk')
plt.ylabel('Return')
plt.xlim(0,std.max()*1.1)
plt.ylim(0,a_rtn.max()*1.1)
plt.legend(loc = 'best')
plt.title('Risk Return Map')
plt.savefig('rr_map.png',bbox_inches="tight")
etfs = ['SPXL','TMF','VTI']
df = make_df(etfs)
df = make_port_df(df)
rr_map(df)
↓ This graph is displayed.
Having about half of the SPXL and half of the TMF is likely to result in a portfolio with better risk-return characteristics than VTI (investment at your own risk).
-[] Diversified portfolio of multiple assets
Recommended Posts