quantjourney-bidask 0.5.0__tar.gz → 0.6.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.
Files changed (41) hide show
  1. quantjourney_bidask-0.6.0/.gitignore +233 -0
  2. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/MANIFEST.in +1 -0
  3. {quantjourney_bidask-0.5.0/quantjourney_bidask.egg-info → quantjourney_bidask-0.6.0}/PKG-INFO +140 -30
  4. quantjourney_bidask-0.6.0/README.md +251 -0
  5. quantjourney_bidask-0.6.0/__init__.py +14 -0
  6. quantjourney_bidask-0.6.0/data/fetch.py +258 -0
  7. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/examples/animated_spread_monitor.py +9 -7
  8. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/examples/crypto_spread_comparison.py +4 -2
  9. quantjourney_bidask-0.6.0/examples/ff.py +1281 -0
  10. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/examples/liquidity_risk_monitor.py +9 -9
  11. quantjourney_bidask-0.6.0/examples/realtime_spread_monitor.py +505 -0
  12. quantjourney_bidask-0.6.0/examples/simple_data_example.py +129 -0
  13. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/examples/spread_estimator.py +8 -1
  14. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/examples/spread_monitor.py +4 -0
  15. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/examples/stock_liquidity_risk.py +10 -6
  16. quantjourney_bidask-0.6.0/examples/visualization.py +142 -0
  17. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/pyproject.toml +4 -3
  18. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/_version.py +1 -1
  19. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0/quantjourney_bidask.egg-info}/PKG-INFO +140 -30
  20. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask.egg-info/SOURCES.txt +7 -0
  21. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask.egg-info/requires.txt +1 -0
  22. quantjourney_bidask-0.6.0/tests/test_data_fetcher.py +106 -0
  23. quantjourney_bidask-0.5.0/README.md +0 -142
  24. quantjourney_bidask-0.5.0/tests/test_data_fetcher.py +0 -61
  25. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/CHANGELOG.md +0 -0
  26. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/LICENSE +0 -0
  27. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/docs/usage_examples.ipynb +0 -0
  28. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/__init__.py +0 -0
  29. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/data_fetcher.py +0 -0
  30. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/edge.py +0 -0
  31. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/edge_expanding.py +0 -0
  32. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/edge_rolling.py +0 -0
  33. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask/websocket_fetcher.py +0 -0
  34. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask.egg-info/dependency_links.txt +0 -0
  35. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/quantjourney_bidask.egg-info/top_level.txt +0 -0
  36. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/requirements.txt +0 -0
  37. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/setup.cfg +0 -0
  38. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/tests/test_edge.py +0 -0
  39. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/tests/test_edge_expanding.py +0 -0
  40. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/tests/test_edge_rolling.py +0 -0
  41. {quantjourney_bidask-0.5.0 → quantjourney_bidask-0.6.0}/tests/test_estimators.py +0 -0
@@ -0,0 +1,233 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ target/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pyenv
85
+ .python-version
86
+
87
+ # pipenv
88
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
90
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
91
+ # install all needed dependencies.
92
+ #Pipfile.lock
93
+
94
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95
+ __pypackages__/
96
+
97
+ # Celery stuff
98
+ celerybeat-schedule
99
+ celerybeat.pid
100
+
101
+ # SageMath parsed files
102
+ *.sage.py
103
+
104
+ # Environments
105
+ .env
106
+ .venv
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+
113
+ # Spyder project settings
114
+ .spyderproject
115
+ .spyproject
116
+
117
+ # Rope project settings
118
+ .ropeproject
119
+
120
+ # mkdocs documentation
121
+ /site
122
+
123
+ # mypy
124
+ .mypy_cache/
125
+ .dmypy.json
126
+ dmypy.json
127
+
128
+ # Pyre type checker
129
+ .pyre/
130
+
131
+ # PyCharm
132
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
133
+ # be added to the global gitignore or merged into this project gitignore.
134
+ .idea/
135
+
136
+ # VS Code
137
+ .vscode/
138
+
139
+ # macOS
140
+ .DS_Store
141
+ .AppleDouble
142
+ .LSOverride
143
+
144
+ # Windows
145
+ Thumbs.db
146
+ ehthumbs.db
147
+ Desktop.ini
148
+ $RECYCLE.BIN/
149
+
150
+ # Linux
151
+ *~
152
+
153
+ # PyPI credentials
154
+ .pypirc
155
+
156
+ # Temporary files
157
+ *.tmp
158
+ *.temp
159
+ *.swp
160
+ *.swo
161
+
162
+ # Log files
163
+ *.log
164
+
165
+ # Database files
166
+ *.db
167
+ *.sqlite
168
+
169
+ # Configuration files with secrets
170
+ config.ini
171
+ secrets.json
172
+ .secrets
173
+
174
+ # Backup files
175
+ *.bak
176
+ *.backup
177
+
178
+ # Editor files
179
+ *.sublime-project
180
+ *.sublime-workspace
181
+
182
+ # Pytest
183
+ .pytest_cache/
184
+
185
+ # Coverage
186
+ .coverage
187
+ htmlcov/
188
+
189
+ # Ruff
190
+ .ruff_cache/
191
+
192
+ # Black
193
+ .black/
194
+
195
+ # isort
196
+ .isort.cfg
197
+
198
+ # Local development
199
+ local/
200
+ scratch/
201
+ playground/
202
+
203
+ # Documentation builds
204
+ docs/build/
205
+ docs/_build/
206
+
207
+ # Example outputs
208
+ examples/output/
209
+ examples/plots/
210
+ examples/*.png
211
+ examples/*.jpg
212
+ examples/*.pdf
213
+
214
+ # Data files (if any)
215
+ data/
216
+ *.csv
217
+ *.json
218
+ *.xlsx
219
+ *.parquet
220
+
221
+ # Model files
222
+ *.pkl
223
+ *.pickle
224
+ *.joblib
225
+ *.h5
226
+ *.hdf5
227
+
228
+ # Jupyter notebook outputs
229
+ *.ipynb_checkpoints/
230
+
231
+ # Archive
232
+ @archive/
233
+ data_full/
@@ -6,4 +6,5 @@ include requirements.txt
6
6
  include docs/usage_examples.ipynb
7
7
  include examples/*.py
8
8
  include tests/*.py
9
+ include data/*.py
9
10
  recursive-include quantjourney_bidask *.py
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quantjourney-bidask
3
- Version: 0.5.0
4
- Summary: Efficient bid-ask spread estimator from OHLC prices
3
+ Version: 0.6.0
4
+ Summary: Efficient bid-ask spread estimator from OHLC prices with simplified data fetching for stocks and crypto
5
5
  Author-email: Jakub Polec <jakub@quantjourney.pro>
6
6
  License-Expression: MIT
7
7
  Project-URL: Homepage, https://github.com/QuantJourneyOrg/qj_bidask
@@ -26,6 +26,7 @@ Requires-Dist: yfinance>=0.2
26
26
  Requires-Dist: matplotlib>=3.5
27
27
  Requires-Dist: plotly>=5.0
28
28
  Requires-Dist: websocket-client>=1.0
29
+ Requires-Dist: ccxt>=4.0
29
30
  Provides-Extra: dev
30
31
  Requires-Dist: pytest>=7.0; extra == "dev"
31
32
  Requires-Dist: pytest-mock>=3.10; extra == "dev"
@@ -54,7 +55,10 @@ This library is designed for quantitative finance professionals, researchers, an
54
55
  ## Features
55
56
 
56
57
  - **Efficient Spread Estimation**: Implements the EDGE estimator for single, rolling, and expanding windows.
57
- - **Data Integration**: Fetch OHLC data from Binance (via custom FastAPI server) and Yahoo Finance (via yfinance).
58
+ - **Real-Time Data**: Websocket support for live cryptocurrency data from Binance and other exchanges.
59
+ - **Data Integration**: Fetch OHLC data from Yahoo Finance and generate synthetic data for testing.
60
+ - **Live Monitoring**: Real-time spread monitoring with animated visualizations.
61
+ - **Local Development**: Works completely locally without cloud dependencies.
58
62
  - **Robust Handling**: Supports missing values, non-positive prices, and various data frequencies.
59
63
  - **Comprehensive Tests**: Extensive unit tests with known test cases from the original paper.
60
64
  - **Clear Documentation**: Detailed docstrings and usage examples.
@@ -67,6 +71,14 @@ Install the library via pip:
67
71
  pip install quantjourney-bidask
68
72
  ```
69
73
 
74
+ For development (local setup):
75
+
76
+ ```bash
77
+ git clone https://github.com/QuantJourneyOrg/qj_bidask
78
+ cd qj_bidask
79
+ pip install -e .
80
+ ```
81
+
70
82
  ## Quick Start
71
83
 
72
84
  ### Basic Usage
@@ -89,42 +101,106 @@ print(f"Estimated bid-ask spread: {spread:.6f}")
89
101
 
90
102
  ```python
91
103
  from quantjourney_bidask import edge_rolling
104
+ import pandas as pd
105
+
106
+ # Create DataFrame with OHLC data
107
+ df = pd.DataFrame({
108
+ 'open': open_prices,
109
+ 'high': high_prices,
110
+ 'low': low_prices,
111
+ 'close': close_prices
112
+ })
92
113
 
93
114
  # Calculate rolling spreads with a 20-period window
94
- rolling_spreads = edge_rolling(
95
- open_prices, high_prices, low_prices, close_prices,
96
- window=20
97
- )
115
+ rolling_spreads = edge_rolling(df, window=20)
98
116
  print(f"Rolling spreads: {rolling_spreads}")
99
117
  ```
100
118
 
101
119
  ### Data Fetching Integration
102
120
 
103
121
  ```python
104
- from quantjourney_bidask import fetch_yfinance_data, edge
122
+ from data.fetch import get_stock_data, get_crypto_data
123
+ from quantjourney_bidask import edge_rolling
124
+ import asyncio
125
+
126
+ # Fetch stock data
127
+ stock_df = get_stock_data("AAPL", period="1mo", interval="1d")
128
+ stock_spreads = edge_rolling(stock_df, window=20)
129
+ print(f"AAPL average spread: {stock_spreads.mean():.6f}")
130
+
131
+ # Fetch crypto data (async)
132
+ async def get_crypto_spreads():
133
+ crypto_df = await get_crypto_data("BTC/USDT", "binance", "1h", 168)
134
+ crypto_spreads = edge_rolling(crypto_df, window=24)
135
+ return crypto_spreads.mean()
136
+
137
+ crypto_avg_spread = asyncio.run(get_crypto_spreads())
138
+ print(f"BTC average spread: {crypto_avg_spread:.6f}")
139
+ ```
140
+
141
+ ### Real-time Data Streaming
142
+
143
+ ```python
144
+ from data.fetch import DataFetcher
145
+ import asyncio
146
+
147
+ async def stream_btc_spreads():
148
+ fetcher = DataFetcher()
149
+ # Stream BTC data for 60 seconds
150
+ btc_stream = await fetcher.get_btc_1m_websocket(duration_seconds=60)
151
+
152
+ # Calculate spread from real-time data
153
+ if not btc_stream.empty:
154
+ avg_spread_pct = (btc_stream['spread'] / btc_stream['price']).mean() * 100
155
+ print(f"Real-time BTC average spread: {avg_spread_pct:.4f}%")
156
+
157
+ asyncio.run(stream_btc_spreads())
158
+ ```
159
+
160
+ ### Real-Time Spread Monitoring
161
+
162
+ ```python
163
+ from data.fetch import create_spread_monitor
105
164
 
106
- # Fetch OHLC data for a stock
107
- data = fetch_yfinance_data("AAPL", period="1mo", interval="1h")
165
+ # Create real-time spread monitor
166
+ monitor = create_spread_monitor(["BTCUSDT", "ETHUSDT"], window=20)
108
167
 
109
- # Calculate spread from fetched data
110
- spread = edge(data['Open'], data['High'], data['Low'], data['Close'])
111
- print(f"AAPL spread estimate: {spread:.6f}")
168
+ # Add callback for spread updates
169
+ def print_spread_update(spread_data):
170
+ print(f"{spread_data['symbol']}: {spread_data['spread_bps']:.2f} bps")
171
+
172
+ monitor.add_spread_callback(print_spread_update)
173
+
174
+ # Start monitoring (uses websockets for live data)
175
+ monitor.start_monitoring("1m")
112
176
  ```
113
177
 
114
- ### Live Monitoring
178
+ ### Animated Real-Time Dashboard
115
179
 
116
180
  ```python
117
- from quantjourney_bidask import LiveSpreadMonitor
181
+ # Run the real-time dashboard
182
+ python examples/realtime_spread_monitor.py --mode dashboard
183
+
184
+ # Or console mode
185
+ python examples/realtime_spread_monitor.py --mode console
186
+ ```
187
+
188
+ ## Examples
118
189
 
119
- # Monitor live spreads for cryptocurrency
120
- monitor = LiveSpreadMonitor("BTCUSDT", window=100)
121
- monitor.start()
190
+ The `examples/` directory contains comprehensive examples:
122
191
 
123
- # Get current spread estimate
124
- current_spread = monitor.get_current_spread()
125
- print(f"Current BTC/USDT spread: {current_spread:.6f}")
192
+ - `spread_estimator.py` - Basic spread estimation examples
193
+ - `spread_monitor.py` - Spread monitoring with threshold alerts
194
+ - `realtime_spread_monitor.py` - Live websocket monitoring with animation
195
+ - `crypto_spread_comparison.py` - Multi-asset spread comparison
196
+ - `liquidity_risk_monitor.py` - Liquidity risk monitoring
197
+ - `stock_liquidity_risk.py` - Stock-specific liquidity analysis
126
198
 
127
- monitor.stop()
199
+ Run any example:
200
+
201
+ ```bash
202
+ python examples/spread_estimator.py
203
+ python examples/realtime_spread_monitor.py
128
204
  ```
129
205
 
130
206
  ## API Reference
@@ -132,25 +208,45 @@ monitor.stop()
132
208
  ### Core Functions
133
209
 
134
210
  - `edge(open, high, low, close, sign=False)`: Single-period spread estimation
135
- - `edge_rolling(open, high, low, close, window, min_periods=None)`: Rolling window estimation
136
- - `edge_expanding(open, high, low, close, min_periods=3)`: Expanding window estimation
211
+ - `edge_rolling(df, window, min_periods=None)`: Rolling window estimation
212
+ - `edge_expanding(df, min_periods=3)`: Expanding window estimation
137
213
 
138
- ### Data Fetching
214
+ ### Data Fetching (`data/fetch.py`)
139
215
 
140
- - `fetch_yfinance_data(symbol, period, interval)`: Fetch data from Yahoo Finance
141
- - `fetch_binance_data(symbol, interval, limit)`: Fetch data from Binance API
216
+ - `fetch_yfinance_data(tickers, period, interval)`: Fetch real market data from Yahoo Finance
217
+ - `generate_synthetic_crypto_data(symbols, hours, interval_minutes)`: Generate synthetic crypto data
218
+ - `fetch_binance_data(*args, **kwargs)`: Compatibility function (returns synthetic data)
219
+ - `create_realtime_stream(symbols, exchange)`: Create websocket data stream
220
+ - `create_spread_monitor(symbols, window)`: Create real-time spread monitor
142
221
 
143
- ### Live Monitoring
222
+ ### Real-Time Classes
144
223
 
145
- - `LiveSpreadMonitor(symbol, window)`: Real-time spread monitoring via WebSocket
224
+ - `RealTimeDataStream`: Websocket data streaming for live market data
225
+ - `RealTimeSpreadMonitor`: Real-time spread calculation and monitoring
226
+ - `AnimatedSpreadMonitor`: Animated real-time visualization
146
227
 
147
228
  ## Requirements
148
229
 
149
- - Python >= 3.8
230
+ - Python >= 3.11
150
231
  - numpy >= 1.20
151
232
  - pandas >= 1.5
152
233
  - requests >= 2.28
153
234
  - yfinance >= 0.2
235
+ - matplotlib >= 3.5
236
+ - websocket-client >= 1.0
237
+
238
+ ## WebSocket Support
239
+
240
+ The library supports real-time data via websockets:
241
+
242
+ - **Binance**: `wss://stream.binance.com:9443/ws/` (cryptocurrency data)
243
+ - **Fallback**: Synthetic data generation for testing when websockets unavailable
244
+
245
+ Real-time features:
246
+ - Live spread calculation
247
+ - Animated visualizations
248
+ - Threshold alerts
249
+ - Multi-symbol monitoring
154
250
 
155
251
  ## Academic Citation
156
252
 
@@ -176,6 +272,20 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
176
272
 
177
273
  Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
178
274
 
275
+ ### Development Setup
276
+
277
+ ```bash
278
+ git clone https://github.com/QuantJourneyOrg/qj_bidask
279
+ cd qj_bidask
280
+ pip install -e ".[dev]"
281
+
282
+ # Run tests
283
+ pytest
284
+
285
+ # Run examples
286
+ python examples/realtime_spread_monitor.py
287
+ ```
288
+
179
289
  ## Support
180
290
 
181
291
  - **Documentation**: [GitHub Repository](https://github.com/QuantJourneyOrg/qj_bidask)