diff --git a/cvxportfolio/simulator.py b/cvxportfolio/simulator.py index 008e8a8c1..e1af87c6b 100644 --- a/cvxportfolio/simulator.py +++ b/cvxportfolio/simulator.py @@ -533,7 +533,7 @@ def _simulate(self, t, t_next, h, policy, mask=None, **kwargs): current_volumes = current_and_past_volumes.iloc[-1] non_tradable_stocks = current_volumes[current_volumes <= 0].index if len(non_tradable_stocks): - logger.info( + logging.info( f"At time {t} the simulator canceled trades on assets {non_tradable_stocks}" " because their market volumes for the period are zero.") u[non_tradable_stocks] = 0. diff --git a/cvxportfolio/tests/test_simulator.py b/cvxportfolio/tests/test_simulator.py index 45ea6cf61..2e8d3e58e 100644 --- a/cvxportfolio/tests/test_simulator.py +++ b/cvxportfolio/tests/test_simulator.py @@ -754,6 +754,22 @@ def test_hyperparameters_optimize(self): base_location=self.datadir) simulator.optimize_hyperparameters(policy, start_time='2023-01-01') + + def test_cancel_trades(self): + + sim = cvx.StockMarketSimulator( + ['AAPL', 'ZM'], + trading_frequency='monthly', + base_location=self.datadir) + + sim.market_data.volumes['ZM'] = 0. + + objective = cvx.ReturnsForecast() - 5 * cvx.FullCovariance() + policy = cvx.SinglePeriodOptimization( + objective, [cvx.LongOnly(), cvx.LeverageLimit(1)]) + + sim.backtest(policy, start_time='2023-01-01') + if __name__ == '__main__':