@@ -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 = False
988
989
):
989
990
"""
990
991
Initialize a backtest. Requires data and a strategy to test.
@@ -1029,6 +1030,9 @@ def __init__(self,
1029
1030
trade/position, making at most a single trade (long or short) in effect
1030
1031
at each time.
1031
1032
1033
+ if 'close_all_at_end' is 'False', the trade will not be close at end,
1034
+ and will not apear in _Stats.
1035
+
1032
1036
[FIFO]: https://www.investopedia.com/terms/n/nfa-compliance-rule-2-43b.asp
1033
1037
"""
1034
1038
@@ -1078,7 +1082,7 @@ def __init__(self,
1078
1082
warnings .warn ('Data index is not datetime. Assuming simple periods, '
1079
1083
'but `pd.DateTimeIndex` is advised.' ,
1080
1084
stacklevel = 2 )
1081
-
1085
+ self . _close_all_at_end = bool ( close_all_at_end )
1082
1086
self ._data : pd .DataFrame = data
1083
1087
self ._broker = partial (
1084
1088
_Broker , cash = cash , commission = commission , margin = margin ,
@@ -1164,14 +1168,15 @@ def run(self, **kwargs) -> pd.Series:
1164
1168
# Next tick, a moment before bar close
1165
1169
strategy .next ()
1166
1170
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 )
1171
+ if self ._close_all_at_end is True :
1172
+ # Close any remaining open trades so they produce some stats
1173
+ for trade in broker .trades :
1174
+ trade .close ()
1175
+
1176
+ # Re-run broker one last time to handle orders placed in the last strategy
1177
+ # iteration. Use the same OHLC values as in the last broker iteration.
1178
+ if start < len (self ._data ):
1179
+ try_ (broker .next , exception = _OutOfMoneyError )
1175
1180
1176
1181
# Set data back to full length
1177
1182
# for future `indicator._opts['data'].index` calls to work
0 commit comments