Completed
Push — master ( c4f3f5...2b4ffe )
by
unknown
01:25
created

update_last_sale()   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 19
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
from __future__ import division
17
18
import logbook
19
import numpy as np
20
import pandas as pd
21
from pandas.lib import checknull
22
from collections import namedtuple
23
try:
24
    # optional cython based OrderedDict
25
    from cyordereddict import OrderedDict
26
except ImportError:
27
    from collections import OrderedDict
28
from six import iteritems, itervalues
29
30
from zipline.protocol import Event, DATASOURCE_TYPE
31
from zipline.finance.transaction import Transaction
32
from zipline.utils.serialization_utils import (
33
    VERSION_LABEL
34
)
35
36
import zipline.protocol as zp
37
from zipline.assets import (
38
    Equity, Future
39
)
40
from zipline.errors import PositionTrackerMissingAssetFinder
41
from . position import positiondict
42
43
log = logbook.Logger('Performance')
44
45
46
PositionStats = namedtuple('PositionStats',
47
                           ['net_exposure',
48
                            'gross_value',
49
                            'gross_exposure',
50
                            'short_value',
51
                            'short_exposure',
52
                            'shorts_count',
53
                            'long_value',
54
                            'long_exposure',
55
                            'longs_count',
56
                            'net_value'])
57
58
59
def calc_position_values(amounts,
60
                         last_sale_prices,
61
                         value_multipliers):
62
    iter_amount_price_multiplier = zip(
63
        amounts,
64
        last_sale_prices,
65
        itervalues(value_multipliers),
66
    )
67
    return [
68
        price * amount * multiplier for
69
        price, amount, multiplier in iter_amount_price_multiplier
70
    ]
71
72
73
def calc_net(values):
74
    # Returns 0.0 if there are no values.
75
    return sum(values, np.float64())
76
77
78
def calc_position_exposures(amounts,
79
                            last_sale_prices,
80
                            exposure_multipliers):
81
    iter_amount_price_multiplier = zip(
82
        amounts,
83
        last_sale_prices,
84
        itervalues(exposure_multipliers),
85
    )
86
    return [
87
        price * amount * multiplier for
88
        price, amount, multiplier in iter_amount_price_multiplier
89
    ]
90
91
92
def calc_long_value(position_values):
93
    return sum(i for i in position_values if i > 0)
94
95
96
def calc_short_value(position_values):
97
    return sum(i for i in position_values if i < 0)
98
99
100
def calc_long_exposure(position_exposures):
101
    return sum(i for i in position_exposures if i > 0)
102
103
104
def calc_short_exposure(position_exposures):
105
    return sum(i for i in position_exposures if i < 0)
106
107
108
def calc_longs_count(position_exposures):
109
    return sum(1 for i in position_exposures if i > 0)
110
111
112
def calc_shorts_count(position_exposures):
113
    return sum(1 for i in position_exposures if i < 0)
114
115
116
def calc_gross_exposure(long_exposure, short_exposure):
117
    return long_exposure + abs(short_exposure)
118
119
120
def calc_gross_value(long_value, short_value):
121
    return long_value + abs(short_value)
122
123
124
class PositionTracker(object):
125
126
    def __init__(self, asset_finder):
127
        self.asset_finder = asset_finder
128
129
        # sid => position object
130
        self.positions = positiondict()
131
        # Arrays for quick calculations of positions value
132
        self._position_value_multipliers = OrderedDict()
133
        self._position_exposure_multipliers = OrderedDict()
134
        self._position_payout_multipliers = OrderedDict()
135
        self._unpaid_dividends = pd.DataFrame(
136
            columns=zp.DIVIDEND_PAYMENT_FIELDS,
137
        )
138
        self._positions_store = zp.Positions()
139
140
        # Dict, keyed on dates, that contains lists of close position events
141
        # for any Assets in this tracker's positions
142
        self._auto_close_position_sids = {}
143
144
    def _update_asset(self, sid):
145
        try:
146
            self._position_value_multipliers[sid]
147
            self._position_exposure_multipliers[sid]
148
            self._position_payout_multipliers[sid]
149
        except KeyError:
150
            # Check if there is an AssetFinder
151
            if self.asset_finder is None:
152
                raise PositionTrackerMissingAssetFinder()
153
154
            # Collect the value multipliers from applicable sids
155
            asset = self.asset_finder.retrieve_asset(sid)
156
            if isinstance(asset, Equity):
157
                self._position_value_multipliers[sid] = 1
158
                self._position_exposure_multipliers[sid] = 1
159
                self._position_payout_multipliers[sid] = 0
160
            if isinstance(asset, Future):
161
                self._position_value_multipliers[sid] = 0
162
                self._position_exposure_multipliers[sid] = \
163
                    asset.contract_multiplier
164
                self._position_payout_multipliers[sid] = \
165
                    asset.contract_multiplier
166
                # Futures auto-close timing is controlled by the Future's
167
                # auto_close_date property
168
                self._insert_auto_close_position_date(
169
                    dt=asset.auto_close_date,
170
                    sid=sid
171
                )
172
173
    def _insert_auto_close_position_date(self, dt, sid):
174
        """
175
        Inserts the given SID in to the list of positions to be auto-closed by
176
        the given dt.
177
178
        Parameters
179
        ----------
180
        dt : pandas.Timestamp
181
            The date before-which the given SID will be auto-closed
182
        sid : int
183
            The SID of the Asset to be auto-closed
184
        """
185
        if dt is not None:
186
            self._auto_close_position_sids.setdefault(dt, set()).add(sid)
187
188
    def auto_close_position_events(self, next_trading_day):
189
        """
190
        Generates CLOSE_POSITION events for any SIDs whose auto-close date is
191
        before or equal to the given date.
192
193
        Parameters
194
        ----------
195
        next_trading_day : pandas.Timestamp
196
            The time before-which certain Assets need to be closed
197
198
        Yields
199
        ------
200
        Event
201
            A close position event for any sids that should be closed before
202
            the next_trading_day parameter
203
        """
204
        past_asset_end_dates = set()
205
206
        # Check the auto_close_position_dates dict for SIDs to close
207
        for date, sids in self._auto_close_position_sids.items():
208
            if date > next_trading_day:
209
                continue
210
            past_asset_end_dates.add(date)
211
212
            for sid in sids:
213
                # Yield a CLOSE_POSITION event
214
                event = Event({
215
                    'dt': date,
216
                    'type': DATASOURCE_TYPE.CLOSE_POSITION,
217
                    'sid': sid,
218
                })
219
                yield event
220
221
        # Clear out past dates
222
        while past_asset_end_dates:
223
            self._auto_close_position_sids.pop(past_asset_end_dates.pop())
224
225
    def update_last_sale(self, event):
226
        # NOTE, PerformanceTracker already vetted as TRADE type
227
        sid = event.sid
228
        if sid not in self.positions:
229
            return 0
230
231
        price = event.price
232
233
        if checknull(price):
234
            return 0
235
236
        pos = self.positions[sid]
237
        old_price = pos.last_sale_price
238
        pos.last_sale_date = event.dt
239
        pos.last_sale_price = price
240
241
        # Calculate cash adjustment on assets with multipliers
242
        return ((price - old_price) * self._position_payout_multipliers[sid]
243
                * pos.amount)
244
245
    def update_positions(self, positions):
246
        # update positions in batch
247
        self.positions.update(positions)
248
        for sid, pos in iteritems(positions):
249
            self._update_asset(sid)
250
251
    def update_position(self, sid, amount=None, last_sale_price=None,
252
                        last_sale_date=None, cost_basis=None):
253
        pos = self.positions[sid]
254
255
        if amount is not None:
256
            pos.amount = amount
257
            self._update_asset(sid=sid)
258
        if last_sale_price is not None:
259
            pos.last_sale_price = last_sale_price
260
        if last_sale_date is not None:
261
            pos.last_sale_date = last_sale_date
262
        if cost_basis is not None:
263
            pos.cost_basis = cost_basis
264
265
    def execute_transaction(self, txn):
266
        # Update Position
267
        # ----------------
268
        sid = txn.sid
269
        position = self.positions[sid]
270
        position.update(txn)
271
        self._update_asset(sid)
272
273
    def handle_commission(self, sid, cost):
274
        # Adjust the cost basis of the stock if we own it
275
        if sid in self.positions:
276
            self.positions[sid].adjust_commission_cost_basis(sid, cost)
277
278
    def handle_split(self, split):
279
        if split.sid in self.positions:
280
            # Make the position object handle the split. It returns the
281
            # leftover cash from a fractional share, if there is any.
282
            position = self.positions[split.sid]
283
            leftover_cash = position.handle_split(split.sid, split.ratio)
284
            self._update_asset(split.sid)
285
            return leftover_cash
286
287
    def _maybe_earn_dividend(self, dividend):
288
        """
289
        Take a historical dividend record and return a Series with fields in
290
        zipline.protocol.DIVIDEND_FIELDS (plus an 'id' field) representing
291
        the cash/stock amount we are owed when the dividend is paid.
292
        """
293
        if dividend['sid'] in self.positions:
294
            return self.positions[dividend['sid']].earn_dividend(dividend)
295
        else:
296
            return zp.dividend_payment()
297
298
    def earn_dividends(self, dividend_frame):
299
        """
300
        Given a frame of dividends whose ex_dates are all the next trading day,
301
        calculate and store the cash and/or stock payments to be paid on each
302
        dividend's pay date.
303
        """
304
        earned = dividend_frame.apply(self._maybe_earn_dividend, axis=1)\
305
                               .dropna(how='all')
306
        if len(earned) > 0:
307
            # Store the earned dividends so that they can be paid on the
308
            # dividends' pay_dates.
309
            self._unpaid_dividends = pd.concat(
310
                [self._unpaid_dividends, earned],
311
            )
312
313
    def _maybe_pay_dividend(self, dividend):
314
        """
315
        Take a historical dividend record, look up any stored record of
316
        cash/stock we are owed for that dividend, and return a Series
317
        with fields drawn from zipline.protocol.DIVIDEND_PAYMENT_FIELDS.
318
        """
319
        try:
320
            unpaid_dividend = self._unpaid_dividends.loc[dividend['id']]
321
            return unpaid_dividend
322
        except KeyError:
323
            return zp.dividend_payment()
324
325
    def pay_dividends(self, dividend_frame):
326
        """
327
        Given a frame of dividends whose pay_dates are all the next trading
328
        day, grant the cash and/or stock payments that were calculated on the
329
        given dividends' ex dates.
330
        """
331
        payments = dividend_frame.apply(self._maybe_pay_dividend, axis=1)\
332
                                 .dropna(how='all')
333
334
        # Mark these dividends as paid by dropping them from our unpaid
335
        # table.
336
        self._unpaid_dividends.drop(payments.index)
337
338
        # Add stock for any stock dividends paid.  Again, the values here may
339
        # be negative in the case of short positions.
340
        stock_payments = payments[payments['payment_sid'].notnull()]
341
        for _, row in stock_payments.iterrows():
342
            stock = row['payment_sid']
343
            share_count = row['share_count']
344
            # note we create a Position for stock dividend if we don't
345
            # already own the asset
346
            position = self.positions[stock]
347
348
            position.amount += share_count
349
            self._update_asset(stock)
350
351
        # Add cash equal to the net cash payed from all dividends.  Note that
352
        # "negative cash" is effectively paid if we're short an asset,
353
        # representing the fact that we're required to reimburse the owner of
354
        # the stock for any dividends paid while borrowing.
355
        net_cash_payment = payments['cash_amount'].fillna(0).sum()
356
        return net_cash_payment
357
358
    def maybe_create_close_position_transaction(self, event):
359
        try:
360
            pos = self.positions[event.sid]
361
            amount = pos.amount
362
            if amount == 0:
363
                return None
364
        except KeyError:
365
            return None
366
        if 'price' in event:
367
            price = event.price
368
        else:
369
            price = pos.last_sale_price
370
        txn = Transaction(
371
            sid=event.sid,
372
            amount=(-1 * pos.amount),
373
            dt=event.dt,
374
            price=price,
375
            commission=0,
376
            order_id=0
377
        )
378
        return txn
379
380
    def get_positions(self):
381
382
        positions = self._positions_store
383
384
        for sid, pos in iteritems(self.positions):
385
386
            if pos.amount == 0:
387
                # Clear out the position if it has become empty since the last
388
                # time get_positions was called.  Catching the KeyError is
389
                # faster than checking `if sid in positions`, and this can be
390
                # potentially called in a tight inner loop.
391
                try:
392
                    del positions[sid]
393
                except KeyError:
394
                    pass
395
                continue
396
397
            # Note that this will create a position if we don't currently have
398
            # an entry
399
            position = positions[sid]
400
            position.amount = pos.amount
401
            position.cost_basis = pos.cost_basis
402
            position.last_sale_price = pos.last_sale_price
403
        return positions
404
405
    def get_positions_list(self):
406
        positions = []
407
        for sid, pos in iteritems(self.positions):
408
            if pos.amount != 0:
409
                positions.append(pos.to_dict())
410
        return positions
411
412
    def stats(self):
413
        amounts = []
414
        last_sale_prices = []
415
        for pos in itervalues(self.positions):
416
            amounts.append(pos.amount)
417
            last_sale_prices.append(pos.last_sale_price)
418
419
        position_values = calc_position_values(
420
            amounts,
421
            last_sale_prices,
422
            self._position_value_multipliers
423
        )
424
425
        position_exposures = calc_position_exposures(
426
            amounts,
427
            last_sale_prices,
428
            self._position_exposure_multipliers
429
        )
430
431
        long_value = calc_long_value(position_values)
432
        short_value = calc_short_value(position_values)
433
        gross_value = calc_gross_value(long_value, short_value)
434
        long_exposure = calc_long_exposure(position_exposures)
435
        short_exposure = calc_short_exposure(position_exposures)
436
        gross_exposure = calc_gross_exposure(long_exposure, short_exposure)
437
        net_exposure = calc_net(position_exposures)
438
        longs_count = calc_longs_count(position_exposures)
439
        shorts_count = calc_shorts_count(position_exposures)
440
        net_value = calc_net(position_values)
441
442
        return PositionStats(
443
            long_value=long_value,
444
            gross_value=gross_value,
445
            short_value=short_value,
446
            long_exposure=long_exposure,
447
            short_exposure=short_exposure,
448
            gross_exposure=gross_exposure,
449
            net_exposure=net_exposure,
450
            longs_count=longs_count,
451
            shorts_count=shorts_count,
452
            net_value=net_value
453
        )
454
455
    def __getstate__(self):
456
        state_dict = {}
457
458
        state_dict['asset_finder'] = self.asset_finder
459
        state_dict['positions'] = dict(self.positions)
460
        state_dict['unpaid_dividends'] = self._unpaid_dividends
461
        state_dict['auto_close_position_sids'] = self._auto_close_position_sids
462
463
        STATE_VERSION = 3
464
        state_dict[VERSION_LABEL] = STATE_VERSION
465
        return state_dict
466
467
    def __setstate__(self, state):
468
        OLDEST_SUPPORTED_STATE = 3
469
        version = state.pop(VERSION_LABEL)
470
471
        if version < OLDEST_SUPPORTED_STATE:
472
            raise BaseException("PositionTracker saved state is too old.")
473
474
        self.asset_finder = state['asset_finder']
475
        self.positions = positiondict()
476
        # note that positions_store is temporary and gets regened from
477
        # .positions
478
        self._positions_store = zp.Positions()
479
480
        self._unpaid_dividends = state['unpaid_dividends']
481
        self._auto_close_position_sids = state['auto_close_position_sids']
482
483
        # Arrays for quick calculations of positions value
484
        self._position_value_multipliers = OrderedDict()
485
        self._position_exposure_multipliers = OrderedDict()
486
        self._position_payout_multipliers = OrderedDict()
487
488
        # Update positions is called without a finder
489
        self.update_positions(state['positions'])
490