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

to_dict()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

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