Total Complexity | 43 |
Total Lines | 402 |
Duplicated Lines | 0 % |
Complex classes like zipline.finance.risk.RiskMetricsCumulative often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # |
||
75 | class RiskMetricsCumulative(object): |
||
76 | """ |
||
77 | :Usage: |
||
78 | Instantiate RiskMetricsCumulative once. |
||
79 | Call update() method on each dt to update the metrics. |
||
80 | """ |
||
81 | |||
82 | METRIC_NAMES = ( |
||
83 | 'alpha', |
||
84 | 'beta', |
||
85 | 'sharpe', |
||
86 | 'algorithm_volatility', |
||
87 | 'benchmark_volatility', |
||
88 | 'downside_risk', |
||
89 | 'sortino', |
||
90 | 'information', |
||
91 | ) |
||
92 | |||
93 | def __init__(self, sim_params, env, |
||
94 | create_first_day_stats=False): |
||
95 | self.treasury_curves = env.treasury_curves |
||
96 | self.start_date = sim_params.period_start.replace( |
||
97 | hour=0, minute=0, second=0, microsecond=0 |
||
98 | ) |
||
99 | self.end_date = sim_params.period_end.replace( |
||
100 | hour=0, minute=0, second=0, microsecond=0 |
||
101 | ) |
||
102 | |||
103 | self.trading_days = env.days_in_range(self.start_date, self.end_date) |
||
104 | |||
105 | # Hold on to the trading day before the start, |
||
106 | # used for index of the zero return value when forcing returns |
||
107 | # on the first day. |
||
108 | self.day_before_start = self.start_date - env.trading_days.freq |
||
109 | |||
110 | last_day = normalize_date(sim_params.period_end) |
||
111 | if last_day not in self.trading_days: |
||
112 | last_day = pd.tseries.index.DatetimeIndex( |
||
113 | [last_day] |
||
114 | ) |
||
115 | self.trading_days = self.trading_days.append(last_day) |
||
116 | |||
117 | self.sim_params = sim_params |
||
118 | self.env = env |
||
119 | |||
120 | self.create_first_day_stats = create_first_day_stats |
||
121 | |||
122 | cont_index = self.trading_days |
||
123 | |||
124 | self.cont_index = cont_index |
||
125 | self.cont_len = len(self.cont_index) |
||
126 | |||
127 | empty_cont = np.full(self.cont_len, np.nan) |
||
128 | |||
129 | self.algorithm_returns_cont = empty_cont.copy() |
||
130 | self.benchmark_returns_cont = empty_cont.copy() |
||
131 | self.algorithm_cumulative_leverages_cont = empty_cont.copy() |
||
132 | self.mean_returns_cont = empty_cont.copy() |
||
133 | self.annualized_mean_returns_cont = empty_cont.copy() |
||
134 | self.mean_benchmark_returns_cont = empty_cont.copy() |
||
135 | self.annualized_mean_benchmark_returns_cont = empty_cont.copy() |
||
136 | |||
137 | # The returns at a given time are read and reset from the respective |
||
138 | # returns container. |
||
139 | self.algorithm_returns = None |
||
140 | self.benchmark_returns = None |
||
141 | self.mean_returns = None |
||
142 | self.annualized_mean_returns = None |
||
143 | self.mean_benchmark_returns = None |
||
144 | self.annualized_mean_benchmark_returns = None |
||
145 | |||
146 | self.algorithm_cumulative_returns = empty_cont.copy() |
||
147 | self.benchmark_cumulative_returns = empty_cont.copy() |
||
148 | self.algorithm_cumulative_leverages = empty_cont.copy() |
||
149 | self.excess_returns = empty_cont.copy() |
||
150 | |||
151 | self.latest_dt_loc = 0 |
||
152 | self.latest_dt = cont_index[0] |
||
153 | |||
154 | self.benchmark_volatility = empty_cont.copy() |
||
155 | self.algorithm_volatility = empty_cont.copy() |
||
156 | self.beta = empty_cont.copy() |
||
157 | self.alpha = empty_cont.copy() |
||
158 | self.sharpe = empty_cont.copy() |
||
159 | self.downside_risk = empty_cont.copy() |
||
160 | self.sortino = empty_cont.copy() |
||
161 | self.information = empty_cont.copy() |
||
162 | |||
163 | self.drawdowns = empty_cont.copy() |
||
164 | self.max_drawdowns = empty_cont.copy() |
||
165 | self.max_drawdown = 0 |
||
166 | self.max_leverages = empty_cont.copy() |
||
167 | self.max_leverage = 0 |
||
168 | self.current_max = -np.inf |
||
169 | self.daily_treasury = pd.Series(index=self.trading_days) |
||
170 | self.treasury_period_return = np.nan |
||
171 | |||
172 | self.num_trading_days = 0 |
||
173 | |||
174 | def update(self, dt, algorithm_returns, benchmark_returns, leverage): |
||
175 | # Keep track of latest dt for use in to_dict and other methods |
||
176 | # that report current state. |
||
177 | self.latest_dt = dt |
||
178 | dt_loc = self.cont_index.get_loc(dt) |
||
179 | self.latest_dt_loc = dt_loc |
||
180 | |||
181 | self.algorithm_returns_cont[dt_loc] = algorithm_returns |
||
182 | self.algorithm_returns = self.algorithm_returns_cont[:dt_loc + 1] |
||
183 | |||
184 | self.num_trading_days = len(self.algorithm_returns) |
||
185 | |||
186 | if self.create_first_day_stats: |
||
187 | if len(self.algorithm_returns) == 1: |
||
188 | self.algorithm_returns = np.append(0.0, self.algorithm_returns) |
||
189 | |||
190 | self.algorithm_cumulative_returns[dt_loc] = \ |
||
191 | self.calculate_cumulative_returns(self.algorithm_returns) |
||
192 | |||
193 | algo_cumulative_returns_to_date = \ |
||
194 | self.algorithm_cumulative_returns[:dt_loc + 1] |
||
195 | |||
196 | self.mean_returns_cont[dt_loc] = \ |
||
197 | algo_cumulative_returns_to_date[dt_loc] / self.num_trading_days |
||
198 | |||
199 | self.mean_returns = self.mean_returns_cont[:dt_loc + 1] |
||
200 | |||
201 | self.annualized_mean_returns_cont[dt_loc] = \ |
||
202 | self.mean_returns_cont[dt_loc] * 252 |
||
203 | |||
204 | self.annualized_mean_returns = \ |
||
205 | self.annualized_mean_returns_cont[:dt_loc + 1] |
||
206 | |||
207 | if self.create_first_day_stats: |
||
208 | if len(self.mean_returns) == 1: |
||
209 | self.mean_returns = np.append(0.0, self.mean_returns) |
||
210 | self.annualized_mean_returns = np.append( |
||
211 | 0.0, self.annualized_mean_returns) |
||
212 | |||
213 | self.benchmark_returns_cont[dt_loc] = benchmark_returns |
||
214 | self.benchmark_returns = self.benchmark_returns_cont[:dt_loc + 1] |
||
215 | |||
216 | if self.create_first_day_stats: |
||
217 | if len(self.benchmark_returns) == 1: |
||
218 | self.benchmark_returns = np.append(0.0, self.benchmark_returns) |
||
219 | |||
220 | self.benchmark_cumulative_returns[dt_loc] = \ |
||
221 | self.calculate_cumulative_returns(self.benchmark_returns) |
||
222 | |||
223 | benchmark_cumulative_returns_to_date = \ |
||
224 | self.benchmark_cumulative_returns[:dt_loc + 1] |
||
225 | |||
226 | self.mean_benchmark_returns_cont[dt_loc] = \ |
||
227 | benchmark_cumulative_returns_to_date[dt_loc] / \ |
||
228 | self.num_trading_days |
||
229 | |||
230 | self.mean_benchmark_returns = self.mean_benchmark_returns_cont[:dt_loc] |
||
231 | |||
232 | self.annualized_mean_benchmark_returns_cont[dt_loc] = \ |
||
233 | self.mean_benchmark_returns_cont[dt_loc] * 252 |
||
234 | |||
235 | self.annualized_mean_benchmark_returns = \ |
||
236 | self.annualized_mean_benchmark_returns_cont[:dt_loc + 1] |
||
237 | |||
238 | self.algorithm_cumulative_leverages_cont[dt_loc] = leverage |
||
239 | self.algorithm_cumulative_leverages = \ |
||
240 | self.algorithm_cumulative_leverages_cont[:dt_loc + 1] |
||
241 | |||
242 | if self.create_first_day_stats: |
||
243 | if len(self.algorithm_cumulative_leverages) == 1: |
||
244 | self.algorithm_cumulative_leverages = np.append( |
||
245 | 0.0, |
||
246 | self.algorithm_cumulative_leverages) |
||
247 | |||
248 | if not len(self.algorithm_returns) and len(self.benchmark_returns): |
||
249 | message = "Mismatch between benchmark_returns ({bm_count}) and \ |
||
250 | algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" |
||
251 | message = message.format( |
||
252 | bm_count=len(self.benchmark_returns), |
||
253 | algo_count=len(self.algorithm_returns), |
||
254 | start=self.start_date, |
||
255 | end=self.end_date, |
||
256 | dt=dt |
||
257 | ) |
||
258 | raise Exception(message) |
||
259 | |||
260 | self.update_current_max() |
||
261 | self.benchmark_volatility[dt_loc] = \ |
||
262 | self.calculate_volatility(self.benchmark_returns) |
||
263 | self.algorithm_volatility[dt_loc] = \ |
||
264 | self.calculate_volatility(self.algorithm_returns) |
||
265 | |||
266 | # caching the treasury rates for the minutely case is a |
||
267 | # big speedup, because it avoids searching the treasury |
||
268 | # curves on every minute. |
||
269 | # In both minutely and daily, the daily curve is always used. |
||
270 | treasury_end = dt.replace(hour=0, minute=0) |
||
271 | if np.isnan(self.daily_treasury[treasury_end]): |
||
272 | treasury_period_return = choose_treasury( |
||
273 | self.treasury_curves, |
||
274 | self.start_date, |
||
275 | treasury_end, |
||
276 | self.env, |
||
277 | ) |
||
278 | self.daily_treasury[treasury_end] = treasury_period_return |
||
279 | self.treasury_period_return = self.daily_treasury[treasury_end] |
||
280 | self.excess_returns[dt_loc] = ( |
||
281 | self.algorithm_cumulative_returns[dt_loc] - |
||
282 | self.treasury_period_return) |
||
283 | self.beta[dt_loc] = self.calculate_beta() |
||
284 | self.alpha[dt_loc] = self.calculate_alpha() |
||
285 | self.sharpe[dt_loc] = self.calculate_sharpe() |
||
286 | self.downside_risk[dt_loc] = \ |
||
287 | self.calculate_downside_risk() |
||
288 | self.sortino[dt_loc] = self.calculate_sortino() |
||
289 | self.information[dt_loc] = self.calculate_information() |
||
290 | self.max_drawdown = self.calculate_max_drawdown() |
||
291 | self.max_drawdowns[dt_loc] = self.max_drawdown |
||
292 | self.max_leverage = self.calculate_max_leverage() |
||
293 | self.max_leverages[dt_loc] = self.max_leverage |
||
294 | |||
295 | def to_dict(self): |
||
296 | """ |
||
297 | Creates a dictionary representing the state of the risk report. |
||
298 | Returns a dict object of the form: |
||
299 | """ |
||
300 | dt = self.latest_dt |
||
301 | dt_loc = self.latest_dt_loc |
||
302 | period_label = dt.strftime("%Y-%m") |
||
303 | rval = { |
||
304 | 'trading_days': self.num_trading_days, |
||
305 | 'benchmark_volatility': |
||
306 | self.benchmark_volatility[dt_loc], |
||
307 | 'algo_volatility': |
||
308 | self.algorithm_volatility[dt_loc], |
||
309 | 'treasury_period_return': self.treasury_period_return, |
||
310 | # Though the two following keys say period return, |
||
311 | # they would be more accurately called the cumulative return. |
||
312 | # However, the keys need to stay the same, for now, for backwards |
||
313 | # compatibility with existing consumers. |
||
314 | 'algorithm_period_return': |
||
315 | self.algorithm_cumulative_returns[dt_loc], |
||
316 | 'benchmark_period_return': |
||
317 | self.benchmark_cumulative_returns[dt_loc], |
||
318 | 'beta': self.beta[dt_loc], |
||
319 | 'alpha': self.alpha[dt_loc], |
||
320 | 'sharpe': self.sharpe[dt_loc], |
||
321 | 'sortino': self.sortino[dt_loc], |
||
322 | 'information': self.information[dt_loc], |
||
323 | 'excess_return': self.excess_returns[dt_loc], |
||
324 | 'max_drawdown': self.max_drawdown, |
||
325 | 'max_leverage': self.max_leverage, |
||
326 | 'period_label': period_label |
||
327 | } |
||
328 | |||
329 | return {k: (None if check_entry(k, v) else v) |
||
330 | for k, v in iteritems(rval)} |
||
331 | |||
332 | def __repr__(self): |
||
333 | statements = [] |
||
334 | for metric in self.METRIC_NAMES: |
||
335 | value = getattr(self, metric)[-1] |
||
336 | if isinstance(value, list): |
||
337 | if len(value) == 0: |
||
338 | value = np.nan |
||
339 | else: |
||
340 | value = value[-1] |
||
341 | statements.append("{m}:{v}".format(m=metric, v=value)) |
||
342 | |||
343 | return '\n'.join(statements) |
||
344 | |||
345 | def calculate_cumulative_returns(self, returns): |
||
346 | return (1. + returns).prod() - 1 |
||
347 | |||
348 | def update_current_max(self): |
||
349 | if len(self.algorithm_cumulative_returns) == 0: |
||
350 | return |
||
351 | current_cumulative_return = \ |
||
352 | self.algorithm_cumulative_returns[self.latest_dt_loc] |
||
353 | if self.current_max < current_cumulative_return: |
||
354 | self.current_max = current_cumulative_return |
||
355 | |||
356 | def calculate_max_drawdown(self): |
||
357 | if len(self.algorithm_cumulative_returns) == 0: |
||
358 | return self.max_drawdown |
||
359 | |||
360 | # The drawdown is defined as: (high - low) / high |
||
361 | # The above factors out to: 1.0 - (low / high) |
||
362 | # |
||
363 | # Instead of explicitly always using the low, use the current total |
||
364 | # return value, and test that against the max drawdown, which will |
||
365 | # exceed the previous max_drawdown iff the current return is lower than |
||
366 | # the previous low in the current drawdown window. |
||
367 | cur_drawdown = 1.0 - ( |
||
368 | (1.0 + self.algorithm_cumulative_returns[self.latest_dt_loc]) |
||
369 | / |
||
370 | (1.0 + self.current_max)) |
||
371 | |||
372 | self.drawdowns[self.latest_dt_loc] = cur_drawdown |
||
373 | |||
374 | if self.max_drawdown < cur_drawdown: |
||
375 | return cur_drawdown |
||
376 | else: |
||
377 | return self.max_drawdown |
||
378 | |||
379 | def calculate_max_leverage(self): |
||
380 | # The leverage is defined as: the gross_exposure/net_liquidation |
||
381 | # gross_exposure = long_exposure + abs(short_exposure) |
||
382 | # net_liquidation = ending_cash + long_exposure + short_exposure |
||
383 | cur_leverage = self.algorithm_cumulative_leverages_cont[ |
||
384 | self.latest_dt_loc] |
||
385 | |||
386 | return max(cur_leverage, self.max_leverage) |
||
387 | |||
388 | def calculate_sharpe(self): |
||
389 | """ |
||
390 | http://en.wikipedia.org/wiki/Sharpe_ratio |
||
391 | """ |
||
392 | return sharpe_ratio( |
||
393 | self.algorithm_volatility[self.latest_dt_loc], |
||
394 | self.annualized_mean_returns_cont[self.latest_dt_loc], |
||
395 | self.daily_treasury[self.latest_dt.date()]) |
||
396 | |||
397 | def calculate_sortino(self): |
||
398 | """ |
||
399 | http://en.wikipedia.org/wiki/Sortino_ratio |
||
400 | """ |
||
401 | return sortino_ratio( |
||
402 | self.annualized_mean_returns_cont[self.latest_dt_loc], |
||
403 | self.daily_treasury[self.latest_dt.date()], |
||
404 | self.downside_risk[self.latest_dt_loc]) |
||
405 | |||
406 | def calculate_information(self): |
||
407 | """ |
||
408 | http://en.wikipedia.org/wiki/Information_ratio |
||
409 | """ |
||
410 | return information_ratio( |
||
411 | self.algorithm_volatility[self.latest_dt_loc], |
||
412 | self.annualized_mean_returns_cont[self.latest_dt_loc], |
||
413 | self.annualized_mean_benchmark_returns_cont[self.latest_dt_loc]) |
||
414 | |||
415 | def calculate_alpha(self): |
||
416 | """ |
||
417 | http://en.wikipedia.org/wiki/Alpha_(investment) |
||
418 | """ |
||
419 | return alpha( |
||
420 | self.annualized_mean_returns_cont[self.latest_dt_loc], |
||
421 | self.treasury_period_return, |
||
422 | self.annualized_mean_benchmark_returns_cont[self.latest_dt_loc], |
||
423 | self.beta[self.latest_dt_loc]) |
||
424 | |||
425 | def calculate_volatility(self, daily_returns): |
||
426 | if len(daily_returns) <= 1: |
||
427 | return 0.0 |
||
428 | return np.std(daily_returns, ddof=1) * math.sqrt(252) |
||
429 | |||
430 | def calculate_downside_risk(self): |
||
431 | return downside_risk(self.algorithm_returns, |
||
432 | self.mean_returns, |
||
433 | 252) |
||
434 | |||
435 | def calculate_beta(self): |
||
436 | """ |
||
437 | |||
438 | .. math:: |
||
439 | |||
440 | \\beta_a = \\frac{\mathrm{Cov}(r_a,r_p)}{\mathrm{Var}(r_p)} |
||
441 | |||
442 | http://en.wikipedia.org/wiki/Beta_(finance) |
||
443 | """ |
||
444 | # it doesn't make much sense to calculate beta for less than two |
||
445 | # values, so return none. |
||
446 | if len(self.algorithm_returns) < 2: |
||
447 | return 0.0 |
||
448 | |||
449 | returns_matrix = np.vstack([self.algorithm_returns, |
||
450 | self.benchmark_returns]) |
||
451 | C = np.cov(returns_matrix, ddof=1) |
||
452 | algorithm_covariance = C[0][1] |
||
453 | benchmark_variance = C[1][1] |
||
454 | beta = algorithm_covariance / benchmark_variance |
||
455 | |||
456 | return beta |
||
457 | |||
458 | def __getstate__(self): |
||
459 | state_dict = {k: v for k, v in iteritems(self.__dict__) |
||
460 | if not k.startswith('_')} |
||
461 | |||
462 | STATE_VERSION = 3 |
||
463 | state_dict[VERSION_LABEL] = STATE_VERSION |
||
464 | |||
465 | return state_dict |
||
466 | |||
467 | def __setstate__(self, state): |
||
468 | |||
469 | OLDEST_SUPPORTED_STATE = 3 |
||
470 | version = state.pop(VERSION_LABEL) |
||
471 | |||
472 | if version < OLDEST_SUPPORTED_STATE: |
||
473 | raise BaseException("RiskMetricsCumulative \ |
||
474 | saved state is too old.") |
||
475 | |||
476 | self.__dict__.update(state) |
||
477 |