Backtest a particular (parameterized) strategy on particular data. Initialize a backtest. Requires data and a strategy to test. After initialization, you can call method `backtesting.backtesting.Backtest.run` to run a backtest instance, or `backtesting.backtesting.Backtest.
| 1094 | |
| 1095 | |
| 1096 | class Backtest: |
| 1097 | """ |
| 1098 | Backtest a particular (parameterized) strategy |
| 1099 | on particular data. |
| 1100 | |
| 1101 | Initialize a backtest. Requires data and a strategy to test. |
| 1102 | After initialization, you can call method |
| 1103 | `backtesting.backtesting.Backtest.run` to run a backtest |
| 1104 | instance, or `backtesting.backtesting.Backtest.optimize` to |
| 1105 | optimize it. |
| 1106 | |
| 1107 | `data` is a `pd.DataFrame` with columns: |
| 1108 | `Open`, `High`, `Low`, `Close`, and (optionally) `Volume`. |
| 1109 | If any columns are missing, set them to what you have available, |
| 1110 | e.g. |
| 1111 | |
| 1112 | df['Open'] = df['High'] = df['Low'] = df['Close'] |
| 1113 | |
| 1114 | The passed data frame can contain additional columns that |
| 1115 | can be used by the strategy (e.g. sentiment info). |
| 1116 | DataFrame index can be either a datetime index (timestamps) |
| 1117 | or a monotonic range index (i.e. a sequence of periods). |
| 1118 | |
| 1119 | `strategy` is a `backtesting.backtesting.Strategy` |
| 1120 | _subclass_ (not an instance). |
| 1121 | |
| 1122 | `cash` is the initial cash to start with. |
| 1123 | |
| 1124 | `spread` is the the constant bid-ask spread rate (relative to the price). |
| 1125 | E.g. set it to `0.0002` for commission-less forex |
| 1126 | trading where the average spread is roughly 0.2‰ of the asking price. |
| 1127 | |
| 1128 | `commission` is the commission rate. E.g. if your broker's commission |
| 1129 | is 1% of order value, set commission to `0.01`. |
| 1130 | The commission is applied twice: at trade entry and at trade exit. |
| 1131 | Besides one single floating value, `commission` can also be a tuple of floating |
| 1132 | values `(fixed, relative)`. E.g. set it to `(100, .01)` |
| 1133 | if your broker charges minimum $100 + 1%. |
| 1134 | Additionally, `commission` can be a callable |
| 1135 | `func(order_size: int, price: float) -> float` |
| 1136 | (note, order size is negative for short orders), |
| 1137 | which can be used to model more complex commission structures. |
| 1138 | Negative commission values are interpreted as market-maker's rebates. |
| 1139 | |
| 1140 | .. note:: |
| 1141 | Before v0.4.0, the commission was only applied once, like `spread` is now. |
| 1142 | If you want to keep the old behavior, simply set `spread` instead. |
| 1143 | |
| 1144 | .. note:: |
| 1145 | With nonzero `commission`, long and short orders will be placed |
| 1146 | at an adjusted price that is slightly higher or lower (respectively) |
| 1147 | than the current price. See e.g. |
| 1148 | [#153](https://github.com/kernc/backtesting.py/issues/153), |
| 1149 | [#538](https://github.com/kernc/backtesting.py/issues/538), |
| 1150 | [#633](https://github.com/kernc/backtesting.py/issues/633). |
| 1151 | |
| 1152 | `margin` is the required margin (ratio) of a leveraged account. |
| 1153 | No difference is made between initial and maintenance margins. |
no outgoing calls