diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index e8781d66e9..c841fa88a3 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -23,7 +23,7 @@ import logbook import toolz from logbook import TestHandler, WARNING -from mock import MagicMock +from mock import MagicMock, patch from nose_parameterized import parameterized from six import iteritems, itervalues, string_types from six.moves import range @@ -3526,6 +3526,24 @@ def handle_data(algo, data): env=self.env) self.check_algo_succeeds(algo, handle_data) + def test_end_of_day_leverage_enforced(self): + # Patch handle data so that it does not perform account validation. + def mocked_handle_data(self, data): + if self._handle_data: + self._handle_data(self, data) + + with patch.object(TradingAlgorithm, 'handle_data', mocked_handle_data): + def handle_data(algo, data): + algo.order(algo.sid(self.sidint), 1) + + algo = SetMaxLeverageAlgorithm( + max_leverage=0, sim_params=self.sim_params, env=self.env, + ) + + # The algo should still fail the account validation because we are + # checking it at the end of the day. + self.check_algo_fails(algo, handle_data) + # FIXME re-implement this testcase in q2 # class TestClosePosAlgo(TestCase): diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 1d5b372f8c..45732694e8 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -229,6 +229,7 @@ def calculate_minute_capital_changes(dt): if emission_rate == 'daily': handle_benchmark(normalize_date(dt)) execute_order_cancellation_policy() + algo.validate_account_controls() yield self._get_daily_message(dt, algo, algo.perf_tracker) elif action == BEFORE_TRADING_START_BAR: