quantjourney-bidask 0.5.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 (32) hide show
  1. quantjourney_bidask-0.5.0/CHANGELOG.md +61 -0
  2. quantjourney_bidask-0.5.0/LICENSE +21 -0
  3. quantjourney_bidask-0.5.0/MANIFEST.in +9 -0
  4. quantjourney_bidask-0.5.0/PKG-INFO +183 -0
  5. quantjourney_bidask-0.5.0/README.md +142 -0
  6. quantjourney_bidask-0.5.0/docs/usage_examples.ipynb +0 -0
  7. quantjourney_bidask-0.5.0/examples/animated_spread_monitor.py +299 -0
  8. quantjourney_bidask-0.5.0/examples/crypto_spread_comparison.py +394 -0
  9. quantjourney_bidask-0.5.0/examples/liquidity_risk_monitor.py +72 -0
  10. quantjourney_bidask-0.5.0/examples/spread_estimator.py +142 -0
  11. quantjourney_bidask-0.5.0/examples/spread_monitor.py +203 -0
  12. quantjourney_bidask-0.5.0/examples/stock_liquidity_risk.py +43 -0
  13. quantjourney_bidask-0.5.0/pyproject.toml +83 -0
  14. quantjourney_bidask-0.5.0/quantjourney_bidask/__init__.py +8 -0
  15. quantjourney_bidask-0.5.0/quantjourney_bidask/_version.py +7 -0
  16. quantjourney_bidask-0.5.0/quantjourney_bidask/data_fetcher.py +160 -0
  17. quantjourney_bidask-0.5.0/quantjourney_bidask/edge.py +148 -0
  18. quantjourney_bidask-0.5.0/quantjourney_bidask/edge_expanding.py +59 -0
  19. quantjourney_bidask-0.5.0/quantjourney_bidask/edge_rolling.py +202 -0
  20. quantjourney_bidask-0.5.0/quantjourney_bidask/websocket_fetcher.py +308 -0
  21. quantjourney_bidask-0.5.0/quantjourney_bidask.egg-info/PKG-INFO +183 -0
  22. quantjourney_bidask-0.5.0/quantjourney_bidask.egg-info/SOURCES.txt +30 -0
  23. quantjourney_bidask-0.5.0/quantjourney_bidask.egg-info/dependency_links.txt +1 -0
  24. quantjourney_bidask-0.5.0/quantjourney_bidask.egg-info/requires.txt +20 -0
  25. quantjourney_bidask-0.5.0/quantjourney_bidask.egg-info/top_level.txt +1 -0
  26. quantjourney_bidask-0.5.0/requirements.txt +10 -0
  27. quantjourney_bidask-0.5.0/setup.cfg +4 -0
  28. quantjourney_bidask-0.5.0/tests/test_data_fetcher.py +61 -0
  29. quantjourney_bidask-0.5.0/tests/test_edge.py +49 -0
  30. quantjourney_bidask-0.5.0/tests/test_edge_expanding.py +39 -0
  31. quantjourney_bidask-0.5.0/tests/test_edge_rolling.py +41 -0
  32. quantjourney_bidask-0.5.0/tests/test_estimators.py +78 -0
@@ -0,0 +1,61 @@
1
+ # Changelog
2
+
3
+ All notable changes to the quantjourney-bidask project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2024-06-24
9
+
10
+ ### Added
11
+ - Initial release of quantjourney-bidask package
12
+ - EDGE (Efficient estimator of bid-ask spreads) implementation based on Ardia, Guidotti & Kroencke (2024)
13
+ - Core functionality:
14
+ - `edge()`: Single spread estimate from OHLC data
15
+ - `edge_rolling()`: Rolling window spread estimates with vectorized operations
16
+ - `edge_expanding()`: Expanding window spread estimates
17
+ - Data fetching capabilities:
18
+ - `fetch_yfinance_data()`: Yahoo Finance integration via yfinance
19
+ - `fetch_binance_data()`: Binance API integration via custom FastAPI server
20
+ - Real-time monitoring:
21
+ - `LiveSpreadMonitor`: WebSocket-based real-time spread monitoring
22
+ - Configurable alert thresholds and callbacks
23
+ - Multi-symbol support for cryptocurrency pairs
24
+ - Comprehensive examples:
25
+ - Basic spread estimation with synthetic and real data
26
+ - Animated spread monitoring for presentations
27
+ - Multi-cryptocurrency spread comparison analysis
28
+ - Liquidity risk monitoring with statistical alerts
29
+ - Real-time WebSocket implementation examples
30
+ - Professional documentation:
31
+ - Detailed docstrings following NumPy style
32
+ - Academic paper validation (matches expected results)
33
+ - Usage examples with both synthetic and real market data
34
+ - Testing framework:
35
+ - Unit tests for core functionality
36
+ - Validation against original research paper results
37
+ - Edge case handling (missing data, insufficient observations)
38
+
39
+ ### Technical Details
40
+ - Implements the methodology from "Efficient estimation of bid–ask spreads from open, high, low, and close prices" (Journal of Financial Economics, 2024)
41
+ - Vectorized operations for high-performance rolling calculations
42
+ - Proper handling of missing values and edge cases
43
+ - Support for multiple data frequencies (minute, hourly, daily)
44
+ - WebSocket integration for real-time cryptocurrency data
45
+ - Modern Python packaging with pyproject.toml
46
+
47
+ ### Dependencies
48
+ - numpy >= 1.20
49
+ - pandas >= 1.5
50
+ - requests >= 2.28
51
+ - yfinance >= 0.2
52
+ - matplotlib >= 3.5
53
+ - plotly >= 5.0
54
+ - websocket-client >= 1.0
55
+
56
+ ### Author
57
+ - Jakub Polec (jakub@quantjourney.pro)
58
+ - QuantJourney
59
+
60
+ ### License
61
+ - MIT License
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jakub Polec, QuantJourney
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,9 @@
1
+ include LICENSE
2
+ include README.md
3
+ include pyproject.toml
4
+ include CHANGELOG.md
5
+ include requirements.txt
6
+ include docs/usage_examples.ipynb
7
+ include examples/*.py
8
+ include tests/*.py
9
+ recursive-include quantjourney_bidask *.py
@@ -0,0 +1,183 @@
1
+ Metadata-Version: 2.4
2
+ Name: quantjourney-bidask
3
+ Version: 0.5.0
4
+ Summary: Efficient bid-ask spread estimator from OHLC prices
5
+ Author-email: Jakub Polec <jakub@quantjourney.pro>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/QuantJourneyOrg/qj_bidask
8
+ Project-URL: Repository, https://github.com/QuantJourneyOrg/qj_bidask
9
+ Project-URL: Bug Tracker, https://github.com/QuantJourneyOrg/qj_bidask/issues
10
+ Keywords: finance,bid-ask,spread,trading,quantitative,OHLC
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Financial and Insurance Industry
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Office/Business :: Financial :: Investment
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: <3.15,>=3.11
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: numpy>=1.20
23
+ Requires-Dist: pandas>=1.5
24
+ Requires-Dist: requests>=2.28
25
+ Requires-Dist: yfinance>=0.2
26
+ Requires-Dist: matplotlib>=3.5
27
+ Requires-Dist: plotly>=5.0
28
+ Requires-Dist: websocket-client>=1.0
29
+ Provides-Extra: dev
30
+ Requires-Dist: pytest>=7.0; extra == "dev"
31
+ Requires-Dist: pytest-mock>=3.10; extra == "dev"
32
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
33
+ Requires-Dist: ruff>=0.1; extra == "dev"
34
+ Requires-Dist: mypy>=1.0; extra == "dev"
35
+ Requires-Dist: black>=22.0; extra == "dev"
36
+ Requires-Dist: isort>=5.0; extra == "dev"
37
+ Provides-Extra: examples
38
+ Requires-Dist: jupyter>=1.0; extra == "examples"
39
+ Requires-Dist: ipywidgets>=7.0; extra == "examples"
40
+ Dynamic: license-file
41
+
42
+ # QuantJourney Bid-Ask Spread Estimator
43
+
44
+ ![PyPI](https://img.shields.io/pypi/v/quantjourney-bidask)
45
+ ![License](https://img.shields.io/github/license/quantjourney/bidask)
46
+ ![Tests](https://img.shields.io/github/workflow/status/quantjourney/bidask/Test)
47
+
48
+ The `quantjourney-bidask` library provides an efficient estimator for calculating bid-ask spreads from open, high, low, and close (OHLC) prices, based on the methodology described in:
49
+
50
+ > Ardia, D., Guidotti, E., Kroencke, T.A. (2024). Efficient Estimation of Bid-Ask Spreads from Open, High, Low, and Close Prices. *Journal of Financial Economics*, 161, 103916. [doi:10.1016/j.jfineco.2024.103916](https://doi.org/10.1016/j.jfineco.2024.103916)
51
+
52
+ This library is designed for quantitative finance professionals, researchers, and traders who need accurate and computationally efficient spread estimates for equities, cryptocurrencies, and other assets.
53
+
54
+ ## Features
55
+
56
+ - **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
+ - **Robust Handling**: Supports missing values, non-positive prices, and various data frequencies.
59
+ - **Comprehensive Tests**: Extensive unit tests with known test cases from the original paper.
60
+ - **Clear Documentation**: Detailed docstrings and usage examples.
61
+
62
+ ## Installation
63
+
64
+ Install the library via pip:
65
+
66
+ ```bash
67
+ pip install quantjourney-bidask
68
+ ```
69
+
70
+ ## Quick Start
71
+
72
+ ### Basic Usage
73
+
74
+ ```python
75
+ from quantjourney_bidask import edge
76
+
77
+ # Example OHLC data (as lists or numpy arrays)
78
+ open_prices = [100.0, 101.5, 99.8, 102.1, 100.9]
79
+ high_prices = [102.3, 103.0, 101.2, 103.5, 102.0]
80
+ low_prices = [99.5, 100.8, 98.9, 101.0, 100.1]
81
+ close_prices = [101.2, 100.2, 101.8, 100.5, 101.5]
82
+
83
+ # Calculate bid-ask spread
84
+ spread = edge(open_prices, high_prices, low_prices, close_prices)
85
+ print(f"Estimated bid-ask spread: {spread:.6f}")
86
+ ```
87
+
88
+ ### Rolling Window Analysis
89
+
90
+ ```python
91
+ from quantjourney_bidask import edge_rolling
92
+
93
+ # 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
+ )
98
+ print(f"Rolling spreads: {rolling_spreads}")
99
+ ```
100
+
101
+ ### Data Fetching Integration
102
+
103
+ ```python
104
+ from quantjourney_bidask import fetch_yfinance_data, edge
105
+
106
+ # Fetch OHLC data for a stock
107
+ data = fetch_yfinance_data("AAPL", period="1mo", interval="1h")
108
+
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}")
112
+ ```
113
+
114
+ ### Live Monitoring
115
+
116
+ ```python
117
+ from quantjourney_bidask import LiveSpreadMonitor
118
+
119
+ # Monitor live spreads for cryptocurrency
120
+ monitor = LiveSpreadMonitor("BTCUSDT", window=100)
121
+ monitor.start()
122
+
123
+ # Get current spread estimate
124
+ current_spread = monitor.get_current_spread()
125
+ print(f"Current BTC/USDT spread: {current_spread:.6f}")
126
+
127
+ monitor.stop()
128
+ ```
129
+
130
+ ## API Reference
131
+
132
+ ### Core Functions
133
+
134
+ - `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
137
+
138
+ ### Data Fetching
139
+
140
+ - `fetch_yfinance_data(symbol, period, interval)`: Fetch data from Yahoo Finance
141
+ - `fetch_binance_data(symbol, interval, limit)`: Fetch data from Binance API
142
+
143
+ ### Live Monitoring
144
+
145
+ - `LiveSpreadMonitor(symbol, window)`: Real-time spread monitoring via WebSocket
146
+
147
+ ## Requirements
148
+
149
+ - Python >= 3.8
150
+ - numpy >= 1.20
151
+ - pandas >= 1.5
152
+ - requests >= 2.28
153
+ - yfinance >= 0.2
154
+
155
+ ## Academic Citation
156
+
157
+ If you use this library in academic research, please cite:
158
+
159
+ ```bibtex
160
+ @article{ardia2024efficient,
161
+ title={Efficient Estimation of Bid-Ask Spreads from Open, High, Low, and Close Prices},
162
+ author={Ardia, David and Guidotti, Emanuele and Kroencke, Tim A},
163
+ journal={Journal of Financial Economics},
164
+ volume={161},
165
+ pages={103916},
166
+ year={2024},
167
+ publisher={Elsevier}
168
+ }
169
+ ```
170
+
171
+ ## License
172
+
173
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
174
+
175
+ ## Contributing
176
+
177
+ 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
+
179
+ ## Support
180
+
181
+ - **Documentation**: [GitHub Repository](https://github.com/QuantJourneyOrg/qj_bidask)
182
+ - **Issues**: [Bug Tracker](https://github.com/QuantJourneyOrg/qj_bidask/issues)
183
+ - **Contact**: jakub@quantjourney.pro
@@ -0,0 +1,142 @@
1
+ # QuantJourney Bid-Ask Spread Estimator
2
+
3
+ ![PyPI](https://img.shields.io/pypi/v/quantjourney-bidask)
4
+ ![License](https://img.shields.io/github/license/quantjourney/bidask)
5
+ ![Tests](https://img.shields.io/github/workflow/status/quantjourney/bidask/Test)
6
+
7
+ The `quantjourney-bidask` library provides an efficient estimator for calculating bid-ask spreads from open, high, low, and close (OHLC) prices, based on the methodology described in:
8
+
9
+ > Ardia, D., Guidotti, E., Kroencke, T.A. (2024). Efficient Estimation of Bid-Ask Spreads from Open, High, Low, and Close Prices. *Journal of Financial Economics*, 161, 103916. [doi:10.1016/j.jfineco.2024.103916](https://doi.org/10.1016/j.jfineco.2024.103916)
10
+
11
+ This library is designed for quantitative finance professionals, researchers, and traders who need accurate and computationally efficient spread estimates for equities, cryptocurrencies, and other assets.
12
+
13
+ ## Features
14
+
15
+ - **Efficient Spread Estimation**: Implements the EDGE estimator for single, rolling, and expanding windows.
16
+ - **Data Integration**: Fetch OHLC data from Binance (via custom FastAPI server) and Yahoo Finance (via yfinance).
17
+ - **Robust Handling**: Supports missing values, non-positive prices, and various data frequencies.
18
+ - **Comprehensive Tests**: Extensive unit tests with known test cases from the original paper.
19
+ - **Clear Documentation**: Detailed docstrings and usage examples.
20
+
21
+ ## Installation
22
+
23
+ Install the library via pip:
24
+
25
+ ```bash
26
+ pip install quantjourney-bidask
27
+ ```
28
+
29
+ ## Quick Start
30
+
31
+ ### Basic Usage
32
+
33
+ ```python
34
+ from quantjourney_bidask import edge
35
+
36
+ # Example OHLC data (as lists or numpy arrays)
37
+ open_prices = [100.0, 101.5, 99.8, 102.1, 100.9]
38
+ high_prices = [102.3, 103.0, 101.2, 103.5, 102.0]
39
+ low_prices = [99.5, 100.8, 98.9, 101.0, 100.1]
40
+ close_prices = [101.2, 100.2, 101.8, 100.5, 101.5]
41
+
42
+ # Calculate bid-ask spread
43
+ spread = edge(open_prices, high_prices, low_prices, close_prices)
44
+ print(f"Estimated bid-ask spread: {spread:.6f}")
45
+ ```
46
+
47
+ ### Rolling Window Analysis
48
+
49
+ ```python
50
+ from quantjourney_bidask import edge_rolling
51
+
52
+ # Calculate rolling spreads with a 20-period window
53
+ rolling_spreads = edge_rolling(
54
+ open_prices, high_prices, low_prices, close_prices,
55
+ window=20
56
+ )
57
+ print(f"Rolling spreads: {rolling_spreads}")
58
+ ```
59
+
60
+ ### Data Fetching Integration
61
+
62
+ ```python
63
+ from quantjourney_bidask import fetch_yfinance_data, edge
64
+
65
+ # Fetch OHLC data for a stock
66
+ data = fetch_yfinance_data("AAPL", period="1mo", interval="1h")
67
+
68
+ # Calculate spread from fetched data
69
+ spread = edge(data['Open'], data['High'], data['Low'], data['Close'])
70
+ print(f"AAPL spread estimate: {spread:.6f}")
71
+ ```
72
+
73
+ ### Live Monitoring
74
+
75
+ ```python
76
+ from quantjourney_bidask import LiveSpreadMonitor
77
+
78
+ # Monitor live spreads for cryptocurrency
79
+ monitor = LiveSpreadMonitor("BTCUSDT", window=100)
80
+ monitor.start()
81
+
82
+ # Get current spread estimate
83
+ current_spread = monitor.get_current_spread()
84
+ print(f"Current BTC/USDT spread: {current_spread:.6f}")
85
+
86
+ monitor.stop()
87
+ ```
88
+
89
+ ## API Reference
90
+
91
+ ### Core Functions
92
+
93
+ - `edge(open, high, low, close, sign=False)`: Single-period spread estimation
94
+ - `edge_rolling(open, high, low, close, window, min_periods=None)`: Rolling window estimation
95
+ - `edge_expanding(open, high, low, close, min_periods=3)`: Expanding window estimation
96
+
97
+ ### Data Fetching
98
+
99
+ - `fetch_yfinance_data(symbol, period, interval)`: Fetch data from Yahoo Finance
100
+ - `fetch_binance_data(symbol, interval, limit)`: Fetch data from Binance API
101
+
102
+ ### Live Monitoring
103
+
104
+ - `LiveSpreadMonitor(symbol, window)`: Real-time spread monitoring via WebSocket
105
+
106
+ ## Requirements
107
+
108
+ - Python >= 3.8
109
+ - numpy >= 1.20
110
+ - pandas >= 1.5
111
+ - requests >= 2.28
112
+ - yfinance >= 0.2
113
+
114
+ ## Academic Citation
115
+
116
+ If you use this library in academic research, please cite:
117
+
118
+ ```bibtex
119
+ @article{ardia2024efficient,
120
+ title={Efficient Estimation of Bid-Ask Spreads from Open, High, Low, and Close Prices},
121
+ author={Ardia, David and Guidotti, Emanuele and Kroencke, Tim A},
122
+ journal={Journal of Financial Economics},
123
+ volume={161},
124
+ pages={103916},
125
+ year={2024},
126
+ publisher={Elsevier}
127
+ }
128
+ ```
129
+
130
+ ## License
131
+
132
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
133
+
134
+ ## Contributing
135
+
136
+ 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.
137
+
138
+ ## Support
139
+
140
+ - **Documentation**: [GitHub Repository](https://github.com/QuantJourneyOrg/qj_bidask)
141
+ - **Issues**: [Bug Tracker](https://github.com/QuantJourneyOrg/qj_bidask/issues)
142
+ - **Contact**: jakub@quantjourney.pro
File without changes