Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. import numpy as np
  2. np.random.seed(1)
  3.  
  4. tickers = ['AAPL', 'GOOGL', 'AMZN']
  5. num_portfs = 5
  6. returns = np.array([])
  7.  
  8. shape = np.genfromtxt('data/AAPL.csv',  delimiter=",")[:,4].shape[0] - 1
  9.  
  10. pt_change = np.zeros(shape)
  11.  
  12. for ticker in tickers:
  13.     data = np.genfromtxt('data/%s.csv'%ticker, delimiter=",")
  14.     close = data[:250,4]
  15.     close = close[::-1]
  16.     diff = np.nan_to_num(np.diff(close) / close[:-1])
  17.     returns = np.append(returns, np.sum(diff))
  18.     pt_change = np.c_[pt_change, diff[::-1]]
  19.  
  20. pt_change = pt_change[:,1:]
  21. cov = np.cov(pt_change.T)
  22.  
  23. weights = np.random.rand(num_portfs,len(tickers))
  24.  
  25. weights = (weights.T/weights.sum(axis=1)).T
  26. returns = (weights*returns).sum(axis=1)
  27.  
  28. std = np.sqrt(weights.T*cov*weights)
  29.  
  30. print(std)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement