Completed
Pull Request — master (#858)
by Eddie
01:43
created

zipline.finance.performance.PerformanceTracker   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 458
Duplicated Lines 0 %
Metric Value
dl 0
loc 458
rs 8.439
wmc 47

23 Methods

Rating   Name   Duplication   Size   Complexity  
B __init__() 0 88 4
A process_transaction() 0 5 1
B handle_simulation_end() 0 29 1
A __getstate__() 0 9 3
B handle_market_close_daily() 0 29 1
B check_upcoming_dividends() 0 31 4
A check_asset_auto_closes() 0 15 2
B _handle_market_close() 0 48 5
A get_portfolio() 0 11 1
A __setstate__() 0 9 2
A perf_periods() 0 3 1
A handle_minute_close() 0 50 2
A copy_state_from() 0 9 2
A handle_splits() 0 5 2
A process_commission() 0 7 1
A to_dict() 0 14 1
B _to_dict() 0 39 4
A progress() 0 7 3
A get_account() 0 8 1
A process_close_position() 0 5 2
A set_date() 0 4 2
A process_order() 0 3 1
A __repr__() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like zipline.finance.performance.PerformanceTracker 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
#
2
# Copyright 2015 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
Performance Tracking
19
====================
20
21
    +-----------------+----------------------------------------------------+
22
    | key             | value                                              |
23
    +=================+====================================================+
24
    | period_start    | The beginning of the period to be tracked. datetime|
25
    |                 | in pytz.utc timezone. Will always be 0:00 on the   |
26
    |                 | date in UTC. The fact that the time may be on the  |
27
    |                 | prior day in the exchange's local time is ignored  |
28
    +-----------------+----------------------------------------------------+
29
    | period_end      | The end of the period to be tracked. datetime      |
30
    |                 | in pytz.utc timezone. Will always be 23:59 on the  |
31
    |                 | date in UTC. The fact that the time may be on the  |
32
    |                 | next day in the exchange's local time is ignored   |
33
    +-----------------+----------------------------------------------------+
34
    | progress        | percentage of test completed                       |
35
    +-----------------+----------------------------------------------------+
36
    | capital_base    | The initial capital assumed for this tracker.      |
37
    +-----------------+----------------------------------------------------+
38
    | cumulative_perf | A dictionary representing the cumulative           |
39
    |                 | performance through all the events delivered to    |
40
    |                 | this tracker. For details see the comments on      |
41
    |                 | :py:meth:`PerformancePeriod.to_dict`               |
42
    +-----------------+----------------------------------------------------+
43
    | todays_perf     | A dictionary representing the cumulative           |
44
    |                 | performance through all the events delivered to    |
45
    |                 | this tracker with datetime stamps between last_open|
46
    |                 | and last_close. For details see the comments on    |
47
    |                 | :py:meth:`PerformancePeriod.to_dict`               |
48
    |                 | TODO: adding this because we calculate it. May be  |
49
    |                 | overkill.                                          |
50
    +-----------------+----------------------------------------------------+
51
    | cumulative_risk | A dictionary representing the risk metrics         |
52
    | _metrics        | calculated based on the positions aggregated       |
53
    |                 | through all the events delivered to this tracker.  |
54
    |                 | For details look at the comments for               |
55
    |                 | :py:meth:`zipline.finance.risk.RiskMetrics.to_dict`|
56
    +-----------------+----------------------------------------------------+
57
58
"""
59
60
from __future__ import division
61
import logbook
62
from six import iteritems
63
from datetime import datetime
64
65
import pandas as pd
66
from pandas.tseries.tools import normalize_date
67
from zipline.finance.performance.period import PerformancePeriod
68
69
import zipline.finance.risk as risk
70
71
from zipline.utils.serialization_utils import (
72
    VERSION_LABEL
73
)
74
from . position_tracker import PositionTracker
75
76
log = logbook.Logger('Performance')
77
78
79
class PerformanceTracker(object):
80
    """
81
    Tracks the performance of the algorithm.
82
    """
83
    def __init__(self, sim_params, env, data_portal):
84
        self.sim_params = sim_params
85
        self.env = env
86
87
        self.period_start = self.sim_params.period_start
88
        self.period_end = self.sim_params.period_end
89
        self.last_close = self.sim_params.last_close
90
        first_open = self.sim_params.first_open.tz_convert(
91
            self.env.exchange_tz
92
        )
93
        self.day = pd.Timestamp(datetime(first_open.year, first_open.month,
94
                                         first_open.day), tz='UTC')
95
        self.market_open, self.market_close = env.get_open_and_close(self.day)
96
        self.total_days = self.sim_params.days_in_period
97
        self.capital_base = self.sim_params.capital_base
98
        self.emission_rate = sim_params.emission_rate
99
100
        all_trading_days = env.trading_days
101
        mask = ((all_trading_days >= normalize_date(self.period_start)) &
102
                (all_trading_days <= normalize_date(self.period_end)))
103
104
        self.trading_days = all_trading_days[mask]
105
106
        self._data_portal = data_portal
107
        if data_portal is not None:
108
            self._adjustment_reader = data_portal._adjustment_reader
109
        else:
110
            self._adjustment_reader = None
111
112
        self.position_tracker = PositionTracker(asset_finder=env.asset_finder,
113
                                                data_portal=data_portal)
114
115
        if self.emission_rate == 'daily':
116
            self.all_benchmark_returns = pd.Series(
117
                index=self.trading_days)
118
            self.cumulative_risk_metrics = \
119
                risk.RiskMetricsCumulative(self.sim_params, self.env)
120
        elif self.emission_rate == 'minute':
121
            self.all_benchmark_returns = pd.Series(index=pd.date_range(
122
                self.sim_params.first_open, self.sim_params.last_close,
123
                freq='Min'))
124
125
            self.cumulative_risk_metrics = \
126
                risk.RiskMetricsCumulative(self.sim_params, self.env,
127
                                           create_first_day_stats=True)
128
129
        # this performance period will span the entire simulation from
130
        # inception.
131
        self.cumulative_performance = PerformancePeriod(
132
            # initial cash is your capital base.
133
            starting_cash=self.capital_base,
134
            # the cumulative period will be calculated over the entire test.
135
            period_open=self.period_start,
136
            period_close=self.period_end,
137
            # don't save the transactions for the cumulative
138
            # period
139
            keep_transactions=False,
140
            keep_orders=False,
141
            # don't serialize positions for cumulative period
142
            serialize_positions=False,
143
            asset_finder=self.env.asset_finder,
144
            name="Cumulative"
145
        )
146
147
        # this performance period will span just the current market day
148
        self.todays_performance = PerformancePeriod(
149
            # initial cash is your capital base.
150
            starting_cash=self.capital_base,
151
            # the daily period will be calculated for the market day
152
            period_open=self.market_open,
153
            period_close=self.market_close,
154
            keep_transactions=True,
155
            keep_orders=True,
156
            serialize_positions=True,
157
            asset_finder=self.env.asset_finder,
158
            name="Daily"
159
        )
160
161
        self.saved_dt = self.period_start
162
        # one indexed so that we reach 100%
163
        self.day_count = 0.0
164
        self.txn_count = 0
165
166
        self.account_needs_update = True
167
        self._account = None
168
169
        self._perf_periods = [self.cumulative_performance,
170
                              self.todays_performance]
171
172
    @property
173
    def perf_periods(self):
174
        return self._perf_periods
175
176
    def __repr__(self):
177
        return "%s(%r)" % (
178
            self.__class__.__name__,
179
            {'simulation parameters': self.sim_params})
180
181
    @property
182
    def progress(self):
183
        if self.emission_rate == 'minute':
184
            # Fake a value
185
            return 1.0
186
        elif self.emission_rate == 'daily':
187
            return self.day_count / self.total_days
188
189
    def set_date(self, date):
190
        if self.emission_rate == 'minute':
191
            self.saved_dt = date
192
            self.todays_performance.period_close = self.saved_dt
193
194
    def get_portfolio(self, dt):
195
        position_tracker = self.position_tracker
196
        position_tracker.sync_last_sale_prices(dt)
197
        pos_stats = position_tracker.stats()
198
        period_stats = self.cumulative_performance.stats(
199
            position_tracker.positions, pos_stats, self._data_portal)
200
        return self.cumulative_performance.as_portfolio(
201
            pos_stats,
202
            period_stats,
203
            position_tracker,
204
            dt)
205
206
    def get_account(self, dt):
207
        self.position_tracker.sync_last_sale_prices(dt)
208
        pos_stats = self.position_tracker.stats()
209
        period_stats = self.cumulative_performance.stats(
210
            self.position_tracker.positions, pos_stats, self._data_portal)
211
        self._account = self.cumulative_performance.as_account(
212
            pos_stats, period_stats)
213
        return self._account
214
215
    def to_dict(self, emission_type=None):
216
        """
217
        Wrapper for serialization compatibility.
218
        """
219
        pos_stats = self.position_tracker.stats()
220
        cumulative_stats = self.cumulative_performance.stats(
221
            self.position_tracker.positions, pos_stats, self._data_portal)
222
        todays_stats = self.todays_performance.stats(
223
            self.position_tracker.positions, pos_stats, self._data_portal)
224
225
        return self._to_dict(pos_stats,
226
                             cumulative_stats,
227
                             todays_stats,
228
                             emission_type)
229
230
    def _to_dict(self, pos_stats, cumulative_stats, todays_stats,
231
                 emission_type=None):
232
        """
233
        Creates a dictionary representing the state of this tracker.
234
        Returns a dict object of the form described in header comments.
235
236
        Use this method internally, when stats are available.
237
        """
238
        # Default to the emission rate of this tracker if no type is provided
239
        if emission_type is None:
240
            emission_type = self.emission_rate
241
242
        position_tracker = self.position_tracker
243
244
        _dict = {
245
            'period_start': self.period_start,
246
            'period_end': self.period_end,
247
            'capital_base': self.capital_base,
248
            'cumulative_perf': self.cumulative_performance.to_dict(
249
                pos_stats, cumulative_stats, position_tracker,
250
            ),
251
            'progress': self.progress,
252
            'cumulative_risk_metrics': self.cumulative_risk_metrics.to_dict()
253
        }
254
        if emission_type == 'daily':
255
            _dict['daily_perf'] = self.todays_performance.to_dict(
256
                pos_stats,
257
                todays_stats,
258
                position_tracker)
259
        elif emission_type == 'minute':
260
            _dict['minute_perf'] = self.todays_performance.to_dict(
261
                pos_stats,
262
                todays_stats,
263
                position_tracker,
264
                self.saved_dt)
265
        else:
266
            raise ValueError("Invalid emission type: %s" % emission_type)
267
268
        return _dict
269
270
    def copy_state_from(self, other_perf_tracker):
271
        self.all_benchmark_returns = other_perf_tracker.all_benchmark_returns
272
273
        if other_perf_tracker.position_tracker:
274
            self.position_tracker._unpaid_dividends = \
275
                other_perf_tracker.position_tracker._unpaid_dividends
276
277
            self.position_tracker._unpaid_stock_dividends = \
278
                other_perf_tracker.position_tracker._unpaid_stock_dividends
279
280
    def process_transaction(self, transaction):
281
        self.txn_count += 1
282
        self.position_tracker.execute_transaction(transaction)
283
        self.cumulative_performance.handle_execution(transaction)
284
        self.todays_performance.handle_execution(transaction)
285
286
    def handle_splits(self, splits):
287
        leftover_cash = self.position_tracker.handle_splits(splits)
288
        if leftover_cash > 0:
289
            self.cumulative_performance.handle_cash_payment(leftover_cash)
290
            self.todays_performance.handle_cash_payment(leftover_cash)
291
292
    def process_order(self, event):
293
        self.cumulative_performance.record_order(event)
294
        self.todays_performance.record_order(event)
295
296
    def process_commission(self, commission):
297
        sid = commission["sid"]
298
        cost = commission["cost"]
299
300
        self.position_tracker.handle_commission(sid, cost)
301
        self.cumulative_performance.handle_commission(cost)
302
        self.todays_performance.handle_commission(cost)
303
304
    def process_close_position(self, event):
305
        txn = self.position_tracker.\
306
            maybe_create_close_position_transaction(event)
307
        if txn:
308
            self.process_transaction(txn)
309
310
    def check_upcoming_dividends(self, next_trading_day):
311
        """
312
        Check if we currently own any stocks with dividends whose ex_date is
313
        the next trading day.  Track how much we should be payed on those
314
        dividends' pay dates.
315
316
        Then check if we are owed cash/stock for any dividends whose pay date
317
        is the next trading day.  Apply all such benefits, then recalculate
318
        performance.
319
        """
320
        if self._adjustment_reader is None:
321
            return
322
        position_tracker = self.position_tracker
323
        held_sids = set(position_tracker.positions)
324
        # Dividends whose ex_date is the next trading day.  We need to check if
325
        # we own any of these stocks so we know to pay them out when the pay
326
        # date comes.
327
        if held_sids:
328
            dividends_earnable = self._adjustment_reader.\
329
                get_dividends_with_ex_date(held_sids, next_trading_day)
330
            stock_dividends = self._adjustment_reader.\
331
                get_stock_dividends_with_ex_date(held_sids, next_trading_day)
332
            position_tracker.earn_dividends(dividends_earnable,
333
                                            stock_dividends)
334
335
        net_cash_payment = position_tracker.pay_dividends(next_trading_day)
336
        if not net_cash_payment:
337
            return
338
339
        self.cumulative_performance.handle_dividends_paid(net_cash_payment)
340
        self.todays_performance.handle_dividends_paid(net_cash_payment)
341
342
    def check_asset_auto_closes(self, next_trading_day):
343
        """
344
        Check if the position tracker currently owns any Assets with an
345
        auto-close date that is the next trading day.  Close those positions.
346
347
        Parameters
348
        ----------
349
        next_trading_day : pandas.Timestamp
350
            The next trading day of the simulation
351
        """
352
        auto_close_events = self.position_tracker.auto_close_position_events(
353
            next_trading_day=next_trading_day
354
        )
355
        for event in auto_close_events:
356
            self.process_close_position(event)
357
358
    def handle_minute_close(self, dt):
359
        """
360
        Handles the close of the given minute. This includes handling
361
        market-close functions if the given minute is the end of the market
362
        day.
363
364
        Parameters
365
        __________
366
        dt : Timestamp
367
            The minute that is ending
368
369
        Returns
370
        _______
371
        (dict, dict/None)
372
            A tuple of the minute perf packet and daily perf packet.
373
            If the market day has not ended, the daily perf packet is None.
374
        """
375
        todays_date = normalize_date(dt)
376
        account = self.get_account(dt)
377
378
        bench_returns = self.all_benchmark_returns.loc[todays_date:dt]
379
        # cumulative returns
380
        bench_since_open = (1. + bench_returns).prod() - 1
381
382
        self.position_tracker.sync_last_sale_prices(dt)
383
        pos_stats = self.position_tracker.stats()
384
        cumulative_stats = self.cumulative_performance.stats(
385
            self.position_tracker.positions, pos_stats, self._data_portal
386
        )
387
        todays_stats = self.todays_performance.stats(
388
            self.position_tracker.positions, pos_stats, self._data_portal
389
        )
390
        self.cumulative_risk_metrics.update(todays_date,
391
                                            todays_stats.returns,
392
                                            bench_since_open,
393
                                            account)
394
395
        minute_packet = self._to_dict(pos_stats,
396
                                      cumulative_stats,
397
                                      todays_stats,
398
                                      emission_type='minute')
399
400
        if dt == self.market_close:
401
            # if this is the last minute of the day, we also want to
402
            # emit a daily packet.
403
            return minute_packet, self._handle_market_close(todays_date,
404
                                                            pos_stats,
405
                                                            todays_stats)
406
        else:
407
            return minute_packet, None
408
409
    def handle_market_close_daily(self, dt):
410
        """
411
        Function called after handle_data when running with daily emission
412
        rate.
413
        """
414
        completed_date = normalize_date(dt)
415
416
        self.position_tracker.sync_last_sale_prices(dt)
417
418
        pos_stats = self.position_tracker.stats()
419
        todays_stats = self.todays_performance.stats(
420
            self.position_tracker.positions, pos_stats, self._data_portal
421
        )
422
        account = self.get_account(completed_date)
423
424
        # update risk metrics for cumulative performance
425
        benchmark_value = self.all_benchmark_returns[completed_date]
426
427
        self.cumulative_risk_metrics.update(
428
            completed_date,
429
            todays_stats.returns,
430
            benchmark_value,
431
            account)
432
433
        daily_packet = self._handle_market_close(completed_date,
434
                                                 pos_stats,
435
                                                 todays_stats)
436
437
        return daily_packet
438
439
    def _handle_market_close(self, completed_date, pos_stats, todays_stats):
440
441
        # increment the day counter before we move markers forward.
442
        self.day_count += 1.0
443
444
        # Get the next trading day and, if it is past the bounds of this
445
        # simulation, return the daily perf packet
446
        next_trading_day = self.env.next_trading_day(completed_date)
447
448
        # Check if any assets need to be auto-closed before generating today's
449
        # perf period
450
        if next_trading_day:
451
            self.check_asset_auto_closes(next_trading_day=next_trading_day)
452
453
        # Take a snapshot of our current performance to return to the
454
        # browser.
455
        cumulative_stats = self.cumulative_performance.stats(
456
            self.position_tracker.positions,
457
            pos_stats, self._data_portal)
458
        daily_update = self._to_dict(pos_stats,
459
                                     cumulative_stats,
460
                                     todays_stats,
461
                                     emission_type='daily')
462
463
        # On the last day of the test, don't create tomorrow's performance
464
        # period.  We may not be able to find the next trading day if we're at
465
        # the end of our historical data
466
        if self.market_close >= self.last_close:
467
            return daily_update
468
469
        # move the market day markers forward
470
        self.market_open, self.market_close = \
471
            self.env.next_open_and_close(self.day)
472
        self.day = self.env.next_trading_day(self.day)
473
474
        # Roll over positions to current day.
475
        self.todays_performance.rollover(pos_stats, todays_stats)
476
        self.todays_performance.period_open = self.market_open
477
        self.todays_performance.period_close = self.market_close
478
479
        # If the next trading day is irrelevant, then return the daily packet
480
        if (next_trading_day is None) or (next_trading_day >= self.last_close):
481
            return daily_update
482
483
        # Check for any dividends and auto-closes, then return the daily perf
484
        # packet
485
        self.check_upcoming_dividends(next_trading_day=next_trading_day)
486
        return daily_update
487
488
    def handle_simulation_end(self):
489
        """
490
        When the simulation is complete, run the full period risk report
491
        and send it out on the results socket.
492
        """
493
494
        log_msg = "Simulated {n} trading days out of {m}."
495
        log.info(log_msg.format(n=int(self.day_count), m=self.total_days))
496
        log.info("first open: {d}".format(
497
            d=self.sim_params.first_open))
498
        log.info("last close: {d}".format(
499
            d=self.sim_params.last_close))
500
501
        bms = pd.Series(
502
            index=self.cumulative_risk_metrics.cont_index,
503
            data=self.cumulative_risk_metrics.benchmark_returns_cont)
504
        ars = pd.Series(
505
            index=self.cumulative_risk_metrics.cont_index,
506
            data=self.cumulative_risk_metrics.algorithm_returns_cont)
507
        acl = self.cumulative_risk_metrics.algorithm_cumulative_leverages
508
        self.risk_report = risk.RiskReport(
509
            ars,
510
            self.sim_params,
511
            benchmark_returns=bms,
512
            algorithm_leverages=acl,
513
            env=self.env)
514
515
        risk_dict = self.risk_report.to_dict()
516
        return risk_dict
517
518
    def __getstate__(self):
519
        state_dict = \
520
            {k: v for k, v in iteritems(self.__dict__)
521
                if not k.startswith('_')}
522
523
        STATE_VERSION = 4
524
        state_dict[VERSION_LABEL] = STATE_VERSION
525
526
        return state_dict
527
528
    def __setstate__(self, state):
529
530
        OLDEST_SUPPORTED_STATE = 4
531
        version = state.pop(VERSION_LABEL)
532
533
        if version < OLDEST_SUPPORTED_STATE:
534
            raise BaseException("PerformanceTracker saved state is too old.")
535
536
        self.__dict__.update(state)
537