TST: Adds TestingSlippage slippage model (#1679)

Allows specifying a constant number of shares filled per tick.

Also adds the WithConstantEquityMinuteBarData fixture, relocated from
internal repo.
This commit is contained in:
Andrew Daniels
2017-02-09 08:56:15 -05:00
committed by GitHub
parent e81287e8e1
commit c2560c6079
3 changed files with 127 additions and 0 deletions
+64
View File
@@ -6,12 +6,22 @@ from unittest import TestCase
from numpy import array, empty
from zipline._protocol import BarData
from zipline.finance.asset_restrictions import NoRestrictions
from zipline.finance.order import Order
from zipline.testing import (
check_arrays,
make_alternating_boolean_array,
make_cascading_boolean_array,
parameter_space,
)
from zipline.testing.fixtures import (
WithConstantEquityMinuteBarData,
WithDataPortal,
ZiplineTestCase,
)
from zipline.testing.slippage import TestingSlippage
from zipline.utils.numpy_utils import bool_dtype
@@ -109,3 +119,57 @@ class TestMakeBooleanArray(TestCase):
make_cascading_boolean_array((3, 0)),
empty((3, 0), dtype=bool_dtype),
)
class TestTestingSlippage(WithConstantEquityMinuteBarData,
WithDataPortal,
ZiplineTestCase):
ASSET_FINDER_EQUITY_SYMBOLS = ('A',)
ASSET_FINDER_EQUITY_SIDS = (1,)
@classmethod
def init_class_fixtures(cls):
super(TestTestingSlippage, cls).init_class_fixtures()
cls.asset = cls.asset_finder.retrieve_asset(1)
cls.minute, _ = (
cls.trading_calendar.open_and_close_for_session(cls.START_DATE)
)
def init_instance_fixtures(self):
super(TestTestingSlippage, self).init_instance_fixtures()
self.bar_data = BarData(
self.data_portal,
lambda: self.minute,
"minute",
self.trading_calendar,
NoRestrictions()
)
def make_order(self, amount):
return Order(
self.minute,
self.asset,
amount,
)
def test_constant_filled_per_tick(self):
filled_per_tick = 1
model = TestingSlippage(filled_per_tick)
order = self.make_order(100)
price, volume = model.process_order(self.bar_data, order)
self.assertEqual(price, self.EQUITY_MINUTE_CONSTANT_CLOSE)
self.assertEqual(volume, filled_per_tick)
def test_fill_all(self):
filled_per_tick = TestingSlippage.ALL
order_amount = 100
model = TestingSlippage(filled_per_tick)
order = self.make_order(order_amount)
price, volume = model.process_order(self.bar_data, order)
self.assertEqual(price, self.EQUITY_MINUTE_CONSTANT_CLOSE)
self.assertEqual(volume, order_amount)