|
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
|
|
|
|
|
65
|
|
|
import zipline.utils.math_utils as zp_math |
|
66
|
|
|
|
|
67
|
|
|
log = logbook.Logger('Risk') |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
TREASURY_DURATIONS = [ |
|
71
|
|
|
'1month', '3month', '6month', |
|
72
|
|
|
'1year', '2year', '3year', '5year', |
|
73
|
|
|
'7year', '10year', '30year' |
|
74
|
|
|
] |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
# check if a field in rval is nan, and replace it with |
|
78
|
|
|
# None. |
|
79
|
|
|
def check_entry(key, value): |
|
80
|
|
|
if key != 'period_label': |
|
81
|
|
|
return np.isnan(value) or np.isinf(value) |
|
82
|
|
|
else: |
|
83
|
|
|
return False |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
############################ |
|
87
|
|
|
# Risk Metric Calculations # |
|
88
|
|
|
############################ |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def sharpe_ratio(algorithm_volatility, algorithm_return, treasury_return): |
|
92
|
|
|
""" |
|
93
|
|
|
http://en.wikipedia.org/wiki/Sharpe_ratio |
|
94
|
|
|
|
|
95
|
|
|
Args: |
|
96
|
|
|
algorithm_volatility (float): Algorithm volatility. |
|
97
|
|
|
algorithm_return (float): Algorithm return percentage. |
|
98
|
|
|
treasury_return (float): Treasury return percentage. |
|
99
|
|
|
|
|
100
|
|
|
Returns: |
|
101
|
|
|
float. The Sharpe ratio. |
|
102
|
|
|
""" |
|
103
|
|
|
if zp_math.tolerant_equals(algorithm_volatility, 0): |
|
104
|
|
|
return np.nan |
|
105
|
|
|
|
|
106
|
|
|
return (algorithm_return - treasury_return) / algorithm_volatility |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
def downside_risk(algorithm_returns, mean_returns, normalization_factor): |
|
110
|
|
|
rets = algorithm_returns.round(8) |
|
111
|
|
|
mar = mean_returns.round(8) |
|
112
|
|
|
mask = rets < mar |
|
113
|
|
|
downside_diff = rets[mask] - mar[mask] |
|
114
|
|
|
if len(downside_diff) <= 1: |
|
115
|
|
|
return 0.0 |
|
116
|
|
|
return np.std(downside_diff, ddof=1) * math.sqrt(normalization_factor) |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
def sortino_ratio(algorithm_period_return, treasury_period_return, mar): |
|
120
|
|
|
""" |
|
121
|
|
|
http://en.wikipedia.org/wiki/Sortino_ratio |
|
122
|
|
|
|
|
123
|
|
|
Args: |
|
124
|
|
|
algorithm_returns (np.array-like): |
|
125
|
|
|
Returns from algorithm lifetime. |
|
126
|
|
|
algorithm_period_return (float): |
|
127
|
|
|
Algorithm return percentage from latest period. |
|
128
|
|
|
mar (float): Minimum acceptable return. |
|
129
|
|
|
|
|
130
|
|
|
Returns: |
|
131
|
|
|
float. The Sortino ratio. |
|
132
|
|
|
""" |
|
133
|
|
|
if zp_math.tolerant_equals(mar, 0): |
|
134
|
|
|
return 0.0 |
|
135
|
|
|
|
|
136
|
|
|
return (algorithm_period_return - treasury_period_return) / mar |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
def information_ratio(algorithm_returns, benchmark_returns): |
|
140
|
|
|
""" |
|
141
|
|
|
http://en.wikipedia.org/wiki/Information_ratio |
|
142
|
|
|
|
|
143
|
|
|
Args: |
|
144
|
|
|
algorithm_returns (np.array-like): |
|
145
|
|
|
All returns during algorithm lifetime. |
|
146
|
|
|
benchmark_returns (np.array-like): |
|
147
|
|
|
All benchmark returns during algo lifetime. |
|
148
|
|
|
|
|
149
|
|
|
Returns: |
|
150
|
|
|
float. Information ratio. |
|
151
|
|
|
""" |
|
152
|
|
|
relative_returns = algorithm_returns - benchmark_returns |
|
153
|
|
|
|
|
154
|
|
|
relative_deviation = relative_returns.std(ddof=1) |
|
155
|
|
|
|
|
156
|
|
|
if zp_math.tolerant_equals(relative_deviation, 0) or \ |
|
157
|
|
|
np.isnan(relative_deviation): |
|
158
|
|
|
return 0.0 |
|
159
|
|
|
|
|
160
|
|
|
return np.mean(relative_returns) / relative_deviation |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
def alpha(algorithm_period_return, treasury_period_return, |
|
164
|
|
|
benchmark_period_returns, beta): |
|
165
|
|
|
""" |
|
166
|
|
|
http://en.wikipedia.org/wiki/Alpha_(investment) |
|
167
|
|
|
|
|
168
|
|
|
Args: |
|
169
|
|
|
algorithm_period_return (float): |
|
170
|
|
|
Return percentage from algorithm period. |
|
171
|
|
|
treasury_period_return (float): |
|
172
|
|
|
Return percentage for treasury period. |
|
173
|
|
|
benchmark_period_return (float): |
|
174
|
|
|
Return percentage for benchmark period. |
|
175
|
|
|
beta (float): |
|
176
|
|
|
beta value for the same period as all other values |
|
177
|
|
|
|
|
178
|
|
|
Returns: |
|
179
|
|
|
float. The alpha of the algorithm. |
|
180
|
|
|
""" |
|
181
|
|
|
return algorithm_period_return - \ |
|
182
|
|
|
(treasury_period_return + beta * |
|
183
|
|
|
(benchmark_period_returns - treasury_period_return)) |
|
184
|
|
|
|
|
185
|
|
|
########################### |
|
186
|
|
|
# End Risk Metric Section # |
|
187
|
|
|
########################### |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
def get_treasury_rate(treasury_curves, treasury_duration, day): |
|
191
|
|
|
rate = None |
|
192
|
|
|
|
|
193
|
|
|
curve = treasury_curves.ix[day] |
|
194
|
|
|
# 1month note data begins in 8/2001, |
|
195
|
|
|
# so we can use 3month instead. |
|
196
|
|
|
idx = TREASURY_DURATIONS.index(treasury_duration) |
|
197
|
|
|
for duration in TREASURY_DURATIONS[idx:]: |
|
198
|
|
|
rate = curve[duration] |
|
199
|
|
|
if rate is not None: |
|
200
|
|
|
break |
|
201
|
|
|
|
|
202
|
|
|
return rate |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
def search_day_distance(end_date, dt, env): |
|
206
|
|
|
tdd = env.trading_day_distance(dt, end_date) |
|
207
|
|
|
if tdd is None: |
|
208
|
|
|
return None |
|
209
|
|
|
assert tdd >= 0 |
|
210
|
|
|
return tdd |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
def select_treasury_duration(start_date, end_date): |
|
214
|
|
|
td = end_date - start_date |
|
215
|
|
|
if td.days <= 31: |
|
216
|
|
|
treasury_duration = '1month' |
|
217
|
|
|
elif td.days <= 93: |
|
218
|
|
|
treasury_duration = '3month' |
|
219
|
|
|
elif td.days <= 186: |
|
220
|
|
|
treasury_duration = '6month' |
|
221
|
|
|
elif td.days <= 366: |
|
222
|
|
|
treasury_duration = '1year' |
|
223
|
|
|
elif td.days <= 365 * 2 + 1: |
|
224
|
|
|
treasury_duration = '2year' |
|
225
|
|
|
elif td.days <= 365 * 3 + 1: |
|
226
|
|
|
treasury_duration = '3year' |
|
227
|
|
|
elif td.days <= 365 * 5 + 2: |
|
228
|
|
|
treasury_duration = '5year' |
|
229
|
|
|
elif td.days <= 365 * 7 + 2: |
|
230
|
|
|
treasury_duration = '7year' |
|
231
|
|
|
elif td.days <= 365 * 10 + 2: |
|
232
|
|
|
treasury_duration = '10year' |
|
233
|
|
|
else: |
|
234
|
|
|
treasury_duration = '30year' |
|
235
|
|
|
|
|
236
|
|
|
return treasury_duration |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
def choose_treasury(select_treasury, treasury_curves, start_date, end_date, |
|
240
|
|
|
env, compound=True): |
|
241
|
|
|
""" |
|
242
|
|
|
Find the latest known interest rate for a given duration within a date |
|
243
|
|
|
range. |
|
244
|
|
|
|
|
245
|
|
|
If we find one but it's more than a trading day ago from the date we're |
|
246
|
|
|
looking for, then we log a warning |
|
247
|
|
|
""" |
|
248
|
|
|
treasury_duration = select_treasury(start_date, end_date) |
|
249
|
|
|
end_day = end_date.replace(hour=0, minute=0, second=0, microsecond=0) |
|
250
|
|
|
search_day = None |
|
251
|
|
|
|
|
252
|
|
|
if end_day in treasury_curves.index: |
|
253
|
|
|
rate = get_treasury_rate(treasury_curves, |
|
254
|
|
|
treasury_duration, |
|
255
|
|
|
end_day) |
|
256
|
|
|
if rate is not None: |
|
257
|
|
|
search_day = end_day |
|
258
|
|
|
|
|
259
|
|
|
if not search_day: |
|
260
|
|
|
# in case end date is not a trading day or there is no treasury |
|
261
|
|
|
# data, search for the previous day with an interest rate. |
|
262
|
|
|
search_days = treasury_curves.index |
|
263
|
|
|
|
|
264
|
|
|
# Find rightmost value less than or equal to end_day |
|
265
|
|
|
i = search_days.searchsorted(end_day) |
|
266
|
|
|
for prev_day in search_days[i - 1::-1]: |
|
267
|
|
|
rate = get_treasury_rate(treasury_curves, |
|
268
|
|
|
treasury_duration, |
|
269
|
|
|
prev_day) |
|
270
|
|
|
if rate is not None: |
|
271
|
|
|
search_day = prev_day |
|
272
|
|
|
search_dist = search_day_distance(end_date, prev_day, env) |
|
273
|
|
|
break |
|
274
|
|
|
|
|
275
|
|
|
if search_day: |
|
276
|
|
|
if (search_dist is None or search_dist > 1) and \ |
|
277
|
|
|
search_days[0] <= end_day <= search_days[-1]: |
|
278
|
|
|
message = "No rate within 1 trading day of end date = \ |
|
279
|
|
|
{dt} and term = {term}. Using {search_day}. Check that date doesn't exceed \ |
|
280
|
|
|
treasury history range." |
|
281
|
|
|
message = message.format(dt=end_date, |
|
282
|
|
|
term=treasury_duration, |
|
283
|
|
|
search_day=search_day) |
|
284
|
|
|
log.warn(message) |
|
285
|
|
|
|
|
286
|
|
|
if search_day: |
|
287
|
|
|
td = end_date - start_date |
|
288
|
|
|
if compound: |
|
289
|
|
|
return rate * (td.days + 1) / 365 |
|
290
|
|
|
else: |
|
291
|
|
|
return rate |
|
292
|
|
|
|
|
293
|
|
|
message = "No rate for end date = {dt} and term = {term}. Check \ |
|
294
|
|
|
that date doesn't exceed treasury history range." |
|
295
|
|
|
message = message.format( |
|
296
|
|
|
dt=end_date, |
|
297
|
|
|
term=treasury_duration |
|
298
|
|
|
) |
|
299
|
|
|
raise Exception(message) |
|
300
|
|
|
|