crypto-lob-stream 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.
- crypto_lob_stream-0.1.0/LICENSE +21 -0
- crypto_lob_stream-0.1.0/PKG-INFO +186 -0
- crypto_lob_stream-0.1.0/README.md +154 -0
- crypto_lob_stream-0.1.0/crypto_lob_stream.egg-info/PKG-INFO +186 -0
- crypto_lob_stream-0.1.0/crypto_lob_stream.egg-info/SOURCES.txt +9 -0
- crypto_lob_stream-0.1.0/crypto_lob_stream.egg-info/dependency_links.txt +1 -0
- crypto_lob_stream-0.1.0/crypto_lob_stream.egg-info/entry_points.txt +2 -0
- crypto_lob_stream-0.1.0/crypto_lob_stream.egg-info/requires.txt +10 -0
- crypto_lob_stream-0.1.0/crypto_lob_stream.egg-info/top_level.txt +1 -0
- crypto_lob_stream-0.1.0/pyproject.toml +54 -0
- crypto_lob_stream-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Goodness Chibueze Kalu
|
|
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.
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crypto-lob-stream
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stream Binance Level 2 order book and trade data to local disk or GCS.
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/Goodie-Goody/crypto_lob_stream_pypi
|
|
7
|
+
Project-URL: Repository, https://github.com/Goodie-Goody/crypto_lob_stream_pypi
|
|
8
|
+
Project-URL: Issues, https://github.com/Goodie-Goody/crypto_lob_stream_pypi/issues
|
|
9
|
+
Keywords: cryptocurrency,order-book,LOB,binance,market-data,streaming
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: websockets>=12.0
|
|
24
|
+
Requires-Dist: pyarrow>=14.0
|
|
25
|
+
Requires-Dist: aiohttp>=3.9
|
|
26
|
+
Requires-Dist: python-dotenv>=1.0
|
|
27
|
+
Provides-Extra: gcs
|
|
28
|
+
Requires-Dist: google-cloud-storage>=2.0; extra == "gcs"
|
|
29
|
+
Provides-Extra: all
|
|
30
|
+
Requires-Dist: google-cloud-storage>=2.0; extra == "all"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# crypto-lob-stream
|
|
34
|
+
|
|
35
|
+
Stream Binance Level 2 order book and trade data to local disk or Google Cloud Storage. Built for researchers and practitioners who need continuous, reconstructable LOB data and find it difficult to access this kind of dataset openly.
|
|
36
|
+
|
|
37
|
+
Publishes quarterly snapshots to [Hugging Face](https://huggingface.co/) for free public use.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# Local output only
|
|
45
|
+
pip install crypto-lob-stream
|
|
46
|
+
|
|
47
|
+
# With GCS support
|
|
48
|
+
pip install crypto-lob-stream[gcs]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Requires Python 3.10+.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Quickstart
|
|
56
|
+
|
|
57
|
+
### Python API
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from crypto_lob_stream import LOBStreamer
|
|
61
|
+
|
|
62
|
+
# Stream to local disk
|
|
63
|
+
streamer = LOBStreamer(
|
|
64
|
+
assets=["BTCUSDT", "ETHUSDT", "SOLUSDT"],
|
|
65
|
+
output="local",
|
|
66
|
+
output_dir="./lob_data",
|
|
67
|
+
)
|
|
68
|
+
streamer.run()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
# Stream to GCS
|
|
73
|
+
streamer = LOBStreamer(
|
|
74
|
+
assets=["BTCUSDT"],
|
|
75
|
+
output="gcs",
|
|
76
|
+
bucket="my-gcs-bucket",
|
|
77
|
+
)
|
|
78
|
+
streamer.run()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
# With a real-time callback on every trade
|
|
83
|
+
def on_trade(record):
|
|
84
|
+
print(f"Trade: {record['asset']} {record['price']} x {record['quantity']}")
|
|
85
|
+
|
|
86
|
+
streamer = LOBStreamer(
|
|
87
|
+
assets=["BTCUSDT"],
|
|
88
|
+
output="local",
|
|
89
|
+
on_trade=on_trade,
|
|
90
|
+
)
|
|
91
|
+
streamer.run()
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### CLI
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
# Local
|
|
98
|
+
crypto-lob-stream --assets BTCUSDT,ETHUSDT --output local --output-dir ./data
|
|
99
|
+
|
|
100
|
+
# GCS
|
|
101
|
+
crypto-lob-stream --assets BTCUSDT,ETHUSDT,SOLUSDT --output gcs --bucket my-bucket
|
|
102
|
+
|
|
103
|
+
# Custom flush interval
|
|
104
|
+
crypto-lob-stream --assets BTCUSDT --output local --flush-interval 60
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Output structure
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
{output_dir}/
|
|
113
|
+
trades/{asset}/YYYY-MM-DD-HH.parquet
|
|
114
|
+
depth/{asset}/YYYY-MM-DD-HH.parquet
|
|
115
|
+
snapshots/{asset}/YYYY-MM-DD-HHmmss.parquet
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
All files are Snappy-compressed Parquet.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Schemas
|
|
123
|
+
|
|
124
|
+
### trades
|
|
125
|
+
| Field | Type |
|
|
126
|
+
|---|---|
|
|
127
|
+
| timestamp_ms | int64 |
|
|
128
|
+
| asset | string |
|
|
129
|
+
| trade_id | int64 |
|
|
130
|
+
| price | float64 |
|
|
131
|
+
| quantity | float64 |
|
|
132
|
+
| buyer_maker | bool |
|
|
133
|
+
|
|
134
|
+
### depth (diff events)
|
|
135
|
+
| Field | Type | Notes |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| timestamp_ms | int64 | |
|
|
138
|
+
| asset | string | |
|
|
139
|
+
| side | string (bid/ask) | |
|
|
140
|
+
| price | float64 | |
|
|
141
|
+
| quantity | float64 | 0.0 = level removed |
|
|
142
|
+
| first_update_id | int64 | `U` field from Binance |
|
|
143
|
+
| last_update_id | int64 | `u` field -- sequence number |
|
|
144
|
+
|
|
145
|
+
### snapshots
|
|
146
|
+
| Field | Type | Notes |
|
|
147
|
+
|---|---|---|
|
|
148
|
+
| timestamp_ms | int64 | |
|
|
149
|
+
| asset | string | |
|
|
150
|
+
| side | string (bid/ask) | |
|
|
151
|
+
| price | float64 | |
|
|
152
|
+
| quantity | float64 | |
|
|
153
|
+
| last_update_id | int64 | Anchor point for diff replay |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## LOB reconstruction
|
|
158
|
+
|
|
159
|
+
A REST snapshot is fetched and stored for every WebSocket connection (including reconnects). To reconstruct the book at any point in time:
|
|
160
|
+
|
|
161
|
+
1. Load the nearest `snapshots/` file preceding your target window.
|
|
162
|
+
2. Discard depth diff rows where `last_update_id <= snapshot.last_update_id`.
|
|
163
|
+
3. Verify the first kept diff satisfies `first_update_id <= snapshot.last_update_id + 1`.
|
|
164
|
+
4. Apply diffs in ascending `last_update_id` order. Remove levels where `quantity == 0.0`.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## LOBStreamer parameters
|
|
169
|
+
|
|
170
|
+
| Parameter | Type | Default | Description |
|
|
171
|
+
|---|---|---|---|
|
|
172
|
+
| assets | list[str] | required | Binance symbols, e.g. `["BTCUSDT"]` |
|
|
173
|
+
| output | str | `"local"` | `"local"` or `"gcs"` |
|
|
174
|
+
| output_dir | str | `"./lob_data"` | Base dir for local output |
|
|
175
|
+
| bucket | str | None | GCS bucket (required for gcs output) |
|
|
176
|
+
| fallback_dir | str | `"./lob_fallback"` | Local fallback if GCS fails |
|
|
177
|
+
| flush_interval | int | `300` | Seconds between flushes |
|
|
178
|
+
| on_trade | callable | None | Callback per trade record |
|
|
179
|
+
| on_depth | callable | None | Callback per depth record |
|
|
180
|
+
| log_dir | str | `"./logs"` | Rotating log directory |
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# crypto-lob-stream
|
|
2
|
+
|
|
3
|
+
Stream Binance Level 2 order book and trade data to local disk or Google Cloud Storage. Built for researchers and practitioners who need continuous, reconstructable LOB data and find it difficult to access this kind of dataset openly.
|
|
4
|
+
|
|
5
|
+
Publishes quarterly snapshots to [Hugging Face](https://huggingface.co/) for free public use.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# Local output only
|
|
13
|
+
pip install crypto-lob-stream
|
|
14
|
+
|
|
15
|
+
# With GCS support
|
|
16
|
+
pip install crypto-lob-stream[gcs]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires Python 3.10+.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Quickstart
|
|
24
|
+
|
|
25
|
+
### Python API
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from crypto_lob_stream import LOBStreamer
|
|
29
|
+
|
|
30
|
+
# Stream to local disk
|
|
31
|
+
streamer = LOBStreamer(
|
|
32
|
+
assets=["BTCUSDT", "ETHUSDT", "SOLUSDT"],
|
|
33
|
+
output="local",
|
|
34
|
+
output_dir="./lob_data",
|
|
35
|
+
)
|
|
36
|
+
streamer.run()
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
# Stream to GCS
|
|
41
|
+
streamer = LOBStreamer(
|
|
42
|
+
assets=["BTCUSDT"],
|
|
43
|
+
output="gcs",
|
|
44
|
+
bucket="my-gcs-bucket",
|
|
45
|
+
)
|
|
46
|
+
streamer.run()
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
# With a real-time callback on every trade
|
|
51
|
+
def on_trade(record):
|
|
52
|
+
print(f"Trade: {record['asset']} {record['price']} x {record['quantity']}")
|
|
53
|
+
|
|
54
|
+
streamer = LOBStreamer(
|
|
55
|
+
assets=["BTCUSDT"],
|
|
56
|
+
output="local",
|
|
57
|
+
on_trade=on_trade,
|
|
58
|
+
)
|
|
59
|
+
streamer.run()
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### CLI
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Local
|
|
66
|
+
crypto-lob-stream --assets BTCUSDT,ETHUSDT --output local --output-dir ./data
|
|
67
|
+
|
|
68
|
+
# GCS
|
|
69
|
+
crypto-lob-stream --assets BTCUSDT,ETHUSDT,SOLUSDT --output gcs --bucket my-bucket
|
|
70
|
+
|
|
71
|
+
# Custom flush interval
|
|
72
|
+
crypto-lob-stream --assets BTCUSDT --output local --flush-interval 60
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Output structure
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
{output_dir}/
|
|
81
|
+
trades/{asset}/YYYY-MM-DD-HH.parquet
|
|
82
|
+
depth/{asset}/YYYY-MM-DD-HH.parquet
|
|
83
|
+
snapshots/{asset}/YYYY-MM-DD-HHmmss.parquet
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
All files are Snappy-compressed Parquet.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Schemas
|
|
91
|
+
|
|
92
|
+
### trades
|
|
93
|
+
| Field | Type |
|
|
94
|
+
|---|---|
|
|
95
|
+
| timestamp_ms | int64 |
|
|
96
|
+
| asset | string |
|
|
97
|
+
| trade_id | int64 |
|
|
98
|
+
| price | float64 |
|
|
99
|
+
| quantity | float64 |
|
|
100
|
+
| buyer_maker | bool |
|
|
101
|
+
|
|
102
|
+
### depth (diff events)
|
|
103
|
+
| Field | Type | Notes |
|
|
104
|
+
|---|---|---|
|
|
105
|
+
| timestamp_ms | int64 | |
|
|
106
|
+
| asset | string | |
|
|
107
|
+
| side | string (bid/ask) | |
|
|
108
|
+
| price | float64 | |
|
|
109
|
+
| quantity | float64 | 0.0 = level removed |
|
|
110
|
+
| first_update_id | int64 | `U` field from Binance |
|
|
111
|
+
| last_update_id | int64 | `u` field -- sequence number |
|
|
112
|
+
|
|
113
|
+
### snapshots
|
|
114
|
+
| Field | Type | Notes |
|
|
115
|
+
|---|---|---|
|
|
116
|
+
| timestamp_ms | int64 | |
|
|
117
|
+
| asset | string | |
|
|
118
|
+
| side | string (bid/ask) | |
|
|
119
|
+
| price | float64 | |
|
|
120
|
+
| quantity | float64 | |
|
|
121
|
+
| last_update_id | int64 | Anchor point for diff replay |
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## LOB reconstruction
|
|
126
|
+
|
|
127
|
+
A REST snapshot is fetched and stored for every WebSocket connection (including reconnects). To reconstruct the book at any point in time:
|
|
128
|
+
|
|
129
|
+
1. Load the nearest `snapshots/` file preceding your target window.
|
|
130
|
+
2. Discard depth diff rows where `last_update_id <= snapshot.last_update_id`.
|
|
131
|
+
3. Verify the first kept diff satisfies `first_update_id <= snapshot.last_update_id + 1`.
|
|
132
|
+
4. Apply diffs in ascending `last_update_id` order. Remove levels where `quantity == 0.0`.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## LOBStreamer parameters
|
|
137
|
+
|
|
138
|
+
| Parameter | Type | Default | Description |
|
|
139
|
+
|---|---|---|---|
|
|
140
|
+
| assets | list[str] | required | Binance symbols, e.g. `["BTCUSDT"]` |
|
|
141
|
+
| output | str | `"local"` | `"local"` or `"gcs"` |
|
|
142
|
+
| output_dir | str | `"./lob_data"` | Base dir for local output |
|
|
143
|
+
| bucket | str | None | GCS bucket (required for gcs output) |
|
|
144
|
+
| fallback_dir | str | `"./lob_fallback"` | Local fallback if GCS fails |
|
|
145
|
+
| flush_interval | int | `300` | Seconds between flushes |
|
|
146
|
+
| on_trade | callable | None | Callback per trade record |
|
|
147
|
+
| on_depth | callable | None | Callback per depth record |
|
|
148
|
+
| log_dir | str | `"./logs"` | Rotating log directory |
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
MIT
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crypto-lob-stream
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stream Binance Level 2 order book and trade data to local disk or GCS.
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/Goodie-Goody/crypto_lob_stream_pypi
|
|
7
|
+
Project-URL: Repository, https://github.com/Goodie-Goody/crypto_lob_stream_pypi
|
|
8
|
+
Project-URL: Issues, https://github.com/Goodie-Goody/crypto_lob_stream_pypi/issues
|
|
9
|
+
Keywords: cryptocurrency,order-book,LOB,binance,market-data,streaming
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: websockets>=12.0
|
|
24
|
+
Requires-Dist: pyarrow>=14.0
|
|
25
|
+
Requires-Dist: aiohttp>=3.9
|
|
26
|
+
Requires-Dist: python-dotenv>=1.0
|
|
27
|
+
Provides-Extra: gcs
|
|
28
|
+
Requires-Dist: google-cloud-storage>=2.0; extra == "gcs"
|
|
29
|
+
Provides-Extra: all
|
|
30
|
+
Requires-Dist: google-cloud-storage>=2.0; extra == "all"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# crypto-lob-stream
|
|
34
|
+
|
|
35
|
+
Stream Binance Level 2 order book and trade data to local disk or Google Cloud Storage. Built for researchers and practitioners who need continuous, reconstructable LOB data and find it difficult to access this kind of dataset openly.
|
|
36
|
+
|
|
37
|
+
Publishes quarterly snapshots to [Hugging Face](https://huggingface.co/) for free public use.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# Local output only
|
|
45
|
+
pip install crypto-lob-stream
|
|
46
|
+
|
|
47
|
+
# With GCS support
|
|
48
|
+
pip install crypto-lob-stream[gcs]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Requires Python 3.10+.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Quickstart
|
|
56
|
+
|
|
57
|
+
### Python API
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from crypto_lob_stream import LOBStreamer
|
|
61
|
+
|
|
62
|
+
# Stream to local disk
|
|
63
|
+
streamer = LOBStreamer(
|
|
64
|
+
assets=["BTCUSDT", "ETHUSDT", "SOLUSDT"],
|
|
65
|
+
output="local",
|
|
66
|
+
output_dir="./lob_data",
|
|
67
|
+
)
|
|
68
|
+
streamer.run()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
# Stream to GCS
|
|
73
|
+
streamer = LOBStreamer(
|
|
74
|
+
assets=["BTCUSDT"],
|
|
75
|
+
output="gcs",
|
|
76
|
+
bucket="my-gcs-bucket",
|
|
77
|
+
)
|
|
78
|
+
streamer.run()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
# With a real-time callback on every trade
|
|
83
|
+
def on_trade(record):
|
|
84
|
+
print(f"Trade: {record['asset']} {record['price']} x {record['quantity']}")
|
|
85
|
+
|
|
86
|
+
streamer = LOBStreamer(
|
|
87
|
+
assets=["BTCUSDT"],
|
|
88
|
+
output="local",
|
|
89
|
+
on_trade=on_trade,
|
|
90
|
+
)
|
|
91
|
+
streamer.run()
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### CLI
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
# Local
|
|
98
|
+
crypto-lob-stream --assets BTCUSDT,ETHUSDT --output local --output-dir ./data
|
|
99
|
+
|
|
100
|
+
# GCS
|
|
101
|
+
crypto-lob-stream --assets BTCUSDT,ETHUSDT,SOLUSDT --output gcs --bucket my-bucket
|
|
102
|
+
|
|
103
|
+
# Custom flush interval
|
|
104
|
+
crypto-lob-stream --assets BTCUSDT --output local --flush-interval 60
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Output structure
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
{output_dir}/
|
|
113
|
+
trades/{asset}/YYYY-MM-DD-HH.parquet
|
|
114
|
+
depth/{asset}/YYYY-MM-DD-HH.parquet
|
|
115
|
+
snapshots/{asset}/YYYY-MM-DD-HHmmss.parquet
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
All files are Snappy-compressed Parquet.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Schemas
|
|
123
|
+
|
|
124
|
+
### trades
|
|
125
|
+
| Field | Type |
|
|
126
|
+
|---|---|
|
|
127
|
+
| timestamp_ms | int64 |
|
|
128
|
+
| asset | string |
|
|
129
|
+
| trade_id | int64 |
|
|
130
|
+
| price | float64 |
|
|
131
|
+
| quantity | float64 |
|
|
132
|
+
| buyer_maker | bool |
|
|
133
|
+
|
|
134
|
+
### depth (diff events)
|
|
135
|
+
| Field | Type | Notes |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| timestamp_ms | int64 | |
|
|
138
|
+
| asset | string | |
|
|
139
|
+
| side | string (bid/ask) | |
|
|
140
|
+
| price | float64 | |
|
|
141
|
+
| quantity | float64 | 0.0 = level removed |
|
|
142
|
+
| first_update_id | int64 | `U` field from Binance |
|
|
143
|
+
| last_update_id | int64 | `u` field -- sequence number |
|
|
144
|
+
|
|
145
|
+
### snapshots
|
|
146
|
+
| Field | Type | Notes |
|
|
147
|
+
|---|---|---|
|
|
148
|
+
| timestamp_ms | int64 | |
|
|
149
|
+
| asset | string | |
|
|
150
|
+
| side | string (bid/ask) | |
|
|
151
|
+
| price | float64 | |
|
|
152
|
+
| quantity | float64 | |
|
|
153
|
+
| last_update_id | int64 | Anchor point for diff replay |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## LOB reconstruction
|
|
158
|
+
|
|
159
|
+
A REST snapshot is fetched and stored for every WebSocket connection (including reconnects). To reconstruct the book at any point in time:
|
|
160
|
+
|
|
161
|
+
1. Load the nearest `snapshots/` file preceding your target window.
|
|
162
|
+
2. Discard depth diff rows where `last_update_id <= snapshot.last_update_id`.
|
|
163
|
+
3. Verify the first kept diff satisfies `first_update_id <= snapshot.last_update_id + 1`.
|
|
164
|
+
4. Apply diffs in ascending `last_update_id` order. Remove levels where `quantity == 0.0`.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## LOBStreamer parameters
|
|
169
|
+
|
|
170
|
+
| Parameter | Type | Default | Description |
|
|
171
|
+
|---|---|---|---|
|
|
172
|
+
| assets | list[str] | required | Binance symbols, e.g. `["BTCUSDT"]` |
|
|
173
|
+
| output | str | `"local"` | `"local"` or `"gcs"` |
|
|
174
|
+
| output_dir | str | `"./lob_data"` | Base dir for local output |
|
|
175
|
+
| bucket | str | None | GCS bucket (required for gcs output) |
|
|
176
|
+
| fallback_dir | str | `"./lob_fallback"` | Local fallback if GCS fails |
|
|
177
|
+
| flush_interval | int | `300` | Seconds between flushes |
|
|
178
|
+
| on_trade | callable | None | Callback per trade record |
|
|
179
|
+
| on_depth | callable | None | Callback per depth record |
|
|
180
|
+
| log_dir | str | `"./logs"` | Rotating log directory |
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
crypto_lob_stream.egg-info/PKG-INFO
|
|
5
|
+
crypto_lob_stream.egg-info/SOURCES.txt
|
|
6
|
+
crypto_lob_stream.egg-info/dependency_links.txt
|
|
7
|
+
crypto_lob_stream.egg-info/entry_points.txt
|
|
8
|
+
crypto_lob_stream.egg-info/requires.txt
|
|
9
|
+
crypto_lob_stream.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "crypto-lob-stream"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Stream Binance Level 2 order book and trade data to local disk or GCS."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = ["cryptocurrency", "order-book", "LOB", "binance", "market-data", "streaming"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Science/Research",
|
|
16
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Office/Business :: Financial",
|
|
23
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# Core dependencies -- always installed
|
|
27
|
+
dependencies = [
|
|
28
|
+
"websockets>=12.0",
|
|
29
|
+
"pyarrow>=14.0",
|
|
30
|
+
"aiohttp>=3.9",
|
|
31
|
+
"python-dotenv>=1.0",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
# Install with: pip install crypto-lob-stream[gcs]
|
|
36
|
+
gcs = [
|
|
37
|
+
"google-cloud-storage>=2.0",
|
|
38
|
+
]
|
|
39
|
+
# Install everything
|
|
40
|
+
all = [
|
|
41
|
+
"google-cloud-storage>=2.0",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[project.scripts]
|
|
45
|
+
crypto-lob-stream = "crypto_lob_stream.cli:main"
|
|
46
|
+
|
|
47
|
+
[project.urls]
|
|
48
|
+
Homepage = "https://github.com/Goodie-Goody/crypto_lob_stream_pypi"
|
|
49
|
+
Repository = "https://github.com/Goodie-Goody/crypto_lob_stream_pypi"
|
|
50
|
+
Issues = "https://github.com/Goodie-Goody/crypto_lob_stream_pypi/issues"
|
|
51
|
+
|
|
52
|
+
[tool.setuptools.packages.find]
|
|
53
|
+
where = ["."]
|
|
54
|
+
include = ["crypto_lob_stream*"]
|