Skip to content

Commit

Permalink
Merge 872b6a9 into 0848a8a
Browse files Browse the repository at this point in the history
  • Loading branch information
richafrank committed Dec 21, 2016
2 parents 0848a8a + 872b6a9 commit 04fd120
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 110 deletions.
4 changes: 0 additions & 4 deletions tests/pipeline/test_quarters_estimates.py
Expand Up @@ -453,10 +453,6 @@ def create_estimates_df(cls,
SID_FIELD_NAME: sid,
})

@classmethod
def init_class_fixtures(cls):
super(WithEstimatesTimeZero, cls).init_class_fixtures()

def get_expected_estimate(self,
q1_knowledge,
q2_knowledge,
Expand Down
133 changes: 123 additions & 10 deletions tests/test_algorithm.py
Expand Up @@ -98,6 +98,7 @@
to_utc,
trades_by_sid_to_dfs,
)
from zipline.testing import RecordBatchBlotter
from zipline.testing.fixtures import (
WithDataPortal,
WithLogger,
Expand All @@ -123,6 +124,7 @@
TestTargetAlgorithm,
TestTargetPercentAlgorithm,
TestTargetValueAlgorithm,
TestBatchTargetPercentAlgorithm,
SetLongOnlyAlgorithm,
SetAssetDateBoundsAlgorithm,
SetMaxPositionSizeAlgorithm,
Expand Down Expand Up @@ -170,6 +172,7 @@
set_benchmark_algo,
no_handle_data,
)
from zipline.testing.predicates import assert_equal
from zipline.utils.api_support import ZiplineAPI, set_algo_instance
from zipline.utils.calendars import get_calendar, register_calendar
from zipline.utils.context_tricks import CallbackManager
Expand Down Expand Up @@ -905,14 +908,15 @@ def test_data_frequency_setting(self):
self.assertEqual(algo.sim_params.data_frequency, 'minute')

@parameterized.expand([
(TestOrderAlgorithm,),
(TestOrderValueAlgorithm,),
(TestTargetAlgorithm,),
(TestOrderPercentAlgorithm,),
(TestTargetPercentAlgorithm,),
(TestTargetValueAlgorithm,),
('order', TestOrderAlgorithm,),
('order_value', TestOrderValueAlgorithm,),
('order_target', TestTargetAlgorithm,),
('order_percent', TestOrderPercentAlgorithm,),
('order_target_percent', TestTargetPercentAlgorithm,),
('order_target_value', TestTargetValueAlgorithm,),
('batch_order_target_percent', TestBatchTargetPercentAlgorithm,),
])
def test_order_methods(self, algo_class):
def test_order_methods(self, test_name, algo_class):
algo = algo_class(
sim_params=self.sim_params,
env=self.env,
Expand All @@ -935,7 +939,7 @@ def test_order_methods_for_future(self, algo_class):
sim_params=self.sim_params,
env=self.env,
)
# Ensure that the environment's asset 0 is a Future
# Ensure that the environment's asset 3 is a Future
asset_to_test = algo.sid(3)
self.assertIsInstance(asset_to_test, Future)

Expand Down Expand Up @@ -1015,7 +1019,7 @@ def test_minute_data(self, algo_class):
sim_params = SimulationParameters(
start_session=start_session,
end_session=period_end,
capital_base=float("1.0e5"),
capital_base=1.0e5,
data_frequency='minute',
trading_calendar=self.trading_calendar,
)
Expand Down Expand Up @@ -1735,6 +1739,115 @@ def test_order_methods(self):
)
test_algo.run(self.data_portal)

def test_batch_order_target_percent_matches_multi_order(self):
weights = pd.Series([.3, .7])

multi_blotter = RecordBatchBlotter(self.SIM_PARAMS_DATA_FREQUENCY,
self.asset_finder)
multi_test_algo = TradingAlgorithm(
script=dedent("""\
from collections import OrderedDict
from six import iteritems
from zipline.api import sid, order_target_percent
def initialize(context):
context.assets = [sid(0), sid(3)]
context.placed = False
def handle_data(context, data):
if not context.placed:
for asset, weight in iteritems(OrderedDict(zip(
context.assets, {weights}
))):
order_target_percent(asset, weight)
context.placed = True
""").format(weights=list(weights)),
blotter=multi_blotter,
env=self.env,
)
multi_stats = multi_test_algo.run(self.data_portal)
self.assertFalse(multi_blotter.order_batch_called)

batch_blotter = RecordBatchBlotter(self.SIM_PARAMS_DATA_FREQUENCY,
self.asset_finder)
batch_test_algo = TradingAlgorithm(
script=dedent("""\
from collections import OrderedDict
from zipline.api import sid, batch_order_target_percent
def initialize(context):
context.assets = [sid(0), sid(3)]
context.placed = False
def handle_data(context, data):
if not context.placed:
orders = batch_order_target_percent(OrderedDict(zip(
context.assets, {weights}
)))
assert len(orders) == 2, \
"len(orders) was %s but expected 2" % len(orders)
for o in orders:
assert o is not None, "An order is None"
context.placed = True
""").format(weights=list(weights)),
blotter=batch_blotter,
env=self.env,
)
batch_stats = batch_test_algo.run(self.data_portal)
self.assertTrue(batch_blotter.order_batch_called)

for stats in (multi_stats, batch_stats):
stats.orders = stats.orders.apply(
lambda orders: [toolz.dissoc(o, 'id') for o in orders]
)
stats.transactions = stats.transactions.apply(
lambda txns: [toolz.dissoc(txn, 'order_id') for txn in txns]
)
assert_equal(multi_stats, batch_stats)

def test_batch_order_target_percent_filters_null_orders(self):
weights = pd.Series([1, 0])

batch_blotter = RecordBatchBlotter(self.SIM_PARAMS_DATA_FREQUENCY,
self.asset_finder)
batch_test_algo = TradingAlgorithm(
script=dedent("""\
from collections import OrderedDict
from zipline.api import sid, batch_order_target_percent
def initialize(context):
context.assets = [sid(0), sid(3)]
context.placed = False
def handle_data(context, data):
if not context.placed:
orders = batch_order_target_percent(OrderedDict(zip(
context.assets, {weights}
)))
assert len(orders) == 1, \
"len(orders) was %s but expected 1" % len(orders)
for o in orders:
assert o is not None, "An order is None"
context.placed = True
""").format(weights=list(weights)),
blotter=batch_blotter,
env=self.env,
)
batch_test_algo.run(self.data_portal)
self.assertTrue(batch_blotter.order_batch_called)

def test_order_dead_asset(self):
# after asset 0 is dead
params = SimulationParameters(
Expand Down Expand Up @@ -3628,7 +3741,7 @@ def init_class_fixtures(cls):
cls.first_asset_expiration = cls.test_days[2]

def make_data(self, auto_close_delta, frequency,
capital_base=float("1.0e5")):
capital_base=1.0e5):

asset_info = make_jagged_equity_info(
num_assets=3,
Expand Down

0 comments on commit 04fd120

Please sign in to comment.