paperswithbacktest 0.0.1__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- paperswithbacktest-0.2.1/LICENSE +201 -0
- paperswithbacktest-0.2.1/NOTICE +45 -0
- paperswithbacktest-0.2.1/PKG-INFO +343 -0
- paperswithbacktest-0.2.1/README.md +296 -0
- paperswithbacktest-0.2.1/paperswithbacktest.egg-info/PKG-INFO +343 -0
- paperswithbacktest-0.2.1/paperswithbacktest.egg-info/SOURCES.txt +100 -0
- paperswithbacktest-0.2.1/paperswithbacktest.egg-info/requires.txt +24 -0
- paperswithbacktest-0.2.1/pwb/__init__.py +11 -0
- paperswithbacktest-0.2.1/pwb/backtesting/__init__.py +84 -0
- paperswithbacktest-0.2.1/pwb/backtesting/backtest_engine.py +2380 -0
- paperswithbacktest-0.2.1/pwb/backtesting/base_strategy.py +46 -0
- paperswithbacktest-0.2.1/pwb/backtesting/checks.py +732 -0
- paperswithbacktest-0.2.1/pwb/backtesting/commission.py +1020 -0
- paperswithbacktest-0.2.1/pwb/backtesting/feeds.py +74 -0
- paperswithbacktest-0.2.1/pwb/backtesting/futures_run.py +573 -0
- paperswithbacktest-0.2.1/pwb/backtesting/indicators.py +267 -0
- paperswithbacktest-0.2.1/pwb/backtesting/lookahead.py +502 -0
- paperswithbacktest-0.2.1/pwb/backtesting/optimization_engine.py +168 -0
- paperswithbacktest-0.2.1/pwb/backtesting/option_costs.py +239 -0
- paperswithbacktest-0.2.1/pwb/backtesting/portfolio.py +134 -0
- paperswithbacktest-0.2.1/pwb/backtesting/strategies.py +672 -0
- paperswithbacktest-0.2.1/pwb/backtesting/universe.py +59 -0
- paperswithbacktest-0.2.1/pwb/datasets/__init__.py +3054 -0
- paperswithbacktest-0.2.1/pwb/datasets/french.py +573 -0
- paperswithbacktest-0.2.1/pwb/datasets/futures.py +374 -0
- paperswithbacktest-0.2.1/pwb/datasets/futures_chain.py +357 -0
- paperswithbacktest-0.2.1/pwb/datasets/index_currencies.py +411 -0
- paperswithbacktest-0.2.1/pwb/datasets/option_cache.py +327 -0
- paperswithbacktest-0.2.1/pwb/datasets/options.py +514 -0
- paperswithbacktest-0.2.1/pwb/datasets/pit_universe.py +262 -0
- paperswithbacktest-0.2.1/pwb/datasets/prepared_cache.py +617 -0
- paperswithbacktest-0.2.1/pwb/docs/__init__.py +28 -0
- paperswithbacktest-0.2.1/pwb/docs/__main__.py +20 -0
- paperswithbacktest-0.2.1/pwb/docs/backtesting.md +635 -0
- paperswithbacktest-0.2.1/pwb/docs/world_model.md +123 -0
- paperswithbacktest-0.2.1/pwb/engine.py +302 -0
- paperswithbacktest-0.2.1/pwb/execution/__init__.py +11 -0
- paperswithbacktest-0.2.1/pwb/execution/broker_factory.py +88 -0
- paperswithbacktest-0.2.1/pwb/execution/ccxt_connector.py +264 -0
- paperswithbacktest-0.2.1/pwb/execution/ib_connector.py +409 -0
- paperswithbacktest-0.2.1/pwb/execution/live_utils.py +135 -0
- paperswithbacktest-0.2.1/pwb/execution/optimal_limit_order.py +104 -0
- paperswithbacktest-0.2.1/pwb/performance/__init__.py +119 -0
- paperswithbacktest-0.2.1/pwb/performance/metrics.py +481 -0
- paperswithbacktest-0.2.1/pwb/performance/nav_metrics.py +55 -0
- paperswithbacktest-0.2.1/pwb/performance/plots.py +426 -0
- paperswithbacktest-0.2.1/pwb/performance/reports.py +216 -0
- paperswithbacktest-0.2.1/pwb/performance/trade_stats.py +141 -0
- paperswithbacktest-0.2.1/pwb/world_model/__init__.py +31 -0
- paperswithbacktest-0.2.1/pwb/world_model/_pit.py +267 -0
- paperswithbacktest-0.2.1/pwb/world_model/_source.py +182 -0
- paperswithbacktest-0.2.1/pwb/world_model/propagate.py +613 -0
- paperswithbacktest-0.2.1/pwb/world_model/reader.py +502 -0
- paperswithbacktest-0.2.1/pyproject.toml +3 -0
- paperswithbacktest-0.2.1/setup.cfg +68 -0
- paperswithbacktest-0.2.1/tests/test_api_cache.py +370 -0
- paperswithbacktest-0.2.1/tests/test_array_feed.py +129 -0
- paperswithbacktest-0.2.1/tests/test_bare_symbol_collisions.py +243 -0
- paperswithbacktest-0.2.1/tests/test_bond_rates.py +77 -0
- paperswithbacktest-0.2.1/tests/test_bounded_loader.py +304 -0
- paperswithbacktest-0.2.1/tests/test_compat_shim.py +118 -0
- paperswithbacktest-0.2.1/tests/test_currency_costs.py +102 -0
- paperswithbacktest-0.2.1/tests/test_docs_shipped.py +32 -0
- paperswithbacktest-0.2.1/tests/test_engine_parity.py +140 -0
- paperswithbacktest-0.2.1/tests/test_engine_runner.py +275 -0
- paperswithbacktest-0.2.1/tests/test_engine_selector.py +130 -0
- paperswithbacktest-0.2.1/tests/test_exposure_cap.py +321 -0
- paperswithbacktest-0.2.1/tests/test_forex_directions.py +226 -0
- paperswithbacktest-0.2.1/tests/test_french.py +314 -0
- paperswithbacktest-0.2.1/tests/test_futures_chain.py +181 -0
- paperswithbacktest-0.2.1/tests/test_futures_loader.py +206 -0
- paperswithbacktest-0.2.1/tests/test_futures_rank6.py +54 -0
- paperswithbacktest-0.2.1/tests/test_futures_run.py +226 -0
- paperswithbacktest-0.2.1/tests/test_harness_checks.py +359 -0
- paperswithbacktest-0.2.1/tests/test_holiday_bars.py +173 -0
- paperswithbacktest-0.2.1/tests/test_hub_snapshot.py +71 -0
- paperswithbacktest-0.2.1/tests/test_index_currencies.py +212 -0
- paperswithbacktest-0.2.1/tests/test_lookahead.py +259 -0
- paperswithbacktest-0.2.1/tests/test_option_cache.py +314 -0
- paperswithbacktest-0.2.1/tests/test_option_chain.py +208 -0
- paperswithbacktest-0.2.1/tests/test_option_chain_sources.py +252 -0
- paperswithbacktest-0.2.1/tests/test_option_costs.py +188 -0
- paperswithbacktest-0.2.1/tests/test_pit_exit.py +173 -0
- paperswithbacktest-0.2.1/tests/test_pit_hold.py +440 -0
- paperswithbacktest-0.2.1/tests/test_pit_hold_accounting.py +364 -0
- paperswithbacktest-0.2.1/tests/test_pit_hold_same_bar_mark.py +93 -0
- paperswithbacktest-0.2.1/tests/test_prepared_cache.py +441 -0
- paperswithbacktest-0.2.1/tests/test_quantile_signal.py +237 -0
- paperswithbacktest-0.2.1/tests/test_read_memo.py +478 -0
- paperswithbacktest-0.2.1/tests/test_resolve_reused_property.py +260 -0
- paperswithbacktest-0.2.1/tests/test_reused_tickers.py +134 -0
- paperswithbacktest-0.2.1/tests/test_same_bar_orders.py +165 -0
- paperswithbacktest-0.2.1/tests/test_sells_first.py +203 -0
- paperswithbacktest-0.2.1/tests/test_short_borrow.py +249 -0
- paperswithbacktest-0.2.1/tests/test_strategy_commission.py +136 -0
- paperswithbacktest-0.2.1/tests/test_universe_alias_costs.py +165 -0
- paperswithbacktest-0.2.1/tests/test_world_model.py +590 -0
- paperswithbacktest-0.2.1/tests/test_world_model_local.py +65 -0
- paperswithbacktest-0.2.1/tests/test_world_model_shock.py +316 -0
- paperswithbacktest-0.0.1/PKG-INFO +0 -28
- paperswithbacktest-0.0.1/README.md +0 -13
- paperswithbacktest-0.0.1/pyproject.toml +0 -17
- paperswithbacktest-0.0.1/setup.cfg +0 -4
- paperswithbacktest-0.0.1/src/paperswithbacktest.egg-info/PKG-INFO +0 -28
- paperswithbacktest-0.0.1/src/paperswithbacktest.egg-info/SOURCES.txt +0 -7
- paperswithbacktest-0.0.1/src/pwb/__init__.py +0 -3
- {paperswithbacktest-0.0.1/src → paperswithbacktest-0.2.1}/paperswithbacktest.egg-info/dependency_links.txt +0 -0
- {paperswithbacktest-0.0.1/src → paperswithbacktest-0.2.1}/paperswithbacktest.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
pwb-toolbox
|
|
2
|
+
Copyright 2022-2026 Papers With Backtest
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
5
|
+
this software except in compliance with the License. You may obtain a copy of
|
|
6
|
+
the License at http://www.apache.org/licenses/LICENSE-2.0.
|
|
7
|
+
|
|
8
|
+
License change
|
|
9
|
+
--------------
|
|
10
|
+
|
|
11
|
+
Starting with the first release published after version 0.1.53, pwb-toolbox is
|
|
12
|
+
distributed under the Apache License, Version 2.0.
|
|
13
|
+
|
|
14
|
+
Versions up to and including 0.1.53 were released under the MIT License. Those
|
|
15
|
+
releases, and copies obtained from them, remain available under the MIT License,
|
|
16
|
+
whose original notice is reproduced below:
|
|
17
|
+
|
|
18
|
+
Copyright (c) 2022-2023 Papers With Backtest and others
|
|
19
|
+
|
|
20
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
21
|
+
a copy of this software and associated documentation files (the
|
|
22
|
+
"Software"), to deal in the Software without restriction, including
|
|
23
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
24
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
25
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
26
|
+
the following conditions:
|
|
27
|
+
|
|
28
|
+
The above copyright notice and this permission notice shall be
|
|
29
|
+
included in all copies or substantial portions of the Software.
|
|
30
|
+
|
|
31
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
32
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
33
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
34
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
35
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
36
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
37
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
38
|
+
|
|
39
|
+
Contributors
|
|
40
|
+
------------
|
|
41
|
+
|
|
42
|
+
The git history of this repository up to version 0.1.53 contains no commit
|
|
43
|
+
authored outside Papers With Backtest: all code was written by Papers With
|
|
44
|
+
Backtest, partly with AI coding assistants. No third-party contribution
|
|
45
|
+
therefore remains under the MIT License only.
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: paperswithbacktest
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Papers With Backtest: datasets, backtesting harness and execution for quant traders (import pwb; formerly pwb-toolbox)
|
|
5
|
+
Home-page: https://paperswithbacktest.com
|
|
6
|
+
Author: Papers With Backtest
|
|
7
|
+
Author-email: hello@paperswithbacktest.com
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Project-URL: Homepage, https://paperswithbacktest.com
|
|
10
|
+
Project-URL: Documentation, https://paperswithbacktest.com/docs/
|
|
11
|
+
Project-URL: Strategies, https://paperswithbacktest.com/strategies
|
|
12
|
+
Project-URL: Datasets, https://paperswithbacktest.com/datasets
|
|
13
|
+
Project-URL: Source, https://github.com/paperswithbacktest/pwb-toolbox
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
License-File: NOTICE
|
|
23
|
+
Requires-Dist: tradewind>=0.0.4
|
|
24
|
+
Requires-Dist: backtrader
|
|
25
|
+
Requires-Dist: beautifulsoup4
|
|
26
|
+
Requires-Dist: ccxt
|
|
27
|
+
Requires-Dist: click
|
|
28
|
+
Requires-Dist: datasets
|
|
29
|
+
Requires-Dist: deap
|
|
30
|
+
Requires-Dist: ffn
|
|
31
|
+
Requires-Dist: huggingface_hub
|
|
32
|
+
Requires-Dist: ib_insync
|
|
33
|
+
Requires-Dist: matplotlib
|
|
34
|
+
Requires-Dist: numpy
|
|
35
|
+
Requires-Dist: pandas
|
|
36
|
+
Requires-Dist: pyarrow
|
|
37
|
+
Requires-Dist: python-dateutil
|
|
38
|
+
Requires-Dist: requests
|
|
39
|
+
Requires-Dist: scikit-learn
|
|
40
|
+
Requires-Dist: scipy
|
|
41
|
+
Requires-Dist: tqdm
|
|
42
|
+
Requires-Dist: transformers
|
|
43
|
+
Requires-Dist: webdriver_manager
|
|
44
|
+
Provides-Extra: tradewind
|
|
45
|
+
Requires-Dist: tradewind>=0.0.4; extra == "tradewind"
|
|
46
|
+
Dynamic: license-file
|
|
47
|
+
|
|
48
|
+
<div align="center">
|
|
49
|
+
<img src="static/images/systematic-trading.jpeg" height=200 alt=""/>
|
|
50
|
+
<h1>Papers With Backtest Toolbox</h1>
|
|
51
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" alt="License: Apache 2.0"/></a>
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
The `paperswithbacktest` package (import name `pwb`; formerly `pwb-toolbox`, import `pwb_toolbox`) is designed to provide tools and resources for systematic trading strategies. It includes datasets and strategy ideas to assist in developing and backtesting trading algorithms.
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
To install the package:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install paperswithbacktest
|
|
63
|
+
```
|
|
64
|
+
This package requires Python 3.10 or higher.
|
|
65
|
+
|
|
66
|
+
To use PWB datasets, you can supply a Papers With Backtest API key via the `PWB_API_KEY` environment variable. When that is set,
|
|
67
|
+
`load_dataset` will download parquet shards directly from the PWB API, four at a time, into a local cache
|
|
68
|
+
(`~/.cache/pwb_toolbox/api`, or `$PWB_CACHE_DIR/api`; capped at `PWB_API_CACHE_MAX_GB`, 10 GB by default, least recently
|
|
69
|
+
used files evicted first; `PWB_API_CACHE=0` turns it off). A file is kept under its content hash, so a second call, or a
|
|
70
|
+
new version of a dataset that leaves a month unchanged, downloads nothing for it. If no API key is available, you can
|
|
71
|
+
instead login to the Huggingface Hub (where public PWB datasets are hosted) with an access token:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
huggingface-cli login
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
What `get_pricing`, `load_dataset` and the cost schedules of the backtest harness return is also kept on disk between
|
|
78
|
+
processes (`~/.cache/pwb_toolbox/prepared`, or `$PWB_CACHE_DIR/prepared`; capped at `PWB_PREPARED_CACHE_MAX_GB`, 10 GB by
|
|
79
|
+
default; `PWB_CACHE=0` turns it off). An entry is keyed on the call, the version of the package and the exact revision
|
|
80
|
+
of every dataset read, so a new revision of a dataset is simply another entry; nothing expires by age. A run that falls
|
|
81
|
+
back to a degraded mode (a cost table it cannot read, a dataset whose revision is unknown) neither reads nor writes it.
|
|
82
|
+
|
|
83
|
+
`load_option_chain` keeps the bytes it fetches from `Options-Daily-Price` on disk too (`$PWB_CACHE_DIR/options`; capped
|
|
84
|
+
at `PWB_OPTIONS_CACHE_MAX_GB`, 20 GB by default; `PWB_CACHE=0` or `PWB_OPTIONS_CACHE=0` turns it off). Blocks are keyed
|
|
85
|
+
on the exact revision of the dataset and the file, so what is read back is what the Hub would have sent; a month read
|
|
86
|
+
once by any strategy is read again from disk, without a request. Offline, degraded, or without a known revision (files
|
|
87
|
+
listed by the subscriber API), it is neither read nor written.
|
|
88
|
+
|
|
89
|
+
Futures: `load_futures("CL")` reads `Futures-Daily-Price` (one row per contract and session; `kind="contracts"`,
|
|
90
|
+
`"continuous"` with `rank` and `adjust`, or `"chain"`), and `add_futures(engine, ["CL"], margins=...)` adds the
|
|
91
|
+
contracts to a Tradewind engine for a `tradewind.futures.Roller`. See [docs/datasets.md](docs/datasets.md).
|
|
92
|
+
|
|
93
|
+
## Usage
|
|
94
|
+
|
|
95
|
+
The `paperswithbacktest` package offers a range of functionalities for systematic trading analysis. Here are some examples of how to utilize the package:
|
|
96
|
+
|
|
97
|
+
### Datasets
|
|
98
|
+
|
|
99
|
+
The `pwb.datasets` module offers to load datasets for different asset classes, such as bonds, commodities, cryptocurrencies, ETFs, forex, indices, and stocks, using the `get_pricing` or the `load_dataset` functions:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
import pwb.datasets as pwb_ds
|
|
103
|
+
|
|
104
|
+
df = pwb_ds.get_pricing(["AAPL", "MSFT", "GOOGL"])
|
|
105
|
+
df = pwb_ds.load_dataset("Bonds-Daily-Price")
|
|
106
|
+
df = pwb_ds.load_dataset("Commodities-Daily-Price")
|
|
107
|
+
df = pwb_ds.load_dataset("Cryptocurrencies-Daily-Price")
|
|
108
|
+
df = pwb_ds.load_dataset("ETFs-Daily-Price")
|
|
109
|
+
df = pwb_ds.load_dataset("Forex-Daily-Price")
|
|
110
|
+
df = pwb_ds.load_dataset("Indices-Daily-Price")
|
|
111
|
+
df = pwb_ds.load_dataset("Stocks-Daily-Price")
|
|
112
|
+
df = pwb_ds.load_dataset("Preferreds-Daily-Price")
|
|
113
|
+
df = pwb_ds.load_dataset("Warrants-Daily-Price")
|
|
114
|
+
df = pwb_ds.load_dataset("StructuredProducts-Daily-Price")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The 1-minute datasets (`Stocks-1Min-Price`, `Cryptocurrencies-1Min-Price`, `Forex-1Min-Price`) are one parquet shard per month. Bound the read with `start`, `end` and `columns` so only the months and columns you need are downloaded and read:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
bars = pwb_ds.load_dataset(
|
|
121
|
+
"Stocks-1Min-Price",
|
|
122
|
+
symbols=["SPY", "QQQ"],
|
|
123
|
+
start="2024-01-01",
|
|
124
|
+
end="2024-03-31",
|
|
125
|
+
columns=["adj_open", "adj_close", "volume"],
|
|
126
|
+
)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Without `start`/`end`, a minute dataset still loads but warns (`UnboundedMinuteLoadWarning`): every month is downloaded, about 75 GB for the stocks.
|
|
130
|
+
|
|
131
|
+
Kenneth French's Data Library (Fama-French factors, momentum, reversals, industry and sorted portfolios) is loaded on the fly with `load_french("ff5")`, `load_french("ind48", frequency="daily")`, etc. These are data from Kenneth R. French's Data Library, downloaded on the user's machine and cached locally; they are not redistributed by Papers With Backtest.
|
|
132
|
+
|
|
133
|
+
For more, see [docs/datasets.md](/docs/datasets.md).
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
### Backtesting
|
|
137
|
+
|
|
138
|
+
The `pwb.backtesting` module offers simple building blocks for running Backtrader simulations.
|
|
139
|
+
|
|
140
|
+
Since 0.1.56 it runs on [Tradewind](https://github.com/paperswithbacktest/tradewind), the Apache 2.0 engine with the backtrader API, by default; backtrader stays available for one month. Choose once, before `pwb.backtesting` is imported: `PWB_ENGINE=backtrader` in the environment, or `pwb.engine.select("backtrader")` in code. The engine that was not chosen is never imported, and `pwb.backtesting.bt` is the one that was.
|
|
141
|
+
|
|
142
|
+
Here is a strategy example:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
import numpy as np
|
|
146
|
+
import backtrader as bt
|
|
147
|
+
import pwb.backtesting as pwb_bt
|
|
148
|
+
import pwb.datasets as pwb_ds
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# ────────────────────────────────────────────────────────────────
|
|
152
|
+
# Toy dual-momentum: each month hold SPY if its lookback return > T-bill,
|
|
153
|
+
# otherwise sit in BIL. Kept minimal while using pwb_bt & pwb_ds.
|
|
154
|
+
# ────────────────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
class SimpleMomentum(bt.Indicator):
|
|
157
|
+
"""12-month rate of change on close."""
|
|
158
|
+
lines = ("roc",)
|
|
159
|
+
params = (("period", 252),)
|
|
160
|
+
|
|
161
|
+
def __init__(self):
|
|
162
|
+
self.lines.roc = bt.indicators.RateOfChange(self.data.close, period=self.p.period)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class MonthlySwitcher(pwb_bt.BaseStrategy):
|
|
166
|
+
params = dict(
|
|
167
|
+
period=252, # ~12 months
|
|
168
|
+
risky="SPY",
|
|
169
|
+
safe="BIL",
|
|
170
|
+
leverage=1.0,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
def __init__(self):
|
|
174
|
+
super().__init__()
|
|
175
|
+
# Attach momentum indicators to each data feed
|
|
176
|
+
self.mom = {d._name: SimpleMomentum(d, period=self.p.period) for d in self.datas}
|
|
177
|
+
self._last_month = -1
|
|
178
|
+
|
|
179
|
+
def next(self):
|
|
180
|
+
super().next()
|
|
181
|
+
|
|
182
|
+
# Rebalance only on month change
|
|
183
|
+
today = self.datas[0].datetime.date(0)
|
|
184
|
+
if today.month == self._last_month:
|
|
185
|
+
return
|
|
186
|
+
self._last_month = today.month
|
|
187
|
+
|
|
188
|
+
# Use prior bar to emulate "signal at month-end, trade next month"
|
|
189
|
+
idx = -1
|
|
190
|
+
spy_m = float(self.mom[self.p.risky].roc[idx])
|
|
191
|
+
bil_m = float(self.mom[self.p.safe].roc[idx])
|
|
192
|
+
|
|
193
|
+
# Decide allocation
|
|
194
|
+
hold_risky = (not np.isnan(spy_m)) and (not np.isnan(bil_m)) and (spy_m > bil_m)
|
|
195
|
+
|
|
196
|
+
targets = {
|
|
197
|
+
self.p.risky: self.p.leverage if hold_risky else 0.0,
|
|
198
|
+
self.p.safe: 0.0 if hold_risky else self.p.leverage,
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
# Set portfolio targets
|
|
202
|
+
for d in self.datas:
|
|
203
|
+
self.order_target_percent(d, targets.get(d._name, 0.0))
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def run_strategy():
|
|
207
|
+
# Minimal universe fetched via pwb_ds inside pwb_bt
|
|
208
|
+
symbols = ["SPY", "BIL"]
|
|
209
|
+
|
|
210
|
+
result = pwb_bt.run_strategy(
|
|
211
|
+
indicator_cls=SimpleMomentum, # kept for compatibility, but not required externally
|
|
212
|
+
indicator_kwargs={"period": 252},
|
|
213
|
+
strategy_cls=MonthlySwitcher,
|
|
214
|
+
strategy_kwargs={"period": 252, "risky": "SPY", "safe": "BIL", "leverage": 1.0},
|
|
215
|
+
symbols=symbols,
|
|
216
|
+
start_date="2005-01-01",
|
|
217
|
+
cash=100_000.0,
|
|
218
|
+
)
|
|
219
|
+
return result
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
if __name__ == "__main__":
|
|
223
|
+
run_strategy()
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
To explore more, you can find over **140 strategy examples** at [https://paperswithbacktest.com/strategies](https://paperswithbacktest.com/strategies)).
|
|
227
|
+
|
|
228
|
+
For more about backtesting, see [docs/backtesting.md](/docs/backtesting.md).
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
### Execution
|
|
232
|
+
|
|
233
|
+
The execution helpers in `pwb.execution` can connect to brokers to run
|
|
234
|
+
strategies in real time. A typical session collects account information,
|
|
235
|
+
computes target positions and submits the necessary orders.
|
|
236
|
+
|
|
237
|
+
Two brokers are supporter today:
|
|
238
|
+
|
|
239
|
+
- Interactive Brokers
|
|
240
|
+
- CCXT (crypto)
|
|
241
|
+
|
|
242
|
+
For more about execution, see [docs/execution.md](/docs/execution.md).
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
### Performance Analysis
|
|
246
|
+
|
|
247
|
+
After running a live trading session, you can analyze the returned equity series using the
|
|
248
|
+
`pwb.performance` module.
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
from pwb.backtesting.examples import GoldenCrossAlpha, EqualWeightPortfolio
|
|
252
|
+
from pwb.backtesting import run_backtest
|
|
253
|
+
from pwb.backtesting.execution_models import ImmediateExecutionModel
|
|
254
|
+
from pwb.performance import total_return, cagr
|
|
255
|
+
from pwb.performance.plots import plot_equity_curve
|
|
256
|
+
|
|
257
|
+
result, equity = run_backtest(
|
|
258
|
+
ManualUniverseSelectionModel(["SPY", "QQQ"]),
|
|
259
|
+
GoldenCrossAlpha(),
|
|
260
|
+
EqualWeightPortfolio(),
|
|
261
|
+
execution=ImmediateExecutionModel(),
|
|
262
|
+
start="2015-01-01",
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
print("Total return:", total_return(equity))
|
|
266
|
+
print("CAGR:", cagr(equity))
|
|
267
|
+
|
|
268
|
+
plot_equity_curve(equity)
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Tools
|
|
272
|
+
|
|
273
|
+
### Interactive Brokers server
|
|
274
|
+
|
|
275
|
+
To trade PWB strategies live with Interactive Brokers, you can use `pwb-toolbox/tools/ib_server`.
|
|
276
|
+
|
|
277
|
+
On a ubuntu server (for instance from https://www.ovhcloud.com/), install [Miniconda](https://www.anaconda.com/), [IB TWS](https://www.interactivebrokers.com/), and RDP with:
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
cd pwb-toolbox/tools/ib_server
|
|
281
|
+
./install.sh
|
|
282
|
+
conda activate pwb
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
If TWS is already started:
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
PWB_API_KEY="" python -m execute_meta_strategy
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
If TWS isn'y already started:
|
|
292
|
+
|
|
293
|
+
```bash
|
|
294
|
+
TWS_USERNAME="" TWS_PASSWORD="" python -m launch_ib && PWB_API_KEY="" python -m execute_meta_strategy
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
And to run the strategy daily, define the environment variables in `.bashrc` and then set up the following cron:
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
30 9 * * Mon-Fri /bin/bash /path/to/run_daily.sh >> /path/to/logfile 2>&1
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
To get logs:
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
python -m monitor --logs-dir $HOME/pwb-data/ib/execution_logs
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
NB: Fix to restart the desktop environment:
|
|
311
|
+
|
|
312
|
+
```bash
|
|
313
|
+
ps aux | grep xfce4
|
|
314
|
+
sudo pkill -u ubuntu
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
## Contributing
|
|
319
|
+
|
|
320
|
+
Contributions to the `pwb-toolbox` package are welcome! If you have any improvements, new datasets, or strategy ideas to share, please follow these guidelines:
|
|
321
|
+
|
|
322
|
+
1. Fork the repository and create a new branch for your feature.
|
|
323
|
+
2. Make your changes and ensure they adhere to the package's coding style.
|
|
324
|
+
3. Write tests to validate the functionality or provide sample usage examples.
|
|
325
|
+
4. Submit a pull request, clearly explaining the purpose and benefits of your contribution.
|
|
326
|
+
|
|
327
|
+
To build the package, run:
|
|
328
|
+
|
|
329
|
+
```bash
|
|
330
|
+
python -m pip install --upgrade build
|
|
331
|
+
rm -r dist
|
|
332
|
+
python -m build
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
To upload the package to PyPI, run:
|
|
336
|
+
|
|
337
|
+
```bash
|
|
338
|
+
twine upload dist/*
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## License
|
|
342
|
+
|
|
343
|
+
`pwb-toolbox` is licensed under the [Apache License 2.0](LICENSE), starting with the first release after 0.1.53. Versions up to and including 0.1.53 were released under the MIT License; see [NOTICE](NOTICE).
|