How to do it...

Execute the following steps to create and evaluate the 1/n portfolio.

  1. Import the libraries:
import yfinance as yf
import numpy as np
import pandas as pd
import pyfolio as pf
  1. Set up the parameters:
RISKY_ASSETS = ['AAPL', 'IBM', 'MSFT', 'TWTR']
START_DATE = '2017-01-01'
END_DATE = '2018-12-31'

n_assets = len(RISKY_ASSETS)
  1. Download the stock prices from Yahoo Finance:
prices_df = yf.download(RISKY_ASSETS, start=START_DATE, 
end=END_DATE, adjusted=True)
  1. Calculate individual asset returns:
returns = prices_df['Adj Close'].pct_change().dropna()
  1. Define the weights:
portfolio_weights = n_assets * [1 / n_assets]
  1. Calculate the portfolio returns:
portfolio_returns = pd.Series(np.dot(portfolio_weights, returns.T), 
index=returns.index)
  1. Create the tear sheet (simple variant):
pf.create_simple_tear_sheet(portfolio_returns)

The preceding line of code generates the simple tear sheet, and the first table looks like this:

We describe the metrics presented in the first table of the tear sheet in the following section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.17.27