Completed
Pull Request — master (#877)
by Thomas
01:28
created

zipline.finance.risk.sharpe_ratio()   B

Complexity

Conditions 6

Size

Total Lines 25

Duplication

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