From 35dc84b850832fb947bd285672e3f28c3961fa62 Mon Sep 17 00:00:00 2001 From: themantalope Date: Mon, 1 Jan 2024 20:52:36 -0600 Subject: [PATCH 1/2] minor updates. added option to leave orders open at end of backtest --- .gitignore | 1 + backtesting/backtesting.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 67123f14..da08dd8c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ doc/build/* *~* .venv/ +build/* diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 9c168703..d39c4c10 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -1030,7 +1030,8 @@ def __init__(self, margin: float = 1., trade_on_close=False, hedging=False, - exclusive_orders=False + exclusive_orders=False, + leave_orders_open=False ): """ Initialize a backtest. Requires data and a strategy to test. @@ -1074,6 +1075,10 @@ def __init__(self, If `exclusive_orders` is `True`, each new order auto-closes the previous trade/position, making at most a single trade (long or short) in effect at each time. + + If `leave_orders_open` is `True`, trades are not automatically closed at + completion of the backtest. This allows strategies to be run in a "forward" + manner, where a user can see open trades with updated data. [FIFO]: https://www.investopedia.com/terms/n/nfa-compliance-rule-2-43b.asp """ @@ -1133,6 +1138,7 @@ def __init__(self, ) self._strategy = strategy self._results: Optional[pd.Series] = None + self.leave_orders_open = leave_orders_open def run(self, **kwargs) -> pd.Series: """ @@ -1218,9 +1224,10 @@ def run(self, **kwargs) -> pd.Series: # Next tick, a moment before bar close strategy.next() else: - # Close any remaining open trades so they produce some stats - for trade in broker.trades: - trade.close() + # Close any remaining open trades so they produce some stats if self.leave_orders_open is False + if self.leave_orders_open is False: + for trade in broker.trades: + trade.close() # Re-run broker one last time to handle orders placed in the last strategy # iteration. Use the same OHLC values as in the last broker iteration. From 84de9ca2ecdbbe0f2649b3c379cc1c1297464af3 Mon Sep 17 00:00:00 2001 From: themantalope Date: Mon, 1 Jan 2024 21:04:26 -0600 Subject: [PATCH 2/2] formatting --- backtesting/backtesting.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index d39c4c10..d5812c4b 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -1074,9 +1074,9 @@ def __init__(self, If `exclusive_orders` is `True`, each new order auto-closes the previous trade/position, making at most a single trade (long or short) in effect - at each time. + at each time. - If `leave_orders_open` is `True`, trades are not automatically closed at + If `leave_orders_open` is `True`, trades are not automatically closed at completion of the backtest. This allows strategies to be run in a "forward" manner, where a user can see open trades with updated data. @@ -1224,7 +1224,8 @@ def run(self, **kwargs) -> pd.Series: # Next tick, a moment before bar close strategy.next() else: - # Close any remaining open trades so they produce some stats if self.leave_orders_open is False + # Close any remaining open trades so they produce some stats + # if self.leave_orders_open is False if self.leave_orders_open is False: for trade in broker.trades: trade.close()