MAINT: PR feedback.

This commit is contained in:
Jean Bredeche
2017-04-24 15:41:23 -04:00
parent 5b8b2f68bc
commit 7196e1e498
6 changed files with 17 additions and 27 deletions
+2 -3
View File
@@ -380,9 +380,8 @@ class FinanceTestCase(WithLogger,
# asset 133 so it should be ignored.
blotter.process_splits([(asset133, 0.5), (asset2, 0.3333)])
for sid in [1, 2]:
order_lists = \
blotter.open_orders[self.asset_finder.retrieve_asset(sid)]
for asset in [asset1, asset2]:
order_lists = blotter.open_orders[asset]
self.assertIsNotNone(order_lists)
self.assertEqual(1, len(order_lists))
+2
View File
@@ -87,8 +87,10 @@ class Order(object):
if self.broker_order_id is None:
del dct['broker_order_id']
# Adding 'sid' for backwards compatibility with downstream consumers.
dct['sid'] = self.asset
dct['status'] = self.status
return dct
@property
+5 -19
View File
@@ -183,10 +183,6 @@ class PerformancePeriod(object):
self._account_store = zp.Account()
self.serialize_positions = serialize_positions
# This dict contains the known cash flow multipliers for assets and is
# keyed on asset
self._execution_cash_flow_multipliers = {}
_position_tracker = None
def initialize(self, starting_cash, starting_value, starting_exposure):
@@ -383,25 +379,15 @@ class PerformancePeriod(object):
except KeyError:
self.processed_transactions[txn.dt] = [txn]
def _calculate_execution_cash_flow(self, txn):
@staticmethod
def _calculate_execution_cash_flow(txn):
"""
Calculates the cash flow from executing the given transaction
"""
# Check if the multiplier is cached. If it is not, look up the asset
# and cache the multiplier.
try:
multiplier = self._execution_cash_flow_multipliers[txn.asset]
except KeyError:
asset = txn.asset
# Futures experience no cash flow on transactions
if isinstance(asset, Future):
multiplier = 0
else:
multiplier = 1
self._execution_cash_flow_multipliers[txn.asset] = multiplier
if txn.asset is Future:
return 0.0
# Calculate and return the cash flow given the multiplier
return -1 * txn.price * txn.amount * multiplier
return -1 * txn.price * txn.amount
# backwards compat. TODO: remove?
@property
@@ -54,7 +54,7 @@ def calc_position_values(positions):
for position in positions:
if isinstance(position.asset, Future):
# Futures don't have an inherent position value.
values.append(0)
values.append(0.0)
else:
values.append(position.last_sale_price * position.amount)
@@ -186,13 +186,12 @@ class PositionTracker(object):
"""
total_leftover_cash = 0
for split in splits:
asset = split[0]
for asset, ratio in splits:
if asset in self.positions:
# Make the position object handle the split. It returns the
# leftover cash from a fractional share, if there is any.
position = self.positions[asset]
leftover_cash = position.handle_split(asset, split[1])
leftover_cash = position.handle_split(asset, ratio)
total_leftover_cash += leftover_cash
return total_leftover_cash
+2
View File
@@ -39,6 +39,8 @@ class Transaction(object):
py = copy(self.__dict__)
del py['type']
del py['asset']
# Adding 'sid' for backwards compatibility with downstrean consumers.
py['sid'] = self.asset
return py
+3 -1
View File
@@ -235,8 +235,10 @@ class Position(object):
self.last_sale_price = 0.0
self.last_sale_date = None
@property
def sid(self):
# for backwards compatibility
self.sid = asset
return self.asset
def __repr__(self):
return "Position({0})".format(self.__dict__)