@@ -1081,11 +1081,81 @@ class Backtest:
1081
1081
Backtest a particular (parameterized) strategy
1082
1082
on particular data.
1083
1083
1084
- Upon initialization, call method
1084
+ Initialize a backtest. Requires data and a strategy to test.
1085
+ After initialization, you can call method
1085
1086
`backtesting.backtesting.Backtest.run` to run a backtest
1086
1087
instance, or `backtesting.backtesting.Backtest.optimize` to
1087
1088
optimize it.
1088
- """
1089
+
1090
+ `data` is a `pd.DataFrame` with columns:
1091
+ `Open`, `High`, `Low`, `Close`, and (optionally) `Volume`.
1092
+ If any columns are missing, set them to what you have available,
1093
+ e.g.
1094
+
1095
+ df['Open'] = df['High'] = df['Low'] = df['Close']
1096
+
1097
+ The passed data frame can contain additional columns that
1098
+ can be used by the strategy (e.g. sentiment info).
1099
+ DataFrame index can be either a datetime index (timestamps)
1100
+ or a monotonic range index (i.e. a sequence of periods).
1101
+
1102
+ `strategy` is a `backtesting.backtesting.Strategy`
1103
+ _subclass_ (not an instance).
1104
+
1105
+ `cash` is the initial cash to start with.
1106
+
1107
+ `spread` is the the constant bid-ask spread rate (relative to the price).
1108
+ E.g. set it to `0.0002` for commission-less forex
1109
+ trading where the average spread is roughly 0.2‰ of the asking price.
1110
+
1111
+ `commission` is the commission rate. E.g. if your broker's commission
1112
+ is 1% of order value, set commission to `0.01`.
1113
+ The commission is applied twice: at trade entry and at trade exit.
1114
+ Besides one single floating value, `commission` can also be a tuple of floating
1115
+ values `(fixed, relative)`. E.g. set it to `(100, .01)`
1116
+ if your broker charges minimum $100 + 1%.
1117
+ Additionally, `commission` can be a callable
1118
+ `func(order_size: int, price: float) -> float`
1119
+ (note, order size is negative for short orders),
1120
+ which can be used to model more complex commission structures.
1121
+ Negative commission values are interpreted as market-maker's rebates.
1122
+
1123
+ .. note::
1124
+ Before v0.4.0, the commission was only applied once, like `spread` is now.
1125
+ If you want to keep the old behavior, simply set `spread` instead.
1126
+
1127
+ .. note::
1128
+ With nonzero `commission`, long and short orders will be placed
1129
+ at an adjusted price that is slightly higher or lower (respectively)
1130
+ than the current price. See e.g.
1131
+ [#153](https://github.com/kernc/backtesting.py/issues/153),
1132
+ [#538](https://github.com/kernc/backtesting.py/issues/538),
1133
+ [#633](https://github.com/kernc/backtesting.py/issues/633).
1134
+
1135
+ `margin` is the required margin (ratio) of a leveraged account.
1136
+ No difference is made between initial and maintenance margins.
1137
+ To run the backtest using e.g. 50:1 leverge that your broker allows,
1138
+ set margin to `0.02` (1 / leverage).
1139
+
1140
+ If `trade_on_close` is `True`, market orders will be filled
1141
+ with respect to the current bar's closing price instead of the
1142
+ next bar's open.
1143
+
1144
+ If `hedging` is `True`, allow trades in both directions simultaneously.
1145
+ If `False`, the opposite-facing orders first close existing trades in
1146
+ a [FIFO] manner.
1147
+
1148
+ If `exclusive_orders` is `True`, each new order auto-closes the previous
1149
+ trade/position, making at most a single trade (long or short) in effect
1150
+ at each time.
1151
+
1152
+ If `finalize_trades` is `True`, the trades that are still
1153
+ [active and ongoing] at the end of the backtest will be closed on
1154
+ the last bar and will contribute to the computed backtest statistics.
1155
+
1156
+ [FIFO]: https://www.investopedia.com/terms/n/nfa-compliance-rule-2-43b.asp
1157
+ [active and ongoing]: https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#backtesting.backtesting.Strategy.trades
1158
+ """ # noqa: E501
1089
1159
def __init__ (self ,
1090
1160
data : pd .DataFrame ,
1091
1161
strategy : Type [Strategy ],
@@ -1099,79 +1169,6 @@ def __init__(self,
1099
1169
exclusive_orders = False ,
1100
1170
finalize_trades = False ,
1101
1171
):
1102
- """
1103
- Initialize a backtest. Requires data and a strategy to test.
1104
-
1105
- `data` is a `pd.DataFrame` with columns:
1106
- `Open`, `High`, `Low`, `Close`, and (optionally) `Volume`.
1107
- If any columns are missing, set them to what you have available,
1108
- e.g.
1109
-
1110
- df['Open'] = df['High'] = df['Low'] = df['Close']
1111
-
1112
- The passed data frame can contain additional columns that
1113
- can be used by the strategy (e.g. sentiment info).
1114
- DataFrame index can be either a datetime index (timestamps)
1115
- or a monotonic range index (i.e. a sequence of periods).
1116
-
1117
- `strategy` is a `backtesting.backtesting.Strategy`
1118
- _subclass_ (not an instance).
1119
-
1120
- `cash` is the initial cash to start with.
1121
-
1122
- `spread` is the the constant bid-ask spread rate (relative to the price).
1123
- E.g. set it to `0.0002` for commission-less forex
1124
- trading where the average spread is roughly 0.2‰ of the asking price.
1125
-
1126
- `commission` is the commission rate. E.g. if your broker's commission
1127
- is 1% of order value, set commission to `0.01`.
1128
- The commission is applied twice: at trade entry and at trade exit.
1129
- Besides one single floating value, `commission` can also be a tuple of floating
1130
- values `(fixed, relative)`. E.g. set it to `(100, .01)`
1131
- if your broker charges minimum $100 + 1%.
1132
- Additionally, `commission` can be a callable
1133
- `func(order_size: int, price: float) -> float`
1134
- (note, order size is negative for short orders),
1135
- which can be used to model more complex commission structures.
1136
- Negative commission values are interpreted as market-maker's rebates.
1137
-
1138
- .. note::
1139
- Before v0.4.0, the commission was only applied once, like `spread` is now.
1140
- If you want to keep the old behavior, simply set `spread` instead.
1141
-
1142
- .. note::
1143
- With nonzero `commission`, long and short orders will be placed
1144
- at an adjusted price that is slightly higher or lower (respectively)
1145
- than the current price. See e.g.
1146
- [#153](https://github.com/kernc/backtesting.py/issues/153),
1147
- [#538](https://github.com/kernc/backtesting.py/issues/538),
1148
- [#633](https://github.com/kernc/backtesting.py/issues/633).
1149
-
1150
- `margin` is the required margin (ratio) of a leveraged account.
1151
- No difference is made between initial and maintenance margins.
1152
- To run the backtest using e.g. 50:1 leverge that your broker allows,
1153
- set margin to `0.02` (1 / leverage).
1154
-
1155
- If `trade_on_close` is `True`, market orders will be filled
1156
- with respect to the current bar's closing price instead of the
1157
- next bar's open.
1158
-
1159
- If `hedging` is `True`, allow trades in both directions simultaneously.
1160
- If `False`, the opposite-facing orders first close existing trades in
1161
- a [FIFO] manner.
1162
-
1163
- If `exclusive_orders` is `True`, each new order auto-closes the previous
1164
- trade/position, making at most a single trade (long or short) in effect
1165
- at each time.
1166
-
1167
- If `finalize_trades` is `True`, the trades that are still
1168
- [active and ongoing] at the end of the backtest will be closed on
1169
- the last bar and will contribute to the computed backtest statistics.
1170
-
1171
- [FIFO]: https://www.investopedia.com/terms/n/nfa-compliance-rule-2-43b.asp
1172
- [active and ongoing]: https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#backtesting.backtesting.Strategy.trades
1173
- """ # noqa: E501
1174
-
1175
1172
if not (isinstance (strategy , type ) and issubclass (strategy , Strategy )):
1176
1173
raise TypeError ('`strategy` must be a Strategy sub-type' )
1177
1174
if not isinstance (data , pd .DataFrame ):
0 commit comments