def test_volume_limit(self, name, first_order_amount, second_order_amount, first_order_fill_amount, second_order_fill_amount): slippage_model = FixedBasisPointsSlippage(basis_points=5, volume_limit=0.1) open_orders = [ Order(dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=order_amount, filled=0, asset=self.ASSET133) for order_amount in [first_order_amount, second_order_amount] ] bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute, ) orders_txns = list( slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) self.assertEquals(len(orders_txns), 2) _, first_txn = orders_txns[0] _, second_txn = orders_txns[1] self.assertEquals(first_txn['amount'], first_order_fill_amount) self.assertEquals(second_txn['amount'], second_order_fill_amount)
def test_volume_limit(self, name, first_order_amount, second_order_amount, first_order_fill_amount, second_order_fill_amount): slippage_model = FixedBasisPointsSlippage(basis_points=5, volume_limit=0.1) open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=order_amount, filled=0, asset=self.ASSET133 ) for order_amount in [first_order_amount, second_order_amount] ] bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute, ) orders_txns = list(slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) self.assertEquals(len(orders_txns), 2) _, first_txn = orders_txns[0] _, second_txn = orders_txns[1] self.assertEquals(first_txn['amount'], first_order_fill_amount) self.assertEquals(second_txn['amount'], second_order_fill_amount)
def test_fill_zero_shares(self): slippage_model = FixedBasisPointsSlippage(basis_points=5, volume_limit=0.1) # since the volume limit for the bar is 20, the first order will be # filled and there will be a transaction for it, and the second order # will order zero shares so there should not be a transaction for it. open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=20, filled=0, asset=self.ASSET133, ) ] * 2 bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute) orders_txns = list( slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) assert 1 == len(orders_txns) # ordering zero shares should result in zero transactions open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=0, filled=0, asset=self.ASSET133, ) ] orders_txns = list( slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) assert 0 == len(orders_txns)
def test_fill_zero_shares(self): slippage_model = FixedBasisPointsSlippage(basis_points=5, volume_limit=0.1) # since the volume limit for the bar is 20, the first order will be # filled and there will be a transaction for it, and the second order # will order zero shares so there should not be a transaction for it. open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=20, filled=0, asset=self.ASSET133 ) ] * 2 bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute ) orders_txns = list(slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) self.assertEqual(1, len(orders_txns)) # ordering zero shares should result in zero transactions open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=0, filled=0, asset=self.ASSET133 ) ] orders_txns = list(slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) self.assertEqual(0, len(orders_txns))
def test_fixed_bps_slippage(self, name, basis_points, volume_limit, order_amount, expected_price, expected_amount): slippage_model = FixedBasisPointsSlippage(basis_points=basis_points, volume_limit=volume_limit) open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=order_amount, filled=0, asset=self.ASSET133 ) ] bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute ) orders_txns = list(slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) self.assertEquals(len(orders_txns), 1) _, txn = orders_txns[0] expected_txn = { 'price': expected_price, 'dt': datetime.datetime( 2006, 1, 5, 14, 31, tzinfo=pytz.utc), 'amount': expected_amount, 'asset': self.ASSET133, 'commission': None, 'type': DATASOURCE_TYPE.TRANSACTION, 'order_id': open_orders[0].id } self.assertIsNotNone(txn) self.assertEquals(expected_txn, txn.__dict__)
def test_fixed_bps_slippage( self, name, basis_points, volume_limit, order_amount, expected_price, expected_amount, ): slippage_model = FixedBasisPointsSlippage(basis_points=basis_points, volume_limit=volume_limit) open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=order_amount, filled=0, asset=self.ASSET133, ) ] bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute) orders_txns = list( slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) assert len(orders_txns) == 1 _, txn = orders_txns[0] expected_txn = { "price": expected_price, "dt": datetime.datetime(2006, 1, 5, 14, 31, tzinfo=pytz.utc), "amount": expected_amount, "asset": self.ASSET133, "type": DATASOURCE_TYPE.TRANSACTION, "order_id": open_orders[0].id, } assert txn is not None assert expected_txn == txn.__dict__
def test_fixed_bps_slippage(self, name, basis_points, volume_limit, order_amount, expected_price, expected_amount): slippage_model = FixedBasisPointsSlippage(basis_points=basis_points, volume_limit=volume_limit) open_orders = [ Order( dt=datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), amount=order_amount, filled=0, asset=self.ASSET133 ) ] bar_data = self.create_bardata( simulation_dt_func=lambda: self.first_minute ) orders_txns = list(slippage_model.simulate( bar_data, self.ASSET133, open_orders, )) self.assertEquals(len(orders_txns), 1) _, txn = orders_txns[0] expected_txn = { 'price': expected_price, 'dt': datetime.datetime(2006, 1, 5, 14, 31, tzinfo=pytz.utc), 'amount': expected_amount, 'asset': self.ASSET133, 'type': DATASOURCE_TYPE.TRANSACTION, 'order_id': open_orders[0].id } self.assertIsNotNone(txn) self.assertEquals(expected_txn, txn.__dict__)