Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 804d77f

Browse filesBrowse files
committed
ENH: Warn on remaining open trades
#1281
1 parent f8f4969 commit 804d77f
Copy full SHA for 804d77f

2 files changed

+7-1Lines changed: 7 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎backtesting/backtesting.py‎

Copy file name to clipboardExpand all lines: backtesting/backtesting.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,11 @@ def run(self, **kwargs) -> pd.Series:
13431343
# strategy iteration. Use the same OHLC values as in the last broker iteration.
13441344
if start < len(self._data):
13451345
try_(broker.next, exception=_OutOfMoneyError)
1346+
elif len(broker.trades):
1347+
warnings.warn(
1348+
'Some trades remain open at the end of backtest. Use '
1349+
'`Backtest(..., finalize_trades=True)` to close them and '
1350+
'include them in stats.', stacklevel=2)
13461351

13471352
# Set data back to full length
13481353
# for future `indicator._opts['data'].index` calls to work
Collapse file

‎backtesting/test/_test.py‎

Copy file name to clipboardExpand all lines: backtesting/test/_test.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ def next(self):
433433
elif len(self.data) == len(SHORT_DATA):
434434
self.position.close()
435435

436-
self.assertTrue(Backtest(SHORT_DATA, S, finalize_trades=False).run()._trades.empty)
436+
with self.assertWarnsRegex(UserWarning, 'finalize_trades'):
437+
self.assertTrue(Backtest(SHORT_DATA, S, finalize_trades=False).run()._trades.empty)
437438
self.assertFalse(Backtest(SHORT_DATA, S, finalize_trades=True).run()._trades.empty)
438439

439440
def test_check_adjusted_price_when_placing_order(self):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.