vectorbt 1.1.0__tar.gz → 1.1.2__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.
Files changed (123) hide show
  1. {vectorbt-1.1.0/vectorbt.egg-info → vectorbt-1.1.2}/PKG-INFO +42 -39
  2. {vectorbt-1.1.0 → vectorbt-1.1.2}/README.md +38 -34
  3. {vectorbt-1.1.0 → vectorbt-1.1.2}/pyproject.toml +3 -4
  4. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_engine.py +44 -0
  5. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_generic.py +48 -0
  6. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_plotting.py +40 -1
  7. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_portfolio.py +131 -7
  8. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_returns.py +91 -3
  9. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_utils.py +72 -0
  10. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/_version.py +1 -1
  11. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/indexing.py +9 -1
  12. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/reshape_fns.py +10 -1
  13. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/data/base.py +5 -0
  14. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/data/custom.py +10 -1
  15. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/data/updater.py +9 -1
  16. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/accessors.py +5 -0
  17. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/nb.py +98 -36
  18. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/ranges.py +6 -0
  19. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/splitters.py +9 -1
  20. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/indicators/basic.py +7 -0
  21. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/indicators/factory.py +7 -0
  22. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/base.py +14 -3
  23. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/trades.py +23 -17
  24. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/returns/accessors.py +8 -7
  25. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/returns/metrics.py +3 -1
  26. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/returns/nb.py +4 -1
  27. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/accessors.py +7 -0
  28. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/templates/dark.json +1 -15
  29. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/templates/light.json +1 -15
  30. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/templates/seaborn.json +1 -4
  31. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/array_.py +1 -1
  32. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/config.py +9 -1
  33. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/datetime_.py +32 -2
  34. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/decorators.py +9 -1
  35. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/params.py +10 -1
  36. {vectorbt-1.1.0 → vectorbt-1.1.2/vectorbt.egg-info}/PKG-INFO +42 -39
  37. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt.egg-info/requires.txt +3 -4
  38. {vectorbt-1.1.0 → vectorbt-1.1.2}/LICENSE.md +0 -0
  39. {vectorbt-1.1.0 → vectorbt-1.1.2}/MANIFEST.in +0 -0
  40. {vectorbt-1.1.0 → vectorbt-1.1.2}/setup.cfg +0 -0
  41. {vectorbt-1.1.0 → vectorbt-1.1.2}/setup.py +0 -0
  42. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_base.py +0 -0
  43. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_data.py +0 -0
  44. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_indicators.py +0 -0
  45. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_labels.py +0 -0
  46. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_records.py +0 -0
  47. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_settings.py +0 -0
  48. {vectorbt-1.1.0 → vectorbt-1.1.2}/tests/test_signals.py +0 -0
  49. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/__init__.py +0 -0
  50. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/_engine.py +0 -0
  51. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/_settings.py +0 -0
  52. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/_typing.py +0 -0
  53. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/__init__.py +0 -0
  54. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/accessors.py +0 -0
  55. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/array_wrapper.py +0 -0
  56. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/column_grouper.py +0 -0
  57. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/combine_fns.py +0 -0
  58. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/base/index_fns.py +0 -0
  59. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/data/__init__.py +0 -0
  60. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/__init__.py +0 -0
  61. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/decorators.py +0 -0
  62. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/dispatch.py +0 -0
  63. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/drawdowns.py +0 -0
  64. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/enums.py +0 -0
  65. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/plots_builder.py +0 -0
  66. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/plotting.py +0 -0
  67. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/generic/stats_builder.py +0 -0
  68. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/indicators/__init__.py +0 -0
  69. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/indicators/configs.py +0 -0
  70. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/indicators/dispatch.py +0 -0
  71. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/indicators/nb.py +0 -0
  72. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/labels/__init__.py +0 -0
  73. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/labels/dispatch.py +0 -0
  74. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/labels/enums.py +0 -0
  75. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/labels/generators.py +0 -0
  76. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/labels/nb.py +0 -0
  77. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/messaging/__init__.py +0 -0
  78. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/messaging/telegram.py +0 -0
  79. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/ohlcv_accessors.py +0 -0
  80. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/__init__.py +0 -0
  81. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/decorators.py +0 -0
  82. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/dispatch.py +0 -0
  83. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/enums.py +0 -0
  84. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/logs.py +0 -0
  85. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/nb.py +0 -0
  86. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/portfolio/orders.py +0 -0
  87. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/px_accessors.py +0 -0
  88. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/__init__.py +0 -0
  89. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/base.py +0 -0
  90. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/col_mapper.py +0 -0
  91. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/decorators.py +0 -0
  92. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/dispatch.py +0 -0
  93. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/mapped_array.py +0 -0
  94. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/records/nb.py +0 -0
  95. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/returns/__init__.py +0 -0
  96. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/returns/dispatch.py +0 -0
  97. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/returns/qs_adapter.py +0 -0
  98. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/root_accessors.py +0 -0
  99. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/__init__.py +0 -0
  100. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/dispatch.py +0 -0
  101. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/enums.py +0 -0
  102. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/factory.py +0 -0
  103. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/generators.py +0 -0
  104. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/signals/nb.py +0 -0
  105. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/__init__.py +0 -0
  106. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/attr_.py +0 -0
  107. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/checks.py +0 -0
  108. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/colors.py +0 -0
  109. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/docs.py +0 -0
  110. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/enum_.py +0 -0
  111. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/figure.py +0 -0
  112. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/image_.py +0 -0
  113. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/mapping.py +0 -0
  114. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/math_.py +0 -0
  115. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/module_.py +0 -0
  116. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/random_.py +0 -0
  117. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/requests_.py +0 -0
  118. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/schedule_.py +0 -0
  119. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/tags.py +0 -0
  120. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt/utils/template.py +0 -0
  121. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt.egg-info/SOURCES.txt +0 -0
  122. {vectorbt-1.1.0 → vectorbt-1.1.2}/vectorbt.egg-info/dependency_links.txt +0 -0
  123. {vectorbt-1.1.0 → vectorbt-1.1.2}/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.1.0
3
+ Version: 1.1.2
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
@@ -37,7 +37,7 @@ Requires-Dist: requests
37
37
  Requires-Dist: pytz
38
38
  Requires-Dist: mypy_extensions
39
39
  Provides-Extra: rust
40
- Requires-Dist: vectorbt-rust==1.1.0; extra == "rust"
40
+ Requires-Dist: vectorbt-rust==1.1.2; extra == "rust"
41
41
  Provides-Extra: full
42
42
  Requires-Dist: TA-Lib; extra == "full"
43
43
  Requires-Dist: yfinance>=0.2.22; extra == "full"
@@ -58,9 +58,8 @@ Provides-Extra: test-rust
58
58
  Requires-Dist: vectorbt[test]; extra == "test-rust"
59
59
  Requires-Dist: maturin<2.0,>=1.14; extra == "test-rust"
60
60
  Provides-Extra: docs
61
- Requires-Dist: mkdocs-material; extra == "docs"
62
- Requires-Dist: mkdocs-minify-plugin; extra == "docs"
63
- Requires-Dist: ruamel.yaml; extra == "docs"
61
+ Requires-Dist: zensical>=0.0.62; extra == "docs"
62
+ Requires-Dist: tomlkit>=0.13; extra == "docs"
64
63
  Requires-Dist: mako; extra == "docs"
65
64
  Provides-Extra: all
66
65
  Requires-Dist: vectorbt[rust]; extra == "all"
@@ -83,20 +82,14 @@ Dynamic: license-file
83
82
  <br>
84
83
 
85
84
  <p align="center">
85
+ <a href="https://pepy.tech/project/vectorbt" title="Downloads">
86
+ <img src="https://img.shields.io/pepy/dt/vectorbt?label=downloads&color=blue" />
87
+ </a>
86
88
  <a href="https://pypi.org/project/vectorbt" title="PyPI">
87
- <img src="https://img.shields.io/pypi/v/vectorbt?label=pypi&color=blueviolet" />
89
+ <img src="https://img.shields.io/pypi/v/vectorbt" />
88
90
  </a>
89
91
  <a href="https://pypi.org/project/vectorbt" title="Supported Python versions">
90
- <img src="https://img.shields.io/pypi/pyversions/vectorbt?logo=python&logoColor=white" />
91
- </a>
92
- <a href="https://pypi.org/project/vectorbt" title="Python implementation">
93
- <img src="https://img.shields.io/pypi/implementation/vectorbt?label=implementation&color=3776ab&logo=python&logoColor=white" />
94
- </a>
95
- <a href="https://pypi.org/project/vectorbt" title="Wheel">
96
- <img src="https://img.shields.io/pypi/wheel/vectorbt?color=4c1&logo=python&logoColor=white" />
97
- </a>
98
- <a href="https://pypi.org/project/vectorbt-rust" title="Rust engine">
99
- <img src="https://img.shields.io/pypi/v/vectorbt-rust?label=rust%20engine&color=dea584&logo=rust" />
92
+ <img src="https://img.shields.io/pypi/pyversions/vectorbt" />
100
93
  </a>
101
94
  <a href="https://hub.docker.com/r/polakowo/vectorbt" title="Docker image version">
102
95
  <img src="https://img.shields.io/docker/v/polakowo/vectorbt?sort=semver&label=docker&color=2496ed&logo=docker&logoColor=white" />
@@ -104,35 +97,17 @@ Dynamic: license-file
104
97
  <a href="https://hub.docker.com/r/polakowo/vectorbt" title="Docker pulls">
105
98
  <img src="https://img.shields.io/docker/pulls/polakowo/vectorbt?label=docker%20pulls&color=2496ed&logo=docker&logoColor=white" />
106
99
  </a>
107
- <a href="https://pepy.tech/project/vectorbt" title="Downloads">
108
- <img src="https://img.shields.io/pepy/dt/vectorbt?label=downloads&color=blue" />
109
- </a>
110
100
  <a href="https://github.com/polakowo/vectorbt/actions/workflows/tests.yml" title="Tests">
111
101
  <img src="https://img.shields.io/github/actions/workflow/status/polakowo/vectorbt/tests.yml?branch=master&label=tests&logo=githubactions&logoColor=white" />
112
102
  </a>
113
103
  <a href="https://github.com/polakowo/vectorbt/actions/workflows/pypi.yml" title="PyPI release">
114
104
  <img src="https://img.shields.io/github/actions/workflow/status/polakowo/vectorbt/pypi.yml?label=release&logo=githubactions&logoColor=white" />
115
105
  </a>
116
- <a href="https://codecov.io/gh/polakowo/vectorbt" title="codecov">
117
- <img src="https://codecov.io/gh/polakowo/vectorbt/branch/master/graph/badge.svg?token=YTLNAI7PS3" />
118
- </a>
119
106
  <a href="https://vectorbt.dev/" title="Website">
120
107
  <img src="https://img.shields.io/website?url=https%3A%2F%2Fvectorbt.dev%2F&label=website" />
121
108
  </a>
122
- <a href="https://mybinder.org/v2/gh/polakowo/vectorbt/HEAD?urlpath=lab" title="Launch Binder">
123
- <img src="https://img.shields.io/badge/launch-binder-d6604a?logo=jupyter&logoColor=white" />
124
- </a>
125
- <a href="https://github.com/polakowo/vectorbt/stargazers" title="Stars">
126
- <img src="https://img.shields.io/github/stars/polakowo/vectorbt?style=flat&color=yellow" />
127
- </a>
128
- <a href="https://github.com/polakowo/vectorbt/network/members" title="Forks">
129
- <img src="https://img.shields.io/github/forks/polakowo/vectorbt?style=flat&color=blue" />
130
- </a>
131
- <a href="https://github.com/polakowo/vectorbt/graphs/contributors" title="Contributors">
132
- <img src="https://img.shields.io/github/contributors/polakowo/vectorbt?style=flat&color=blue" />
133
- </a>
134
- <a href="https://gitter.im/vectorbt/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" title="Join the chat">
135
- <img src="https://img.shields.io/gitter/room/vectorbt/community?logo=gitter&logoColor=white" />
109
+ <a href="https://pypi.org/project/vectorbt-rust" title="Rust engine">
110
+ <img src="https://img.shields.io/pypi/v/vectorbt-rust?label=rust%20engine&color=dea584&logo=rust" />
136
111
  </a>
137
112
  <a href="https://github.com/polakowo/vectorbt/blob/master/LICENSE.md" title="License">
138
113
  <img src="https://img.shields.io/badge/license-Fair%20Code-yellow" />
@@ -147,7 +122,13 @@ Dynamic: license-file
147
122
 
148
123
  Explore thousands of trading ideas across assets and timeframes, analyze portfolio performance down to individual trades, and visualize results interactively, all in a few lines of code. Built for both human researchers and AI agents, VectorBT combines large-scale experimentation with a mature, battle-tested backtesting stack refined through years of community use.
149
124
 
150
- VectorBT is the open-source community edition of [VectorBT PRO](https://vectorbt.pro/), a state-of-the-art hybrid backtesting library.
125
+ VectorBT is the open-source community edition of [VectorBT PRO](https://vectorbt.pro/).
126
+ PRO extends the library with parallelization, additional data integrations, portfolio optimization,
127
+ pattern recognition, event projections, limit orders, leverage, and over 100 other features,
128
+ with new features added weekly.
129
+
130
+ See [VectorBT vs PRO](https://vectorbt.dev/getting-started/upgrade/) or browse
131
+ [PRO feature examples](https://vectorbt.pro/features/).
151
132
 
152
133
  ## Features
153
134
 
@@ -222,6 +203,13 @@ print(pf.total_profit())
222
203
  34417.80960086067
223
204
  ```
224
205
 
206
+ > [!TIP]
207
+ > **VectorBT PRO**
208
+ >
209
+ > Simulate [limit orders](https://vectorbt.pro/features/backtesting/orders-and-execution/#limit-orders),
210
+ > use [leverage](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#leverage), and model futures with
211
+ > [contract multipliers](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#contract-multiplier).
212
+
225
213
  ### Generate 1,000 random strategies
226
214
 
227
215
  ```python
@@ -263,6 +251,15 @@ fig.show()
263
251
 
264
252
  <img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/usage_dmac_heatmap.gif">
265
253
 
254
+ > [!TIP]
255
+ > **VectorBT PRO**
256
+ >
257
+ > Use [random search](https://vectorbt.pro/features/optimization/strategy-optimization/#random-search) to sample a large
258
+ > parameter grid, or [conditional parameters](https://vectorbt.pro/features/optimization/strategy-optimization/#conditional-parameters)
259
+ > to exclude combinations such as a fast MA window longer than the slow MA window. The
260
+ > [cross-validation tutorial](https://vectorbt.pro/tutorials/cross-validation/)
261
+ > shows how to select parameters on training data and evaluate them on later periods.
262
+
266
263
  ### Inspect any strategy configuration
267
264
 
268
265
  ```python
@@ -370,9 +367,15 @@ If you have questions or want to request a license exception, please [contact th
370
367
 
371
368
  Installing optional dependencies may be subject to a more restrictive license.
372
369
 
373
- ## Star history
370
+ ## Star History
374
371
 
375
- [![Star History Chart](https://api.star-history.com/svg?repos=polakowo/vectorbt&type=Timeline)](https://star-history.com/#polakowo/vectorbt&Timeline)
372
+ <a href="https://www.star-history.com/?repos=polakowo%2Fvectorbt&type=timeline&legend=top-left">
373
+ <picture>
374
+ <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=polakowo/vectorbt&type=timeline&theme=dark&legend=top-left&sealed_token=482an5rek002Q4c3VCaRd7goV3LwR6GP6m56UOqTunBJKoEq19_RkbQldrL-OP4f5XnDtU9tOOTrs-NTtnqFoQoUZJdWPQEQ-eO2q1McxpYu-STLc1fj9tY60p-spkcSQ9bxs7WJkumrqYI1LSc3mBd7CIu2K7IuYNAgv1pYuFJOQaN1yD5p3wWNgcFM" />
375
+ <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=polakowo/vectorbt&type=timeline&legend=top-left&sealed_token=482an5rek002Q4c3VCaRd7goV3LwR6GP6m56UOqTunBJKoEq19_RkbQldrL-OP4f5XnDtU9tOOTrs-NTtnqFoQoUZJdWPQEQ-eO2q1McxpYu-STLc1fj9tY60p-spkcSQ9bxs7WJkumrqYI1LSc3mBd7CIu2K7IuYNAgv1pYuFJOQaN1yD5p3wWNgcFM" />
376
+ <img alt="Star History Chart" src="https://api.star-history.com/chart?repos=polakowo/vectorbt&type=timeline&legend=top-left&sealed_token=482an5rek002Q4c3VCaRd7goV3LwR6GP6m56UOqTunBJKoEq19_RkbQldrL-OP4f5XnDtU9tOOTrs-NTtnqFoQoUZJdWPQEQ-eO2q1McxpYu-STLc1fj9tY60p-spkcSQ9bxs7WJkumrqYI1LSc3mBd7CIu2K7IuYNAgv1pYuFJOQaN1yD5p3wWNgcFM" />
377
+ </picture>
378
+ </a>
376
379
 
377
380
  ## Disclaimer
378
381
 
@@ -12,20 +12,14 @@
12
12
  <br>
13
13
 
14
14
  <p align="center">
15
+ <a href="https://pepy.tech/project/vectorbt" title="Downloads">
16
+ <img src="https://img.shields.io/pepy/dt/vectorbt?label=downloads&color=blue" />
17
+ </a>
15
18
  <a href="https://pypi.org/project/vectorbt" title="PyPI">
16
- <img src="https://img.shields.io/pypi/v/vectorbt?label=pypi&color=blueviolet" />
19
+ <img src="https://img.shields.io/pypi/v/vectorbt" />
17
20
  </a>
18
21
  <a href="https://pypi.org/project/vectorbt" title="Supported Python versions">
19
- <img src="https://img.shields.io/pypi/pyversions/vectorbt?logo=python&logoColor=white" />
20
- </a>
21
- <a href="https://pypi.org/project/vectorbt" title="Python implementation">
22
- <img src="https://img.shields.io/pypi/implementation/vectorbt?label=implementation&color=3776ab&logo=python&logoColor=white" />
23
- </a>
24
- <a href="https://pypi.org/project/vectorbt" title="Wheel">
25
- <img src="https://img.shields.io/pypi/wheel/vectorbt?color=4c1&logo=python&logoColor=white" />
26
- </a>
27
- <a href="https://pypi.org/project/vectorbt-rust" title="Rust engine">
28
- <img src="https://img.shields.io/pypi/v/vectorbt-rust?label=rust%20engine&color=dea584&logo=rust" />
22
+ <img src="https://img.shields.io/pypi/pyversions/vectorbt" />
29
23
  </a>
30
24
  <a href="https://hub.docker.com/r/polakowo/vectorbt" title="Docker image version">
31
25
  <img src="https://img.shields.io/docker/v/polakowo/vectorbt?sort=semver&label=docker&color=2496ed&logo=docker&logoColor=white" />
@@ -33,35 +27,17 @@
33
27
  <a href="https://hub.docker.com/r/polakowo/vectorbt" title="Docker pulls">
34
28
  <img src="https://img.shields.io/docker/pulls/polakowo/vectorbt?label=docker%20pulls&color=2496ed&logo=docker&logoColor=white" />
35
29
  </a>
36
- <a href="https://pepy.tech/project/vectorbt" title="Downloads">
37
- <img src="https://img.shields.io/pepy/dt/vectorbt?label=downloads&color=blue" />
38
- </a>
39
30
  <a href="https://github.com/polakowo/vectorbt/actions/workflows/tests.yml" title="Tests">
40
31
  <img src="https://img.shields.io/github/actions/workflow/status/polakowo/vectorbt/tests.yml?branch=master&label=tests&logo=githubactions&logoColor=white" />
41
32
  </a>
42
33
  <a href="https://github.com/polakowo/vectorbt/actions/workflows/pypi.yml" title="PyPI release">
43
34
  <img src="https://img.shields.io/github/actions/workflow/status/polakowo/vectorbt/pypi.yml?label=release&logo=githubactions&logoColor=white" />
44
35
  </a>
45
- <a href="https://codecov.io/gh/polakowo/vectorbt" title="codecov">
46
- <img src="https://codecov.io/gh/polakowo/vectorbt/branch/master/graph/badge.svg?token=YTLNAI7PS3" />
47
- </a>
48
36
  <a href="https://vectorbt.dev/" title="Website">
49
37
  <img src="https://img.shields.io/website?url=https%3A%2F%2Fvectorbt.dev%2F&label=website" />
50
38
  </a>
51
- <a href="https://mybinder.org/v2/gh/polakowo/vectorbt/HEAD?urlpath=lab" title="Launch Binder">
52
- <img src="https://img.shields.io/badge/launch-binder-d6604a?logo=jupyter&logoColor=white" />
53
- </a>
54
- <a href="https://github.com/polakowo/vectorbt/stargazers" title="Stars">
55
- <img src="https://img.shields.io/github/stars/polakowo/vectorbt?style=flat&color=yellow" />
56
- </a>
57
- <a href="https://github.com/polakowo/vectorbt/network/members" title="Forks">
58
- <img src="https://img.shields.io/github/forks/polakowo/vectorbt?style=flat&color=blue" />
59
- </a>
60
- <a href="https://github.com/polakowo/vectorbt/graphs/contributors" title="Contributors">
61
- <img src="https://img.shields.io/github/contributors/polakowo/vectorbt?style=flat&color=blue" />
62
- </a>
63
- <a href="https://gitter.im/vectorbt/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" title="Join the chat">
64
- <img src="https://img.shields.io/gitter/room/vectorbt/community?logo=gitter&logoColor=white" />
39
+ <a href="https://pypi.org/project/vectorbt-rust" title="Rust engine">
40
+ <img src="https://img.shields.io/pypi/v/vectorbt-rust?label=rust%20engine&color=dea584&logo=rust" />
65
41
  </a>
66
42
  <a href="https://github.com/polakowo/vectorbt/blob/master/LICENSE.md" title="License">
67
43
  <img src="https://img.shields.io/badge/license-Fair%20Code-yellow" />
@@ -76,7 +52,13 @@
76
52
 
77
53
  Explore thousands of trading ideas across assets and timeframes, analyze portfolio performance down to individual trades, and visualize results interactively, all in a few lines of code. Built for both human researchers and AI agents, VectorBT combines large-scale experimentation with a mature, battle-tested backtesting stack refined through years of community use.
78
54
 
79
- VectorBT is the open-source community edition of [VectorBT PRO](https://vectorbt.pro/), a state-of-the-art hybrid backtesting library.
55
+ VectorBT is the open-source community edition of [VectorBT PRO](https://vectorbt.pro/).
56
+ PRO extends the library with parallelization, additional data integrations, portfolio optimization,
57
+ pattern recognition, event projections, limit orders, leverage, and over 100 other features,
58
+ with new features added weekly.
59
+
60
+ See [VectorBT vs PRO](https://vectorbt.dev/getting-started/upgrade/) or browse
61
+ [PRO feature examples](https://vectorbt.pro/features/).
80
62
 
81
63
  ## Features
82
64
 
@@ -151,6 +133,13 @@ print(pf.total_profit())
151
133
  34417.80960086067
152
134
  ```
153
135
 
136
+ > [!TIP]
137
+ > **VectorBT PRO**
138
+ >
139
+ > Simulate [limit orders](https://vectorbt.pro/features/backtesting/orders-and-execution/#limit-orders),
140
+ > use [leverage](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#leverage), and model futures with
141
+ > [contract multipliers](https://vectorbt.pro/features/backtesting/fees-slippage-and-leverage/#contract-multiplier).
142
+
154
143
  ### Generate 1,000 random strategies
155
144
 
156
145
  ```python
@@ -192,6 +181,15 @@ fig.show()
192
181
 
193
182
  <img width="750" src="https://raw.githubusercontent.com/polakowo/vectorbt/master/docs/docs/assets/images/usage_dmac_heatmap.gif">
194
183
 
184
+ > [!TIP]
185
+ > **VectorBT PRO**
186
+ >
187
+ > Use [random search](https://vectorbt.pro/features/optimization/strategy-optimization/#random-search) to sample a large
188
+ > parameter grid, or [conditional parameters](https://vectorbt.pro/features/optimization/strategy-optimization/#conditional-parameters)
189
+ > to exclude combinations such as a fast MA window longer than the slow MA window. The
190
+ > [cross-validation tutorial](https://vectorbt.pro/tutorials/cross-validation/)
191
+ > shows how to select parameters on training data and evaluate them on later periods.
192
+
195
193
  ### Inspect any strategy configuration
196
194
 
197
195
  ```python
@@ -299,9 +297,15 @@ If you have questions or want to request a license exception, please [contact th
299
297
 
300
298
  Installing optional dependencies may be subject to a more restrictive license.
301
299
 
302
- ## Star history
300
+ ## Star History
303
301
 
304
- [![Star History Chart](https://api.star-history.com/svg?repos=polakowo/vectorbt&type=Timeline)](https://star-history.com/#polakowo/vectorbt&Timeline)
302
+ <a href="https://www.star-history.com/?repos=polakowo%2Fvectorbt&type=timeline&legend=top-left">
303
+ <picture>
304
+ <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=polakowo/vectorbt&type=timeline&theme=dark&legend=top-left&sealed_token=482an5rek002Q4c3VCaRd7goV3LwR6GP6m56UOqTunBJKoEq19_RkbQldrL-OP4f5XnDtU9tOOTrs-NTtnqFoQoUZJdWPQEQ-eO2q1McxpYu-STLc1fj9tY60p-spkcSQ9bxs7WJkumrqYI1LSc3mBd7CIu2K7IuYNAgv1pYuFJOQaN1yD5p3wWNgcFM" />
305
+ <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=polakowo/vectorbt&type=timeline&legend=top-left&sealed_token=482an5rek002Q4c3VCaRd7goV3LwR6GP6m56UOqTunBJKoEq19_RkbQldrL-OP4f5XnDtU9tOOTrs-NTtnqFoQoUZJdWPQEQ-eO2q1McxpYu-STLc1fj9tY60p-spkcSQ9bxs7WJkumrqYI1LSc3mBd7CIu2K7IuYNAgv1pYuFJOQaN1yD5p3wWNgcFM" />
306
+ <img alt="Star History Chart" src="https://api.star-history.com/chart?repos=polakowo/vectorbt&type=timeline&legend=top-left&sealed_token=482an5rek002Q4c3VCaRd7goV3LwR6GP6m56UOqTunBJKoEq19_RkbQldrL-OP4f5XnDtU9tOOTrs-NTtnqFoQoUZJdWPQEQ-eO2q1McxpYu-STLc1fj9tY60p-spkcSQ9bxs7WJkumrqYI1LSc3mBd7CIu2K7IuYNAgv1pYuFJOQaN1yD5p3wWNgcFM" />
307
+ </picture>
308
+ </a>
305
309
 
306
310
  ## Disclaimer
307
311
 
@@ -48,7 +48,7 @@ dependencies = [
48
48
 
49
49
  [project.optional-dependencies]
50
50
  rust = [
51
- "vectorbt-rust==1.1.0",
51
+ "vectorbt-rust==1.1.2",
52
52
  ]
53
53
  full = [
54
54
  "TA-Lib",
@@ -73,9 +73,8 @@ test-rust = [
73
73
  "maturin>=1.14,<2.0",
74
74
  ]
75
75
  docs = [
76
- "mkdocs-material",
77
- "mkdocs-minify-plugin",
78
- "ruamel.yaml",
76
+ "zensical>=0.0.62",
77
+ "tomlkit>=0.13",
79
78
  "mako",
80
79
  ]
81
80
  all = [
@@ -539,6 +539,16 @@ class TestGenericRustParity:
539
539
  np.full(a.shape, np.nan),
540
540
  equal_nan=True,
541
541
  )
542
+ np.testing.assert_allclose(
543
+ dispatch.rolling_std_1d(a_1d, 0, 0, 0, engine="rust"),
544
+ np.full(a_1d.shape, np.nan),
545
+ equal_nan=True,
546
+ )
547
+ np.testing.assert_allclose(
548
+ dispatch.rolling_std(a, 0, 0, 0, engine="rust"),
549
+ np.full(a.shape, np.nan),
550
+ equal_nan=True,
551
+ )
542
552
  np.testing.assert_allclose(
543
553
  dispatch.expanding_std_1d(a_1d, 1, 5, engine="rust"),
544
554
  np.full(a_1d.shape, np.nan),
@@ -550,6 +560,40 @@ class TestGenericRustParity:
550
560
  equal_nan=True,
551
561
  )
552
562
 
563
+ def test_rust_rolling_std_stability(self):
564
+ rng = np.random.default_rng(42)
565
+ window = 4000
566
+ a_1d = 1e8 + rng.normal(0, 0.01, 5000)
567
+ a_1d[::37] = np.nan
568
+ expected_1d = pd.Series(a_1d).rolling(window, min_periods=10).std(ddof=1).to_numpy()
569
+ np.testing.assert_allclose(
570
+ dispatch.rolling_std_1d(a_1d, window, 10, 1, engine="rust"),
571
+ expected_1d,
572
+ atol=1e-12,
573
+ rtol=0,
574
+ equal_nan=True,
575
+ )
576
+
577
+ base = np.column_stack((a_1d, a_1d[::-1]))
578
+ expected = pd.DataFrame(base).rolling(window, min_periods=10).std(ddof=1).to_numpy()
579
+ for a in (np.ascontiguousarray(base), np.asfortranarray(base)):
580
+ np.testing.assert_allclose(
581
+ dispatch.rolling_std(a, window, 10, 1, engine="rust"),
582
+ expected,
583
+ atol=1e-12,
584
+ rtol=0,
585
+ equal_nan=True,
586
+ )
587
+
588
+ rng = np.random.default_rng(7)
589
+ window = 30
590
+ unstable = 1e14 + rng.normal(0, 1.0, 7000)
591
+ base = np.column_stack((unstable, unstable[::-1]))
592
+ np.testing.assert_array_equal(
593
+ dispatch.rolling_std(base, window, window, 1, engine="rust"),
594
+ nb.rolling_std_nb(base, window, window, 1),
595
+ )
596
+
553
597
  def test_dispatch_drawdown_helpers_broadcast(self):
554
598
  peak = np.array([10.0, 8.0, 12.0])
555
599
  valley = np.array([5.0])
@@ -8,6 +8,7 @@ from numba import njit
8
8
  from sklearn.model_selection import TimeSeriesSplit
9
9
 
10
10
  import vectorbt as vbt
11
+ from vectorbt import _engine
11
12
  from vectorbt.generic import dispatch, nb
12
13
 
13
14
  seed = 42
@@ -276,6 +277,41 @@ class TestAccessors:
276
277
  )
277
278
  pd.testing.assert_frame_equal(df.vbt.rolling_std(test_window), df.rolling(test_window).std())
278
279
 
280
+ @pytest.mark.parametrize("test_ddof", [0, 1])
281
+ def test_rolling_std_numerical_stability(self, test_ddof):
282
+ rng = np.random.default_rng(42)
283
+ window = 4000
284
+ large_offset_series = pd.Series(1e8 + rng.normal(0, 0.01, 5000))
285
+ got = large_offset_series.vbt.rolling_std(window, minp=window, ddof=test_ddof)
286
+ expected = large_offset_series.rolling(window, min_periods=window).std(ddof=test_ddof)
287
+ pd.testing.assert_series_equal(got, expected, atol=1e-6, rtol=0)
288
+
289
+ large_offset_nan_series = large_offset_series.copy()
290
+ large_offset_nan_series.iloc[::37] = np.nan
291
+ got_nan = large_offset_nan_series.vbt.rolling_std(window, minp=10, ddof=test_ddof)
292
+ expected_nan = large_offset_nan_series.rolling(window, min_periods=10).std(ddof=test_ddof)
293
+ pd.testing.assert_series_equal(got_nan, expected_nan, atol=1e-6, rtol=0)
294
+
295
+ def test_rolling_std_ddof_exceeds_window(self):
296
+ a = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
297
+ got = a.vbt.rolling_std(window=2, minp=1, ddof=5)
298
+ expected = a.rolling(window=2, min_periods=1).std(ddof=5)
299
+ pd.testing.assert_series_equal(got, expected)
300
+
301
+ got = a.vbt.rolling_std(window=0, minp=0, ddof=0)
302
+ expected = a.rolling(window=0, min_periods=0).std(ddof=0)
303
+ pd.testing.assert_series_equal(got, expected)
304
+
305
+ def test_rolling_std_recompute(self):
306
+ rng = np.random.default_rng(7)
307
+ window = 30
308
+ a = 1e14 + rng.normal(0, 1.0, 50000)
309
+ windows = np.lib.stride_tricks.sliding_window_view(a, window)
310
+ expected = np.std(windows - windows.mean(axis=1, keepdims=True), axis=1, ddof=1)
311
+ got = pd.Series(a).vbt.rolling_std(window, minp=window, ddof=1).values[window - 1 :]
312
+ assert not np.any(np.isnan(got) | (got == 0))
313
+ assert np.median(np.abs(got - expected) / expected) < 0.2
314
+
279
315
  @pytest.mark.parametrize(
280
316
  "test_window,test_minp,test_adjust", list(product([1, 2, 3, 4, 5], [1, None], [False, True]))
281
317
  )
@@ -344,6 +380,18 @@ class TestAccessors:
344
380
  )
345
381
  pd.testing.assert_frame_equal(df.vbt.expanding_std(), df.expanding().std())
346
382
 
383
+ @pytest.mark.parametrize("test_engine", ["numba", "rust"])
384
+ def test_expanding_minp_above_length(self, test_engine):
385
+ if test_engine == "rust" and not _engine.is_rust_available():
386
+ pytest.skip("vectorbt-rust is not installed or version-compatible")
387
+ minp = len(df.index) + 1
388
+ pd.testing.assert_frame_equal(
389
+ df.vbt.expanding_mean(minp=minp, engine=test_engine), df.expanding(min_periods=minp).mean()
390
+ )
391
+ pd.testing.assert_frame_equal(
392
+ df.vbt.expanding_std(minp=minp, engine=test_engine), df.expanding(min_periods=minp).std()
393
+ )
394
+
347
395
  def test_apply_along_axis(self):
348
396
  pd.testing.assert_frame_equal(
349
397
  df.vbt.apply_along_axis(i_or_col_pow_nb, 2, axis=0), df.apply(pow_nb, args=(2,), axis=0, raw=True)
@@ -22,9 +22,9 @@ from plotly.basedatatypes import BaseFigure
22
22
 
23
23
  import vectorbt as vbt
24
24
  from vectorbt.generic.plotting import Scatter, Bar, Histogram, Heatmap, Gauge, Box, Volume, TraceUpdater
25
+ from vectorbt.portfolio.base import Portfolio
25
26
  from vectorbt.utils.figure import make_figure, make_subplots, Figure, FigureWidget
26
27
 
27
-
28
28
  # ############# Fixtures ############# #
29
29
 
30
30
  index_5 = pd.DatetimeIndex([datetime(2020, 1, d) for d in range(1, 6)])
@@ -625,6 +625,45 @@ class TestTradesPlotPnl:
625
625
  # pct_scale=False: y-values are absolute PnL
626
626
  np.testing.assert_array_almost_equal(fig_abs.data[0].y, [1.78, 2.77])
627
627
 
628
+ def test_trades_plot_pnl_non_finite_returns(self):
629
+ """Non-finite returns should not invalidate Plotly marker metadata."""
630
+ # Cover the issue's exact case where no finite return is available for scaling.
631
+ open_pf = Portfolio.from_orders(
632
+ pd.Series([1.0, np.nan]),
633
+ pd.Series([1.0, 0.0]),
634
+ ffill_val_price=False,
635
+ )
636
+
637
+ # Exercise both the direct records API and its Portfolio convenience delegate.
638
+ for fig in (open_pf.get_trades().plot_pnl(), open_pf.plot_trade_pnl()):
639
+ assert isinstance(fig, BaseFigure)
640
+ nt = named_traces(fig)
641
+
642
+ # Without a finite reference, the open marker should use both lower bounds.
643
+ np.testing.assert_array_equal(nt["Open"].marker.size, [7.0])
644
+ np.testing.assert_array_equal(nt["Open"].marker.opacity, [0.75])
645
+
646
+ # One unknown open trade must not alter the scaling of finite closed trades.
647
+ close = pd.Series([1.0, 2.0, 1.0, 3.0, 1.0, np.nan])
648
+ size = pd.Series([1.0, -1.0, 1.0, -1.0, 1.0, 0.0])
649
+ pf = Portfolio.from_orders(close, size, ffill_val_price=False)
650
+
651
+ # Require identical marker behavior from the direct and delegated plotting paths.
652
+ for fig in (pf.get_trades().plot_pnl(), pf.plot_trade_pnl()):
653
+ assert isinstance(fig, BaseFigure)
654
+ nt = named_traces(fig)
655
+
656
+ # Finite trades keep their relative endpoints while the unknown trade uses the minima.
657
+ np.testing.assert_array_equal(nt["Closed - Profit"].marker.size, [7.0, 14.0])
658
+ np.testing.assert_array_equal(nt["Closed - Profit"].marker.opacity, [0.75, 0.9])
659
+ np.testing.assert_array_equal(nt["Open"].marker.size, [7.0])
660
+ np.testing.assert_array_equal(nt["Open"].marker.opacity, [0.75])
661
+
662
+ # Guard every emitted trace against metadata that Plotly would reject.
663
+ for trace in fig.data:
664
+ assert np.all(np.isfinite(trace.marker.size))
665
+ assert np.all(np.isfinite(trace.marker.opacity))
666
+
628
667
 
629
668
  class TestDrawdownsPlot:
630
669
  def test_drawdowns_plot_with_zones(self):
@@ -84,17 +84,51 @@ def test_order_func_init_records(test_row_wise, test_flexible):
84
84
  assert pf.orders.count() == 0
85
85
  record_arrays_close(
86
86
  seen["order_records"],
87
- np.array([( -1, -1, -1, np.nan, np.nan, np.nan, -1)], dtype=order_dt),
87
+ np.array([(-1, -1, -1, np.nan, np.nan, np.nan, -1)], dtype=order_dt),
88
88
  )
89
89
  record_arrays_close(
90
90
  seen["log_records"],
91
91
  np.array(
92
92
  [
93
93
  (
94
- -1, -1, -1, -1, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
95
- np.nan, np.nan, -1, -1, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
96
- np.nan, False, False, False, False, np.nan, np.nan, np.nan, np.nan, np.nan,
97
- np.nan, np.nan, np.nan, np.nan, -1, -1, -1, -1
94
+ -1,
95
+ -1,
96
+ -1,
97
+ -1,
98
+ np.nan,
99
+ np.nan,
100
+ np.nan,
101
+ np.nan,
102
+ np.nan,
103
+ np.nan,
104
+ np.nan,
105
+ np.nan,
106
+ -1,
107
+ -1,
108
+ np.nan,
109
+ np.nan,
110
+ np.nan,
111
+ np.nan,
112
+ np.nan,
113
+ np.nan,
114
+ np.nan,
115
+ False,
116
+ False,
117
+ False,
118
+ False,
119
+ np.nan,
120
+ np.nan,
121
+ np.nan,
122
+ np.nan,
123
+ np.nan,
124
+ np.nan,
125
+ np.nan,
126
+ np.nan,
127
+ np.nan,
128
+ -1,
129
+ -1,
130
+ -1,
131
+ -1,
98
132
  )
99
133
  ],
100
134
  dtype=log_dt,
@@ -102,12 +136,16 @@ def test_order_func_init_records(test_row_wise, test_flexible):
102
136
  )
103
137
  record_arrays_close(
104
138
  seen["last_pos_record"],
105
- np.array([(-1, -1, np.nan, -1, np.nan, np.nan, -1, np.nan, np.nan, np.nan, np.nan, -1, -1, -1)], dtype=trade_dt),
139
+ np.array(
140
+ [(-1, -1, np.nan, -1, np.nan, np.nan, -1, np.nan, np.nan, np.nan, np.nan, -1, -1, -1)], dtype=trade_dt
141
+ ),
106
142
  )
107
143
  if not test_flexible:
108
144
  record_arrays_close(
109
145
  seen["pos_record_now"],
110
- np.array((-1, -1, np.nan, -1, np.nan, np.nan, -1, np.nan, np.nan, np.nan, np.nan, -1, -1, -1), dtype=trade_dt),
146
+ np.array(
147
+ (-1, -1, np.nan, -1, np.nan, np.nan, -1, np.nan, np.nan, np.nan, np.nan, -1, -1, -1), dtype=trade_dt
148
+ ),
111
149
  )
112
150
 
113
151
 
@@ -10829,3 +10867,89 @@ class TestPortfolio:
10829
10867
  _ = pf.plot(subplots="all")
10830
10868
  with pytest.raises(Exception):
10831
10869
  _ = pf_grouped.plot(subplots="all")
10870
+
10871
+
10872
+ # ############# Issue #810: Best Trade [%] definition ############# #
10873
+
10874
+
10875
+ def test_best_trade_pct_definition():
10876
+ """Regression test for issue #810.
10877
+
10878
+ Best Trade [%] is the maximum individual trade return, net of fees, relative
10879
+ to gross entry notional (NOT portfolio initial cash). By default, only closed
10880
+ trades are included; incl_open=True also includes open trades.
10881
+ """
10882
+ # Two short trades: B has larger PnL, A has higher return
10883
+ # Trade A: entry=100, exit=90, size=10 -> PnL=100, Return=10%
10884
+ # Trade B: entry=200, exit=190, size=100 -> PnL=1000, Return=5%
10885
+ price = pd.Series(
10886
+ [100.0, 90.0, 200.0, 190.0],
10887
+ index=pd.date_range("2020-01-01", periods=4, freq="D"),
10888
+ )
10889
+ # Entries at 0 and 2, exits at 1 and 3
10890
+ entries = pd.Series([True, False, True, False], index=price.index)
10891
+ exits = pd.Series([False, True, False, True], index=price.index)
10892
+ size = pd.Series([10.0, np.nan, 100.0, np.nan], index=price.index)
10893
+
10894
+ pf = vbt.Portfolio.from_signals(price, entries, exits, size=size, direction="shortonly", init_cash=1000000)
10895
+
10896
+ # B has the larger PnL
10897
+ assert pf.trades.pnl.max() == 1000.0
10898
+ # But A has the higher return -> Best Trade [%] must be 10.0, not 5.0
10899
+ np.testing.assert_allclose(pf.trades.returns.max(), 0.10)
10900
+ np.testing.assert_allclose(pf.stats()["Best Trade [%]"], 10.0)
10901
+
10902
+ # Definition consistency: stats value == max trade return * 100
10903
+ expected = pf.trades.closed.returns.max() * 100
10904
+ np.testing.assert_allclose(pf.stats()["Best Trade [%]"], expected)
10905
+
10906
+
10907
+ @pytest.mark.parametrize("fees", [0.0, 0.001])
10908
+ def test_short_trade_return_math(fees):
10909
+ """Short trade return is net of fees, relative to gross entry notional."""
10910
+ price = pd.Series(
10911
+ [100.0, 90.0],
10912
+ index=pd.date_range("2020-01-01", periods=2, freq="D"),
10913
+ )
10914
+ entries = pd.Series([True, False], index=price.index)
10915
+ exits = pd.Series([False, True], index=price.index)
10916
+
10917
+ pf = vbt.Portfolio.from_signals(price, entries, exits, size=10.0, direction="shortonly", fees=fees)
10918
+ trade = pf.trades.records_readable.iloc[0]
10919
+ # Short: profit when price falls. entry=100, exit=90, size=10
10920
+ # Both entry and exit fees reduce PnL; the denominator excludes fees.
10921
+ expected_pnl = 10.0 * (100.0 - 90.0) - fees * 10.0 * (100.0 + 90.0)
10922
+ np.testing.assert_allclose(trade["PnL"], expected_pnl)
10923
+ np.testing.assert_allclose(trade["Return"], trade["PnL"] / (trade["Size"] * trade["Avg Entry Price"]))
10924
+ np.testing.assert_allclose(trade["Return"], expected_pnl / 1000.0)
10925
+
10926
+
10927
+ @pytest.mark.parametrize("incl_open", [False, True])
10928
+ def test_best_worst_trade_pct_incl_open(incl_open):
10929
+ """Best/Worst Trade [%] use closed trades unless incl_open=True."""
10930
+ price = pd.DataFrame(
10931
+ {"best": [100.0, 90.0, 100.0, 80.0, 80.0, 80.0], "worst": [100.0, 90.0, 100.0, 80.0, 100.0, 110.0]},
10932
+ index=pd.date_range("2020-01-01", periods=6, freq="D"),
10933
+ )
10934
+ entries = pd.DataFrame(
10935
+ {
10936
+ "best": [True, False, True, False, False, False],
10937
+ "worst": [True, False, False, False, True, False],
10938
+ },
10939
+ index=price.index,
10940
+ )
10941
+ exits = pd.Series([False, True, False, False, False, False], index=price.index)
10942
+ pf = vbt.Portfolio.from_signals(price, entries, exits, size=10.0, direction="shortonly", fees=0.001)
10943
+ for column, metric, reducer in [
10944
+ ("best", "Best Trade [%]", "max"),
10945
+ ("worst", "Worst Trade [%]", "min"),
10946
+ ]:
10947
+ trades = pf[column].trades
10948
+ assert trades.closed.count() == 1
10949
+ assert trades.open.count() == 1
10950
+ selected = trades if incl_open else trades.closed
10951
+ expected = getattr(selected.returns, reducer)() * 100
10952
+ actual = pf.stats(column=column, settings=dict(incl_open=incl_open))[metric]
10953
+ np.testing.assert_allclose(actual, expected)
10954
+ if incl_open:
10955
+ assert not np.isclose(actual, getattr(trades.closed.returns, reducer)() * 100)