def test_price_based_slippage__nan_prices(self): slippage_rate = 0.1 slippage_model = PriceBasedSlippage(slippage_rate, self.data_provider, self.contract_ticker_mapper) prices_without_slippage = [float('nan'), np.nan, float('nan')] expected_fill_prices = [float('nan'), float('nan'), float('nan')] actual_fill_prices, actual_fill_volumes = slippage_model.process_orders(str_to_date("2020-01-01"), self.orders, prices_without_slippage) assert_lists_equal(expected_fill_prices, actual_fill_prices)
def test_price_based_slippage__no_volume_limits(self): """Volume should remain the same. Prices should be increased by the slippage rate.""" slippage_rate = 0.1 slippage_model = PriceBasedSlippage(slippage_rate, self.data_provider, self.contract_ticker_mapper) prices_without_slippage = [20.0, 30.0, 40.0] # Each price should be changed by +0.1 / -0.1 depending on whether it is a BUY or SELL expected_fill_prices = [22.0, 27.0, 44.0] # Volumes should remain equal to the initial quantities expected_fill_volumes = [order.quantity for order in self.orders] # [1250, -200, 1] actual_fill_prices, actual_fill_volumes = slippage_model.process_orders(str_to_date("2020-01-01"), self.orders, prices_without_slippage) assert_lists_equal(expected_fill_prices, actual_fill_prices) assert_lists_equal(expected_fill_volumes, actual_fill_volumes)
def test_price_based_slippage__with_volume(self): """Volume should be changed in case of exceeding limits. Slippage rate is set to 0.0, so the prices should remain unchanged.""" slippage_rate = 0.0 max_volume_share_limit = 0.1 slippage_model = PriceBasedSlippage(slippage_rate, self.data_provider, max_volume_share_limit) prices_without_slippage = [20.0, 30.0, 40.0] expected_fill_prices = prices_without_slippage # Mean historical volume is set to 50.0 for each of the tickers. As the max_volume_share_limit = 0.1, the limit # is set to +/-5.0 expected_fill_volumes = [5.0, -5.0, 1] actual_fill_prices, actual_fill_volumes = slippage_model.process_orders(str_to_date("2020-01-01"), self.orders, prices_without_slippage) assert_lists_equal(expected_fill_prices, actual_fill_prices) assert_lists_equal(expected_fill_volumes, actual_fill_volumes)