-
Notifications
You must be signed in to change notification settings - Fork 240
/
costs.py
221 lines (175 loc) · 7.02 KB
/
costs.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Copyright 2016 Stephen Boyd, Enzo Busseti, Steven Diamond, BlackRock Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import cvxpy as cvx
import numpy as np
import copy
from .expression import Expression
from .utils.data_management import null_checker, time_locator
__all__ = ['HcostModel', 'TcostModel']
class BaseCost(Expression):
def __init__(self):
self.gamma = 1. # it is changed by gamma * BaseCost()
def weight_expr(self, t, w_plus, z, value):
cost, constr = self._estimate(t, w_plus, z, value)
return self.gamma * cost, constr
def weight_expr_ahead(self, t, tau, w_plus, z, value):
cost, constr = self._estimate_ahead(t, tau, w_plus, z, value)
return self.gamma * cost, constr
def __mul__(self, other):
"""Read the gamma parameter as a multiplication."""
newobj = copy.copy(self)
newobj.gamma *= other
return newobj
def __rmul__(self, other):
"""Read the gamma parameter as a multiplication."""
return self.__mul__(other)
class HcostModel(BaseCost):
"""A model for holding costs.
Attributes:
borrow_costs: A dataframe of borrow costs.
dividends: A dataframe of dividends.
"""
def __init__(self, borrow_costs, dividends=0.):
null_checker(borrow_costs)
self.borrow_costs = borrow_costs
null_checker(dividends)
self.dividends = dividends
super(HcostModel, self).__init__()
def _estimate(self, t, w_plus, z, value):
"""Estimate holding costs.
Args:
t: time of estimate
wplus: holdings
tau: time to estimate (default=t)
"""
try:
w_plus = w_plus[w_plus.index != self.cash_key]
w_plus = w_plus.values
except AttributeError:
w_plus = w_plus[:-1] # TODO fix when cvxpy pandas ready
try:
self.expression = cvx.multiply(
time_locator(self.borrow_costs, t), cvx.neg(w_plus))
except TypeError:
self.expression = cvx.multiply(time_locator(
self.borrow_costs, t).values, cvx.neg(w_plus))
try:
self.expression -= cvx.multiply(
time_locator(self.dividends, t), w_plus)
except TypeError:
self.expression -= cvx.multiply(
time_locator(self.dividends, t).values, w_plus)
return cvx.sum(self.expression), []
def _estimate_ahead(self, t, tau, w_plus, z, value):
return self._estimate(t, w_plus, z, value)
def value_expr(self, t, h_plus, u):
self.last_cost = -np.minimum(0, h_plus.iloc[:-1]) * time_locator(
self.borrow_costs, t)
self.last_cost -= h_plus.iloc[:-1] * time_locator(self.dividends, t)
return sum(self.last_cost)
def optimization_log(self, t):
return self.expression.value
def simulation_log(self, t):
return self.last_cost
class TcostModel(BaseCost):
"""A model for transaction costs.
(See figure 2.3 in the paper
https://stanford.edu/~boyd/papers/pdf/cvx_portfolio.pdf)
Attributes:
volume: A dataframe of volumes.
sigma: A dataframe of daily volatilities.
half_spread: A dataframe of bid-ask spreads divided by 2.
nonlin_coeff: A dataframe of coefficients for the nonlinear cost.
power: The nonlinear tcost power.
"""
def __init__(self, half_spread, nonlin_coeff=0., sigma=0., volume=1.,
power=1.5):
null_checker(half_spread)
self.half_spread = half_spread
null_checker(sigma)
self.sigma = sigma
null_checker(volume)
self.volume = volume
null_checker(nonlin_coeff)
self.nonlin_coeff = nonlin_coeff
null_checker(power)
self.power = power
super(TcostModel, self).__init__()
def _estimate(self, t, w_plus, z, value):
"""Estimate tcosts given trades.
Args:
t: time of estimate
z: trades
value: portfolio value
Returns:
An expression for the tcosts.
"""
try:
z = z[z.index != self.cash_key]
z = z.values
except AttributeError:
z = z[:-1] # TODO fix when cvxpy pandas ready
constr = []
second_term = time_locator(self.nonlin_coeff, t) * time_locator(
self.sigma, t) * (value / time_locator(self.volume, t)) ** (
self.power - 1)
# no trade conditions
if np.isscalar(second_term):
if np.isnan(second_term):
constr += [z == 0]
second_term = 0
else: # it is a pd series
no_trade = second_term.index[second_term.isnull()]
second_term[no_trade] = 0
constr += [z[second_term.index.get_loc(tick)] == 0
for tick in no_trade]
try:
self.expression = cvx.multiply(
time_locator(self.half_spread, t), cvx.abs(z))
except TypeError:
self.expression = cvx.multiply(
time_locator(self.half_spread, t).values, cvx.abs(z))
try:
self.expression += cvx.multiply(second_term,
cvx.abs(z) ** self.power)
except TypeError:
self.expression += cvx.multiply(
second_term.values, cvx.abs(z) ** self.power)
return cvx.sum(self.expression), constr
def value_expr(self, t, h_plus, u):
u_nc = u.iloc[:-1]
self.tmp_tcosts = (
np.abs(u_nc) * time_locator(self.half_spread, t) +
time_locator(self.nonlin_coeff, t) * time_locator(self.sigma,
t) *
np.abs(u_nc) ** self.power /
(time_locator(self.volume, t) ** (self.power - 1)))
return self.tmp_tcosts.sum()
def optimization_log(self, t):
try:
return self.expression.value
except AttributeError:
return np.nan
def simulation_log(self, t):
# TODO find another way
return self.tmp_tcosts
def _estimate_ahead(self, t, tau, w_plus, z, value):
"""Returns the estimate at time t of tcost at time tau.
"""
return self._estimate(t, w_plus, z, value)
def est_period(self, t, tau_start, tau_end, w_plus, z, value):
"""Returns the estimate at time t of tcost over given period.
"""
K = (tau_end - tau_start).days
tcost, constr = self.weight_expr(t, None, z / K, value)
return tcost * K, constr