lobflow 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.
- lobflow-0.1.0/LICENSE +21 -0
- lobflow-0.1.0/PKG-INFO +247 -0
- lobflow-0.1.0/README.md +214 -0
- lobflow-0.1.0/lobflow/__init__.py +43 -0
- lobflow-0.1.0/lobflow/book.py +246 -0
- lobflow-0.1.0/lobflow/detector.py +393 -0
- lobflow-0.1.0/lobflow/exceptions.py +21 -0
- lobflow-0.1.0/lobflow/metrics.py +395 -0
- lobflow-0.1.0/lobflow/py.typed +0 -0
- lobflow-0.1.0/lobflow/simulator.py +321 -0
- lobflow-0.1.0/lobflow/viz.py +158 -0
- lobflow-0.1.0/lobflow.egg-info/PKG-INFO +247 -0
- lobflow-0.1.0/lobflow.egg-info/SOURCES.txt +20 -0
- lobflow-0.1.0/lobflow.egg-info/dependency_links.txt +1 -0
- lobflow-0.1.0/lobflow.egg-info/requires.txt +10 -0
- lobflow-0.1.0/lobflow.egg-info/top_level.txt +1 -0
- lobflow-0.1.0/pyproject.toml +74 -0
- lobflow-0.1.0/setup.cfg +4 -0
- lobflow-0.1.0/tests/test_book.py +176 -0
- lobflow-0.1.0/tests/test_detector.py +203 -0
- lobflow-0.1.0/tests/test_metrics.py +236 -0
- lobflow-0.1.0/tests/test_simulator.py +217 -0
lobflow-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Elsinore Claw 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.
|
lobflow-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lobflow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Real-time order book microstructure analysis with manipulation detection
|
|
5
|
+
Author-email: ElsinoreClaw <dev@elsinoreclaw.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ElsinoreClaw/lobflow
|
|
8
|
+
Project-URL: Documentation, https://github.com/ElsinoreClaw/lobflow#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/ElsinoreClaw/lobflow
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/ElsinoreClaw/lobflow/issues
|
|
11
|
+
Keywords: quantitative-finance,market-microstructure,order-book,manipulation-detection,trading,high-frequency
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: numpy>=1.24.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
28
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
29
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
31
|
+
Provides-Extra: viz
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# lobflow
|
|
35
|
+
|
|
36
|
+
**Real-time order book microstructure analysis with manipulation detection**
|
|
37
|
+
|
|
38
|
+
[](https://pypi.org/project/lobflow/)
|
|
39
|
+
[](https://pypi.org/project/lobflow/)
|
|
40
|
+
[](https://opensource.org/licenses/MIT)
|
|
41
|
+
|
|
42
|
+
**lobflow** is a Python library for ingesting Level 2 order book data, computing market microstructure metrics in real time, and detecting statistically anomalous patterns that indicate market manipulation.
|
|
43
|
+
|
|
44
|
+
## The Problem
|
|
45
|
+
|
|
46
|
+
Retail traders and algorithmic researchers have no accessible Python tool that can:
|
|
47
|
+
1. Ingest raw Level 2 order book data (bids/asks at each price level)
|
|
48
|
+
2. Compute microstructure metrics in real time
|
|
49
|
+
3. Detect statistically anomalous patterns indicating market manipulation
|
|
50
|
+
|
|
51
|
+
lobflow solves this end-to-end, open-source, with clean Python APIs.
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install lobflow
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Or from source:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
git clone https://github.com/ElsinoreClaw/lobflow.git
|
|
63
|
+
cd lobflow
|
|
64
|
+
pip install -e .
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Quick Start
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from lobflow import OrderBook, MicrostructureMetrics, ManipulationDetector
|
|
71
|
+
|
|
72
|
+
# Create an order book
|
|
73
|
+
book = OrderBook("BTC/USD")
|
|
74
|
+
|
|
75
|
+
# Update with Level 2 data
|
|
76
|
+
bids = [(49999.0, 1.5), (49998.0, 2.0), (49997.0, 0.8)]
|
|
77
|
+
asks = [(50001.0, 1.0), (50002.0, 2.5), (50003.0, 1.8)]
|
|
78
|
+
book.update(bids, asks)
|
|
79
|
+
|
|
80
|
+
# Compute microstructure metrics
|
|
81
|
+
metrics = MicrostructureMetrics(book)
|
|
82
|
+
print(metrics.summary())
|
|
83
|
+
# {
|
|
84
|
+
# 'mid_price': 50000.0,
|
|
85
|
+
# 'spread': 2.0,
|
|
86
|
+
# 'order_book_imbalance': 0.1304,
|
|
87
|
+
# 'weighted_mid_price': 50000.2,
|
|
88
|
+
# 'kyle_lambda': 0.0,
|
|
89
|
+
# 'amihud_illiquidity': 0.0,
|
|
90
|
+
# 'roll_spread': 0.0,
|
|
91
|
+
# 'vwap_deviation': 0.0,
|
|
92
|
+
# 'queue_imbalance': 0.2,
|
|
93
|
+
# 'depth_weighted_spread': 2.0,
|
|
94
|
+
# }
|
|
95
|
+
|
|
96
|
+
# Detect manipulation
|
|
97
|
+
detector = ManipulationDetector(book)
|
|
98
|
+
results = detector.scan()
|
|
99
|
+
for result in results:
|
|
100
|
+
print(f"{result.pattern}: confidence={result.confidence:.2%}")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Features
|
|
104
|
+
|
|
105
|
+
### Order Book Management
|
|
106
|
+
|
|
107
|
+
- **OrderBook** class maintains Level 2 state from streams of bid/ask updates
|
|
108
|
+
- Automatic sorting and depth management
|
|
109
|
+
- Historical snapshot tracking
|
|
110
|
+
- Query mid price, spread, imbalance, depth profile
|
|
111
|
+
|
|
112
|
+
### Microstructure Metrics
|
|
113
|
+
|
|
114
|
+
All metrics are mathematically correct with references to academic literature:
|
|
115
|
+
|
|
116
|
+
| Metric | Description |
|
|
117
|
+
|--------|-------------|
|
|
118
|
+
| **Order Book Imbalance** | Ratio of bid vs ask volume at top N levels |
|
|
119
|
+
| **Weighted Mid Price** | Mid price weighted by volume at best levels |
|
|
120
|
+
| **Kyle's Lambda** | Price impact coefficient from order flow regression |
|
|
121
|
+
| **Amihud Illiquidity** | \|return\| / volume measure |
|
|
122
|
+
| **Roll Spread** | Effective spread from price autocorrelation |
|
|
123
|
+
| **VWAP Deviation** | Distance from rolling volume-weighted average price |
|
|
124
|
+
| **Queue Imbalance** | Volume ratio at best bid vs best ask |
|
|
125
|
+
| **Depth-Weighted Spread** | Spread adjusted for available liquidity |
|
|
126
|
+
|
|
127
|
+
### Manipulation Detection
|
|
128
|
+
|
|
129
|
+
Detects three common manipulation patterns with confidence scores:
|
|
130
|
+
|
|
131
|
+
1. **Spoofing** — Large orders placed then cancelled before execution
|
|
132
|
+
2. **Layering** — Multiple stacked orders creating false depth impression
|
|
133
|
+
3. **Quote Stuffing** — Extreme update rates overwhelming competitors
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
detector = ManipulationDetector(book, sensitivity=1.0)
|
|
137
|
+
result = detector.check_spoofing()
|
|
138
|
+
if result.confidence > 0.5:
|
|
139
|
+
print(f"Spoofing detected! Evidence: {result.evidence}")
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Synthetic Data Generation
|
|
143
|
+
|
|
144
|
+
Generate realistic order book data for testing:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from lobflow import OrderBookSimulator
|
|
148
|
+
|
|
149
|
+
sim = OrderBookSimulator(base_price=50000.0, seed=42)
|
|
150
|
+
sim.inject_spoofing("bid", 49900.0, 100.0, duration=5)
|
|
151
|
+
history = sim.run(n_ticks=1000)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Visualization
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from lobflow import print_order_book, print_detection_results
|
|
158
|
+
|
|
159
|
+
print_order_book(book, depth=10, show_metrics=True)
|
|
160
|
+
print_detection_results(detector.scan())
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Examples
|
|
164
|
+
|
|
165
|
+
See the `examples/` directory:
|
|
166
|
+
|
|
167
|
+
- **basic_usage.py** — Core OrderBook and metrics usage
|
|
168
|
+
- **manipulation_demo.py** — Full demonstration of all detection patterns
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
python examples/basic_usage.py
|
|
172
|
+
python examples/manipulation_demo.py
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## API Reference
|
|
176
|
+
|
|
177
|
+
### OrderBook
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
class OrderBook:
|
|
181
|
+
def __init__(self, symbol: str, max_depth: int = 20)
|
|
182
|
+
def update(self, bids, asks, timestamp: float = None)
|
|
183
|
+
def snapshot(self) -> dict
|
|
184
|
+
def mid_price(self) -> float
|
|
185
|
+
def spread(self) -> float
|
|
186
|
+
def imbalance(self, depth: int = 5) -> float
|
|
187
|
+
def depth_profile(self) -> dict
|
|
188
|
+
def history(self, n: int = 100) -> list[dict]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### MicrostructureMetrics
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
class MicrostructureMetrics:
|
|
195
|
+
def __init__(self, book: OrderBook)
|
|
196
|
+
def obi(self, depth: int = 5) -> float
|
|
197
|
+
def weighted_mid(self) -> float
|
|
198
|
+
def kyle_lambda(self, window: int = 50) -> float
|
|
199
|
+
def amihud(self, window: int = 20) -> float
|
|
200
|
+
def roll_spread(self, window: int = 30) -> float
|
|
201
|
+
def vwap_deviation(self, window: int = 100) -> float
|
|
202
|
+
def queue_imbalance(self) -> float
|
|
203
|
+
def depth_weighted_spread(self) -> float
|
|
204
|
+
def summary(self) -> dict
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### ManipulationDetector
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
class ManipulationDetector:
|
|
211
|
+
def __init__(self, book: OrderBook, sensitivity: float = 1.0)
|
|
212
|
+
def check_spoofing(self) -> DetectionResult
|
|
213
|
+
def check_layering(self) -> DetectionResult
|
|
214
|
+
def check_quote_stuffing(self) -> DetectionResult
|
|
215
|
+
def scan(self) -> list[DetectionResult]
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## References
|
|
219
|
+
|
|
220
|
+
The implementation is based on established market microstructure literature:
|
|
221
|
+
|
|
222
|
+
- Harris, L. (2003). *Trading and Exchanges: Market Microstructure for Practitioners*. Oxford University Press.
|
|
223
|
+
- Kyle, A. S. (1985). Continuous Auctions and Insider Trading. *Econometrica*, 53(6), 1315-1335.
|
|
224
|
+
- Amihud, Y. (2002). Illiquidity and Stock Returns. *Journal of Financial Markets*, 5(1), 31-56.
|
|
225
|
+
- Roll, R. (1984). A Simple Implicit Measure of the Effective Bid-Ask Spread. *Journal of Finance*, 39(4), 1127-1139.
|
|
226
|
+
- Cartea, Á., Jaimungal, S., & Penalva, J. (2015). *Algorithmic and High-Frequency Trading*. Cambridge University Press.
|
|
227
|
+
- Comerton-Forde, C., & Putniņš, T. J. (2014). Stock Price Manipulation. *Review of Finance*.
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
232
|
+
|
|
233
|
+
## Authorship
|
|
234
|
+
|
|
235
|
+
lobflow is a project of [Elsinore Claw](https://github.com/ElsinoreClaw), developed using an AI-assisted workflow. See [CONTRIBUTORS.md](CONTRIBUTORS.md) for details.
|
|
236
|
+
|
|
237
|
+
## Contributing
|
|
238
|
+
|
|
239
|
+
Contributions welcome — open an issue or submit a pull request at
|
|
240
|
+
https://github.com/ElsinoreClaw/lobflow
|
|
241
|
+
|
|
242
|
+
## Testing
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
pip install pytest
|
|
246
|
+
python -m pytest tests/ -v
|
|
247
|
+
```
|
lobflow-0.1.0/README.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# lobflow
|
|
2
|
+
|
|
3
|
+
**Real-time order book microstructure analysis with manipulation detection**
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/lobflow/)
|
|
6
|
+
[](https://pypi.org/project/lobflow/)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
**lobflow** is a Python library for ingesting Level 2 order book data, computing market microstructure metrics in real time, and detecting statistically anomalous patterns that indicate market manipulation.
|
|
10
|
+
|
|
11
|
+
## The Problem
|
|
12
|
+
|
|
13
|
+
Retail traders and algorithmic researchers have no accessible Python tool that can:
|
|
14
|
+
1. Ingest raw Level 2 order book data (bids/asks at each price level)
|
|
15
|
+
2. Compute microstructure metrics in real time
|
|
16
|
+
3. Detect statistically anomalous patterns indicating market manipulation
|
|
17
|
+
|
|
18
|
+
lobflow solves this end-to-end, open-source, with clean Python APIs.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install lobflow
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or from source:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
git clone https://github.com/ElsinoreClaw/lobflow.git
|
|
30
|
+
cd lobflow
|
|
31
|
+
pip install -e .
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from lobflow import OrderBook, MicrostructureMetrics, ManipulationDetector
|
|
38
|
+
|
|
39
|
+
# Create an order book
|
|
40
|
+
book = OrderBook("BTC/USD")
|
|
41
|
+
|
|
42
|
+
# Update with Level 2 data
|
|
43
|
+
bids = [(49999.0, 1.5), (49998.0, 2.0), (49997.0, 0.8)]
|
|
44
|
+
asks = [(50001.0, 1.0), (50002.0, 2.5), (50003.0, 1.8)]
|
|
45
|
+
book.update(bids, asks)
|
|
46
|
+
|
|
47
|
+
# Compute microstructure metrics
|
|
48
|
+
metrics = MicrostructureMetrics(book)
|
|
49
|
+
print(metrics.summary())
|
|
50
|
+
# {
|
|
51
|
+
# 'mid_price': 50000.0,
|
|
52
|
+
# 'spread': 2.0,
|
|
53
|
+
# 'order_book_imbalance': 0.1304,
|
|
54
|
+
# 'weighted_mid_price': 50000.2,
|
|
55
|
+
# 'kyle_lambda': 0.0,
|
|
56
|
+
# 'amihud_illiquidity': 0.0,
|
|
57
|
+
# 'roll_spread': 0.0,
|
|
58
|
+
# 'vwap_deviation': 0.0,
|
|
59
|
+
# 'queue_imbalance': 0.2,
|
|
60
|
+
# 'depth_weighted_spread': 2.0,
|
|
61
|
+
# }
|
|
62
|
+
|
|
63
|
+
# Detect manipulation
|
|
64
|
+
detector = ManipulationDetector(book)
|
|
65
|
+
results = detector.scan()
|
|
66
|
+
for result in results:
|
|
67
|
+
print(f"{result.pattern}: confidence={result.confidence:.2%}")
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Features
|
|
71
|
+
|
|
72
|
+
### Order Book Management
|
|
73
|
+
|
|
74
|
+
- **OrderBook** class maintains Level 2 state from streams of bid/ask updates
|
|
75
|
+
- Automatic sorting and depth management
|
|
76
|
+
- Historical snapshot tracking
|
|
77
|
+
- Query mid price, spread, imbalance, depth profile
|
|
78
|
+
|
|
79
|
+
### Microstructure Metrics
|
|
80
|
+
|
|
81
|
+
All metrics are mathematically correct with references to academic literature:
|
|
82
|
+
|
|
83
|
+
| Metric | Description |
|
|
84
|
+
|--------|-------------|
|
|
85
|
+
| **Order Book Imbalance** | Ratio of bid vs ask volume at top N levels |
|
|
86
|
+
| **Weighted Mid Price** | Mid price weighted by volume at best levels |
|
|
87
|
+
| **Kyle's Lambda** | Price impact coefficient from order flow regression |
|
|
88
|
+
| **Amihud Illiquidity** | \|return\| / volume measure |
|
|
89
|
+
| **Roll Spread** | Effective spread from price autocorrelation |
|
|
90
|
+
| **VWAP Deviation** | Distance from rolling volume-weighted average price |
|
|
91
|
+
| **Queue Imbalance** | Volume ratio at best bid vs best ask |
|
|
92
|
+
| **Depth-Weighted Spread** | Spread adjusted for available liquidity |
|
|
93
|
+
|
|
94
|
+
### Manipulation Detection
|
|
95
|
+
|
|
96
|
+
Detects three common manipulation patterns with confidence scores:
|
|
97
|
+
|
|
98
|
+
1. **Spoofing** — Large orders placed then cancelled before execution
|
|
99
|
+
2. **Layering** — Multiple stacked orders creating false depth impression
|
|
100
|
+
3. **Quote Stuffing** — Extreme update rates overwhelming competitors
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
detector = ManipulationDetector(book, sensitivity=1.0)
|
|
104
|
+
result = detector.check_spoofing()
|
|
105
|
+
if result.confidence > 0.5:
|
|
106
|
+
print(f"Spoofing detected! Evidence: {result.evidence}")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Synthetic Data Generation
|
|
110
|
+
|
|
111
|
+
Generate realistic order book data for testing:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from lobflow import OrderBookSimulator
|
|
115
|
+
|
|
116
|
+
sim = OrderBookSimulator(base_price=50000.0, seed=42)
|
|
117
|
+
sim.inject_spoofing("bid", 49900.0, 100.0, duration=5)
|
|
118
|
+
history = sim.run(n_ticks=1000)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Visualization
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from lobflow import print_order_book, print_detection_results
|
|
125
|
+
|
|
126
|
+
print_order_book(book, depth=10, show_metrics=True)
|
|
127
|
+
print_detection_results(detector.scan())
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Examples
|
|
131
|
+
|
|
132
|
+
See the `examples/` directory:
|
|
133
|
+
|
|
134
|
+
- **basic_usage.py** — Core OrderBook and metrics usage
|
|
135
|
+
- **manipulation_demo.py** — Full demonstration of all detection patterns
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
python examples/basic_usage.py
|
|
139
|
+
python examples/manipulation_demo.py
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## API Reference
|
|
143
|
+
|
|
144
|
+
### OrderBook
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
class OrderBook:
|
|
148
|
+
def __init__(self, symbol: str, max_depth: int = 20)
|
|
149
|
+
def update(self, bids, asks, timestamp: float = None)
|
|
150
|
+
def snapshot(self) -> dict
|
|
151
|
+
def mid_price(self) -> float
|
|
152
|
+
def spread(self) -> float
|
|
153
|
+
def imbalance(self, depth: int = 5) -> float
|
|
154
|
+
def depth_profile(self) -> dict
|
|
155
|
+
def history(self, n: int = 100) -> list[dict]
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### MicrostructureMetrics
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
class MicrostructureMetrics:
|
|
162
|
+
def __init__(self, book: OrderBook)
|
|
163
|
+
def obi(self, depth: int = 5) -> float
|
|
164
|
+
def weighted_mid(self) -> float
|
|
165
|
+
def kyle_lambda(self, window: int = 50) -> float
|
|
166
|
+
def amihud(self, window: int = 20) -> float
|
|
167
|
+
def roll_spread(self, window: int = 30) -> float
|
|
168
|
+
def vwap_deviation(self, window: int = 100) -> float
|
|
169
|
+
def queue_imbalance(self) -> float
|
|
170
|
+
def depth_weighted_spread(self) -> float
|
|
171
|
+
def summary(self) -> dict
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### ManipulationDetector
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
class ManipulationDetector:
|
|
178
|
+
def __init__(self, book: OrderBook, sensitivity: float = 1.0)
|
|
179
|
+
def check_spoofing(self) -> DetectionResult
|
|
180
|
+
def check_layering(self) -> DetectionResult
|
|
181
|
+
def check_quote_stuffing(self) -> DetectionResult
|
|
182
|
+
def scan(self) -> list[DetectionResult]
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## References
|
|
186
|
+
|
|
187
|
+
The implementation is based on established market microstructure literature:
|
|
188
|
+
|
|
189
|
+
- Harris, L. (2003). *Trading and Exchanges: Market Microstructure for Practitioners*. Oxford University Press.
|
|
190
|
+
- Kyle, A. S. (1985). Continuous Auctions and Insider Trading. *Econometrica*, 53(6), 1315-1335.
|
|
191
|
+
- Amihud, Y. (2002). Illiquidity and Stock Returns. *Journal of Financial Markets*, 5(1), 31-56.
|
|
192
|
+
- Roll, R. (1984). A Simple Implicit Measure of the Effective Bid-Ask Spread. *Journal of Finance*, 39(4), 1127-1139.
|
|
193
|
+
- Cartea, Á., Jaimungal, S., & Penalva, J. (2015). *Algorithmic and High-Frequency Trading*. Cambridge University Press.
|
|
194
|
+
- Comerton-Forde, C., & Putniņš, T. J. (2014). Stock Price Manipulation. *Review of Finance*.
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
199
|
+
|
|
200
|
+
## Authorship
|
|
201
|
+
|
|
202
|
+
lobflow is a project of [Elsinore Claw](https://github.com/ElsinoreClaw), developed using an AI-assisted workflow. See [CONTRIBUTORS.md](CONTRIBUTORS.md) for details.
|
|
203
|
+
|
|
204
|
+
## Contributing
|
|
205
|
+
|
|
206
|
+
Contributions welcome — open an issue or submit a pull request at
|
|
207
|
+
https://github.com/ElsinoreClaw/lobflow
|
|
208
|
+
|
|
209
|
+
## Testing
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
pip install pytest
|
|
213
|
+
python -m pytest tests/ -v
|
|
214
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""
|
|
2
|
+
lobflow - Real-time order book microstructure analysis with manipulation detection.
|
|
3
|
+
|
|
4
|
+
A Python library for ingesting Level 2 order book data, computing
|
|
5
|
+
microstructure metrics, and detecting market manipulation patterns.
|
|
6
|
+
|
|
7
|
+
Example:
|
|
8
|
+
>>> from lobflow import OrderBook, MicrostructureMetrics, ManipulationDetector
|
|
9
|
+
>>> book = OrderBook("BTC/USD")
|
|
10
|
+
>>> book.update(bids=[(49999, 1.5), (49998, 2.0)], asks=[(50001, 1.0), (50002, 2.5)])
|
|
11
|
+
>>> metrics = MicrostructureMetrics(book)
|
|
12
|
+
>>> print(metrics.summary())
|
|
13
|
+
>>> detector = ManipulationDetector(book)
|
|
14
|
+
>>> results = detector.scan()
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from .book import OrderBook
|
|
18
|
+
from .detector import DetectionResult, ManipulationDetector
|
|
19
|
+
from .exceptions import (
|
|
20
|
+
InsufficientDataError,
|
|
21
|
+
InvalidOrderBookError,
|
|
22
|
+
ManipulationDetectionError,
|
|
23
|
+
OrderflowError,
|
|
24
|
+
)
|
|
25
|
+
from .metrics import MicrostructureMetrics
|
|
26
|
+
from .simulator import OrderBookSimulator
|
|
27
|
+
from .viz import print_detection_results, print_metrics, print_order_book
|
|
28
|
+
|
|
29
|
+
__version__ = "0.1.0"
|
|
30
|
+
__all__ = [
|
|
31
|
+
"OrderBook",
|
|
32
|
+
"MicrostructureMetrics",
|
|
33
|
+
"ManipulationDetector",
|
|
34
|
+
"DetectionResult",
|
|
35
|
+
"OrderBookSimulator",
|
|
36
|
+
"OrderflowError",
|
|
37
|
+
"InvalidOrderBookError",
|
|
38
|
+
"InsufficientDataError",
|
|
39
|
+
"ManipulationDetectionError",
|
|
40
|
+
"print_order_book",
|
|
41
|
+
"print_metrics",
|
|
42
|
+
"print_detection_results",
|
|
43
|
+
]
|