-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrategies.py
61 lines (43 loc) · 1.77 KB
/
strategies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import pandas as pd
from scipy.optimize import minimize
def equal_weight(assets):
optimal = [1/len(assets) for i in range(len(assets))]
return optimal
def minimum_variance(ret, bound):
def find_port_variance(weights):
# this is actually std
cov = ret.cov()
port_var = np.sqrt(np.dot(weights.T, np.dot(cov, weights)) * 250)
return port_var
def weight_cons(weights):
return np.sum(weights) - 1
bounds_lim = [bound for x in range(len(ret.columns))] # change to (-1, 1) if you want to short
init = [1/len(ret.columns) for i in range(len(ret.columns))]
constraint = {'type': 'eq', 'fun': weight_cons}
optimal = minimize(fun=find_port_variance,
x0=init,
bounds=bounds_lim,
constraints=constraint,
method='SLSQP'
)
return list(optimal['x'])
def max_sharpe(ret, bound):
def sharpe_func(weights):
hist_mean = ret.mean(axis=0).to_frame()
hist_cov = ret.cov()
port_ret = np.dot(weights.T, hist_mean.values) * 250
port_std = np.sqrt(np.dot(weights.T, np.dot(hist_cov, weights)) * 250)
return -1 * port_ret / port_std
def weight_cons(weights):
return np.sum(weights) - 1
bounds_lim = [bound for x in range(len(ret.columns))] # change to (-1, 1) if you want to short
init = [1/len(ret.columns) for i in range(len(ret.columns))]
constraint = {'type': 'eq', 'fun': weight_cons}
optimal = minimize(fun=sharpe_func,
x0=init,
bounds=bounds_lim,
constraints=constraint,
method='SLSQP'
)
return list(optimal['x'])