Commit b0beb00
committed
Overhaul backtesting engine
1.
A `Decision` no longer only represents a set of weights in a target
portfolio. It is now an extensible union of actions that can be taken as
part of a strategy, with differing layers of abstraction.
The original `Decision` -- which was mainly parameterised by a set of
normalised weights and a cash weight -- is now known as a
`TargetWeightsDecision`. Other decisions involve a `MakeTradeDecision`,
or a `ReallocateDecision`.
The public API for creating these decision objects is intended to be one
of the free functions provided in backtest_lib.engine.decision. See the
updated notebooks for examples on how a decision should be constructed
by the end-user.
TODO: Re-export the free decision functions into the top-level library
import.
2.
The application of the strategy to the portfolio is now handled by an
`Engine`. An `Engine` consists of a `PlanGenerator` and a
`PlanExecutor`.
A `PlanGenerator` takes a `Decision` as input (along with some market
context) and returns a `Plan` which holds sequence of `PlanOp`s. The
exact union of ops that can constitute a plan generated by a
`PlanGenerator[T]` (that is, a `Plan[T]`) is T. This allows for varying
levels of realism, the highest of which possibly being a plan that
generates nothing except `MakeTradeOp`s, wherein the generator would
have type `PlanGenerator[MakeTradeOp]`.
Similarly, a `PlanExecutor[T]` will take a `Plan[T]`, as well as a
portfolio on which to execute (and market conditions), and produce an
artifact containing an "after" portfolio as well as a summary of the
effects of executing the plan (possibly with data representing "fees
paid" and actual fills etc.). This allows, again, for increasing levels
of realism (sophisticated slippage modelling etc.), and the ability to
pair any executor to any planner as long as they work with compatible
ops. This compatibility is statically checked at type-checking time.
Currently, the result of this execution is stored as an
`ExecutionResult`.
3.
This commit also fixes some typechecker errors and touches a bunch of
extra parts of the codebase because I got distracted.1 parent 93eb23d commit b0beb00
File tree
27 files changed
+1321
-1350
lines changed- src/backtest_lib
- backtest
- engine
- decision
- execute
- plan
- examples
- market
- polars_impl
- portfolio
- strategy
- universe
- tests/engine/plan
27 files changed
+1321
-1350
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | 19 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | | - | |
16 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| |||
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
| 121 | + | |
118 | 122 | | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
122 | | - | |
123 | 126 | | |
124 | 127 | | |
| 128 | + | |
125 | 129 | | |
126 | 130 | | |
| 131 | + | |
127 | 132 | | |
128 | 133 | | |
129 | 134 | | |
130 | 135 | | |
131 | | - | |
| 136 | + | |
132 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
133 | 142 | | |
134 | 143 | | |
135 | 144 | | |
| |||
142 | 151 | | |
143 | 152 | | |
144 | 153 | | |
145 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
146 | 161 | | |
147 | 162 | | |
148 | 163 | | |
149 | 164 | | |
150 | | - | |
151 | 165 | | |
152 | 166 | | |
153 | | - | |
154 | | - | |
| 167 | + | |
155 | 168 | | |
156 | 169 | | |
157 | 170 | | |
158 | 171 | | |
159 | 172 | | |
160 | 173 | | |
| 174 | + | |
161 | 175 | | |
162 | 176 | | |
163 | | - | |
| 177 | + | |
164 | 178 | | |
165 | 179 | | |
166 | 180 | | |
| |||
173 | 187 | | |
174 | 188 | | |
175 | 189 | | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
180 | 196 | | |
| 197 | + | |
181 | 198 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | 199 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
212 | 213 | | |
213 | 214 | | |
214 | 215 | | |
215 | | - | |
| 216 | + | |
216 | 217 | | |
217 | 218 | | |
218 | 219 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | 220 | | |
240 | | - | |
241 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
242 | 229 | | |
243 | | - | |
244 | 230 | | |
245 | | - | |
246 | 231 | | |
247 | 232 | | |
248 | | - | |
| 233 | + | |
249 | 234 | | |
250 | 235 | | |
251 | 236 | | |
252 | 237 | | |
253 | 238 | | |
254 | | - | |
255 | 239 | | |
256 | | - | |
257 | | - | |
| 240 | + | |
| 241 | + | |
258 | 242 | | |
259 | 243 | | |
260 | 244 | | |
| |||
285 | 269 | | |
286 | 270 | | |
287 | 271 | | |
| 272 | + | |
288 | 273 | | |
| 274 | + | |
289 | 275 | | |
290 | 276 | | |
291 | 277 | | |
292 | 278 | | |
293 | | - | |
| 279 | + | |
294 | 280 | | |
295 | 281 | | |
296 | 282 | | |
297 | 283 | | |
298 | 284 | | |
299 | 285 | | |
300 | 286 | | |
301 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
302 | 290 | | |
303 | 291 | | |
304 | 292 | | |
305 | | - | |
| 293 | + | |
306 | 294 | | |
307 | 295 | | |
308 | 296 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
| |||
246 | 244 | | |
247 | 245 | | |
248 | 246 | | |
249 | | - | |
| 247 | + | |
250 | 248 | | |
251 | 249 | | |
252 | 250 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
0 commit comments