In [1]:
import numpy as np
import pandas as pd
import pandas_datareader.data as wb
import matplotlib.pyplot as plt
In [2]:
stocks = ["TSLA", "NFLX", "GOOG", "MSFT"]
data = pd.DataFrame()
for s in stocks:
data[s] = wb.DataReader(s, data_source='google', start='2012-1-1')['Close']
In [3]:
data.info()
In [4]:
data.head()
Out[4]:
In [5]:
data.tail()
Out[5]:
In [6]:
data.iloc[0]
Out[6]:
In [7]:
(data / data.iloc[0] * 100).plot(figsize = (15, 6))
plt.show()
In [8]:
returns = (data / data.shift(1)) -1 # simple returns
returns.head()
Out[8]:
In [9]:
weights = np.array([0.45, 0.45, 0.05, 0.05])
np.dot(returns, weights)
Out[9]:
In [10]:
annual_returns = returns.mean() * 250
annual_returns
Out[10]:
In [11]:
annual = np.dot(annual_returns, weights)
In [12]:
portfolio = str(round(annual, 5) * 100) + "%"
print(portfolio)
In [ ]: