pyvsmc 0.1.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.
- pyvsmc-0.1.0/LICENSE +21 -0
- pyvsmc-0.1.0/PKG-INFO +299 -0
- pyvsmc-0.1.0/README.md +253 -0
- pyvsmc-0.1.0/pyproject.toml +115 -0
- pyvsmc-0.1.0/setup.cfg +4 -0
- pyvsmc-0.1.0/src/pyvsmc/__init__.py +71 -0
- pyvsmc-0.1.0/src/pyvsmc/fvg.py +444 -0
- pyvsmc-0.1.0/src/pyvsmc/order_blocks.py +436 -0
- pyvsmc-0.1.0/src/pyvsmc/polars_ext.py +375 -0
- pyvsmc-0.1.0/src/pyvsmc/py.typed +0 -0
- pyvsmc-0.1.0/src/pyvsmc/structure.py +438 -0
- pyvsmc-0.1.0/src/pyvsmc/swings.py +202 -0
- pyvsmc-0.1.0/src/pyvsmc.egg-info/PKG-INFO +299 -0
- pyvsmc-0.1.0/src/pyvsmc.egg-info/SOURCES.txt +20 -0
- pyvsmc-0.1.0/src/pyvsmc.egg-info/dependency_links.txt +1 -0
- pyvsmc-0.1.0/src/pyvsmc.egg-info/requires.txt +16 -0
- pyvsmc-0.1.0/src/pyvsmc.egg-info/top_level.txt +1 -0
- pyvsmc-0.1.0/tests/test_fvg.py +293 -0
- pyvsmc-0.1.0/tests/test_order_blocks.py +105 -0
- pyvsmc-0.1.0/tests/test_polars_ext.py +99 -0
- pyvsmc-0.1.0/tests/test_structure.py +235 -0
- pyvsmc-0.1.0/tests/test_swings.py +167 -0
pyvsmc-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vector_smc contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pyvsmc-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyvsmc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Ultra-fast, fully vectorized market structure and Smart Money Concepts (SMC) technical analysis using NumPy and Polars
|
|
5
|
+
Author-email: pyvsmc contributors <hello@pyvsmc.dev>
|
|
6
|
+
Maintainer-email: pyvsmc maintainers <hello@pyvsmc.dev>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/Khaymat/pyvsmc
|
|
9
|
+
Project-URL: Repository, https://github.com/Khaymat/pyvsmc
|
|
10
|
+
Project-URL: Documentation, https://github.com/Khaymat/pyvsmc#readme
|
|
11
|
+
Project-URL: Issues, https://github.com/Khaymat/pyvsmc/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/Khaymat/pyvsmc/blob/main/CHANGELOG.md
|
|
13
|
+
Keywords: trading,technical-analysis,smart-money-concepts,smc,market-structure,numpy,polars,quant
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Intended Audience :: Science/Research
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering
|
|
26
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
27
|
+
Classifier: Typing :: Typed
|
|
28
|
+
Requires-Python: >=3.10
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Requires-Dist: numpy>=1.24.0
|
|
32
|
+
Requires-Dist: polars>=0.20.0
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
36
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
38
|
+
Requires-Dist: numpy>=1.24.0; extra == "dev"
|
|
39
|
+
Requires-Dist: polars>=0.20.0; extra == "dev"
|
|
40
|
+
Provides-Extra: test
|
|
41
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
|
42
|
+
Requires-Dist: pytest-cov>=4.0; extra == "test"
|
|
43
|
+
Requires-Dist: numpy>=1.24.0; extra == "test"
|
|
44
|
+
Requires-Dist: polars>=0.20.0; extra == "test"
|
|
45
|
+
Dynamic: license-file
|
|
46
|
+
|
|
47
|
+
# pyvsmc
|
|
48
|
+
|
|
49
|
+
[](https://python.org)
|
|
50
|
+
[](https://opensource.org/licenses/MIT)
|
|
51
|
+
[](https://pypi.org/project/pyvsmc/)
|
|
52
|
+
[](#testing)
|
|
53
|
+
[](#type-safety)
|
|
54
|
+
[](https://github.com/astral-sh/ruff)
|
|
55
|
+
|
|
56
|
+
**Ultra-fast, fully vectorized market structure & Smart Money Concepts (SMC) for Python.**
|
|
57
|
+
|
|
58
|
+
`pyvsmc` provides pure NumPy + Polars implementations of the most widely used SMC / ICT concepts — Fair Value Gaps, fractal swings, Break of Structure (BOS), Change of Character (CHOCH), and Order Blocks — with **zero Python for-loops over time-series**, strict typing, and a clean Polars plugin.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Features
|
|
63
|
+
|
|
64
|
+
| Module | Concept | Key Function |
|
|
65
|
+
|--------|---------|--------------|
|
|
66
|
+
| `fvg` | Fair Value Gap / Imbalance | `detect_fvg()` |
|
|
67
|
+
| `swings` | Fractal Swing Highs & Lows | `detect_swings()` |
|
|
68
|
+
| `structure` | BOS & CHOCH Engine | `detect_structure()` |
|
|
69
|
+
| `order_blocks` | Order Block Zones | `detect_order_blocks()` |
|
|
70
|
+
| `polars_ext` | Polars `.smc` namespace | `df.smc.add_all()` |
|
|
71
|
+
|
|
72
|
+
- **Performance:** 100% vectorized (`NumPy` / `Polars` vector expressions). No `.iterrows()`, `.apply()`, or Python loops over bars. Handles 100k+ candles in milliseconds.
|
|
73
|
+
- **Type Safety:** Strict `mypy` — all public APIs are fully typed with Google-style docstrings.
|
|
74
|
+
- **Polars Native:** Optional `pl.DataFrame.smc.*` namespace + `add_smc_columns()` helper.
|
|
75
|
+
- **Tested:** Comprehensive `pytest` suite covering normal, edge (empty, flat, NaN, length < 3), and benchmark cases.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Installation
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install pyvsmc
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
With Polars (recommended):
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install "pyvsmc[dev]" # includes polars, pytest, ruff, mypy
|
|
89
|
+
# or
|
|
90
|
+
pip install pyvsmc polars
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
From source:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
git clone https://github.com/Khaymat/pyvsmc
|
|
97
|
+
cd pyvsmc
|
|
98
|
+
pip install -e ".[dev]"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Requirements:** Python >= 3.10, `numpy>=1.24.0`, `polars>=0.20.0` (optional but recommended).
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Quickstart
|
|
106
|
+
|
|
107
|
+
### NumPy API
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import numpy as np
|
|
111
|
+
import pyvsmc as smc
|
|
112
|
+
|
|
113
|
+
# OHLC arrays (float)
|
|
114
|
+
high = np.array([10.0, 11.2, 10.8, 12.5, 11.0, 13.0])
|
|
115
|
+
low = np.array([ 9.5, 9.8, 10.0, 11.8, 10.5, 12.2])
|
|
116
|
+
close = np.array([10.0, 10.5, 10.2, 12.2, 11.1, 12.8])
|
|
117
|
+
open_ = np.array([ 9.8, 10.0, 10.4, 11.0, 11.5, 12.0])
|
|
118
|
+
|
|
119
|
+
# 1. Fair Value Gaps (with mitigation tracking)
|
|
120
|
+
fvg = smc.detect_fvg(high, low, min_gap_size=0.3, compute_mitigation=True)
|
|
121
|
+
print(fvg.bullish) # boolean mask
|
|
122
|
+
print(fvg.bullish_upper) # upper boundary (Low[i])
|
|
123
|
+
print(fvg.mitigated) # has price revisited the gap?
|
|
124
|
+
|
|
125
|
+
# 2. Fractal Swings
|
|
126
|
+
swings = smc.detect_swings(high, low, window_size=2)
|
|
127
|
+
print(swings.swing_high) # True where High[i] == max(window)
|
|
128
|
+
print(swings.swing_high_price)
|
|
129
|
+
|
|
130
|
+
# 3. Market Structure — BOS & CHOCH
|
|
131
|
+
structure = smc.detect_structure(high, low, close, window_size=2)
|
|
132
|
+
print(structure.bos_bullish) # continuation breaks
|
|
133
|
+
print(structure.choch_bearish) # reversal breaks
|
|
134
|
+
print(structure.trend) # 1=bull, -1=bear, 0=neutral
|
|
135
|
+
|
|
136
|
+
# 4. Order Blocks
|
|
137
|
+
obs = smc.detect_order_blocks(open_, high, low, close, lookback=5)
|
|
138
|
+
print(obs.bullish_ob) # True at the bearish candle before a bullish impulse
|
|
139
|
+
print(obs.ob_high, obs.ob_low)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Polars API
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
import polars as pl
|
|
146
|
+
import pyvsmc # registers .smc namespace
|
|
147
|
+
|
|
148
|
+
df = pl.DataFrame({
|
|
149
|
+
"open": open_,
|
|
150
|
+
"high": high,
|
|
151
|
+
"low": low,
|
|
152
|
+
"close": close,
|
|
153
|
+
"volume": [100, 120, 80, 200, 150, 180],
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
# Functional helper — adds all SMC columns at once
|
|
157
|
+
from pyvsmc.polars_ext import add_smc_columns
|
|
158
|
+
df = add_smc_columns(df, window_size=2, fvg_mitigation=True)
|
|
159
|
+
|
|
160
|
+
# Or via the .smc namespace (more granular)
|
|
161
|
+
df = pl.DataFrame({"open": open_, "high": high, "low": low, "close": close})
|
|
162
|
+
df = df.smc.add_all(window_size=2, ob_lookback=10)
|
|
163
|
+
df = df.smc.fvg(min_gap_size=0.5)
|
|
164
|
+
df = df.smc.swings(window_size=2)
|
|
165
|
+
df = df.smc.structure(window_size=2)
|
|
166
|
+
df = df.smc.order_blocks(lookback=5)
|
|
167
|
+
|
|
168
|
+
print(df)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Re-using Swings in Structure
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from pyvsmc.swings import detect_swings
|
|
175
|
+
from pyvsmc.structure import detect_structure
|
|
176
|
+
|
|
177
|
+
swings = detect_swings(high, low, window_size=3)
|
|
178
|
+
structure = detect_structure(
|
|
179
|
+
high, low, close,
|
|
180
|
+
swing_high=swings.swing_high,
|
|
181
|
+
swing_low=swings.swing_low,
|
|
182
|
+
)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## API Reference
|
|
188
|
+
|
|
189
|
+
### `detect_fvg(high, low, min_gap_size=None, min_gap_size_pct=None, *, compute_mitigation=False)`
|
|
190
|
+
|
|
191
|
+
Detects 3-candle Fair Value Gaps.
|
|
192
|
+
|
|
193
|
+
- **Bullish FVG:** `Low[i] > High[i-2]` — gap zone `[High[i-2], Low[i]]`
|
|
194
|
+
- **Bearish FVG:** `High[i] < Low[i-2]` — gap zone `[High[i], Low[i-2]]`
|
|
195
|
+
|
|
196
|
+
Returns `FVGResult` with `bullish`, `bearish`, `bullish_upper/lower`, `bearish_upper/lower`, `gap_size`, `gap_size_pct`, `mitigated`, `mitigated_index`.
|
|
197
|
+
|
|
198
|
+
### `detect_swings(high, low, window_size=2)`
|
|
199
|
+
|
|
200
|
+
Fractal swing detection. `High[i] == max(High[i-N:i+N+1])`, `Low[i] == min(Low[i-N:i+N+1])`. Returns `SwingResult`.
|
|
201
|
+
|
|
202
|
+
### `detect_structure(high, low, close, window_size=2, *, swing_high=None, swing_low=None)`
|
|
203
|
+
|
|
204
|
+
BOS (continuation) vs CHOCH (reversal) classification with trend tracking. Returns `StructureResult` with `bos_bullish/bearish`, `choch_bullish/bearish`, `bos_level`, `choch_level`, `trend`.
|
|
205
|
+
|
|
206
|
+
### `detect_order_blocks(open_, high, low, close, *, lookback=10, ...)`
|
|
207
|
+
|
|
208
|
+
Finds last opposing candle before FVG/BOS impulses. Returns `OrderBlockResult` with `bullish_ob/bearish_ob`, `ob_high/low`, `validated_index`, `impulse_type`.
|
|
209
|
+
|
|
210
|
+
All functions also have `*_polars(df, ...)` variants and are available via `df.smc.*`.
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## Testing
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
pip install -e ".[dev]"
|
|
218
|
+
pytest -v
|
|
219
|
+
pytest --cov=pyvsmc --cov-report=term-missing
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Run type checks and lint:
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
mypy src/pyvsmc
|
|
226
|
+
ruff check src/pyvsmc tests
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## Project Structure
|
|
232
|
+
|
|
233
|
+
```
|
|
234
|
+
pyvsmc/
|
|
235
|
+
├── pyproject.toml
|
|
236
|
+
├── README.md
|
|
237
|
+
├── src/pyvsmc/
|
|
238
|
+
│ ├── __init__.py
|
|
239
|
+
│ ├── py.typed
|
|
240
|
+
│ ├── fvg.py
|
|
241
|
+
│ ├── swings.py
|
|
242
|
+
│ ├── structure.py
|
|
243
|
+
│ ├── order_blocks.py
|
|
244
|
+
│ └── polars_ext.py
|
|
245
|
+
└── tests/
|
|
246
|
+
├── test_fvg.py
|
|
247
|
+
├── test_swings.py
|
|
248
|
+
├── test_structure.py
|
|
249
|
+
├── test_order_blocks.py
|
|
250
|
+
└── test_polars_ext.py
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Performance Notes
|
|
256
|
+
|
|
257
|
+
- All indicators use `numpy.lib.stride_tricks.sliding_window_view`, `np.maximum.accumulate`, broadcasting, and chunked evaluation — **no Python loops over bars**.
|
|
258
|
+
- The single exception is the BOS/CHOCH trend tracker, which requires sequential state. It is JIT-compiled with `numba` when available and falls back to a single O(n) scan otherwise.
|
|
259
|
+
- Benchmark: ~100k candles — FVG < 10ms, swings < 20ms, structure < 30ms (CPython 3.11, NumPy 1.26).
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## Financial & Legal Disclaimer
|
|
264
|
+
|
|
265
|
+
**IMPORTANT — PLEASE READ CAREFULLY**
|
|
266
|
+
|
|
267
|
+
`pyvsmc` is an **open-source analytics and research library**. It is provided solely for **educational, informational, and research purposes**.
|
|
268
|
+
|
|
269
|
+
- **Not Financial Advice.** Nothing in this library, its documentation, examples, or outputs constitutes financial, investment, trading, or other professional advice. No recommendation to buy, sell, or hold any financial instrument is made or implied.
|
|
270
|
+
- **No Warranty of Accuracy or Fitness.** Market structure and Smart Money Concepts are *interpretive frameworks*; their definitions vary across practitioners. The library implements one set of rules that may not match your trading methodology. Outputs may be incorrect, incomplete, or inappropriate for your use case.
|
|
271
|
+
- **Use at Your Own Risk.** Trading and investing involve substantial risk of loss, including loss of principal. Past simulated or historical performance is not indicative of future results. You are solely responsible for your own trading decisions, risk management, and compliance with applicable laws and regulations.
|
|
272
|
+
- **No Liability.** To the fullest extent permitted by law, the authors, contributors, and distributors of `pyvsmc` disclaim all liability for any loss, damage, cost, or expense arising directly or indirectly from use of this software.
|
|
273
|
+
- **Do Your Own Research (DYOR).** Always validate any signal or analysis with independent research, additional data sources, and, where appropriate, advice from a qualified professional.
|
|
274
|
+
|
|
275
|
+
By using this software you acknowledge that you have read, understood, and agree to this disclaimer.
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## License
|
|
280
|
+
|
|
281
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
282
|
+
|
|
283
|
+
Copyright (c) 2026 pyvsmc contributors.
|
|
284
|
+
|
|
285
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
286
|
+
|
|
287
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
288
|
+
|
|
289
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Contributing
|
|
294
|
+
|
|
295
|
+
Issues and pull requests are welcome. Please run `ruff`, `mypy`, and `pytest` before submitting.
|
|
296
|
+
|
|
297
|
+
## Acknowledgements
|
|
298
|
+
|
|
299
|
+
Built with [NumPy](https://numpy.org) and [Polars](https://pola.rs). SMC concepts as described by the broader ICT / Smart Money community.
|
pyvsmc-0.1.0/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# pyvsmc
|
|
2
|
+
|
|
3
|
+
[](https://python.org)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://pypi.org/project/pyvsmc/)
|
|
6
|
+
[](#testing)
|
|
7
|
+
[](#type-safety)
|
|
8
|
+
[](https://github.com/astral-sh/ruff)
|
|
9
|
+
|
|
10
|
+
**Ultra-fast, fully vectorized market structure & Smart Money Concepts (SMC) for Python.**
|
|
11
|
+
|
|
12
|
+
`pyvsmc` provides pure NumPy + Polars implementations of the most widely used SMC / ICT concepts — Fair Value Gaps, fractal swings, Break of Structure (BOS), Change of Character (CHOCH), and Order Blocks — with **zero Python for-loops over time-series**, strict typing, and a clean Polars plugin.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
| Module | Concept | Key Function |
|
|
19
|
+
|--------|---------|--------------|
|
|
20
|
+
| `fvg` | Fair Value Gap / Imbalance | `detect_fvg()` |
|
|
21
|
+
| `swings` | Fractal Swing Highs & Lows | `detect_swings()` |
|
|
22
|
+
| `structure` | BOS & CHOCH Engine | `detect_structure()` |
|
|
23
|
+
| `order_blocks` | Order Block Zones | `detect_order_blocks()` |
|
|
24
|
+
| `polars_ext` | Polars `.smc` namespace | `df.smc.add_all()` |
|
|
25
|
+
|
|
26
|
+
- **Performance:** 100% vectorized (`NumPy` / `Polars` vector expressions). No `.iterrows()`, `.apply()`, or Python loops over bars. Handles 100k+ candles in milliseconds.
|
|
27
|
+
- **Type Safety:** Strict `mypy` — all public APIs are fully typed with Google-style docstrings.
|
|
28
|
+
- **Polars Native:** Optional `pl.DataFrame.smc.*` namespace + `add_smc_columns()` helper.
|
|
29
|
+
- **Tested:** Comprehensive `pytest` suite covering normal, edge (empty, flat, NaN, length < 3), and benchmark cases.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install pyvsmc
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
With Polars (recommended):
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install "pyvsmc[dev]" # includes polars, pytest, ruff, mypy
|
|
43
|
+
# or
|
|
44
|
+
pip install pyvsmc polars
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
From source:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
git clone https://github.com/Khaymat/pyvsmc
|
|
51
|
+
cd pyvsmc
|
|
52
|
+
pip install -e ".[dev]"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Requirements:** Python >= 3.10, `numpy>=1.24.0`, `polars>=0.20.0` (optional but recommended).
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Quickstart
|
|
60
|
+
|
|
61
|
+
### NumPy API
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import numpy as np
|
|
65
|
+
import pyvsmc as smc
|
|
66
|
+
|
|
67
|
+
# OHLC arrays (float)
|
|
68
|
+
high = np.array([10.0, 11.2, 10.8, 12.5, 11.0, 13.0])
|
|
69
|
+
low = np.array([ 9.5, 9.8, 10.0, 11.8, 10.5, 12.2])
|
|
70
|
+
close = np.array([10.0, 10.5, 10.2, 12.2, 11.1, 12.8])
|
|
71
|
+
open_ = np.array([ 9.8, 10.0, 10.4, 11.0, 11.5, 12.0])
|
|
72
|
+
|
|
73
|
+
# 1. Fair Value Gaps (with mitigation tracking)
|
|
74
|
+
fvg = smc.detect_fvg(high, low, min_gap_size=0.3, compute_mitigation=True)
|
|
75
|
+
print(fvg.bullish) # boolean mask
|
|
76
|
+
print(fvg.bullish_upper) # upper boundary (Low[i])
|
|
77
|
+
print(fvg.mitigated) # has price revisited the gap?
|
|
78
|
+
|
|
79
|
+
# 2. Fractal Swings
|
|
80
|
+
swings = smc.detect_swings(high, low, window_size=2)
|
|
81
|
+
print(swings.swing_high) # True where High[i] == max(window)
|
|
82
|
+
print(swings.swing_high_price)
|
|
83
|
+
|
|
84
|
+
# 3. Market Structure — BOS & CHOCH
|
|
85
|
+
structure = smc.detect_structure(high, low, close, window_size=2)
|
|
86
|
+
print(structure.bos_bullish) # continuation breaks
|
|
87
|
+
print(structure.choch_bearish) # reversal breaks
|
|
88
|
+
print(structure.trend) # 1=bull, -1=bear, 0=neutral
|
|
89
|
+
|
|
90
|
+
# 4. Order Blocks
|
|
91
|
+
obs = smc.detect_order_blocks(open_, high, low, close, lookback=5)
|
|
92
|
+
print(obs.bullish_ob) # True at the bearish candle before a bullish impulse
|
|
93
|
+
print(obs.ob_high, obs.ob_low)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Polars API
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import polars as pl
|
|
100
|
+
import pyvsmc # registers .smc namespace
|
|
101
|
+
|
|
102
|
+
df = pl.DataFrame({
|
|
103
|
+
"open": open_,
|
|
104
|
+
"high": high,
|
|
105
|
+
"low": low,
|
|
106
|
+
"close": close,
|
|
107
|
+
"volume": [100, 120, 80, 200, 150, 180],
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
# Functional helper — adds all SMC columns at once
|
|
111
|
+
from pyvsmc.polars_ext import add_smc_columns
|
|
112
|
+
df = add_smc_columns(df, window_size=2, fvg_mitigation=True)
|
|
113
|
+
|
|
114
|
+
# Or via the .smc namespace (more granular)
|
|
115
|
+
df = pl.DataFrame({"open": open_, "high": high, "low": low, "close": close})
|
|
116
|
+
df = df.smc.add_all(window_size=2, ob_lookback=10)
|
|
117
|
+
df = df.smc.fvg(min_gap_size=0.5)
|
|
118
|
+
df = df.smc.swings(window_size=2)
|
|
119
|
+
df = df.smc.structure(window_size=2)
|
|
120
|
+
df = df.smc.order_blocks(lookback=5)
|
|
121
|
+
|
|
122
|
+
print(df)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Re-using Swings in Structure
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from pyvsmc.swings import detect_swings
|
|
129
|
+
from pyvsmc.structure import detect_structure
|
|
130
|
+
|
|
131
|
+
swings = detect_swings(high, low, window_size=3)
|
|
132
|
+
structure = detect_structure(
|
|
133
|
+
high, low, close,
|
|
134
|
+
swing_high=swings.swing_high,
|
|
135
|
+
swing_low=swings.swing_low,
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## API Reference
|
|
142
|
+
|
|
143
|
+
### `detect_fvg(high, low, min_gap_size=None, min_gap_size_pct=None, *, compute_mitigation=False)`
|
|
144
|
+
|
|
145
|
+
Detects 3-candle Fair Value Gaps.
|
|
146
|
+
|
|
147
|
+
- **Bullish FVG:** `Low[i] > High[i-2]` — gap zone `[High[i-2], Low[i]]`
|
|
148
|
+
- **Bearish FVG:** `High[i] < Low[i-2]` — gap zone `[High[i], Low[i-2]]`
|
|
149
|
+
|
|
150
|
+
Returns `FVGResult` with `bullish`, `bearish`, `bullish_upper/lower`, `bearish_upper/lower`, `gap_size`, `gap_size_pct`, `mitigated`, `mitigated_index`.
|
|
151
|
+
|
|
152
|
+
### `detect_swings(high, low, window_size=2)`
|
|
153
|
+
|
|
154
|
+
Fractal swing detection. `High[i] == max(High[i-N:i+N+1])`, `Low[i] == min(Low[i-N:i+N+1])`. Returns `SwingResult`.
|
|
155
|
+
|
|
156
|
+
### `detect_structure(high, low, close, window_size=2, *, swing_high=None, swing_low=None)`
|
|
157
|
+
|
|
158
|
+
BOS (continuation) vs CHOCH (reversal) classification with trend tracking. Returns `StructureResult` with `bos_bullish/bearish`, `choch_bullish/bearish`, `bos_level`, `choch_level`, `trend`.
|
|
159
|
+
|
|
160
|
+
### `detect_order_blocks(open_, high, low, close, *, lookback=10, ...)`
|
|
161
|
+
|
|
162
|
+
Finds last opposing candle before FVG/BOS impulses. Returns `OrderBlockResult` with `bullish_ob/bearish_ob`, `ob_high/low`, `validated_index`, `impulse_type`.
|
|
163
|
+
|
|
164
|
+
All functions also have `*_polars(df, ...)` variants and are available via `df.smc.*`.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Testing
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
pip install -e ".[dev]"
|
|
172
|
+
pytest -v
|
|
173
|
+
pytest --cov=pyvsmc --cov-report=term-missing
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Run type checks and lint:
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
mypy src/pyvsmc
|
|
180
|
+
ruff check src/pyvsmc tests
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Project Structure
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
pyvsmc/
|
|
189
|
+
├── pyproject.toml
|
|
190
|
+
├── README.md
|
|
191
|
+
├── src/pyvsmc/
|
|
192
|
+
│ ├── __init__.py
|
|
193
|
+
│ ├── py.typed
|
|
194
|
+
│ ├── fvg.py
|
|
195
|
+
│ ├── swings.py
|
|
196
|
+
│ ├── structure.py
|
|
197
|
+
│ ├── order_blocks.py
|
|
198
|
+
│ └── polars_ext.py
|
|
199
|
+
└── tests/
|
|
200
|
+
├── test_fvg.py
|
|
201
|
+
├── test_swings.py
|
|
202
|
+
├── test_structure.py
|
|
203
|
+
├── test_order_blocks.py
|
|
204
|
+
└── test_polars_ext.py
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Performance Notes
|
|
210
|
+
|
|
211
|
+
- All indicators use `numpy.lib.stride_tricks.sliding_window_view`, `np.maximum.accumulate`, broadcasting, and chunked evaluation — **no Python loops over bars**.
|
|
212
|
+
- The single exception is the BOS/CHOCH trend tracker, which requires sequential state. It is JIT-compiled with `numba` when available and falls back to a single O(n) scan otherwise.
|
|
213
|
+
- Benchmark: ~100k candles — FVG < 10ms, swings < 20ms, structure < 30ms (CPython 3.11, NumPy 1.26).
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## Financial & Legal Disclaimer
|
|
218
|
+
|
|
219
|
+
**IMPORTANT — PLEASE READ CAREFULLY**
|
|
220
|
+
|
|
221
|
+
`pyvsmc` is an **open-source analytics and research library**. It is provided solely for **educational, informational, and research purposes**.
|
|
222
|
+
|
|
223
|
+
- **Not Financial Advice.** Nothing in this library, its documentation, examples, or outputs constitutes financial, investment, trading, or other professional advice. No recommendation to buy, sell, or hold any financial instrument is made or implied.
|
|
224
|
+
- **No Warranty of Accuracy or Fitness.** Market structure and Smart Money Concepts are *interpretive frameworks*; their definitions vary across practitioners. The library implements one set of rules that may not match your trading methodology. Outputs may be incorrect, incomplete, or inappropriate for your use case.
|
|
225
|
+
- **Use at Your Own Risk.** Trading and investing involve substantial risk of loss, including loss of principal. Past simulated or historical performance is not indicative of future results. You are solely responsible for your own trading decisions, risk management, and compliance with applicable laws and regulations.
|
|
226
|
+
- **No Liability.** To the fullest extent permitted by law, the authors, contributors, and distributors of `pyvsmc` disclaim all liability for any loss, damage, cost, or expense arising directly or indirectly from use of this software.
|
|
227
|
+
- **Do Your Own Research (DYOR).** Always validate any signal or analysis with independent research, additional data sources, and, where appropriate, advice from a qualified professional.
|
|
228
|
+
|
|
229
|
+
By using this software you acknowledge that you have read, understood, and agree to this disclaimer.
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## License
|
|
234
|
+
|
|
235
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
236
|
+
|
|
237
|
+
Copyright (c) 2026 pyvsmc contributors.
|
|
238
|
+
|
|
239
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
240
|
+
|
|
241
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
242
|
+
|
|
243
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Contributing
|
|
248
|
+
|
|
249
|
+
Issues and pull requests are welcome. Please run `ruff`, `mypy`, and `pytest` before submitting.
|
|
250
|
+
|
|
251
|
+
## Acknowledgements
|
|
252
|
+
|
|
253
|
+
Built with [NumPy](https://numpy.org) and [Polars](https://pola.rs). SMC concepts as described by the broader ICT / Smart Money community.
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pyvsmc"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Ultra-fast, fully vectorized market structure and Smart Money Concepts (SMC) technical analysis using NumPy and Polars"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "pyvsmc contributors", email = "hello@pyvsmc.dev" },
|
|
14
|
+
]
|
|
15
|
+
maintainers = [
|
|
16
|
+
{ name = "pyvsmc maintainers", email = "hello@pyvsmc.dev" },
|
|
17
|
+
]
|
|
18
|
+
keywords = ["trading", "technical-analysis", "smart-money-concepts", "smc", "market-structure", "numpy", "polars", "quant"]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
22
|
+
"Intended Audience :: Developers",
|
|
23
|
+
"Intended Audience :: Science/Research",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3.10",
|
|
28
|
+
"Programming Language :: Python :: 3.11",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
"Programming Language :: Python :: 3.13",
|
|
31
|
+
"Topic :: Scientific/Engineering",
|
|
32
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
33
|
+
"Typing :: Typed",
|
|
34
|
+
]
|
|
35
|
+
dependencies = [
|
|
36
|
+
"numpy>=1.24.0",
|
|
37
|
+
"polars>=0.20.0",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.optional-dependencies]
|
|
41
|
+
dev = [
|
|
42
|
+
"pytest>=7.0",
|
|
43
|
+
"pytest-cov>=4.0",
|
|
44
|
+
"ruff>=0.1.0",
|
|
45
|
+
"mypy>=1.0",
|
|
46
|
+
"numpy>=1.24.0",
|
|
47
|
+
"polars>=0.20.0",
|
|
48
|
+
]
|
|
49
|
+
test = [
|
|
50
|
+
"pytest>=7.0",
|
|
51
|
+
"pytest-cov>=4.0",
|
|
52
|
+
"numpy>=1.24.0",
|
|
53
|
+
"polars>=0.20.0",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[project.urls]
|
|
57
|
+
Homepage = "https://github.com/Khaymat/pyvsmc"
|
|
58
|
+
Repository = "https://github.com/Khaymat/pyvsmc"
|
|
59
|
+
Documentation = "https://github.com/Khaymat/pyvsmc#readme"
|
|
60
|
+
Issues = "https://github.com/Khaymat/pyvsmc/issues"
|
|
61
|
+
Changelog = "https://github.com/Khaymat/pyvsmc/blob/main/CHANGELOG.md"
|
|
62
|
+
|
|
63
|
+
[tool.setuptools.packages.find]
|
|
64
|
+
where = ["src"]
|
|
65
|
+
|
|
66
|
+
[tool.setuptools.package-data]
|
|
67
|
+
pyvsmc = ["py.typed"]
|
|
68
|
+
|
|
69
|
+
[tool.pytest.ini_options]
|
|
70
|
+
testpaths = ["tests"]
|
|
71
|
+
python_files = ["test_*.py"]
|
|
72
|
+
python_classes = ["Test*"]
|
|
73
|
+
python_functions = ["test_*"]
|
|
74
|
+
addopts = "-ra -q --strict-markers --strict-config"
|
|
75
|
+
|
|
76
|
+
[tool.ruff]
|
|
77
|
+
target-version = "py310"
|
|
78
|
+
line-length = 100
|
|
79
|
+
extend-exclude = ["__pycache__"]
|
|
80
|
+
|
|
81
|
+
[tool.ruff.lint]
|
|
82
|
+
select = ["E", "F", "W", "I", "B", "C4", "UP", "SIM", "TCH"]
|
|
83
|
+
ignore = []
|
|
84
|
+
|
|
85
|
+
[tool.ruff.lint.pydocstyle]
|
|
86
|
+
convention = "google"
|
|
87
|
+
|
|
88
|
+
[tool.mypy]
|
|
89
|
+
python_version = "3.10"
|
|
90
|
+
warn_return_any = true
|
|
91
|
+
warn_unused_configs = true
|
|
92
|
+
disallow_untyped_defs = true
|
|
93
|
+
disallow_incomplete_defs = true
|
|
94
|
+
check_untyped_defs = true
|
|
95
|
+
disallow_untyped_decorators = true
|
|
96
|
+
no_implicit_optional = true
|
|
97
|
+
warn_redundant_casts = true
|
|
98
|
+
warn_unused_ignores = true
|
|
99
|
+
warn_no_return = true
|
|
100
|
+
warn_unreachable = true
|
|
101
|
+
strict_equality = true
|
|
102
|
+
show_error_codes = true
|
|
103
|
+
exclude = ["tests"]
|
|
104
|
+
|
|
105
|
+
[[tool.mypy.overrides]]
|
|
106
|
+
module = "polars.*"
|
|
107
|
+
ignore_missing_imports = true
|
|
108
|
+
|
|
109
|
+
[tool.coverage.run]
|
|
110
|
+
source = ["pyvsmc"]
|
|
111
|
+
branch = true
|
|
112
|
+
|
|
113
|
+
[tool.coverage.report]
|
|
114
|
+
show_missing = true
|
|
115
|
+
skip_covered = false
|
pyvsmc-0.1.0/setup.cfg
ADDED