def test_calculate_impact_without_history(self): model = VolatilityVolumeShare(volume_limit=1) late_start_asset = self.asset_finder.retrieve_asset(1000) early_start_asset = self.asset_finder.retrieve_asset(1001) cases = [ # History will look for data before the start date. (pd.Timestamp('2006-01-05 11:35AM', tz='UTC'), early_start_asset), # Start day of the futures contract; no history yet. (pd.Timestamp('2006-02-10 11:35AM', tz='UTC'), late_start_asset), # Only a week's worth of history data. (pd.Timestamp('2006-02-17 11:35AM', tz='UTC'), late_start_asset), ] for minute, asset in cases: data = self.create_bardata(simulation_dt_func=lambda: minute) order = Order(dt=data.current_dt, asset=asset, amount=10) price, amount = model.process_order(data, order) avg_price = ( data.current(asset, 'high') + data.current(asset, 'low') ) / 2 expected_price = \ avg_price * (1 + model.NO_DATA_VOLATILITY_SLIPPAGE_IMPACT) self.assertAlmostEqual(price, expected_price, delta=0.001) self.assertEqual(amount, 10)
def test_calculate_impact_without_history(self): model = VolatilityVolumeShare(volume_limit=1) late_start_asset = self.asset_finder.retrieve_asset(1000) early_start_asset = self.asset_finder.retrieve_asset(1001) cases = [ # History will look for data before the start date. (pd.Timestamp('2006-01-05 11:35AM', tz='UTC'), early_start_asset), # Start day of the futures contract; no history yet. (pd.Timestamp('2006-02-10 11:35AM', tz='UTC'), late_start_asset), # Only a week's worth of history data. (pd.Timestamp('2006-02-17 11:35AM', tz='UTC'), late_start_asset), ] for minute, asset in cases: data = self.create_bardata(simulation_dt_func=lambda: minute) order = Order(dt=data.current_dt, asset=asset, amount=10) price, amount = model.process_order(data, order) avg_price = (data.current(asset, 'high') + data.current(asset, 'low')) / 2 expected_price = \ avg_price * (1 + model.NO_DATA_VOLATILITY_SLIPPAGE_IMPACT) self.assertAlmostEqual(price, expected_price, delta=0.001) self.assertEqual(amount, 10)
def test_low_transaction_volume(self): # With a volume limit of 0.001, and a bar volume of 100, we should # compute a transaction volume of 100 * 0.001 = 0.1, which gets rounded # down to zero. In this case we expect no amount to be transacted. model = VolatilityVolumeShare(volume_limit=0.001) minute = pd.Timestamp('2006-03-01 11:35AM', tz='UTC') data = self.create_bardata(simulation_dt_func=lambda: minute) order = Order(dt=data.current_dt, asset=self.ASSET, amount=10) price, amount = model.process_order(data, order) self.assertIsNone(price) self.assertIsNone(amount)
def test_impacted_price_worse_than_limit(self): model = VolatilityVolumeShare(volume_limit=0.05) # Use all the same numbers from the 'calculate_impact' tests. Since the # impacted price is 59805.5, which is worse than the limit price of # 59800, the model should return None. minute = pd.Timestamp('2006-03-01 11:35AM', tz='UTC') data = self.create_bardata(simulation_dt_func=lambda: minute) order = Order( dt=data.current_dt, asset=self.ASSET, amount=10, limit=59800, ) price, amount = model.process_order(data, order) self.assertIsNone(price) self.assertIsNone(amount)
def _calculate_impact(self, test_order, answer_key): model = VolatilityVolumeShare(volume_limit=0.05) first_minute = pd.Timestamp('2006-03-31 11:35AM', tz='UTC') next_3_minutes = self.trading_calendar.minutes_window(first_minute, 3) remaining_shares = test_order.open_amount for i, minute in enumerate(next_3_minutes): data = self.create_bardata(simulation_dt_func=lambda: minute) new_order = Order( dt=data.current_dt, asset=self.ASSET, amount=remaining_shares, ) price, amount = model.process_order(data, new_order) self.assertEqual(price, answer_key[i][0]) self.assertEqual(amount, answer_key[i][1]) amount = amount or 0 if remaining_shares < 0: remaining_shares = min(0, remaining_shares - amount) else: remaining_shares = max(0, remaining_shares - amount)
def test_calculate_impact_without_history(self): model = VolatilityVolumeShare(volume_limit=1) minutes = [ # Start day of the futures contract; no history yet. pd.Timestamp('2006-02-10 11:35AM', tz='UTC'), # Only a week's worth of history data. pd.Timestamp('2006-02-17 11:35AM', tz='UTC'), ] for minute in minutes: data = self.create_bardata(simulation_dt_func=lambda: minute) order = Order(dt=data.current_dt, asset=self.ASSET, amount=10) price, amount = model.process_order(data, order) avg_price = (data.current(self.ASSET, 'high') + data.current(self.ASSET, 'low')) / 2 expected_price = \ avg_price + (avg_price * NO_DATA_VOLATILITY_SLIPPAGE_IMPACT) self.assertEqual(price, expected_price) self.assertEqual(amount, 10)
def test_calculate_impact_without_history(self): model = VolatilityVolumeShare(volume_limit=1) minutes = [ # Start day of the futures contract; no history yet. pd.Timestamp('2006-02-10 11:35AM', tz='UTC'), # Only a week's worth of history data. pd.Timestamp('2006-02-17 11:35AM', tz='UTC'), ] for minute in minutes: data = self.create_bardata(simulation_dt_func=lambda: minute) order = Order(dt=data.current_dt, asset=self.ASSET, amount=10) price, amount = model.process_order(data, order) avg_price = ( data.current(self.ASSET, 'high') + data.current(self.ASSET, 'low') ) / 2 expected_price = \ avg_price + (avg_price * NO_DATA_VOLATILITY_SLIPPAGE_IMPACT) self.assertEqual(price, expected_price) self.assertEqual(amount, 10)