1
|
|
|
# |
2
|
|
|
# Copyright 2014 Quantopian, Inc. |
3
|
|
|
# |
4
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
# you may not use this file except in compliance with the License. |
6
|
|
|
# You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
Risk Report |
19
|
|
|
=========== |
20
|
|
|
|
21
|
|
|
+-----------------+----------------------------------------------------+ |
22
|
|
|
| key | value | |
23
|
|
|
+=================+====================================================+ |
24
|
|
|
| trading_days | The number of trading days between self.start_date | |
25
|
|
|
| | and self.end_date | |
26
|
|
|
+-----------------+----------------------------------------------------+ |
27
|
|
|
| benchmark_volat\| The volatility of the benchmark between | |
28
|
|
|
| ility | self.start_date and self.end_date. | |
29
|
|
|
+-----------------+----------------------------------------------------+ |
30
|
|
|
| algo_volatility | The volatility of the algo between self.start_date | |
31
|
|
|
| | and self.end_date. | |
32
|
|
|
+-----------------+----------------------------------------------------+ |
33
|
|
|
| treasury_period\| The return of treasuries over the period. Treasury | |
34
|
|
|
| _return | maturity is chosen to match the duration of the | |
35
|
|
|
| | test period. | |
36
|
|
|
+-----------------+----------------------------------------------------+ |
37
|
|
|
| sharpe | The sharpe ratio based on the _algorithm_ (rather | |
38
|
|
|
| | than the static portfolio) returns. | |
39
|
|
|
+-----------------+----------------------------------------------------+ |
40
|
|
|
| information | The information ratio based on the _algorithm_ | |
41
|
|
|
| | (rather than the static portfolio) returns. | |
42
|
|
|
+-----------------+----------------------------------------------------+ |
43
|
|
|
| beta | The _algorithm_ beta to the benchmark. | |
44
|
|
|
+-----------------+----------------------------------------------------+ |
45
|
|
|
| alpha | The _algorithm_ alpha to the benchmark. | |
46
|
|
|
+-----------------+----------------------------------------------------+ |
47
|
|
|
| excess_return | The excess return of the algorithm over the | |
48
|
|
|
| | treasuries. | |
49
|
|
|
+-----------------+----------------------------------------------------+ |
50
|
|
|
| max_drawdown | The largest relative peak to relative trough move | |
51
|
|
|
| | for the portfolio returns between self.start_date | |
52
|
|
|
| | and self.end_date. | |
53
|
|
|
+-----------------+----------------------------------------------------+ |
54
|
|
|
| max_leverage | The largest gross leverage between self.start_date | |
55
|
|
|
| | and self.end_date | |
56
|
|
|
+-----------------+----------------------------------------------------+ |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
""" |
60
|
|
|
|
61
|
|
|
import logbook |
62
|
|
|
import math |
63
|
|
|
import numpy as np |
64
|
|
|
import pandas as pd |
65
|
|
|
|
66
|
|
|
import zipline.utils.math_utils as zp_math |
67
|
|
|
|
68
|
|
|
log = logbook.Logger('Risk') |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
TREASURY_DURATIONS = [ |
72
|
|
|
'1month', '3month', '6month', |
73
|
|
|
'1year', '2year', '3year', '5year', |
74
|
|
|
'7year', '10year', '30year' |
75
|
|
|
] |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
# check if a field in rval is nan, and replace it with |
79
|
|
|
# None. |
80
|
|
|
def check_entry(key, value): |
81
|
|
|
if key != 'period_label': |
82
|
|
|
return np.isnan(value) or np.isinf(value) |
83
|
|
|
else: |
84
|
|
|
return False |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
############################ |
88
|
|
|
# Risk Metric Calculations # |
89
|
|
|
############################ |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def sharpe_ratio(algorithm_return, treasury_return): |
93
|
|
|
""" |
94
|
|
|
http://en.wikipedia.org/wiki/Sharpe_ratio |
95
|
|
|
|
96
|
|
|
Args: |
97
|
|
|
algorithm_return (array): Daily algorithm return percentage. |
98
|
|
|
treasury_return (array): Annual treasury return percentage. |
99
|
|
|
|
100
|
|
|
Returns: |
101
|
|
|
float. The Sharpe ratio. |
102
|
|
|
""" |
103
|
|
|
if len(algorithm_return) < 2: |
104
|
|
|
return np.nan |
105
|
|
|
|
106
|
|
|
# compute daily returns from provided annual treasury yields |
107
|
|
|
if not isinstance(treasury_return, pd.Series) and (treasury_return != 0): |
108
|
|
|
treasury_return = (1 + treasury_return)**(1./252) - 1 |
109
|
|
|
|
110
|
|
|
return_risk_adj = algorithm_return - treasury_return |
111
|
|
|
|
112
|
|
|
return np.mean(return_risk_adj) / \ |
113
|
|
|
np.std(return_risk_adj) * \ |
114
|
|
|
np.sqrt(252) |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
def downside_risk(algorithm_returns, mean_returns, normalization_factor): |
118
|
|
|
rets = algorithm_returns.round(8) |
119
|
|
|
mar = mean_returns.round(8) |
120
|
|
|
mask = rets < mar |
121
|
|
|
downside_diff = rets[mask] - mar[mask] |
122
|
|
|
if len(downside_diff) <= 1: |
123
|
|
|
return 0.0 |
124
|
|
|
return np.std(downside_diff, ddof=1) * math.sqrt(normalization_factor) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def sortino_ratio(algorithm_period_return, treasury_period_return, mar): |
128
|
|
|
""" |
129
|
|
|
http://en.wikipedia.org/wiki/Sortino_ratio |
130
|
|
|
|
131
|
|
|
Args: |
132
|
|
|
algorithm_returns (np.array-like): |
133
|
|
|
Returns from algorithm lifetime. |
134
|
|
|
algorithm_period_return (float): |
135
|
|
|
Algorithm return percentage from latest period. |
136
|
|
|
mar (float): Minimum acceptable return. |
137
|
|
|
|
138
|
|
|
Returns: |
139
|
|
|
float. The Sortino ratio. |
140
|
|
|
""" |
141
|
|
|
if zp_math.tolerant_equals(mar, 0): |
142
|
|
|
return 0.0 |
143
|
|
|
|
144
|
|
|
return (algorithm_period_return - treasury_period_return) / mar |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
def information_ratio(algorithm_returns, benchmark_returns): |
148
|
|
|
""" |
149
|
|
|
http://en.wikipedia.org/wiki/Information_ratio |
150
|
|
|
|
151
|
|
|
Args: |
152
|
|
|
algorithm_returns (np.array-like): |
153
|
|
|
All returns during algorithm lifetime. |
154
|
|
|
benchmark_returns (np.array-like): |
155
|
|
|
All benchmark returns during algo lifetime. |
156
|
|
|
|
157
|
|
|
Returns: |
158
|
|
|
float. Information ratio. |
159
|
|
|
""" |
160
|
|
|
relative_returns = algorithm_returns - benchmark_returns |
161
|
|
|
|
162
|
|
|
relative_deviation = relative_returns.std(ddof=1) |
163
|
|
|
|
164
|
|
|
if zp_math.tolerant_equals(relative_deviation, 0) or \ |
165
|
|
|
np.isnan(relative_deviation): |
166
|
|
|
return 0.0 |
167
|
|
|
|
168
|
|
|
return np.mean(relative_returns) / relative_deviation |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
def alpha(algorithm_period_return, treasury_period_return, |
172
|
|
|
benchmark_period_returns, beta): |
173
|
|
|
""" |
174
|
|
|
http://en.wikipedia.org/wiki/Alpha_(investment) |
175
|
|
|
|
176
|
|
|
Args: |
177
|
|
|
algorithm_period_return (float): |
178
|
|
|
Return percentage from algorithm period. |
179
|
|
|
treasury_period_return (float): |
180
|
|
|
Return percentage for treasury period. |
181
|
|
|
benchmark_period_return (float): |
182
|
|
|
Return percentage for benchmark period. |
183
|
|
|
beta (float): |
184
|
|
|
beta value for the same period as all other values |
185
|
|
|
|
186
|
|
|
Returns: |
187
|
|
|
float. The alpha of the algorithm. |
188
|
|
|
""" |
189
|
|
|
return algorithm_period_return - \ |
190
|
|
|
(treasury_period_return + beta * |
191
|
|
|
(benchmark_period_returns - treasury_period_return)) |
192
|
|
|
|
193
|
|
|
########################### |
194
|
|
|
# End Risk Metric Section # |
195
|
|
|
########################### |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def get_treasury_rate(treasury_curves, treasury_duration, day): |
199
|
|
|
rate = None |
200
|
|
|
|
201
|
|
|
curve = treasury_curves.ix[day] |
202
|
|
|
# 1month note data begins in 8/2001, |
203
|
|
|
# so we can use 3month instead. |
204
|
|
|
idx = TREASURY_DURATIONS.index(treasury_duration) |
205
|
|
|
for duration in TREASURY_DURATIONS[idx:]: |
206
|
|
|
rate = curve[duration] |
207
|
|
|
if rate is not None: |
208
|
|
|
break |
209
|
|
|
|
210
|
|
|
return rate |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
def search_day_distance(end_date, dt, env): |
214
|
|
|
tdd = env.trading_day_distance(dt, end_date) |
215
|
|
|
if tdd is None: |
216
|
|
|
return None |
217
|
|
|
assert tdd >= 0 |
218
|
|
|
return tdd |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
def select_treasury_duration(start_date, end_date): |
222
|
|
|
td = end_date - start_date |
223
|
|
|
if td.days <= 31: |
224
|
|
|
treasury_duration = '1month' |
225
|
|
|
elif td.days <= 93: |
226
|
|
|
treasury_duration = '3month' |
227
|
|
|
elif td.days <= 186: |
228
|
|
|
treasury_duration = '6month' |
229
|
|
|
elif td.days <= 366: |
230
|
|
|
treasury_duration = '1year' |
231
|
|
|
elif td.days <= 365 * 2 + 1: |
232
|
|
|
treasury_duration = '2year' |
233
|
|
|
elif td.days <= 365 * 3 + 1: |
234
|
|
|
treasury_duration = '3year' |
235
|
|
|
elif td.days <= 365 * 5 + 2: |
236
|
|
|
treasury_duration = '5year' |
237
|
|
|
elif td.days <= 365 * 7 + 2: |
238
|
|
|
treasury_duration = '7year' |
239
|
|
|
elif td.days <= 365 * 10 + 2: |
240
|
|
|
treasury_duration = '10year' |
241
|
|
|
else: |
242
|
|
|
treasury_duration = '30year' |
243
|
|
|
|
244
|
|
|
return treasury_duration |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
def choose_treasury(select_treasury, treasury_curves, start_date, end_date, |
248
|
|
|
env, compound=True): |
249
|
|
|
""" |
250
|
|
|
Find the latest known interest rate for a given duration within a date |
251
|
|
|
range. |
252
|
|
|
|
253
|
|
|
If we find one but it's more than a trading day ago from the date we're |
254
|
|
|
looking for, then we log a warning |
255
|
|
|
""" |
256
|
|
|
treasury_duration = select_treasury(start_date, end_date) |
257
|
|
|
end_day = end_date.replace(hour=0, minute=0, second=0, microsecond=0) |
258
|
|
|
search_day = None |
259
|
|
|
|
260
|
|
|
if end_day in treasury_curves.index: |
261
|
|
|
rate = get_treasury_rate(treasury_curves, |
262
|
|
|
treasury_duration, |
263
|
|
|
end_day) |
264
|
|
|
if rate is not None: |
265
|
|
|
search_day = end_day |
266
|
|
|
|
267
|
|
|
if not search_day: |
268
|
|
|
# in case end date is not a trading day or there is no treasury |
269
|
|
|
# data, search for the previous day with an interest rate. |
270
|
|
|
search_days = treasury_curves.index |
271
|
|
|
|
272
|
|
|
# Find rightmost value less than or equal to end_day |
273
|
|
|
i = search_days.searchsorted(end_day) |
274
|
|
|
for prev_day in search_days[i - 1::-1]: |
275
|
|
|
rate = get_treasury_rate(treasury_curves, |
276
|
|
|
treasury_duration, |
277
|
|
|
prev_day) |
278
|
|
|
if rate is not None: |
279
|
|
|
search_day = prev_day |
280
|
|
|
search_dist = search_day_distance(end_date, prev_day, env) |
281
|
|
|
break |
282
|
|
|
|
283
|
|
|
if search_day: |
284
|
|
|
if (search_dist is None or search_dist > 1) and \ |
285
|
|
|
search_days[0] <= end_day <= search_days[-1]: |
286
|
|
|
message = "No rate within 1 trading day of end date = \ |
287
|
|
|
{dt} and term = {term}. Using {search_day}. Check that date doesn't exceed \ |
288
|
|
|
treasury history range." |
289
|
|
|
message = message.format(dt=end_date, |
290
|
|
|
term=treasury_duration, |
291
|
|
|
search_day=search_day) |
292
|
|
|
log.warn(message) |
293
|
|
|
|
294
|
|
|
if search_day: |
295
|
|
|
td = end_date - start_date |
296
|
|
|
if compound: |
297
|
|
|
return rate * (td.days + 1) / 365 |
298
|
|
|
else: |
299
|
|
|
return rate |
300
|
|
|
|
301
|
|
|
message = "No rate for end date = {dt} and term = {term}. Check \ |
302
|
|
|
that date doesn't exceed treasury history range." |
303
|
|
|
message = message.format( |
304
|
|
|
dt=end_date, |
305
|
|
|
term=treasury_duration |
306
|
|
|
) |
307
|
|
|
raise Exception(message) |
308
|
|
|
|