|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import TYPE_CHECKING, List, Union |
| 3 | +from typing import TYPE_CHECKING, List, Union, cast |
4 | 4 |
|
5 | 5 | import numpy as np
|
6 | 6 | import pandas as pd
|
@@ -119,11 +119,16 @@ def _round_timedelta(value, _period=_data_period(index)):
|
119 | 119 | annual_trading_days = np.nan
|
120 | 120 | is_datetime_index = isinstance(index, pd.DatetimeIndex)
|
121 | 121 | if is_datetime_index:
|
122 |
| - day_returns = equity_df['Equity'].resample('D').last().dropna().pct_change() |
| 122 | + freq_days = cast(pd.Timedelta, _data_period(index)).days |
| 123 | + have_weekends = index.dayofweek.to_series().between(5, 6).mean() > 2 / 7 * .6 |
| 124 | + annual_trading_days = ( |
| 125 | + 52 if freq_days == 7 else |
| 126 | + 12 if freq_days == 31 else |
| 127 | + 1 if freq_days == 365 else |
| 128 | + (365 if have_weekends else 252)) |
| 129 | + freq = {7: 'W', 31: 'ME', 365: 'YE'}.get(freq_days, 'D') |
| 130 | + day_returns = equity_df['Equity'].resample(freq).last().dropna().pct_change() |
123 | 131 | gmean_day_return = geometric_mean(day_returns)
|
124 |
| - annual_trading_days = float( |
125 |
| - 365 if index.dayofweek.to_series().between(5, 6).mean() > 2/7 * .6 else |
126 |
| - 252) |
127 | 132 |
|
128 | 133 | # Annualized return and risk metrics are computed based on the (mostly correct)
|
129 | 134 | # assumption that the returns are compounded. See: https://dx.doi.org/10.2139/ssrn.3054517
|
|
0 commit comments