mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-31 12:10:31 +08:00
REF: Explicitly use Assets in Position, Order, Transaction
(Instead of `sid`, which were already usually assets) Perf packets are unchanged and still emit `sid`: int
This commit is contained in:
@@ -154,7 +154,7 @@ class BlotterTestCase(WithCreateBarData,
|
||||
blotter = Blotter('minute', self.asset_finder,
|
||||
cancel_policy=EODCancel())
|
||||
|
||||
# Make two orders for the same sid, so we can test that we are not
|
||||
# Make two orders for the same asset, so we can test that we are not
|
||||
# mutating the orders list as we are cancelling orders
|
||||
blotter.order(self.asset_24, 100, MarketOrder())
|
||||
blotter.order(self.asset_24, -100, MarketOrder())
|
||||
@@ -343,7 +343,7 @@ class BlotterTestCase(WithCreateBarData,
|
||||
|
||||
other_order = Order(
|
||||
dt=blotter.current_dt,
|
||||
sid=self.asset_25,
|
||||
asset=self.asset_25,
|
||||
amount=1
|
||||
)
|
||||
|
||||
@@ -406,7 +406,7 @@ class BlotterTestCase(WithCreateBarData,
|
||||
equity_txn = txns[0]
|
||||
self.assertEqual(
|
||||
equity_txn.price,
|
||||
bar_data.current(equity_txn.sid, 'price'),
|
||||
bar_data.current(equity_txn.asset, 'price'),
|
||||
)
|
||||
self.assertEqual(commissions[0]['cost'], 1.0)
|
||||
|
||||
@@ -416,6 +416,6 @@ class BlotterTestCase(WithCreateBarData,
|
||||
future_txn = txns[1]
|
||||
self.assertEqual(
|
||||
future_txn.price,
|
||||
bar_data.current(future_txn.sid, 'price') + 1.0,
|
||||
bar_data.current(future_txn.asset, 'price') + 1.0,
|
||||
)
|
||||
self.assertEqual(commissions[1]['cost'], 2.0)
|
||||
|
||||
@@ -21,16 +21,16 @@ class CommissionUnitTests(WithAssetFinder, ZiplineTestCase):
|
||||
asset1 = self.asset_finder.retrieve_asset(1)
|
||||
|
||||
# one order
|
||||
order = Order(dt=None, sid=asset1, amount=500)
|
||||
order = Order(dt=None, asset=asset1, amount=500)
|
||||
|
||||
# three fills
|
||||
txn1 = Transaction(sid=asset1, amount=230, dt=None,
|
||||
txn1 = Transaction(asset=asset1, amount=230, dt=None,
|
||||
price=100, order_id=order.id)
|
||||
|
||||
txn2 = Transaction(sid=asset1, amount=170, dt=None,
|
||||
txn2 = Transaction(asset=asset1, amount=170, dt=None,
|
||||
price=101, order_id=order.id)
|
||||
|
||||
txn3 = Transaction(sid=asset1, amount=100, dt=None,
|
||||
txn3 = Transaction(asset=asset1, amount=100, dt=None,
|
||||
price=102, order_id=order.id)
|
||||
|
||||
return order, [txn1, txn2, txn3]
|
||||
|
||||
@@ -126,7 +126,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'limit': 3.5})
|
||||
]
|
||||
|
||||
@@ -148,7 +148,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'limit': 3.5})
|
||||
]
|
||||
|
||||
@@ -170,7 +170,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'limit': 3.6})
|
||||
]
|
||||
|
||||
@@ -194,7 +194,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
# we ordered 100 shares, but default volume slippage only allows
|
||||
# for 2.5% of the volume. 2.5% * 2000 = 50 shares
|
||||
'amount': int(50),
|
||||
'sid': int(133),
|
||||
'asset': self.ASSET133,
|
||||
'order_id': open_orders[0].id
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'limit': 3.5})
|
||||
]
|
||||
|
||||
@@ -231,7 +231,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'limit': 3.5})
|
||||
]
|
||||
|
||||
@@ -253,7 +253,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'limit': 3.4})
|
||||
]
|
||||
|
||||
@@ -275,7 +275,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(
|
||||
2006, 1, 5, 14, 32, tzinfo=pytz.utc),
|
||||
'amount': int(-50),
|
||||
'sid': int(133)
|
||||
'asset': self.ASSET133,
|
||||
}
|
||||
|
||||
self.assertIsNotNone(txn)
|
||||
@@ -293,7 +293,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'stop': 4.0,
|
||||
'limit': 3.0})
|
||||
]
|
||||
@@ -328,7 +328,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'stop': 4.0,
|
||||
'limit': 3.5})
|
||||
]
|
||||
@@ -363,7 +363,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'stop': 4.0,
|
||||
'limit': 3.6})
|
||||
]
|
||||
@@ -398,7 +398,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(
|
||||
2006, 1, 5, 14, 34, tzinfo=pytz.utc),
|
||||
'amount': int(50),
|
||||
'sid': int(133)
|
||||
'asset': self.ASSET133
|
||||
}
|
||||
|
||||
for key, value in expected_txn.items():
|
||||
@@ -411,7 +411,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'stop': 3.0,
|
||||
'limit': 4.0})
|
||||
]
|
||||
@@ -446,7 +446,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'stop': 3.0,
|
||||
'limit': 3.5})
|
||||
]
|
||||
@@ -481,7 +481,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': self.ASSET133,
|
||||
'asset': self.ASSET133,
|
||||
'stop': 3.0,
|
||||
'limit': 3.4})
|
||||
]
|
||||
@@ -516,7 +516,7 @@ class SlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(
|
||||
2006, 1, 5, 14, 32, tzinfo=pytz.utc),
|
||||
'amount': int(-50),
|
||||
'sid': int(133)
|
||||
'asset': self.ASSET133,
|
||||
}
|
||||
|
||||
for key, value in expected_txn.items():
|
||||
@@ -574,7 +574,7 @@ class VolumeShareSlippageTestCase(WithCreateBarData,
|
||||
dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
amount=100,
|
||||
filled=0,
|
||||
sid=self.ASSET133
|
||||
asset=self.ASSET133
|
||||
)
|
||||
]
|
||||
|
||||
@@ -596,7 +596,7 @@ class VolumeShareSlippageTestCase(WithCreateBarData,
|
||||
'dt': datetime.datetime(
|
||||
2006, 1, 5, 14, 31, tzinfo=pytz.utc),
|
||||
'amount': int(5),
|
||||
'sid': int(133),
|
||||
'asset': self.ASSET133,
|
||||
'commission': None,
|
||||
'type': DATASOURCE_TYPE.TRANSACTION,
|
||||
'order_id': open_orders[0].id
|
||||
@@ -613,7 +613,7 @@ class VolumeShareSlippageTestCase(WithCreateBarData,
|
||||
dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
|
||||
amount=100,
|
||||
filled=0,
|
||||
sid=self.ASSET133
|
||||
asset=self.ASSET133
|
||||
)
|
||||
]
|
||||
|
||||
@@ -684,7 +684,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'dt': pd.Timestamp('2006-01-05 14:30', tz='UTC'),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': 133,
|
||||
'stop': 3.5
|
||||
},
|
||||
'event': {
|
||||
@@ -693,7 +692,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'price': 4.0,
|
||||
'high': 3.15,
|
||||
'low': 2.85,
|
||||
'sid': 133,
|
||||
'close': 4.0,
|
||||
'open': 3.5
|
||||
},
|
||||
@@ -702,7 +700,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'price': 4.00025,
|
||||
'dt': pd.Timestamp('2006-01-05 14:31', tz='UTC'),
|
||||
'amount': 50,
|
||||
'sid': 133,
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -711,7 +708,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'dt': pd.Timestamp('2006-01-05 14:30', tz='UTC'),
|
||||
'amount': 100,
|
||||
'filled': 0,
|
||||
'sid': 133,
|
||||
'stop': 3.6
|
||||
},
|
||||
'event': {
|
||||
@@ -720,7 +716,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'price': 3.5,
|
||||
'high': 3.15,
|
||||
'low': 2.85,
|
||||
'sid': 133,
|
||||
'close': 3.5,
|
||||
'open': 4.0
|
||||
},
|
||||
@@ -733,7 +728,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'dt': pd.Timestamp('2006-01-05 14:30', tz='UTC'),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': 133,
|
||||
'stop': 3.4
|
||||
},
|
||||
'event': {
|
||||
@@ -742,7 +736,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'price': 3.5,
|
||||
'high': 3.15,
|
||||
'low': 2.85,
|
||||
'sid': 133,
|
||||
'close': 3.5,
|
||||
'open': 3.0
|
||||
},
|
||||
@@ -755,7 +748,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'dt': pd.Timestamp('2006-01-05 14:30', tz='UTC'),
|
||||
'amount': -100,
|
||||
'filled': 0,
|
||||
'sid': 133,
|
||||
'stop': 3.5
|
||||
},
|
||||
'event': {
|
||||
@@ -764,7 +756,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'price': 3.0,
|
||||
'high': 3.15,
|
||||
'low': 2.85,
|
||||
'sid': 133,
|
||||
'close': 3.0,
|
||||
'open': 3.0
|
||||
},
|
||||
@@ -773,7 +764,6 @@ class OrdersStopTestCase(WithSimParams,
|
||||
'price': 2.9998125,
|
||||
'dt': pd.Timestamp('2006-01-05 14:31', tz='UTC'),
|
||||
'amount': -50,
|
||||
'sid': 133,
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -785,9 +775,13 @@ class OrdersStopTestCase(WithSimParams,
|
||||
])
|
||||
def test_orders_stop(self, name, order_data, event_data, expected):
|
||||
data = order_data
|
||||
data['sid'] = self.ASSET133
|
||||
data['asset'] = self.ASSET133
|
||||
order = Order(**data)
|
||||
|
||||
if expected['transaction']:
|
||||
expected['transaction']['asset'] = self.ASSET133
|
||||
event_data['asset'] = self.ASSET133
|
||||
|
||||
assets = (
|
||||
(133, pd.DataFrame(
|
||||
{
|
||||
|
||||
+11
-13
@@ -3802,7 +3802,7 @@ class TestOrderCancelation(WithDataPortal,
|
||||
np.copysign(389, direction),
|
||||
daily_positions[0]["amount"],
|
||||
)
|
||||
self.assertEqual(1, results.positions[0][0]["sid"].sid)
|
||||
self.assertEqual(1, results.positions[0][0]["sid"])
|
||||
|
||||
# should be an order on day1, but no more orders afterwards
|
||||
np.testing.assert_array_equal([1, 0, 0],
|
||||
@@ -4067,7 +4067,6 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
resources = self.make_data(auto_close_delta, 'daily', capital_base)
|
||||
|
||||
assets = resources.assets
|
||||
sids = [asset.sid for asset in assets]
|
||||
final_prices = resources.final_prices
|
||||
|
||||
# Prices at which we expect our orders to be filled.
|
||||
@@ -4174,14 +4173,14 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
self.test_days[1]
|
||||
)[1]
|
||||
|
||||
for sid, txn in zip(sids, initial_fills):
|
||||
for asset, txn in zip(assets, initial_fills):
|
||||
self.assertDictContainsSubset(
|
||||
{
|
||||
'amount': order_size,
|
||||
'commission': None,
|
||||
'dt': last_minute_of_session,
|
||||
'price': initial_fill_prices[sid],
|
||||
'sid': sid,
|
||||
'price': initial_fill_prices[asset],
|
||||
'sid': asset,
|
||||
},
|
||||
txn,
|
||||
)
|
||||
@@ -4203,7 +4202,7 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
'commission': 0.0,
|
||||
'dt': assets[0].auto_close_date,
|
||||
'price': fp0,
|
||||
'sid': sids[0],
|
||||
'sid': assets[0],
|
||||
'order_id': None, # Auto-close txns emit Nones for order_id.
|
||||
},
|
||||
)
|
||||
@@ -4218,7 +4217,7 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
'commission': 0.0,
|
||||
'dt': assets[1].auto_close_date,
|
||||
'price': fp1,
|
||||
'sid': sids[1],
|
||||
'sid': assets[1],
|
||||
'order_id': None, # Auto-close txns emit Nones for order_id.
|
||||
},
|
||||
)
|
||||
@@ -4313,7 +4312,6 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
|
||||
env = resources.env
|
||||
assets = resources.assets
|
||||
sids = [a.sid for a in assets]
|
||||
final_prices = resources.final_prices
|
||||
backtest_minutes = resources.trade_data_by_sid[0].index.tolist()
|
||||
|
||||
@@ -4390,14 +4388,14 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
# the backtest, which is still on the first day in minute mode.
|
||||
initial_fills = transactions.iloc[0]
|
||||
self.assertEqual(len(initial_fills), len(assets))
|
||||
for sid, txn in zip(sids, initial_fills):
|
||||
for asset, txn in zip(assets, initial_fills):
|
||||
self.assertDictContainsSubset(
|
||||
{
|
||||
'amount': order_size,
|
||||
'commission': None,
|
||||
'dt': backtest_minutes[1],
|
||||
'price': initial_fill_prices[sid],
|
||||
'sid': sid,
|
||||
'price': initial_fill_prices[asset],
|
||||
'sid': asset,
|
||||
},
|
||||
txn,
|
||||
)
|
||||
@@ -4419,7 +4417,7 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
'commission': 0.0,
|
||||
'dt': assets[0].auto_close_date,
|
||||
'price': fp0,
|
||||
'sid': sids[0],
|
||||
'sid': assets[0],
|
||||
'order_id': None, # Auto-close txns emit Nones for order_id.
|
||||
},
|
||||
)
|
||||
@@ -4434,7 +4432,7 @@ class TestEquityAutoClose(WithTmpDir, WithTradingCalendars, ZiplineTestCase):
|
||||
'commission': 0.0,
|
||||
'dt': assets[1].auto_close_date,
|
||||
'price': fp1,
|
||||
'sid': sids[1],
|
||||
'sid': assets[1],
|
||||
'order_id': None, # Auto-close txns emit Nones for order_id.
|
||||
},
|
||||
)
|
||||
|
||||
+22
-17
@@ -187,8 +187,8 @@ class FinanceTestCase(WithLogger,
|
||||
# if present, expect transaction amounts to match orders exactly.
|
||||
complete_fill = params.get('complete_fill')
|
||||
|
||||
sid = 1
|
||||
metadata = make_simple_equity_info([sid], self.start, self.end)
|
||||
asset1 = self.asset_finder.retrieve_asset(1)
|
||||
metadata = make_simple_equity_info([asset1.sid], self.start, self.end)
|
||||
with TempDirectory() as tempdir, \
|
||||
tmp_trading_env(equities=metadata) as env:
|
||||
|
||||
@@ -206,7 +206,7 @@ class FinanceTestCase(WithLogger,
|
||||
|
||||
price_data = np.array([10.1] * len(minutes))
|
||||
assets = {
|
||||
sid: pd.DataFrame({
|
||||
asset1.sid: pd.DataFrame({
|
||||
"open": price_data,
|
||||
"high": price_data,
|
||||
"low": price_data,
|
||||
@@ -305,7 +305,7 @@ class FinanceTestCase(WithLogger,
|
||||
# place an order
|
||||
direction = alternator ** len(order_list)
|
||||
order_id = blotter.order(
|
||||
blotter.asset_finder.retrieve_asset(sid),
|
||||
asset1,
|
||||
order_amount * direction,
|
||||
MarketOrder())
|
||||
order_list.append(blotter.orders[order_id])
|
||||
@@ -333,7 +333,7 @@ class FinanceTestCase(WithLogger,
|
||||
|
||||
for i in range(order_count):
|
||||
order = order_list[i]
|
||||
self.assertEqual(order.sid, sid)
|
||||
self.assertEqual(order.asset, asset1)
|
||||
self.assertEqual(order.amount, order_amount * alternator ** i)
|
||||
|
||||
if complete_fill:
|
||||
@@ -351,15 +351,19 @@ class FinanceTestCase(WithLogger,
|
||||
|
||||
self.assertEqual(len(transactions), expected_txn_count)
|
||||
|
||||
cumulative_pos = tracker.position_tracker.positions[sid]
|
||||
cumulative_pos = tracker.position_tracker.positions[asset1]
|
||||
if total_volume == 0:
|
||||
self.assertIsNone(cumulative_pos)
|
||||
else:
|
||||
self.assertEqual(total_volume, cumulative_pos.amount)
|
||||
|
||||
# the open orders should not contain sid.
|
||||
# the open orders should not contain the asset.
|
||||
oo = blotter.open_orders
|
||||
self.assertNotIn(sid, oo, "Entry is removed when no open orders")
|
||||
self.assertNotIn(
|
||||
asset1,
|
||||
oo,
|
||||
"Entry is removed when no open orders"
|
||||
)
|
||||
|
||||
def test_blotter_processes_splits(self):
|
||||
blotter = Blotter('daily', self.env.asset_finder,
|
||||
@@ -376,23 +380,24 @@ class FinanceTestCase(WithLogger,
|
||||
blotter.process_splits([(2, 0.3333)])
|
||||
|
||||
for sid in [1, 2]:
|
||||
order_lists = blotter.open_orders[sid]
|
||||
order_lists = \
|
||||
blotter.open_orders[blotter.asset_finder.retrieve_asset(sid)]
|
||||
self.assertIsNotNone(order_lists)
|
||||
self.assertEqual(1, len(order_lists))
|
||||
|
||||
aapl_order = blotter.open_orders[1][0].to_dict()
|
||||
fls_order = blotter.open_orders[2][0].to_dict()
|
||||
aapl_order = blotter.open_orders[1][0]
|
||||
fls_order = blotter.open_orders[2][0]
|
||||
|
||||
# make sure the aapl order didn't change
|
||||
self.assertEqual(100, aapl_order['amount'])
|
||||
self.assertEqual(10, aapl_order['limit'])
|
||||
self.assertEqual(1, aapl_order['sid'])
|
||||
self.assertEqual(100, aapl_order.amount)
|
||||
self.assertEqual(10, aapl_order.limit)
|
||||
self.assertEqual(1, aapl_order.asset)
|
||||
|
||||
# make sure the fls order did change
|
||||
# to 300 shares at 3.33
|
||||
self.assertEqual(300, fls_order['amount'])
|
||||
self.assertEqual(3.33, fls_order['limit'])
|
||||
self.assertEqual(2, fls_order['sid'])
|
||||
self.assertEqual(300, fls_order.amount)
|
||||
self.assertEqual(3.33, fls_order.limit)
|
||||
self.assertEqual(2, fls_order.asset)
|
||||
|
||||
|
||||
class TradingEnvironmentTestCase(WithLogger,
|
||||
|
||||
+33
-26
@@ -347,7 +347,7 @@ class TestSplitPerformance(WithSimParams, WithTmpDir, ZiplineTestCase):
|
||||
# check the last position to make sure it's been updated
|
||||
position = latest_positions[0]
|
||||
|
||||
self.assertEqual(1, position['sid'])
|
||||
self.assertEqual(self.asset1, position['sid'])
|
||||
self.assertEqual(33, position['amount'])
|
||||
self.assertEqual(60, position['cost_basis'])
|
||||
self.assertEqual(60, position['last_sale_price'])
|
||||
@@ -1326,8 +1326,8 @@ class TestPositionPerformance(WithInstanceTmpDir, WithTradingCalendars,
|
||||
"should be just one position")
|
||||
|
||||
self.assertEqual(
|
||||
pp.positions[1].sid,
|
||||
txn.sid,
|
||||
pp.positions[1].asset,
|
||||
txn.asset,
|
||||
"position should be in security with id 1")
|
||||
|
||||
self.assertEqual(
|
||||
@@ -1437,8 +1437,8 @@ single short-sale transaction"""
|
||||
"should be just one position")
|
||||
|
||||
self.assertEqual(
|
||||
pp.positions[1].sid,
|
||||
txn.sid,
|
||||
pp.positions[1].asset,
|
||||
txn.asset,
|
||||
"position should be in security from the transaction"
|
||||
)
|
||||
|
||||
@@ -1494,8 +1494,8 @@ single short-sale transaction"""
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
pp.positions[1].sid,
|
||||
txn.sid,
|
||||
pp.positions[1].asset,
|
||||
txn.asset,
|
||||
"position should be in security from the transaction"
|
||||
)
|
||||
|
||||
@@ -1556,8 +1556,8 @@ cost of sole txn in test"
|
||||
"should be just one position"
|
||||
)
|
||||
self.assertEqual(
|
||||
ppTotal.positions[1].sid,
|
||||
txn.sid,
|
||||
ppTotal.positions[1].asset,
|
||||
txn.asset,
|
||||
"position should be in security from the transaction"
|
||||
)
|
||||
|
||||
@@ -1834,7 +1834,7 @@ shares in position"
|
||||
self.create_environment_stuff(num_days=8)
|
||||
|
||||
history_args = (
|
||||
1,
|
||||
self.asset1,
|
||||
[10, 9, 11, 8, 9, 12, 13, 14],
|
||||
[200, -100, -100, 100, -300, 100, 500, 400],
|
||||
oneday,
|
||||
@@ -1973,6 +1973,16 @@ class TestPositionTracker(WithTradingEnvironment,
|
||||
ZiplineTestCase):
|
||||
ASSET_FINDER_EQUITY_SIDS = 1, 2
|
||||
|
||||
@classmethod
|
||||
def init_class_fixtures(cls):
|
||||
super(TestPositionTracker, cls).init_class_fixtures()
|
||||
|
||||
cls.EQUITY1 = cls.asset_finder.retrieve_asset(1)
|
||||
cls.EQUITY2 = cls.asset_finder.retrieve_asset(2)
|
||||
cls.FUTURE3 = cls.asset_finder.retrieve_asset(3)
|
||||
cls.FUTURE4 = cls.asset_finder.retrieve_asset(4)
|
||||
cls.FUTURE5 = cls.asset_finder.retrieve_asset(1032201401)
|
||||
|
||||
@classmethod
|
||||
def make_futures_info(cls):
|
||||
return pd.DataFrame.from_dict(
|
||||
@@ -2017,13 +2027,13 @@ class TestPositionTracker(WithTradingEnvironment,
|
||||
def test_position_values_and_exposures(self):
|
||||
pt = perf.PositionTracker(self.env.asset_finder, None)
|
||||
dt = pd.Timestamp("1984/03/06 3:00PM")
|
||||
pos1 = perf.Position(1, amount=np.float64(10.0),
|
||||
pos1 = perf.Position(self.EQUITY1, amount=np.float64(10.0),
|
||||
last_sale_date=dt, last_sale_price=10)
|
||||
pos2 = perf.Position(2, amount=np.float64(-20.0),
|
||||
pos2 = perf.Position(self.EQUITY2, amount=np.float64(-20.0),
|
||||
last_sale_date=dt, last_sale_price=10)
|
||||
pos3 = perf.Position(3, amount=np.float64(30.0),
|
||||
pos3 = perf.Position(self.FUTURE3, amount=np.float64(30.0),
|
||||
last_sale_date=dt, last_sale_price=10)
|
||||
pos4 = perf.Position(4, amount=np.float64(-40.0),
|
||||
pos4 = perf.Position(self.FUTURE4, amount=np.float64(-40.0),
|
||||
last_sale_date=dt, last_sale_price=10)
|
||||
pt.update_positions({1: pos1, 2: pos2, 3: pos3, 4: pos4})
|
||||
|
||||
@@ -2049,11 +2059,11 @@ class TestPositionTracker(WithTradingEnvironment,
|
||||
def test_update_positions(self):
|
||||
pt = perf.PositionTracker(self.env.asset_finder, None)
|
||||
dt = pd.Timestamp("2014/01/01 3:00PM")
|
||||
pos1 = perf.Position(1, amount=np.float64(10.0),
|
||||
pos1 = perf.Position(self.EQUITY1, amount=np.float64(10.0),
|
||||
last_sale_date=dt, last_sale_price=10)
|
||||
pos2 = perf.Position(2, amount=np.float64(-20.0),
|
||||
pos2 = perf.Position(self.EQUITY2, amount=np.float64(-20.0),
|
||||
last_sale_date=dt, last_sale_price=10)
|
||||
pos3 = perf.Position(1032201401, amount=np.float64(30.0),
|
||||
pos3 = perf.Position(self.FUTURE5, amount=np.float64(30.0),
|
||||
last_sale_date=dt, last_sale_price=100)
|
||||
|
||||
# Call update_positions twice. When the second call is made,
|
||||
@@ -2063,8 +2073,8 @@ class TestPositionTracker(WithTradingEnvironment,
|
||||
# were to be stored as a dict, then its order could change in arbitrary
|
||||
# ways when the second update_positions call is made. Hence we also
|
||||
# store it as an OrderedDict.
|
||||
pt.update_positions({1: pos1, 1032201401: pos3})
|
||||
pt.update_positions({2: pos2})
|
||||
pt.update_positions({self.EQUITY1: pos1, self.FUTURE5: pos3})
|
||||
pt.update_positions({self.EQUITY2: pos2})
|
||||
|
||||
pos_stats = pt.stats()
|
||||
# Test long-only methods
|
||||
@@ -2087,19 +2097,17 @@ class TestPositionTracker(WithTradingEnvironment,
|
||||
self.assertEqual(100 + 150000 - 200, pos_stats.net_exposure)
|
||||
|
||||
def test_close_position(self):
|
||||
future_sid = 1032201401
|
||||
equity_sid = 1
|
||||
pt = perf.PositionTracker(self.env.asset_finder, None)
|
||||
dt = pd.Timestamp('2017/01/04 3:00PM')
|
||||
|
||||
pos1 = perf.Position(
|
||||
sid=future_sid,
|
||||
asset=self.FUTURE5,
|
||||
amount=np.float64(30.0),
|
||||
last_sale_date=dt,
|
||||
last_sale_price=100,
|
||||
)
|
||||
pos2 = perf.Position(
|
||||
sid=equity_sid,
|
||||
asset=self.EQUITY1,
|
||||
amount=np.float64(10.0),
|
||||
last_sale_date=dt,
|
||||
last_sale_price=10,
|
||||
@@ -2110,10 +2118,9 @@ class TestPositionTracker(WithTradingEnvironment,
|
||||
# OrderedDicts. If `future_sid` is not removed from the multipliers
|
||||
# dictionaries, equities will hit the incorrect multiplier when
|
||||
# computing `pt.stats()`.
|
||||
pt.update_positions({future_sid: pos1, equity_sid: pos2})
|
||||
pt.update_positions({self.FUTURE5: pos1, self.EQUITY1: pos2})
|
||||
|
||||
asset_to_close = self.env.asset_finder.retrieve_asset(future_sid)
|
||||
txn = create_txn(asset_to_close, dt, 100, -30)
|
||||
txn = create_txn(self.FUTURE5, dt, 100, -30)
|
||||
pt.execute_transaction(txn)
|
||||
|
||||
pos_stats = pt.stats()
|
||||
|
||||
Reference in New Issue
Block a user