diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index cb900afb..4101f48b 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -1290,6 +1290,7 @@ class TestAccountControls(TestCase): algo = SetMaxLeverageAlgorithm(1) self.check_algo_succeeds(algo, handle_data) + class TestClosePosAlgo(TestCase): def setUp(self): diff --git a/zipline/finance/performance/tracker.py b/zipline/finance/performance/tracker.py index fdfd91e7..1b8b21e2 100644 --- a/zipline/finance/performance/tracker.py +++ b/zipline/finance/performance/tracker.py @@ -337,6 +337,11 @@ class PerformanceTracker(object): self.all_benchmark_returns[midnight] = event.returns def process_close_position(self, event): + + # CLOSE_POSITION events contain prices that must be handled as a final + # trade event + self.process_trade(event) + txn = self.position_tracker.create_close_position_transaction(event) if txn: self.process_transaction(txn) diff --git a/zipline/sources/simulated.py b/zipline/sources/simulated.py index 45a91ed9..d0a9c63d 100644 --- a/zipline/sources/simulated.py +++ b/zipline/sources/simulated.py @@ -23,7 +23,7 @@ import pandas as pd from zipline.sources.data_source import DataSource from zipline.utils import tradingcalendar as calendar_nyse from zipline.gens.utils import hash_args -from zipline.finance import trading +from zipline.finance.trading import TradingEnvironment class RandomWalkSource(DataSource): @@ -93,7 +93,9 @@ class RandomWalkSource(DataSource): self.sd = sd self.sids = self.start_prices.keys() - trading.environment.update_asset_finder(identifiers=self.sids) + TradingEnvironment.instance().update_asset_finder( + identifiers=self.sids + ) self.open_and_closes = \ calendar.open_and_closes[self.start:self.end]