@@ -984,7 +984,8 @@ def __init__(self,
984
984
margin : float = 1. ,
985
985
trade_on_close = False ,
986
986
hedging = False ,
987
- exclusive_orders = False
987
+ exclusive_orders = False ,
988
+ close_all_at_end = True
988
989
):
989
990
"""
990
991
Initialize a backtest. Requires data and a strategy to test.
@@ -1078,7 +1079,7 @@ def __init__(self,
1078
1079
warnings .warn ('Data index is not datetime. Assuming simple periods, '
1079
1080
'but `pd.DateTimeIndex` is advised.' ,
1080
1081
stacklevel = 2 )
1081
-
1082
+ self . _close_all_at_end = close_all_at_end
1082
1083
self ._data : pd .DataFrame = data
1083
1084
self ._broker = partial (
1084
1085
_Broker , cash = cash , commission = commission , margin = margin ,
@@ -1164,14 +1165,15 @@ def run(self, **kwargs) -> pd.Series:
1164
1165
# Next tick, a moment before bar close
1165
1166
strategy .next ()
1166
1167
else :
1167
- # Close any remaining open trades so they produce some stats
1168
- for trade in broker .trades :
1169
- trade .close ()
1170
-
1171
- # Re-run broker one last time to handle orders placed in the last strategy
1172
- # iteration. Use the same OHLC values as in the last broker iteration.
1173
- if start < len (self ._data ):
1174
- try_ (broker .next , exception = _OutOfMoneyError )
1168
+ if self ._close_all_at_end is True :
1169
+ # Close any remaining open trades so they produce some stats
1170
+ for trade in broker .trades :
1171
+ trade .close ()
1172
+
1173
+ # Re-run broker one last time to handle orders placed in the last strategy
1174
+ # iteration. Use the same OHLC values as in the last broker iteration.
1175
+ if start < len (self ._data ):
1176
+ try_ (broker .next , exception = _OutOfMoneyError )
1175
1177
1176
1178
# Set data back to full length
1177
1179
# for future `indicator._opts['data'].index` calls to work
0 commit comments