vectorbt 1.1.1__tar.gz → 1.2.0__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.
- {vectorbt-1.1.1/vectorbt.egg-info → vectorbt-1.2.0}/PKG-INFO +93 -25
- {vectorbt-1.1.1 → vectorbt-1.2.0}/README.md +89 -23
- {vectorbt-1.1.1 → vectorbt-1.2.0}/pyproject.toml +3 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_base.py +205 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_engine.py +131 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_generic.py +23 -6
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_indicators.py +75 -135
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_labels.py +18 -51
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_plotting.py +40 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_portfolio.py +194 -7
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_records.py +122 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_returns.py +216 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_utils.py +72 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/_version.py +1 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/array_wrapper.py +2 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/indexing.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/reshape_fns.py +7 -3
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/data/base.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/data/custom.py +3 -3
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/data/updater.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/accessors.py +14 -15
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/nb.py +25 -13
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/ranges.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/splitters.py +3 -3
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/indicators/basic.py +10 -5
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/indicators/factory.py +3 -3
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/indicators/nb.py +18 -3
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/labels/generators.py +4 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/labels/nb.py +4 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/base.py +9 -6
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/trades.py +33 -23
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/returns/accessors.py +87 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/returns/dispatch.py +53 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/returns/nb.py +77 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/accessors.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/array_.py +1 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/config.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/datetime_.py +24 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/decorators.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/params.py +2 -2
- {vectorbt-1.1.1 → vectorbt-1.2.0/vectorbt.egg-info}/PKG-INFO +93 -25
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt.egg-info/requires.txt +3 -1
- {vectorbt-1.1.1 → vectorbt-1.2.0}/LICENSE.md +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/MANIFEST.in +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/setup.cfg +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/setup.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_data.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_settings.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/tests/test_signals.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/_engine.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/_settings.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/_typing.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/accessors.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/column_grouper.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/combine_fns.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/base/index_fns.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/data/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/decorators.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/dispatch.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/drawdowns.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/enums.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/plots_builder.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/plotting.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/generic/stats_builder.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/indicators/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/indicators/configs.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/indicators/dispatch.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/labels/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/labels/dispatch.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/labels/enums.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/messaging/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/messaging/telegram.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/ohlcv_accessors.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/decorators.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/dispatch.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/enums.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/logs.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/nb.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/portfolio/orders.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/px_accessors.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/base.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/col_mapper.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/decorators.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/dispatch.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/mapped_array.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/records/nb.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/returns/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/returns/metrics.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/returns/qs_adapter.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/root_accessors.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/dispatch.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/enums.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/factory.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/generators.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/signals/nb.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/templates/dark.json +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/templates/light.json +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/templates/seaborn.json +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/__init__.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/attr_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/checks.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/colors.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/docs.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/enum_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/figure.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/image_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/mapping.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/math_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/module_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/random_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/requests_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/schedule_.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/tags.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt/utils/template.py +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt.egg-info/SOURCES.txt +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt.egg-info/dependency_links.txt +0 -0
- {vectorbt-1.1.1 → vectorbt-1.2.0}/vectorbt.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vectorbt
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Python library for backtesting and analyzing trading strategies at scale
|
|
5
5
|
Author-email: Oleg Polakow <olegpolakow@vectorbt.pro>
|
|
6
6
|
Project-URL: Homepage, https://github.com/polakowo/vectorbt
|
|
@@ -24,6 +24,7 @@ Requires-Dist: pandas<4.0,>=3.0.3
|
|
|
24
24
|
Requires-Dist: scipy
|
|
25
25
|
Requires-Dist: matplotlib
|
|
26
26
|
Requires-Dist: plotly>=4.12.0
|
|
27
|
+
Requires-Dist: nbformat>=4.2.0
|
|
27
28
|
Requires-Dist: ipywidgets>=7.0.0
|
|
28
29
|
Requires-Dist: anywidget
|
|
29
30
|
Requires-Dist: numba>=0.66
|
|
@@ -37,7 +38,7 @@ Requires-Dist: requests
|
|
|
37
38
|
Requires-Dist: pytz
|
|
38
39
|
Requires-Dist: mypy_extensions
|
|
39
40
|
Provides-Extra: rust
|
|
40
|
-
Requires-Dist: vectorbt-rust==1.
|
|
41
|
+
Requires-Dist: vectorbt-rust==1.2.0; extra == "rust"
|
|
41
42
|
Provides-Extra: full
|
|
42
43
|
Requires-Dist: TA-Lib; extra == "full"
|
|
43
44
|
Requires-Dist: yfinance>=0.2.22; extra == "full"
|
|
@@ -49,6 +50,7 @@ Requires-Dist: ta; extra == "full"
|
|
|
49
50
|
Requires-Dist: pandas-ta-classic; extra == "full"
|
|
50
51
|
Requires-Dist: python-telegram-bot>=13.4; extra == "full"
|
|
51
52
|
Requires-Dist: quantstats>=0.0.37; extra == "full"
|
|
53
|
+
Requires-Dist: kaleido>=1; extra == "full"
|
|
52
54
|
Provides-Extra: test
|
|
53
55
|
Requires-Dist: pytest; extra == "test"
|
|
54
56
|
Requires-Dist: pytest-cov; extra == "test"
|
|
@@ -114,7 +116,15 @@ Dynamic: license-file
|
|
|
114
116
|
</a>
|
|
115
117
|
</p>
|
|
116
118
|
|
|
117
|
-
<
|
|
119
|
+
<p align="center">
|
|
120
|
+
<a href="https://github.com/polakowo/vectorbt/blob/master/README.zh-CN.md">简体中文</a> / <b>English</b> |
|
|
121
|
+
<a href="#features">Features</a> ·
|
|
122
|
+
<a href="#installation">Installation</a> ·
|
|
123
|
+
<a href="#examples">Examples</a> ·
|
|
124
|
+
<a href="#links">Links</a>
|
|
125
|
+
</p>
|
|
126
|
+
|
|
127
|
+
<h3 align="center"><b>Think in matrices. Backtest at scale.</b></h3>
|
|
118
128
|
|
|
119
129
|
<p align="center">VectorBT takes a radically different approach to backtesting: instead of looping through bars one strategy at a time, it packs thousands of configurations into NumPy arrays, accelerates the hot path with Numba and Rust, and runs them all at once, turning hours of grid search into seconds.</p>
|
|
120
130
|
|
|
@@ -132,18 +142,68 @@ See [VectorBT vs PRO](https://vectorbt.dev/getting-started/upgrade/) or browse
|
|
|
132
142
|
|
|
133
143
|
## Features
|
|
134
144
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
<table>
|
|
146
|
+
<tr>
|
|
147
|
+
<td width="50%" valign="top">
|
|
148
|
+
<p><strong>Fast, vectorized backtesting</strong><br>
|
|
149
|
+
Research strategies at scale with pandas, NumPy, and Numba.</p>
|
|
150
|
+
</td>
|
|
151
|
+
<td width="50%" valign="top">
|
|
152
|
+
<p><strong>Optional Rust engine</strong><br>
|
|
153
|
+
Use precompiled kernels for supported operations without JIT compilation.</p>
|
|
154
|
+
</td>
|
|
155
|
+
</tr>
|
|
156
|
+
<tr>
|
|
157
|
+
<td width="50%" valign="top">
|
|
158
|
+
<p><strong>Pandas-native API</strong><br>
|
|
159
|
+
Work with custom accessors and high-performance operations directly on pandas objects.</p>
|
|
160
|
+
</td>
|
|
161
|
+
<td width="50%" valign="top">
|
|
162
|
+
<p><strong>Flexible broadcasting</strong><br>
|
|
163
|
+
Analyze multiple assets and sweep large parameter grids with flexible array broadcasting.</p>
|
|
164
|
+
</td>
|
|
165
|
+
</tr>
|
|
166
|
+
<tr>
|
|
167
|
+
<td width="50%" valign="top">
|
|
168
|
+
<p><strong>Rich indicator ecosystem</strong><br>
|
|
169
|
+
Build custom indicators and use integrations for TA-Lib, Pandas TA, and more.</p>
|
|
170
|
+
</td>
|
|
171
|
+
<td width="50%" valign="top">
|
|
172
|
+
<p><strong>Portfolio backtesting</strong><br>
|
|
173
|
+
Analyze trades, drawdowns, and performance, with QuantStats integration.</p>
|
|
174
|
+
</td>
|
|
175
|
+
</tr>
|
|
176
|
+
<tr>
|
|
177
|
+
<td width="50%" valign="top">
|
|
178
|
+
<p><strong>Signal tooling</strong><br>
|
|
179
|
+
Generate, rank, map, and analyze the distribution of trading signals.</p>
|
|
180
|
+
</td>
|
|
181
|
+
<td width="50%" valign="top">
|
|
182
|
+
<p><strong>Built-in data access</strong><br>
|
|
183
|
+
Fetch market data, preprocess it, and generate synthetic time series.</p>
|
|
184
|
+
</td>
|
|
185
|
+
</tr>
|
|
186
|
+
<tr>
|
|
187
|
+
<td width="50%" valign="top">
|
|
188
|
+
<p><strong>Robustness testing</strong><br>
|
|
189
|
+
Run walk-forward optimization and generate labels for machine learning workflows.</p>
|
|
190
|
+
</td>
|
|
191
|
+
<td width="50%" valign="top">
|
|
192
|
+
<p><strong>Interactive visualization</strong><br>
|
|
193
|
+
Explore results with Plotly charts, Jupyter widgets, and browser dashboards.</p>
|
|
194
|
+
</td>
|
|
195
|
+
</tr>
|
|
196
|
+
<tr>
|
|
197
|
+
<td width="50%" valign="top">
|
|
198
|
+
<p><strong>Automation tools</strong><br>
|
|
199
|
+
Schedule data updates and send Telegram notifications.</p>
|
|
200
|
+
</td>
|
|
201
|
+
<td width="50%" valign="top">
|
|
202
|
+
<p><strong>Composable Python API</strong><br>
|
|
203
|
+
Combine building blocks for rapid experimentation and AI agent-driven workflows.</p>
|
|
204
|
+
</td>
|
|
205
|
+
</tr>
|
|
206
|
+
</table>
|
|
147
207
|
|
|
148
208
|
## Installation
|
|
149
209
|
|
|
@@ -206,9 +266,9 @@ print(pf.total_profit())
|
|
|
206
266
|
> [!TIP]
|
|
207
267
|
> **VectorBT PRO**
|
|
208
268
|
>
|
|
209
|
-
> Simulate [limit orders](https://vectorbt.pro/features/
|
|
210
|
-
> use [leverage](https://vectorbt.pro/features/
|
|
211
|
-
> [contract multipliers](https://vectorbt.pro/features/
|
|
269
|
+
> Simulate [limit orders](https://vectorbt.pro/features/backtesting/orders-and-execution/#limit-orders),
|
|
270
|
+
> use [leverage](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#leverage), and model futures with
|
|
271
|
+
> [contract multipliers](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#contract-multiplier).
|
|
212
272
|
|
|
213
273
|
### Generate 1,000 random strategies
|
|
214
274
|
|
|
@@ -227,7 +287,7 @@ fig = mean_expectancy.unstack().vbt.scatterplot(xaxis_title="randnx_n", yaxis_ti
|
|
|
227
287
|
fig.show()
|
|
228
288
|
```
|
|
229
289
|
|
|
230
|
-
|
|
290
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-scatter.gif" alt="Hovering and zooming into random-strategy results" />
|
|
231
291
|
|
|
232
292
|
### Test 10,000 dual-SMA window combinations
|
|
233
293
|
|
|
@@ -249,13 +309,13 @@ fig = pf.total_return().vbt.heatmap(
|
|
|
249
309
|
fig.show()
|
|
250
310
|
```
|
|
251
311
|
|
|
252
|
-
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/
|
|
312
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-heatmap.gif" alt="Switching assets and inspecting dual-SMA parameter returns" />
|
|
253
313
|
|
|
254
314
|
> [!TIP]
|
|
255
315
|
> **VectorBT PRO**
|
|
256
316
|
>
|
|
257
|
-
> Use [random search](https://vectorbt.pro/features/optimization/#random-search) to sample a large
|
|
258
|
-
> parameter grid, or [conditional parameters](https://vectorbt.pro/features/optimization/#conditional-parameters)
|
|
317
|
+
> Use [random search](https://vectorbt.pro/features/optimization/strategy-optimization/#random-search) to sample a large
|
|
318
|
+
> parameter grid, or [conditional parameters](https://vectorbt.pro/features/optimization/strategy-optimization/#conditional-parameters)
|
|
259
319
|
> to exclude combinations such as a fast MA window longer than the slow MA window. The
|
|
260
320
|
> [cross-validation tutorial](https://vectorbt.pro/tutorials/cross-validation/)
|
|
261
321
|
> shows how to select parameters on training data and evaluate them on later periods.
|
|
@@ -304,7 +364,7 @@ Name: (10, 20, ETH-USD), dtype: object
|
|
|
304
364
|
pf[(10, 20, "ETH-USD")].plot().show()
|
|
305
365
|
```
|
|
306
366
|
|
|
307
|
-
|
|
367
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-portfolio.gif" alt="Inspecting trades, zooming, and toggling the portfolio benchmark" />
|
|
308
368
|
|
|
309
369
|
### Animate Bollinger Bands across multiple symbols
|
|
310
370
|
|
|
@@ -339,7 +399,7 @@ vbt.save_animation("bbands.gif", bbands.wrapper.index, plot, bbands, delta=90, s
|
|
|
339
399
|
100%|██████████| 31/31 [00:21<00:00, 1.21it/s]
|
|
340
400
|
```
|
|
341
401
|
|
|
342
|
-
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/
|
|
402
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-bbands.gif" alt="Inspecting Bollinger Bands and playing the rolling-window animation" />
|
|
343
403
|
|
|
344
404
|
Visit the [website](https://vectorbt.dev/) for more examples, documentation, and guides.
|
|
345
405
|
|
|
@@ -349,7 +409,7 @@ Visit the [website](https://vectorbt.dev/) for more examples, documentation, and
|
|
|
349
409
|
|
|
350
410
|
Explore candlestick patterns interactively and backtest their signals with VectorBT.
|
|
351
411
|
|
|
352
|
-
|
|
412
|
+
<img width="900" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-candlestick.gif" alt="Exploring candlestick patterns, inspecting trades, and comparing strategy statistics in the app" />
|
|
353
413
|
|
|
354
414
|
## Links
|
|
355
415
|
|
|
@@ -367,6 +427,14 @@ If you have questions or want to request a license exception, please [contact th
|
|
|
367
427
|
|
|
368
428
|
Installing optional dependencies may be subject to a more restrictive license.
|
|
369
429
|
|
|
430
|
+
## Contributors
|
|
431
|
+
|
|
432
|
+
Thanks to everyone who has contributed to VectorBT!
|
|
433
|
+
|
|
434
|
+
<a href="https://github.com/polakowo/vectorbt/graphs/contributors">
|
|
435
|
+
<img src="https://contrib.rocks/image?repo=polakowo/vectorbt" alt="VectorBT contributors" />
|
|
436
|
+
</a>
|
|
437
|
+
|
|
370
438
|
## Star History
|
|
371
439
|
|
|
372
440
|
<a href="https://www.star-history.com/?repos=polakowo%2Fvectorbt&type=timeline&legend=top-left">
|
|
@@ -44,7 +44,15 @@
|
|
|
44
44
|
</a>
|
|
45
45
|
</p>
|
|
46
46
|
|
|
47
|
-
<
|
|
47
|
+
<p align="center">
|
|
48
|
+
<a href="https://github.com/polakowo/vectorbt/blob/master/README.zh-CN.md">简体中文</a> / <b>English</b> |
|
|
49
|
+
<a href="#features">Features</a> ·
|
|
50
|
+
<a href="#installation">Installation</a> ·
|
|
51
|
+
<a href="#examples">Examples</a> ·
|
|
52
|
+
<a href="#links">Links</a>
|
|
53
|
+
</p>
|
|
54
|
+
|
|
55
|
+
<h3 align="center"><b>Think in matrices. Backtest at scale.</b></h3>
|
|
48
56
|
|
|
49
57
|
<p align="center">VectorBT takes a radically different approach to backtesting: instead of looping through bars one strategy at a time, it packs thousands of configurations into NumPy arrays, accelerates the hot path with Numba and Rust, and runs them all at once, turning hours of grid search into seconds.</p>
|
|
50
58
|
|
|
@@ -62,18 +70,68 @@ See [VectorBT vs PRO](https://vectorbt.dev/getting-started/upgrade/) or browse
|
|
|
62
70
|
|
|
63
71
|
## Features
|
|
64
72
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
<table>
|
|
74
|
+
<tr>
|
|
75
|
+
<td width="50%" valign="top">
|
|
76
|
+
<p><strong>Fast, vectorized backtesting</strong><br>
|
|
77
|
+
Research strategies at scale with pandas, NumPy, and Numba.</p>
|
|
78
|
+
</td>
|
|
79
|
+
<td width="50%" valign="top">
|
|
80
|
+
<p><strong>Optional Rust engine</strong><br>
|
|
81
|
+
Use precompiled kernels for supported operations without JIT compilation.</p>
|
|
82
|
+
</td>
|
|
83
|
+
</tr>
|
|
84
|
+
<tr>
|
|
85
|
+
<td width="50%" valign="top">
|
|
86
|
+
<p><strong>Pandas-native API</strong><br>
|
|
87
|
+
Work with custom accessors and high-performance operations directly on pandas objects.</p>
|
|
88
|
+
</td>
|
|
89
|
+
<td width="50%" valign="top">
|
|
90
|
+
<p><strong>Flexible broadcasting</strong><br>
|
|
91
|
+
Analyze multiple assets and sweep large parameter grids with flexible array broadcasting.</p>
|
|
92
|
+
</td>
|
|
93
|
+
</tr>
|
|
94
|
+
<tr>
|
|
95
|
+
<td width="50%" valign="top">
|
|
96
|
+
<p><strong>Rich indicator ecosystem</strong><br>
|
|
97
|
+
Build custom indicators and use integrations for TA-Lib, Pandas TA, and more.</p>
|
|
98
|
+
</td>
|
|
99
|
+
<td width="50%" valign="top">
|
|
100
|
+
<p><strong>Portfolio backtesting</strong><br>
|
|
101
|
+
Analyze trades, drawdowns, and performance, with QuantStats integration.</p>
|
|
102
|
+
</td>
|
|
103
|
+
</tr>
|
|
104
|
+
<tr>
|
|
105
|
+
<td width="50%" valign="top">
|
|
106
|
+
<p><strong>Signal tooling</strong><br>
|
|
107
|
+
Generate, rank, map, and analyze the distribution of trading signals.</p>
|
|
108
|
+
</td>
|
|
109
|
+
<td width="50%" valign="top">
|
|
110
|
+
<p><strong>Built-in data access</strong><br>
|
|
111
|
+
Fetch market data, preprocess it, and generate synthetic time series.</p>
|
|
112
|
+
</td>
|
|
113
|
+
</tr>
|
|
114
|
+
<tr>
|
|
115
|
+
<td width="50%" valign="top">
|
|
116
|
+
<p><strong>Robustness testing</strong><br>
|
|
117
|
+
Run walk-forward optimization and generate labels for machine learning workflows.</p>
|
|
118
|
+
</td>
|
|
119
|
+
<td width="50%" valign="top">
|
|
120
|
+
<p><strong>Interactive visualization</strong><br>
|
|
121
|
+
Explore results with Plotly charts, Jupyter widgets, and browser dashboards.</p>
|
|
122
|
+
</td>
|
|
123
|
+
</tr>
|
|
124
|
+
<tr>
|
|
125
|
+
<td width="50%" valign="top">
|
|
126
|
+
<p><strong>Automation tools</strong><br>
|
|
127
|
+
Schedule data updates and send Telegram notifications.</p>
|
|
128
|
+
</td>
|
|
129
|
+
<td width="50%" valign="top">
|
|
130
|
+
<p><strong>Composable Python API</strong><br>
|
|
131
|
+
Combine building blocks for rapid experimentation and AI agent-driven workflows.</p>
|
|
132
|
+
</td>
|
|
133
|
+
</tr>
|
|
134
|
+
</table>
|
|
77
135
|
|
|
78
136
|
## Installation
|
|
79
137
|
|
|
@@ -136,9 +194,9 @@ print(pf.total_profit())
|
|
|
136
194
|
> [!TIP]
|
|
137
195
|
> **VectorBT PRO**
|
|
138
196
|
>
|
|
139
|
-
> Simulate [limit orders](https://vectorbt.pro/features/
|
|
140
|
-
> use [leverage](https://vectorbt.pro/features/
|
|
141
|
-
> [contract multipliers](https://vectorbt.pro/features/
|
|
197
|
+
> Simulate [limit orders](https://vectorbt.pro/features/backtesting/orders-and-execution/#limit-orders),
|
|
198
|
+
> use [leverage](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#leverage), and model futures with
|
|
199
|
+
> [contract multipliers](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#contract-multiplier).
|
|
142
200
|
|
|
143
201
|
### Generate 1,000 random strategies
|
|
144
202
|
|
|
@@ -157,7 +215,7 @@ fig = mean_expectancy.unstack().vbt.scatterplot(xaxis_title="randnx_n", yaxis_ti
|
|
|
157
215
|
fig.show()
|
|
158
216
|
```
|
|
159
217
|
|
|
160
|
-
|
|
218
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-scatter.gif" alt="Hovering and zooming into random-strategy results" />
|
|
161
219
|
|
|
162
220
|
### Test 10,000 dual-SMA window combinations
|
|
163
221
|
|
|
@@ -179,13 +237,13 @@ fig = pf.total_return().vbt.heatmap(
|
|
|
179
237
|
fig.show()
|
|
180
238
|
```
|
|
181
239
|
|
|
182
|
-
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/
|
|
240
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-heatmap.gif" alt="Switching assets and inspecting dual-SMA parameter returns" />
|
|
183
241
|
|
|
184
242
|
> [!TIP]
|
|
185
243
|
> **VectorBT PRO**
|
|
186
244
|
>
|
|
187
|
-
> Use [random search](https://vectorbt.pro/features/optimization/#random-search) to sample a large
|
|
188
|
-
> parameter grid, or [conditional parameters](https://vectorbt.pro/features/optimization/#conditional-parameters)
|
|
245
|
+
> Use [random search](https://vectorbt.pro/features/optimization/strategy-optimization/#random-search) to sample a large
|
|
246
|
+
> parameter grid, or [conditional parameters](https://vectorbt.pro/features/optimization/strategy-optimization/#conditional-parameters)
|
|
189
247
|
> to exclude combinations such as a fast MA window longer than the slow MA window. The
|
|
190
248
|
> [cross-validation tutorial](https://vectorbt.pro/tutorials/cross-validation/)
|
|
191
249
|
> shows how to select parameters on training data and evaluate them on later periods.
|
|
@@ -234,7 +292,7 @@ Name: (10, 20, ETH-USD), dtype: object
|
|
|
234
292
|
pf[(10, 20, "ETH-USD")].plot().show()
|
|
235
293
|
```
|
|
236
294
|
|
|
237
|
-
|
|
295
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-portfolio.gif" alt="Inspecting trades, zooming, and toggling the portfolio benchmark" />
|
|
238
296
|
|
|
239
297
|
### Animate Bollinger Bands across multiple symbols
|
|
240
298
|
|
|
@@ -269,7 +327,7 @@ vbt.save_animation("bbands.gif", bbands.wrapper.index, plot, bbands, delta=90, s
|
|
|
269
327
|
100%|██████████| 31/31 [00:21<00:00, 1.21it/s]
|
|
270
328
|
```
|
|
271
329
|
|
|
272
|
-
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/
|
|
330
|
+
<img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-bbands.gif" alt="Inspecting Bollinger Bands and playing the rolling-window animation" />
|
|
273
331
|
|
|
274
332
|
Visit the [website](https://vectorbt.dev/) for more examples, documentation, and guides.
|
|
275
333
|
|
|
@@ -279,7 +337,7 @@ Visit the [website](https://vectorbt.dev/) for more examples, documentation, and
|
|
|
279
337
|
|
|
280
338
|
Explore candlestick patterns interactively and backtest their signals with VectorBT.
|
|
281
339
|
|
|
282
|
-
|
|
340
|
+
<img width="900" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/readme-candlestick.gif" alt="Exploring candlestick patterns, inspecting trades, and comparing strategy statistics in the app" />
|
|
283
341
|
|
|
284
342
|
## Links
|
|
285
343
|
|
|
@@ -297,6 +355,14 @@ If you have questions or want to request a license exception, please [contact th
|
|
|
297
355
|
|
|
298
356
|
Installing optional dependencies may be subject to a more restrictive license.
|
|
299
357
|
|
|
358
|
+
## Contributors
|
|
359
|
+
|
|
360
|
+
Thanks to everyone who has contributed to VectorBT!
|
|
361
|
+
|
|
362
|
+
<a href="https://github.com/polakowo/vectorbt/graphs/contributors">
|
|
363
|
+
<img src="https://contrib.rocks/image?repo=polakowo/vectorbt" alt="VectorBT contributors" />
|
|
364
|
+
</a>
|
|
365
|
+
|
|
300
366
|
## Star History
|
|
301
367
|
|
|
302
368
|
<a href="https://www.star-history.com/?repos=polakowo%2Fvectorbt&type=timeline&legend=top-left">
|
|
@@ -32,6 +32,7 @@ dependencies = [
|
|
|
32
32
|
"scipy",
|
|
33
33
|
"matplotlib",
|
|
34
34
|
"plotly>=4.12.0",
|
|
35
|
+
"nbformat>=4.2.0",
|
|
35
36
|
"ipywidgets>=7.0.0",
|
|
36
37
|
"anywidget",
|
|
37
38
|
"numba>=0.66",
|
|
@@ -48,7 +49,7 @@ dependencies = [
|
|
|
48
49
|
|
|
49
50
|
[project.optional-dependencies]
|
|
50
51
|
rust = [
|
|
51
|
-
"vectorbt-rust==1.
|
|
52
|
+
"vectorbt-rust==1.2.0",
|
|
52
53
|
]
|
|
53
54
|
full = [
|
|
54
55
|
"TA-Lib",
|
|
@@ -61,6 +62,7 @@ full = [
|
|
|
61
62
|
"pandas-ta-classic",
|
|
62
63
|
"python-telegram-bot>=13.4",
|
|
63
64
|
"quantstats>=0.0.37",
|
|
65
|
+
"kaleido>=1",
|
|
64
66
|
]
|
|
65
67
|
test = [
|
|
66
68
|
"pytest",
|
|
@@ -421,6 +421,71 @@ class TestArrayWrapper:
|
|
|
421
421
|
np.testing.assert_array_equal(b, np.array([0, 1]))
|
|
422
422
|
np.testing.assert_array_equal(c, np.array([0, 1, 2]))
|
|
423
423
|
|
|
424
|
+
@pytest.mark.parametrize("column_only", [False, True])
|
|
425
|
+
@pytest.mark.parametrize(
|
|
426
|
+
"groups,selection,col_idxs,expected_groups",
|
|
427
|
+
[
|
|
428
|
+
(["a", "a", "b", "b", "c", "c"], [0, 1, 2], [0, 1, 2, 3, 4, 5], ["a", "a", "b", "b", "c", "c"]),
|
|
429
|
+
(["a", "a", "b", "b", "c", "c"], [2, 0, 1], [4, 5, 0, 1, 2, 3], ["c", "c", "a", "a", "b", "b"]),
|
|
430
|
+
(["a", "b", "b", "c", "c", "c"], [0, 1, 2], [0, 1, 2, 3, 4, 5], ["a", "b", "b", "c", "c", "c"]),
|
|
431
|
+
(["a", "b", "b", "c", "c", "c"], [2, 0, 1], [3, 4, 5, 0, 1, 2], ["c", "c", "c", "a", "b", "b"]),
|
|
432
|
+
(["a", "a", "b", "b", "c", "c"], [1, 1, 0], [2, 3, 2, 3, 0, 1], ["b", "b", "b", "b", "a", "a"]),
|
|
433
|
+
],
|
|
434
|
+
)
|
|
435
|
+
def test_group_selection_membership(self, column_only, groups, selection, col_idxs, expected_groups):
|
|
436
|
+
# Each selected physical block must keep its own label, including repeated adjacent selections.
|
|
437
|
+
wrapper = array_wrapper.ArrayWrapper(
|
|
438
|
+
index=pd.Index(["x", "y", "z"], name="row"),
|
|
439
|
+
columns=pd.Index(list("abcdef"), name="asset"),
|
|
440
|
+
ndim=2,
|
|
441
|
+
group_by=pd.Index(groups, name="strategy"),
|
|
442
|
+
group_select=True,
|
|
443
|
+
column_only_select=column_only,
|
|
444
|
+
)
|
|
445
|
+
selected, row_idxs, group_idxs, physical_idxs = wrapper.indexing_func_meta(
|
|
446
|
+
lambda obj: obj.iloc[selection] if column_only else obj.iloc[[2, 0], selection]
|
|
447
|
+
)
|
|
448
|
+
pd.testing.assert_index_equal(selected.grouper.group_by, pd.Index(expected_groups, name="strategy"))
|
|
449
|
+
pd.testing.assert_index_equal(selected.columns, wrapper.columns[col_idxs])
|
|
450
|
+
pd.testing.assert_index_equal(selected.index, wrapper.index if column_only else wrapper.index[[2, 0]])
|
|
451
|
+
np.testing.assert_array_equal(row_idxs, [0, 1, 2] if column_only else [2, 0])
|
|
452
|
+
np.testing.assert_array_equal(group_idxs, selection)
|
|
453
|
+
np.testing.assert_array_equal(physical_idxs, col_idxs)
|
|
454
|
+
assert selected.ndim == selected.grouped_ndim == 2
|
|
455
|
+
pd.testing.assert_index_equal(wrapper.grouper.group_by, pd.Index(groups, name="strategy"))
|
|
456
|
+
|
|
457
|
+
@pytest.mark.parametrize("column_only", [False, True])
|
|
458
|
+
def test_group_selection_renamed_labels(self, column_only):
|
|
459
|
+
groups = pd.MultiIndex.from_tuples(
|
|
460
|
+
[("s", 1), ("s", 2), ("s", 2), ("t", 3), ("t", 3), ("t", 3)], names=["strategy", "number"]
|
|
461
|
+
)
|
|
462
|
+
wrapper = array_wrapper.ArrayWrapper(
|
|
463
|
+
index=[0, 1],
|
|
464
|
+
columns=list("abcdef"),
|
|
465
|
+
ndim=2,
|
|
466
|
+
group_by=groups,
|
|
467
|
+
group_select=True,
|
|
468
|
+
column_only_select=column_only,
|
|
469
|
+
)
|
|
470
|
+
renamed = pd.MultiIndex.from_tuples([("new", 30), ("new", 10), ("new", 20)], names=["model", "bucket"])
|
|
471
|
+
|
|
472
|
+
def select(obj):
|
|
473
|
+
# Indexing callbacks may rename groups; copying labels from the source would lose that change.
|
|
474
|
+
selected = (
|
|
475
|
+
obj.loc[[("t", 3), ("s", 1), ("s", 2)]] if column_only else obj.loc[:, [("t", 3), ("s", 1), ("s", 2)]]
|
|
476
|
+
)
|
|
477
|
+
return selected.set_axis(renamed, axis=0 if column_only else 1)
|
|
478
|
+
|
|
479
|
+
selected = wrapper.indexing_func(select)
|
|
480
|
+
expected = pd.MultiIndex.from_tuples(
|
|
481
|
+
[("new", 30), ("new", 30), ("new", 30), ("new", 10), ("new", 20), ("new", 20)],
|
|
482
|
+
names=["model", "bucket"],
|
|
483
|
+
)
|
|
484
|
+
pd.testing.assert_index_equal(selected.grouper.group_by, expected)
|
|
485
|
+
pd.testing.assert_index_equal(selected.grouper.get_columns(), renamed)
|
|
486
|
+
pd.testing.assert_index_equal(selected.columns, pd.Index(list("defabc")))
|
|
487
|
+
pd.testing.assert_index_equal(wrapper.grouper.group_by, groups)
|
|
488
|
+
|
|
424
489
|
def test_indexing(self):
|
|
425
490
|
# not grouped
|
|
426
491
|
pd.testing.assert_index_equal(sr2_wrapper.iloc[:2].index, pd.Index(["x2", "y2"], dtype="object", name="i2"))
|
|
@@ -1928,6 +1993,146 @@ class TestReshapeFns:
|
|
|
1928
1993
|
),
|
|
1929
1994
|
)
|
|
1930
1995
|
|
|
1996
|
+
@pytest.mark.parametrize(
|
|
1997
|
+
"rows, columns",
|
|
1998
|
+
[(["row1", "row2"], ["b", "a"]), (["row2", "row1"], ["a", "b"]), (["row2", "row1"], ["b", "a"])],
|
|
1999
|
+
)
|
|
2000
|
+
def test_unstack_to_df_unsorted(self, rows, columns):
|
|
2001
|
+
# Each row of values follows encounter order, independently of lexical label order.
|
|
2002
|
+
sr = pd.Series(
|
|
2003
|
+
[10.0, 20.0, 30.0, 40.0], index=pd.MultiIndex.from_product([rows, columns], names=["row", "column"])
|
|
2004
|
+
)
|
|
2005
|
+
before = sr.copy(deep=True)
|
|
2006
|
+
expected = pd.DataFrame(
|
|
2007
|
+
[[10.0, 20.0], [30.0, 40.0]], index=pd.Index(rows, name="row"), columns=pd.Index(columns, name="column")
|
|
2008
|
+
)
|
|
2009
|
+
|
|
2010
|
+
# Direct and accessor paths accept Series and one-column DataFrames.
|
|
2011
|
+
for obj in (sr, sr.to_frame()):
|
|
2012
|
+
pd.testing.assert_frame_equal(reshape_fns.unstack_to_df(obj), expected)
|
|
2013
|
+
pd.testing.assert_frame_equal(obj.vbt.unstack_to_df(sort=False), expected)
|
|
2014
|
+
# Raw arrays retain sorted coordinates, as required by the volume consumer.
|
|
2015
|
+
np.testing.assert_array_equal(sr.vbt.unstack_to_array(), expected.sort_index().sort_index(axis=1).values)
|
|
2016
|
+
pd.testing.assert_series_equal(sr, before)
|
|
2017
|
+
|
|
2018
|
+
def test_unstack_to_df_unsorted_metadata(self):
|
|
2019
|
+
# Category order differs from NumPy's lexical order; sparse cells retain NaN.
|
|
2020
|
+
dates = pd.DatetimeIndex(["2026-10-02", "2026-10-02", "2026-10-01"], tz="UTC", name="date")
|
|
2021
|
+
labels = pd.CategoricalIndex(["b", "a", "b"], categories=["b", "unused", "a"], ordered=True, name="column")
|
|
2022
|
+
sr = pd.Series([10.0, 20.0, 30.0], index=pd.MultiIndex.from_arrays([dates, labels]))
|
|
2023
|
+
expected = pd.DataFrame(
|
|
2024
|
+
[[10.0, 20.0], [30.0, np.nan]],
|
|
2025
|
+
index=pd.DatetimeIndex(["2026-10-02", "2026-10-01"], tz="UTC", name="date"),
|
|
2026
|
+
columns=pd.CategoricalIndex(["b", "a"], categories=labels.categories, ordered=True, name="column"),
|
|
2027
|
+
)
|
|
2028
|
+
pd.testing.assert_frame_equal(reshape_fns.unstack_to_df(sr), expected)
|
|
2029
|
+
|
|
2030
|
+
def test_unstack_to_df_unsorted_levels(self):
|
|
2031
|
+
# Explicit composite levels retain their requested hierarchy and encounter order.
|
|
2032
|
+
index = pd.MultiIndex.from_tuples(
|
|
2033
|
+
[(2, "b", 20), (1, "a", 10), (2, "a", 10), (1, "b", 20)], names=["row", "kind", "column"]
|
|
2034
|
+
)
|
|
2035
|
+
sr = pd.Series([10.0, 20.0, 30.0, 40.0], index=index)
|
|
2036
|
+
expected = pd.DataFrame(
|
|
2037
|
+
[[10.0, np.nan], [np.nan, 20.0], [np.nan, 30.0], [40.0, np.nan]],
|
|
2038
|
+
index=pd.MultiIndex.from_tuples([("b", 2), ("a", 1), ("a", 2), ("b", 1)], names=["kind", "row"]),
|
|
2039
|
+
columns=pd.Index([20, 10], name="column"),
|
|
2040
|
+
)
|
|
2041
|
+
pd.testing.assert_frame_equal(
|
|
2042
|
+
sr.vbt.unstack_to_df(index_levels=("kind", "row"), column_levels="column"), expected
|
|
2043
|
+
)
|
|
2044
|
+
|
|
2045
|
+
@pytest.mark.parametrize("sort", [False, True])
|
|
2046
|
+
def test_unstack_to_df_unsorted_symmetric(self, sort):
|
|
2047
|
+
# Mirroring must use each source label/value pair before sorting the combined axes.
|
|
2048
|
+
sr = pd.Series(
|
|
2049
|
+
[10.0, 20.0, 30.0, 40.0], index=pd.MultiIndex.from_product([[3, 1], [4, 2]], names=["row", "column"])
|
|
2050
|
+
)
|
|
2051
|
+
labels = pd.Index([3, 1, 4, 2], name=("row", "column"))
|
|
2052
|
+
expected = pd.DataFrame(
|
|
2053
|
+
[
|
|
2054
|
+
[np.nan, np.nan, 10.0, 20.0],
|
|
2055
|
+
[np.nan, np.nan, 30.0, 40.0],
|
|
2056
|
+
[10.0, 30.0, np.nan, np.nan],
|
|
2057
|
+
[20.0, 40.0, np.nan, np.nan],
|
|
2058
|
+
],
|
|
2059
|
+
index=labels,
|
|
2060
|
+
columns=labels,
|
|
2061
|
+
)
|
|
2062
|
+
if sort:
|
|
2063
|
+
expected = expected.sort_index().sort_index(axis=1)
|
|
2064
|
+
pd.testing.assert_frame_equal(sr.vbt.unstack_to_df(symmetric=True, sort=sort), expected)
|
|
2065
|
+
|
|
2066
|
+
@pytest.mark.parametrize("slider", [False, True])
|
|
2067
|
+
def test_unstack_to_df_unsorted_heatmap(self, slider):
|
|
2068
|
+
# Assert the displayed label/value pairing, including each slider trace.
|
|
2069
|
+
sr = pd.Series(
|
|
2070
|
+
[10.0, 20.0, 30.0, 40.0],
|
|
2071
|
+
index=pd.MultiIndex.from_product([["row2", "row1"], ["b", "a"]], names=["row", "column"]),
|
|
2072
|
+
)
|
|
2073
|
+
if slider:
|
|
2074
|
+
sr = pd.concat([sr, sr + 100.0], keys=["first", "second"], names=["batch"])
|
|
2075
|
+
fig = sr.vbt.heatmap(x_level="column", y_level="row", slider_level="batch" if slider else None)
|
|
2076
|
+
assert len(fig.data) == (2 if slider else 1)
|
|
2077
|
+
for i, trace in enumerate(fig.data):
|
|
2078
|
+
np.testing.assert_array_equal(trace.x, ["b", "a"])
|
|
2079
|
+
np.testing.assert_array_equal(trace.y, ["row2", "row1"])
|
|
2080
|
+
np.testing.assert_array_equal(trace.z, np.array([[10.0, 20.0], [30.0, 40.0]]) + 100.0 * i)
|
|
2081
|
+
assert fig.layout.xaxis.title.text == "column"
|
|
2082
|
+
assert fig.layout.yaxis.title.text == "row"
|
|
2083
|
+
assert fig.to_json()
|
|
2084
|
+
|
|
2085
|
+
@pytest.mark.parametrize("second_order", [[2, 3, 0, 1], [1, 0, 3, 2], [3, 2, 1, 0]])
|
|
2086
|
+
def test_unstack_to_df_unsorted_heatmap_frame_order(self, second_order):
|
|
2087
|
+
# A slider frame's automatic axes must describe that frame, even if only
|
|
2088
|
+
# its row order, column order, or both differ from the first frame.
|
|
2089
|
+
first = pd.Series(
|
|
2090
|
+
[10.0, 20.0, 30.0, 40.0],
|
|
2091
|
+
index=pd.MultiIndex.from_product([["row1", "row2"], ["a", "b"]], names=["row", "column"]),
|
|
2092
|
+
)
|
|
2093
|
+
second = (first + 100.0).iloc[second_order]
|
|
2094
|
+
sr = pd.concat([first, second], keys=["first", "second"], names=["batch"])
|
|
2095
|
+
before = sr.copy(deep=True)
|
|
2096
|
+
fig = sr.vbt.heatmap(x_level="column", y_level="row", slider_level="batch", active=1)
|
|
2097
|
+
assert len(fig.data) == 2
|
|
2098
|
+
for trace, source in zip(fig.data, (first, second)):
|
|
2099
|
+
np.testing.assert_array_equal(trace.x, source.index.get_level_values("column").unique())
|
|
2100
|
+
np.testing.assert_array_equal(trace.y, source.index.get_level_values("row").unique())
|
|
2101
|
+
# Look up each displayed coordinate in the original source, without
|
|
2102
|
+
# reproducing the reshape implementation's ordering rule.
|
|
2103
|
+
expected = [[source.loc[(y, x)] for x in trace.x] for y in trace.y]
|
|
2104
|
+
np.testing.assert_array_equal(trace.z, expected)
|
|
2105
|
+
assert fig.data[1].visible
|
|
2106
|
+
assert len(fig.layout.sliders[0].steps) == 2
|
|
2107
|
+
assert fig.to_json()
|
|
2108
|
+
pd.testing.assert_series_equal(sr, before)
|
|
2109
|
+
|
|
2110
|
+
@pytest.mark.parametrize("override_x, override_y", [(True, False), (False, True), (True, True)])
|
|
2111
|
+
def test_unstack_to_df_unsorted_heatmap_label_overrides(self, override_x, override_y):
|
|
2112
|
+
# Explicit labels remain positional replacements; omitted labels follow
|
|
2113
|
+
# each frame independently. User-owned label lists remain unchanged.
|
|
2114
|
+
first = pd.Series(
|
|
2115
|
+
[10.0, 20.0, 30.0, 40.0],
|
|
2116
|
+
index=pd.MultiIndex.from_product([["row1", "row2"], ["a", "b"]], names=["row", "column"]),
|
|
2117
|
+
)
|
|
2118
|
+
sr = pd.concat([first, (first + 100.0).iloc[::-1]], keys=["first", "second"], names=["batch"])
|
|
2119
|
+
x_labels = ["X1", "X2"] if override_x else None
|
|
2120
|
+
y_labels = ["Y1", "Y2"] if override_y else None
|
|
2121
|
+
fig = sr.vbt.heatmap(
|
|
2122
|
+
x_level="column", y_level="row", slider_level="batch", x_labels=x_labels, y_labels=y_labels
|
|
2123
|
+
)
|
|
2124
|
+
for i, trace in enumerate(fig.data):
|
|
2125
|
+
np.testing.assert_array_equal(trace.x, x_labels if override_x else (["a", "b"] if i == 0 else ["b", "a"]))
|
|
2126
|
+
np.testing.assert_array_equal(
|
|
2127
|
+
trace.y, y_labels if override_y else (["row1", "row2"] if i == 0 else ["row2", "row1"])
|
|
2128
|
+
)
|
|
2129
|
+
np.testing.assert_array_equal(
|
|
2130
|
+
trace.z, [[10.0, 20.0], [30.0, 40.0]] if i == 0 else [[140.0, 130.0], [120.0, 110.0]]
|
|
2131
|
+
)
|
|
2132
|
+
assert x_labels == (["X1", "X2"] if override_x else None)
|
|
2133
|
+
assert y_labels == (["Y1", "Y2"] if override_y else None)
|
|
2134
|
+
assert fig.to_json()
|
|
2135
|
+
|
|
1931
2136
|
def test_unstack_to_df(self):
|
|
1932
2137
|
pd.testing.assert_frame_equal(
|
|
1933
2138
|
reshape_fns.unstack_to_df(df5.iloc[0]),
|