From e3faf10deeba11c06e5b1292718377fee95d352b Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 21 Apr 2014 09:32:16 -0400 Subject: [PATCH] MAINT: Add tests for UnsupportedOrderParameters. Add a test case in test_algorithms to verify that appropriate exceptions are thrown if an algorithm makes a call to the order api with a stop/limit price and a style. --- tests/test_algorithm.py | 55 +++++++++++++++++------------- zipline/test_algorithms.py | 69 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 25 deletions(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index a9799a33..d32ea9d9 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -23,29 +23,31 @@ from zipline.utils.test_utils import setup_logger import zipline.utils.factory as factory import zipline.utils.simfactory as simfactory -from zipline.test_algorithms import (TestRegisterTransformAlgorithm, - RecordAlgorithm, - TestOrderAlgorithm, - TestOrderStyleForwardingAlgorithm, - TestOrderInstantAlgorithm, - TestOrderValueAlgorithm, - TestTargetAlgorithm, - TestOrderPercentAlgorithm, - TestTargetPercentAlgorithm, - TestTargetValueAlgorithm, - EmptyPositionsAlgorithm, - initialize_noop, - handle_data_noop, - initialize_api, - handle_data_api, - noop_algo, - api_algo, - api_symbol_algo, - call_all_order_methods, - record_variables, - record_float_magic, - AmbitiousStopLimitAlgorithm - ) +from zipline.test_algorithms import ( + AmbitiousStopLimitAlgorithm, + EmptyPositionsAlgorithm, + InvalidOrderAlgorithm, + RecordAlgorithm, + TestOrderAlgorithm, + TestOrderInstantAlgorithm, + TestOrderPercentAlgorithm, + TestOrderStyleForwardingAlgorithm, + TestOrderValueAlgorithm, + TestRegisterTransformAlgorithm, + TestTargetAlgorithm, + TestTargetPercentAlgorithm, + TestTargetValueAlgorithm, + api_algo, + api_symbol_algo, + call_all_order_methods, + handle_data_api, + handle_data_noop, + initialize_api, + initialize_noop, + noop_algo, + record_float_magic, + record_variables, +) from zipline.utils.test_utils import drain_zipline, assert_single_position @@ -123,6 +125,13 @@ class TestTransformAlgorithm(TestCase): with self.assertRaises(AssertionError): algo.run([self.source, self.df_source]) + def test_invalid_order_parameters(self): + algo = InvalidOrderAlgorithm( + sids=[133], + sim_params=self.sim_params + ) + algo.run(self.source) + def test_multi_source_as_input(self): sim_params = SimulationParameters( self.df.index[0], diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index 0d3dbb68..c5113536 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -36,7 +36,7 @@ The algorithm must expose methods: of the current state of the simulation universe. An example data object: .. This outputs the table as an HTML table but for some reason there - is no bounding box. Make the previous paragraph ending colon a + is no bounding box. Make the previous paraagraph ending colon a double-colon to turn this back into blockquoted table in ASCII art. +-----------------+--------------+----------------+-------------------+ @@ -74,12 +74,20 @@ The algorithm must expose methods: from copy import deepcopy import numpy as np +from nose.tools import assert_raises + from six.moves import range from six import itervalues from zipline.algorithm import TradingAlgorithm from zipline.api import FixedSlippage -from zipline.finance.execution import StopLimitOrder +from zipline.errors import UnsupportedOrderParameters +from zipline.finance.execution import ( + LimitOrder, + MarketOrder, + StopLimitOrder, + StopOrder, +) class TestAlgorithm(TradingAlgorithm): @@ -760,6 +768,63 @@ class EmptyPositionsAlgorithm(TradingAlgorithm): self.record(num_positions=len(self.portfolio.positions)) +class InvalidOrderAlgorithm(TradingAlgorithm): + """ + An algorithm that tries to make various invalid order calls, verifying that + appropriate exceptions are raised. + """ + def initialize(self, *args, **kwargs): + self.sid = kwargs.pop('sids')[0] + + def handle_data(self, data): + from zipline.api import ( + order_percent, + order_target, + order_target_percent, + order_target_value, + order_value, + ) + + for style in [MarketOrder(), LimitOrder(10), + StopOrder(10), StopLimitOrder(10, 10)]: + + with assert_raises(UnsupportedOrderParameters): + order(self.sid, 10, limit_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order(self.sid, 10, stop_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_value(self.sid, 300, limit_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_value(self.sid, 300, stop_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_percent(self.sid, .1, limit_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_percent(self.sid, .1, stop_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_target(self.sid, 100, limit_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_target(self.sid, 100, stop_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_target_value(self.sid, 100, limit_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_target_value(self.sid, 100, stop_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_target_percent(self.sid, .2, limit_price=10, style=style) + + with assert_raises(UnsupportedOrderParameters): + order_target_percent(self.sid, .2, stop_price=10, style=style) + + ############################## # Quantopian style algorithms from zipline.api import (order,