本文整理汇总了Python中zipline.finance.blotter.Blotter类的典型用法代码示例。如果您正苦于以下问题:Python Blotter类的具体用法?Python Blotter怎么用?Python Blotter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Blotter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_blotter_processes_splits
def test_blotter_processes_splits(self):
blotter = Blotter('daily', equity_slippage=FixedSlippage())
# set up two open limit orders with very low limit prices,
# one for sid 1 and one for sid 2
asset1 = self.asset_finder.retrieve_asset(1)
asset2 = self.asset_finder.retrieve_asset(2)
asset133 = self.asset_finder.retrieve_asset(133)
blotter.order(asset1, 100, LimitOrder(10))
blotter.order(asset2, 100, LimitOrder(10))
# send in splits for assets 133 and 2. We have no open orders for
# asset 133 so it should be ignored.
blotter.process_splits([(asset133, 0.5), (asset2, 0.3333)])
for asset in [asset1, asset2]:
order_lists = blotter.open_orders[asset]
self.assertIsNotNone(order_lists)
self.assertEqual(1, len(order_lists))
asset1_order = blotter.open_orders[1][0]
asset2_order = blotter.open_orders[2][0]
# make sure the asset1 order didn't change
self.assertEqual(100, asset1_order.amount)
self.assertEqual(10, asset1_order.limit)
self.assertEqual(1, asset1_order.asset)
# make sure the asset2 order did change
# to 300 shares at 3.33
self.assertEqual(300, asset2_order.amount)
self.assertEqual(3.33, asset2_order.limit)
self.assertEqual(2, asset2_order.asset)
示例2: test_batch_order_matches_multiple_orders
def test_batch_order_matches_multiple_orders(self):
"""
Ensure the effect of order_batch is the same as multiple calls to
order.
"""
blotter1 = Blotter(self.sim_params.data_frequency,
self.asset_finder)
blotter2 = Blotter(self.sim_params.data_frequency,
self.asset_finder)
for i in range(1, 4):
order_arg_lists = [
(self.asset_24, i * 100, MarketOrder()),
(self.asset_25, i * 100, LimitOrder(i * 100 + 1)),
]
order_batch_ids = blotter1.batch_order(order_arg_lists)
order_ids = []
for order_args in order_arg_lists:
order_ids.append(blotter2.order(*order_args))
self.assertEqual(len(order_batch_ids), len(order_ids))
self.assertEqual(len(blotter1.open_orders),
len(blotter2.open_orders))
for (asset, _, _), order_batch_id, order_id in zip(
order_arg_lists, order_batch_ids, order_ids
):
self.assertEqual(len(blotter1.open_orders[asset]),
len(blotter2.open_orders[asset]))
self.assertEqual(order_batch_id,
blotter1.open_orders[asset][i-1].id)
self.assertEqual(order_id,
blotter2.open_orders[asset][i-1].id)
示例3: test_blotter_processes_splits
def test_blotter_processes_splits(self):
blotter = Blotter("daily", self.env.asset_finder, slippage_func=FixedSlippage())
# set up two open limit orders with very low limit prices,
# one for sid 1 and one for sid 2
blotter.order(blotter.asset_finder.retrieve_asset(1), 100, LimitOrder(10))
blotter.order(blotter.asset_finder.retrieve_asset(2), 100, LimitOrder(10))
# send in a split for sid 2
blotter.process_splits([(2, 0.3333)])
for sid in [1, 2]:
order_lists = blotter.open_orders[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()
# 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"])
# 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"])
示例4: test_blotter_order_types
def test_blotter_order_types(self, style_obj, expected_lmt, expected_stp):
blotter = Blotter()
blotter.order(24, 100, style_obj)
result = blotter.open_orders[24][0]
self.assertEqual(result.limit, expected_lmt)
self.assertEqual(result.stop, expected_stp)
示例5: test_blotter_order_types
def test_blotter_order_types(self, style_obj, expected_lmt, expected_stp):
blotter = Blotter('daily', self.env.asset_finder)
asset_24 = blotter.asset_finder.retrieve_asset(24)
blotter.order(asset_24, 100, style_obj)
result = blotter.open_orders[asset_24][0]
self.assertEqual(result.limit, expected_lmt)
self.assertEqual(result.stop, expected_stp)
示例6: test_cancel
def test_cancel(self):
blotter = Blotter('daily', self.asset_finder)
oid_1 = blotter.order(self.asset_24, 100, MarketOrder())
oid_2 = blotter.order(self.asset_24, 200, MarketOrder())
oid_3 = blotter.order(self.asset_24, 300, MarketOrder())
# Create an order for another asset to verify that we don't remove it
# when we do cancel_all on 24.
blotter.order(self.asset_25, 150, MarketOrder())
self.assertEqual(len(blotter.open_orders), 2)
self.assertEqual(len(blotter.open_orders[self.asset_24]), 3)
self.assertEqual(
[o.amount for o in blotter.open_orders[self.asset_24]],
[100, 200, 300],
)
blotter.cancel(oid_2)
self.assertEqual(len(blotter.open_orders), 2)
self.assertEqual(len(blotter.open_orders[self.asset_24]), 2)
self.assertEqual(
[o.amount for o in blotter.open_orders[self.asset_24]],
[100, 300],
)
self.assertEqual(
[o.id for o in blotter.open_orders[self.asset_24]],
[oid_1, oid_3],
)
blotter.cancel_all_orders_for_asset(self.asset_24)
self.assertEqual(len(blotter.open_orders), 1)
self.assertEqual(list(blotter.open_orders), [self.asset_25])
示例7: test_prune_orders
def test_prune_orders(self):
blotter = Blotter(self.sim_params.data_frequency,
self.env.asset_finder)
asset_24 = blotter.asset_finder.retrieve_asset(24)
asset_25 = blotter.asset_finder.retrieve_asset(25)
blotter.order(asset_24, 100, MarketOrder())
open_order = blotter.open_orders[asset_24][0]
blotter.prune_orders([])
self.assertEqual(1, len(blotter.open_orders[asset_24]))
blotter.prune_orders([open_order])
self.assertEqual(0, len(blotter.open_orders[asset_24]))
# prune an order that isn't in our our open orders list, make sure
# nothing blows up
other_order = Order(
dt=blotter.current_dt,
sid=asset_25,
amount=1
)
blotter.prune_orders([other_order])
示例8: test_blotter_processes_splits
def test_blotter_processes_splits(self):
sim_params = factory.create_simulation_parameters()
blotter = Blotter()
blotter.set_date(sim_params.period_start)
# set up two open limit orders with very low limit prices,
# one for sid 1 and one for sid 2
blotter.order(1, 100, LimitOrder(10))
blotter.order(2, 100, LimitOrder(10))
# send in a split for sid 2
split_event = factory.create_split(2, 0.33333,
sim_params.period_start +
timedelta(days=1))
blotter.process_split(split_event)
for sid in [1, 2]:
order_lists = blotter.open_orders[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()
# 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'])
# 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'])
示例9: __init__
def __init__(self, *args, **kwargs):
"""Initialize sids and other state variables.
:Arguments:
data_frequency : str (daily, hourly or minutely)
The duration of the bars.
annualizer : int <optional>
Which constant to use for annualizing risk metrics.
If not provided, will extract from data_frequency.
capital_base : float <default: 1.0e5>
How much capital to start with.
"""
self._portfolio = None
self.datetime = None
self.registered_transforms = {}
self.transforms = []
self.sources = []
self._recorded_vars = {}
self.logger = None
self.benchmark_return_source = None
# default components for transact
self.slippage = VolumeShareSlippage()
self.commission = PerShare()
self.leverage = NullLeverage()
if 'data_frequency' in kwargs:
self.set_data_frequency(kwargs.pop('data_frequency'))
else:
self.data_frequency = None
# Override annualizer if set
if 'annualizer' in kwargs:
self.annualizer = kwargs['annualizer']
# set the capital base
self.capital_base = kwargs.pop('capital_base', DEFAULT_CAPITAL_BASE)
self.sim_params = kwargs.pop('sim_params', None)
if self.sim_params:
self.sim_params.data_frequency = self.data_frequency
self.blotter = kwargs.pop('blotter', None)
if not self.blotter:
self.blotter = Blotter()
# an algorithm subclass needs to set initialized to True when
# it is fully initialized.
self.initialized = False
# call to user-defined constructor method
self.initialize(*args, **kwargs)
示例10: test_slippage_and_commission_dispatching
def test_slippage_and_commission_dispatching(self):
blotter = Blotter(
self.sim_params.data_frequency,
equity_slippage=FixedSlippage(spread=0.0),
future_slippage=FixedSlippage(spread=2.0),
equity_commission=PerTrade(cost=1.0),
future_commission=PerTrade(cost=2.0),
)
blotter.order(self.asset_24, 1, MarketOrder())
blotter.order(self.future_cl, 1, MarketOrder())
bar_data = self.create_bardata(
simulation_dt_func=lambda: self.sim_params.sessions[-1],
)
txns, commissions, _ = blotter.get_transactions(bar_data)
# The equity transaction should have the same price as its current
# price because the slippage spread is zero. Its commission should be
# $1.00.
equity_txn = txns[0]
self.assertEqual(
equity_txn.price,
bar_data.current(equity_txn.asset, 'price'),
)
self.assertEqual(commissions[0]['cost'], 1.0)
# The future transaction price should be 1.0 more than its current
# price because half of the 'future_slippage' spread is added. Its
# commission should be $2.00.
future_txn = txns[1]
self.assertEqual(
future_txn.price,
bar_data.current(future_txn.asset, 'price') + 1.0,
)
self.assertEqual(commissions[1]['cost'], 2.0)
示例11: test_blotter_never_cancel
def test_blotter_never_cancel(self):
blotter = Blotter('minute', cancel_policy=NeverCancel())
blotter.order(self.asset_24, 100, MarketOrder())
self.assertEqual(len(blotter.new_orders), 1)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(BAR)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(SESSION_END)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
示例12: test_blotter_never_cancel
def test_blotter_never_cancel(self):
blotter = Blotter("minute", self.env.asset_finder, cancel_policy=NeverCancel())
blotter.order(blotter.asset_finder.retrieve_asset(24), 100, MarketOrder())
self.assertEqual(len(blotter.new_orders), 1)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(BAR)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(DAY_END)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
示例13: test_prune_orders
def test_prune_orders(self):
blotter = Blotter(self.sim_params.data_frequency)
blotter.order(self.asset_24, 100, MarketOrder())
open_order = blotter.open_orders[self.asset_24][0]
blotter.prune_orders([])
self.assertEqual(1, len(blotter.open_orders[self.asset_24]))
blotter.prune_orders([open_order])
self.assertEqual(0, len(blotter.open_orders[self.asset_24]))
# prune an order that isn't in our our open orders list, make sure
# nothing blows up
other_order = Order(
dt=blotter.current_dt,
asset=self.asset_25,
amount=1
)
blotter.prune_orders([other_order])
示例14: test_blotter_eod_cancellation
def test_blotter_eod_cancellation(self):
blotter = Blotter('minute', cancel_policy=EODCancel())
# 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())
self.assertEqual(len(blotter.new_orders), 2)
order_ids = [order.id for order in blotter.open_orders[self.asset_24]]
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
self.assertEqual(blotter.new_orders[1].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(BAR)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
self.assertEqual(blotter.new_orders[1].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(SESSION_END)
for order_id in order_ids:
order = blotter.orders[order_id]
self.assertEqual(order.status, ORDER_STATUS.CANCELLED)
示例15: test_blotter_eod_cancellation
def test_blotter_eod_cancellation(self):
blotter = Blotter("minute", self.env.asset_finder, cancel_policy=EODCancel())
asset_24 = blotter.asset_finder.retrieve_asset(24)
# Make two orders for the same sid, so we can test that we are not
# mutating the orders list as we are cancelling orders
blotter.order(asset_24, 100, MarketOrder())
blotter.order(asset_24, -100, MarketOrder())
self.assertEqual(len(blotter.new_orders), 2)
order_ids = [order.id for order in blotter.open_orders[asset_24]]
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
self.assertEqual(blotter.new_orders[1].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(BAR)
self.assertEqual(blotter.new_orders[0].status, ORDER_STATUS.OPEN)
self.assertEqual(blotter.new_orders[1].status, ORDER_STATUS.OPEN)
blotter.execute_cancel_policy(DAY_END)
for order_id in order_ids:
order = blotter.orders[order_id]
self.assertEqual(order.status, ORDER_STATUS.CANCELLED)