| Total Complexity | 52 |
| Total Lines | 366 |
| Duplicated Lines | 0 % |
Complex classes like zipline.finance.performance.PositionTracker 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 | # |
||
| 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 |