コード例 #1
0
    def test_cancel(self):
        blotter = SimulationBlotter(self.sim_params)

        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])
コード例 #2
0
ファイル: test_blotter.py プロジェクト: barrygolden/zipline
    def test_cancel(self):
        blotter = SimulationBlotter()

        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])
コード例 #3
0
    def test_cancel(self):
        blotter = SimulationBlotter()

        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())

        assert len(blotter.open_orders) == 2
        assert len(blotter.open_orders[self.asset_24]) == 3
        assert [o.amount for o in blotter.open_orders[self.asset_24]
                ] == [100, 200, 300]

        blotter.cancel(oid_2)
        assert len(blotter.open_orders) == 2
        assert len(blotter.open_orders[self.asset_24]) == 2
        assert [o.amount
                for o in blotter.open_orders[self.asset_24]] == [100, 300]
        assert [o.id
                for o in blotter.open_orders[self.asset_24]] == [oid_1, oid_3]

        blotter.cancel_all_orders_for_asset(self.asset_24)
        assert len(blotter.open_orders) == 1
        assert list(blotter.open_orders) == [self.asset_25]
コード例 #4
0
    def test_order_hold(self):
        """
        Held orders act almost identically to open orders, except for the
        status indication. When a fill happens, the order should switch
        status to OPEN/FILLED as necessary
        """
        blotter = SimulationBlotter(self.sim_params,
                                    equity_slippage=VolumeShareSlippage())

        # Nothing happens on held of a non-existent order
        blotter.hold(56)
        self.assertEqual(blotter.new_orders, [])

        open_id = blotter.order(self.asset_24, 100, MarketOrder())
        open_order = blotter.open_orders[self.asset_24][0]
        self.assertEqual(open_order.id, open_id)

        blotter.hold(open_id)
        self.assertEqual(len(blotter.new_orders), 1)
        self.assertEqual(len(blotter.open_orders[self.asset_24]), 1)
        held_order = blotter.new_orders[0]
        self.assertEqual(held_order.status, ORDER_STATUS.HELD)
        self.assertEqual(held_order.reason, '')

        blotter.cancel(held_order.id)
        self.assertEqual(len(blotter.new_orders), 1)
        self.assertEqual(len(blotter.open_orders[self.asset_24]), 0)
        cancelled_order = blotter.new_orders[0]
        self.assertEqual(cancelled_order.id, held_order.id)
        self.assertEqual(cancelled_order.status, ORDER_STATUS.CANCELLED)

        for data in ([100, self.sim_params.sessions[0]],
                     [400, self.sim_params.sessions[1]]):
            # Verify that incoming fills will change the order status.
            trade_amt = data[0]
            dt = data[1]

            order_size = 100
            expected_filled = int(trade_amt *
                                  DEFAULT_EQUITY_VOLUME_SLIPPAGE_BAR_LIMIT)
            expected_open = order_size - expected_filled
            expected_status = ORDER_STATUS.OPEN if expected_open else \
                ORDER_STATUS.FILLED

            blotter = SimulationBlotter(self.sim_params,
                                        equity_slippage=VolumeShareSlippage())
            open_id = blotter.order(self.asset_24, order_size, MarketOrder())
            open_order = blotter.open_orders[self.asset_24][0]
            self.assertEqual(open_id, open_order.id)
            blotter.hold(open_id)
            held_order = blotter.new_orders[0]

            filled_order = None
            blotter.current_dt = dt
            bar_data = self.create_bardata(simulation_dt_func=lambda: dt, )
            txns, _, _ = blotter.get_transactions(bar_data)
            for txn in txns:
                filled_order = blotter.orders[txn.order_id]

            self.assertEqual(filled_order.id, held_order.id)
            self.assertEqual(filled_order.status, expected_status)
            self.assertEqual(filled_order.filled, expected_filled)
            self.assertEqual(filled_order.open_amount, expected_open)
コード例 #5
0
ファイル: test_blotter.py プロジェクト: barrygolden/zipline
    def test_order_hold(self):
        """
        Held orders act almost identically to open orders, except for the
        status indication. When a fill happens, the order should switch
        status to OPEN/FILLED as necessary
        """
        blotter = SimulationBlotter(equity_slippage=VolumeShareSlippage())

        # Nothing happens on held of a non-existent order
        blotter.hold(56)
        self.assertEqual(blotter.new_orders, [])

        open_id = blotter.order(self.asset_24, 100, MarketOrder())
        open_order = blotter.open_orders[self.asset_24][0]
        self.assertEqual(open_order.id, open_id)

        blotter.hold(open_id)
        self.assertEqual(len(blotter.new_orders), 1)
        self.assertEqual(len(blotter.open_orders[self.asset_24]), 1)
        held_order = blotter.new_orders[0]
        self.assertEqual(held_order.status, ORDER_STATUS.HELD)
        self.assertEqual(held_order.reason, '')

        blotter.cancel(held_order.id)
        self.assertEqual(len(blotter.new_orders), 1)
        self.assertEqual(len(blotter.open_orders[self.asset_24]), 0)
        cancelled_order = blotter.new_orders[0]
        self.assertEqual(cancelled_order.id, held_order.id)
        self.assertEqual(cancelled_order.status, ORDER_STATUS.CANCELLED)

        for data in ([100, self.sim_params.sessions[0]],
                     [400, self.sim_params.sessions[1]]):
            # Verify that incoming fills will change the order status.
            trade_amt = data[0]
            dt = data[1]

            order_size = 100
            expected_filled = int(trade_amt *
                                  DEFAULT_EQUITY_VOLUME_SLIPPAGE_BAR_LIMIT)
            expected_open = order_size - expected_filled
            expected_status = ORDER_STATUS.OPEN if expected_open else \
                ORDER_STATUS.FILLED

            blotter = SimulationBlotter(equity_slippage=VolumeShareSlippage())
            open_id = blotter.order(self.asset_24, order_size, MarketOrder())
            open_order = blotter.open_orders[self.asset_24][0]
            self.assertEqual(open_id, open_order.id)
            blotter.hold(open_id)
            held_order = blotter.new_orders[0]

            filled_order = None
            blotter.current_dt = dt
            bar_data = self.create_bardata(
                simulation_dt_func=lambda: dt,
            )
            txns, _, _ = blotter.get_transactions(bar_data)
            for txn in txns:
                filled_order = blotter.orders[txn.order_id]

            self.assertEqual(filled_order.id, held_order.id)
            self.assertEqual(filled_order.status, expected_status)
            self.assertEqual(filled_order.filled, expected_filled)
            self.assertEqual(filled_order.open_amount, expected_open)