crypto-bs 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_bs-0.1.0/LICENSE +22 -0
- crypto_bs-0.1.0/MANIFEST.in +6 -0
- crypto_bs-0.1.0/PKG-INFO +265 -0
- crypto_bs-0.1.0/README.md +216 -0
- crypto_bs-0.1.0/crypto_bs/__init__.py +31 -0
- crypto_bs-0.1.0/crypto_bs/black_scholes.py +465 -0
- crypto_bs-0.1.0/crypto_bs/data_fetch.py +70 -0
- crypto_bs-0.1.0/crypto_bs/greeks.py +40 -0
- crypto_bs-0.1.0/crypto_bs/greeks_calculator.py +560 -0
- crypto_bs-0.1.0/crypto_bs/models.py +0 -0
- crypto_bs-0.1.0/crypto_bs/pricing.py +36 -0
- crypto_bs-0.1.0/crypto_bs/utils.py +43 -0
- crypto_bs-0.1.0/crypto_bs.egg-info/PKG-INFO +265 -0
- crypto_bs-0.1.0/crypto_bs.egg-info/SOURCES.txt +19 -0
- crypto_bs-0.1.0/crypto_bs.egg-info/dependency_links.txt +1 -0
- crypto_bs-0.1.0/crypto_bs.egg-info/requires.txt +4 -0
- crypto_bs-0.1.0/crypto_bs.egg-info/top_level.txt +1 -0
- crypto_bs-0.1.0/examples/basic_usage.py +167 -0
- crypto_bs-0.1.0/pyproject.toml +46 -0
- crypto_bs-0.1.0/setup.cfg +4 -0
- crypto_bs-0.1.0/tests/test_pricing.py +227 -0
crypto_bs-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Seyed Mohammad Hossein Fasihi
|
|
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.
|
|
22
|
+
|
crypto_bs-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crypto_bs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python library for pricing coin-settled crypto options
|
|
5
|
+
Author-email: Seyed Mohammad Hossein Fasihi <mhmd.fasihi@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Seyed Mohammad Hossein Fasihi
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Project-URL: Homepage, https://github.com/MhmdFasihi/crypto_black_scholes
|
|
30
|
+
Project-URL: Repository, https://github.com/MhmdFasihi/crypto_black_scholes
|
|
31
|
+
Project-URL: Issues, https://github.com/MhmdFasihi/crypto_black_scholes/issues
|
|
32
|
+
Keywords: black-scholes,black-76,options,greeks,implied-volatility,crypto,bitcoin,deribit,pricing,risk
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Programming Language :: Python :: 3
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
39
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
40
|
+
Classifier: Operating System :: OS Independent
|
|
41
|
+
Requires-Python: >=3.10
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
License-File: LICENSE
|
|
44
|
+
Requires-Dist: numpy>=1.21.0
|
|
45
|
+
Requires-Dist: scipy>=1.7.0
|
|
46
|
+
Requires-Dist: requests>=2.25.0
|
|
47
|
+
Requires-Dist: pandas>=1.3.0
|
|
48
|
+
Dynamic: license-file
|
|
49
|
+
|
|
50
|
+
# Crypto Black-Scholes
|
|
51
|
+
|
|
52
|
+
A comprehensive Python library for pricing coin-settled cryptocurrency options using advanced Black-Scholes and Black-76 models.
|
|
53
|
+
|
|
54
|
+
## ๐ Features
|
|
55
|
+
|
|
56
|
+
- **Multiple Pricing Models**: Black-Scholes, Black-76, and coin-based adaptations
|
|
57
|
+
- **Complete Greeks Calculation**: Delta, Gamma, Theta, Vega, Rho + second-order Greeks (Speed, Charm, Vanna, Vomma)
|
|
58
|
+
- **Portfolio Risk Analysis**: Multi-position portfolio Greeks and risk metrics
|
|
59
|
+
- **Real-time Data Integration**: Live data from Deribit exchange
|
|
60
|
+
- **Coin-Settled Options Support**: Premiums and payoffs in cryptocurrency units
|
|
61
|
+
- **Breakeven Analysis**: Accurate breakeven calculations for coin-settled options
|
|
62
|
+
- **Implied Volatility**: Calculate IV from market prices
|
|
63
|
+
- **Advanced Validation**: Compare model prices with exchange data
|
|
64
|
+
|
|
65
|
+
## ๐ Model Details
|
|
66
|
+
|
|
67
|
+
This package implements **three pricing approaches** optimized for cryptocurrency options:
|
|
68
|
+
|
|
69
|
+
### 1. Black-76 Model (Primary for Coin-Settled)
|
|
70
|
+
- Designed for futures options (perfect for coin-settled crypto options)
|
|
71
|
+
- Uses forward/futures price instead of spot price
|
|
72
|
+
- No risk-free rate discounting (r=0)
|
|
73
|
+
- Premium and payoff in cryptocurrency units
|
|
74
|
+
|
|
75
|
+
### 2. Enhanced Black-Scholes
|
|
76
|
+
- Standard Black-Scholes with coin-based adaptations
|
|
77
|
+
- Supports both USD and cryptocurrency denominated options
|
|
78
|
+
- Advanced Greeks with second-order derivatives
|
|
79
|
+
|
|
80
|
+
### 3. Portfolio-Level Analysis
|
|
81
|
+
- Aggregate Greeks across multiple positions
|
|
82
|
+
- Risk metrics including gamma exposure and pin risk
|
|
83
|
+
- Greeks breakdown by underlying asset and expiry
|
|
84
|
+
|
|
85
|
+
## ๐ฐ Coin-Settled Options Focus
|
|
86
|
+
|
|
87
|
+
**Key Differences from Standard Options:**
|
|
88
|
+
- **Premium**: Paid in cryptocurrency (e.g., 0.1 BTC for a $120K BTC option)
|
|
89
|
+
- **Settlement**: Payoff delivered in cryptocurrency
|
|
90
|
+
- **Pricing**: Uses Black-76 model with forward prices
|
|
91
|
+
- **Greeks**: Adjusted for cryptocurrency denomination
|
|
92
|
+
- **Breakeven**: Calculated in USD terms but based on crypto premium
|
|
93
|
+
|
|
94
|
+
## ๐ Installation
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pip install .
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## ๐ฏ Quick Start
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from crypto_bs import price_option, delta, gamma, breakeven_price, breakeven_price_coin_based
|
|
104
|
+
|
|
105
|
+
# Basic Black-76 pricing for coin-settled options
|
|
106
|
+
price = price_option(F=110000, K=105000, T=1/365, sigma=0.6, option_type='call')
|
|
107
|
+
print(f"Option Price: {price:.6f} BTC")
|
|
108
|
+
|
|
109
|
+
# Calculate Greeks
|
|
110
|
+
d = delta(110000, 105000, 1/365, 0.6, 'call')
|
|
111
|
+
g = gamma(110000, 105000, 1/365, 0.6)
|
|
112
|
+
|
|
113
|
+
# Breakeven analysis (USD premium)
|
|
114
|
+
be_usd = breakeven_price(105000, 500, 'call')
|
|
115
|
+
print(f"Breakeven (USD premium): ${be_usd:.2f}")
|
|
116
|
+
|
|
117
|
+
# Breakeven analysis (coin-settled premium)
|
|
118
|
+
be_coin = breakeven_price_coin_based(105000, price, 'call')
|
|
119
|
+
print(f"Breakeven (coin-based): ${be_coin:.2f}")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## ๐ง Advanced Usage
|
|
123
|
+
|
|
124
|
+
### Coin-Based Pricing with Full Analysis
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from crypto_bs import BlackScholesModel, OptionParameters, OptionType
|
|
128
|
+
|
|
129
|
+
# Advanced pricing with coin-based support
|
|
130
|
+
bs_model = BlackScholesModel()
|
|
131
|
+
params = OptionParameters(
|
|
132
|
+
spot_price=110000,
|
|
133
|
+
strike_price=105000,
|
|
134
|
+
time_to_maturity=1/365,
|
|
135
|
+
volatility=0.6,
|
|
136
|
+
option_type=OptionType.CALL,
|
|
137
|
+
is_coin_based=True # Key for coin-settled options
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
result = bs_model.calculate_option_price(params)
|
|
141
|
+
print(f"Coin Price: {result.coin_based_price:.6f} BTC")
|
|
142
|
+
print(f"USD Equivalent: ${result.usd_price:.2f}")
|
|
143
|
+
print(f"Delta: {result.delta:.6f}")
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Portfolio Risk Analysis
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from crypto_bs import analyze_portfolio_risk
|
|
150
|
+
|
|
151
|
+
portfolio = [
|
|
152
|
+
{
|
|
153
|
+
'quantity': 10,
|
|
154
|
+
'spot_price': 110000,
|
|
155
|
+
'strike_price': 105000,
|
|
156
|
+
'time_to_maturity': 1/365,
|
|
157
|
+
'volatility': 0.6,
|
|
158
|
+
'option_type': 'call',
|
|
159
|
+
'underlying': 'BTC',
|
|
160
|
+
'is_coin_based': True
|
|
161
|
+
}
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
risk_analysis = analyze_portfolio_risk(portfolio)
|
|
165
|
+
print("Portfolio Delta:", risk_analysis['portfolio_summary']['total_delta'])
|
|
166
|
+
print("Gamma Exposure:", risk_analysis['risk_metrics']['gamma_exposure'])
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Real-Time Deribit Integration
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
from crypto_bs import get_btc_forward_price, get_option_data, validate_deribit_pricing
|
|
173
|
+
|
|
174
|
+
# Get live data
|
|
175
|
+
F = get_btc_forward_price()
|
|
176
|
+
option_data = get_option_data('BTC-3SEP25-105000-C')
|
|
177
|
+
|
|
178
|
+
# Validate model against exchange
|
|
179
|
+
validation = validate_deribit_pricing(
|
|
180
|
+
deribit_price_btc=option_data['mark_price'],
|
|
181
|
+
spot=F,
|
|
182
|
+
strike=105000,
|
|
183
|
+
time_to_maturity=1/365,
|
|
184
|
+
option_type='call'
|
|
185
|
+
)
|
|
186
|
+
print(f"Model vs Exchange difference: {validation['price_difference_btc']:.6f} BTC")
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Breakeven for Coin-Settled Options
|
|
190
|
+
|
|
191
|
+
For coin-settled options where the premium is paid in coin units (e.g., BTC):
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
from crypto_bs import breakeven_price_coin_based
|
|
195
|
+
|
|
196
|
+
K = 105000
|
|
197
|
+
premium_btc = 0.0123 # premium in BTC
|
|
198
|
+
|
|
199
|
+
be = breakeven_price_coin_based(K, premium_btc, 'call')
|
|
200
|
+
print(f"Coin-based breakeven: ${be:.2f}")
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
## ๐ API Reference
|
|
205
|
+
|
|
206
|
+
### Core Functions
|
|
207
|
+
- `price_option(F, K, T, sigma, option_type)` - Basic Black-76 pricing
|
|
208
|
+
- `delta(F, K, T, sigma, option_type)` - Option delta
|
|
209
|
+
- `gamma(F, K, T, sigma)` - Option gamma
|
|
210
|
+
- `vega(F, K, T, sigma)` - Option vega
|
|
211
|
+
- `theta(F, K, T, sigma, option_type)` - Option theta
|
|
212
|
+
- `breakeven_price(K, premium, option_type)` - Breakeven calculation
|
|
213
|
+
|
|
214
|
+
### Advanced Classes
|
|
215
|
+
- `BlackScholesModel` - Advanced pricing with coin-based support
|
|
216
|
+
- `GreeksCalculator` - Portfolio-level Greeks and risk analysis
|
|
217
|
+
- `OptionParameters` - Structured option parameters
|
|
218
|
+
- `PortfolioGreeks` - Portfolio Greeks aggregation
|
|
219
|
+
|
|
220
|
+
### Data Integration
|
|
221
|
+
- `get_btc_forward_price()` - BTC perpetual price from Deribit
|
|
222
|
+
- `get_option_data(instrument)` - Option data from Deribit
|
|
223
|
+
- `get_available_instruments()` - List available options
|
|
224
|
+
|
|
225
|
+
## โ
Validation Results
|
|
226
|
+
|
|
227
|
+
**Real Deribit Data Test (Sept 2025):**
|
|
228
|
+
- **Exchange Price**: 0.0535 BTC
|
|
229
|
+
- **Model Price**: 0.0542 BTC
|
|
230
|
+
- **Difference**: 0.0007 BTC (1.3% relative difference)
|
|
231
|
+
- **Implied Volatility Match**: Within market expectations
|
|
232
|
+
|
|
233
|
+
## ๐ฏ Use Cases
|
|
234
|
+
|
|
235
|
+
- **Crypto Options Trading**: Price and risk-manage BTC/ETH options
|
|
236
|
+
- **Portfolio Hedging**: Calculate Greeks for complex option portfolios
|
|
237
|
+
- **Risk Analysis**: Assess gamma exposure and pin risk
|
|
238
|
+
- **Model Validation**: Compare theoretical prices with exchange data
|
|
239
|
+
- **Breakeven Analysis**: Determine profitable exercise points
|
|
240
|
+
|
|
241
|
+
## ๐งช Testing
|
|
242
|
+
|
|
243
|
+
Run the test suite using pytest:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
# Using pytest (recommended)
|
|
247
|
+
pytest tests/ -v
|
|
248
|
+
|
|
249
|
+
# Or using the test runner script
|
|
250
|
+
python run_tests.py
|
|
251
|
+
|
|
252
|
+
# Or run tests directly
|
|
253
|
+
python tests/test_pricing.py
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
All 16 tests should pass, covering:
|
|
257
|
+
- Basic Black-76 pricing
|
|
258
|
+
- Greeks calculations
|
|
259
|
+
- Coin-based pricing
|
|
260
|
+
- Advanced portfolio analysis
|
|
261
|
+
- Breakeven calculations
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
**Built for cryptocurrency options traders who need accurate, coin-settled pricing models.**
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# Crypto Black-Scholes
|
|
2
|
+
|
|
3
|
+
A comprehensive Python library for pricing coin-settled cryptocurrency options using advanced Black-Scholes and Black-76 models.
|
|
4
|
+
|
|
5
|
+
## ๐ Features
|
|
6
|
+
|
|
7
|
+
- **Multiple Pricing Models**: Black-Scholes, Black-76, and coin-based adaptations
|
|
8
|
+
- **Complete Greeks Calculation**: Delta, Gamma, Theta, Vega, Rho + second-order Greeks (Speed, Charm, Vanna, Vomma)
|
|
9
|
+
- **Portfolio Risk Analysis**: Multi-position portfolio Greeks and risk metrics
|
|
10
|
+
- **Real-time Data Integration**: Live data from Deribit exchange
|
|
11
|
+
- **Coin-Settled Options Support**: Premiums and payoffs in cryptocurrency units
|
|
12
|
+
- **Breakeven Analysis**: Accurate breakeven calculations for coin-settled options
|
|
13
|
+
- **Implied Volatility**: Calculate IV from market prices
|
|
14
|
+
- **Advanced Validation**: Compare model prices with exchange data
|
|
15
|
+
|
|
16
|
+
## ๐ Model Details
|
|
17
|
+
|
|
18
|
+
This package implements **three pricing approaches** optimized for cryptocurrency options:
|
|
19
|
+
|
|
20
|
+
### 1. Black-76 Model (Primary for Coin-Settled)
|
|
21
|
+
- Designed for futures options (perfect for coin-settled crypto options)
|
|
22
|
+
- Uses forward/futures price instead of spot price
|
|
23
|
+
- No risk-free rate discounting (r=0)
|
|
24
|
+
- Premium and payoff in cryptocurrency units
|
|
25
|
+
|
|
26
|
+
### 2. Enhanced Black-Scholes
|
|
27
|
+
- Standard Black-Scholes with coin-based adaptations
|
|
28
|
+
- Supports both USD and cryptocurrency denominated options
|
|
29
|
+
- Advanced Greeks with second-order derivatives
|
|
30
|
+
|
|
31
|
+
### 3. Portfolio-Level Analysis
|
|
32
|
+
- Aggregate Greeks across multiple positions
|
|
33
|
+
- Risk metrics including gamma exposure and pin risk
|
|
34
|
+
- Greeks breakdown by underlying asset and expiry
|
|
35
|
+
|
|
36
|
+
## ๐ฐ Coin-Settled Options Focus
|
|
37
|
+
|
|
38
|
+
**Key Differences from Standard Options:**
|
|
39
|
+
- **Premium**: Paid in cryptocurrency (e.g., 0.1 BTC for a $120K BTC option)
|
|
40
|
+
- **Settlement**: Payoff delivered in cryptocurrency
|
|
41
|
+
- **Pricing**: Uses Black-76 model with forward prices
|
|
42
|
+
- **Greeks**: Adjusted for cryptocurrency denomination
|
|
43
|
+
- **Breakeven**: Calculated in USD terms but based on crypto premium
|
|
44
|
+
|
|
45
|
+
## ๐ Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install .
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## ๐ฏ Quick Start
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from crypto_bs import price_option, delta, gamma, breakeven_price, breakeven_price_coin_based
|
|
55
|
+
|
|
56
|
+
# Basic Black-76 pricing for coin-settled options
|
|
57
|
+
price = price_option(F=110000, K=105000, T=1/365, sigma=0.6, option_type='call')
|
|
58
|
+
print(f"Option Price: {price:.6f} BTC")
|
|
59
|
+
|
|
60
|
+
# Calculate Greeks
|
|
61
|
+
d = delta(110000, 105000, 1/365, 0.6, 'call')
|
|
62
|
+
g = gamma(110000, 105000, 1/365, 0.6)
|
|
63
|
+
|
|
64
|
+
# Breakeven analysis (USD premium)
|
|
65
|
+
be_usd = breakeven_price(105000, 500, 'call')
|
|
66
|
+
print(f"Breakeven (USD premium): ${be_usd:.2f}")
|
|
67
|
+
|
|
68
|
+
# Breakeven analysis (coin-settled premium)
|
|
69
|
+
be_coin = breakeven_price_coin_based(105000, price, 'call')
|
|
70
|
+
print(f"Breakeven (coin-based): ${be_coin:.2f}")
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## ๐ง Advanced Usage
|
|
74
|
+
|
|
75
|
+
### Coin-Based Pricing with Full Analysis
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from crypto_bs import BlackScholesModel, OptionParameters, OptionType
|
|
79
|
+
|
|
80
|
+
# Advanced pricing with coin-based support
|
|
81
|
+
bs_model = BlackScholesModel()
|
|
82
|
+
params = OptionParameters(
|
|
83
|
+
spot_price=110000,
|
|
84
|
+
strike_price=105000,
|
|
85
|
+
time_to_maturity=1/365,
|
|
86
|
+
volatility=0.6,
|
|
87
|
+
option_type=OptionType.CALL,
|
|
88
|
+
is_coin_based=True # Key for coin-settled options
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
result = bs_model.calculate_option_price(params)
|
|
92
|
+
print(f"Coin Price: {result.coin_based_price:.6f} BTC")
|
|
93
|
+
print(f"USD Equivalent: ${result.usd_price:.2f}")
|
|
94
|
+
print(f"Delta: {result.delta:.6f}")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Portfolio Risk Analysis
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from crypto_bs import analyze_portfolio_risk
|
|
101
|
+
|
|
102
|
+
portfolio = [
|
|
103
|
+
{
|
|
104
|
+
'quantity': 10,
|
|
105
|
+
'spot_price': 110000,
|
|
106
|
+
'strike_price': 105000,
|
|
107
|
+
'time_to_maturity': 1/365,
|
|
108
|
+
'volatility': 0.6,
|
|
109
|
+
'option_type': 'call',
|
|
110
|
+
'underlying': 'BTC',
|
|
111
|
+
'is_coin_based': True
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
|
|
115
|
+
risk_analysis = analyze_portfolio_risk(portfolio)
|
|
116
|
+
print("Portfolio Delta:", risk_analysis['portfolio_summary']['total_delta'])
|
|
117
|
+
print("Gamma Exposure:", risk_analysis['risk_metrics']['gamma_exposure'])
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Real-Time Deribit Integration
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from crypto_bs import get_btc_forward_price, get_option_data, validate_deribit_pricing
|
|
124
|
+
|
|
125
|
+
# Get live data
|
|
126
|
+
F = get_btc_forward_price()
|
|
127
|
+
option_data = get_option_data('BTC-3SEP25-105000-C')
|
|
128
|
+
|
|
129
|
+
# Validate model against exchange
|
|
130
|
+
validation = validate_deribit_pricing(
|
|
131
|
+
deribit_price_btc=option_data['mark_price'],
|
|
132
|
+
spot=F,
|
|
133
|
+
strike=105000,
|
|
134
|
+
time_to_maturity=1/365,
|
|
135
|
+
option_type='call'
|
|
136
|
+
)
|
|
137
|
+
print(f"Model vs Exchange difference: {validation['price_difference_btc']:.6f} BTC")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Breakeven for Coin-Settled Options
|
|
141
|
+
|
|
142
|
+
For coin-settled options where the premium is paid in coin units (e.g., BTC):
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from crypto_bs import breakeven_price_coin_based
|
|
146
|
+
|
|
147
|
+
K = 105000
|
|
148
|
+
premium_btc = 0.0123 # premium in BTC
|
|
149
|
+
|
|
150
|
+
be = breakeven_price_coin_based(K, premium_btc, 'call')
|
|
151
|
+
print(f"Coin-based breakeven: ${be:.2f}")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
## ๐ API Reference
|
|
156
|
+
|
|
157
|
+
### Core Functions
|
|
158
|
+
- `price_option(F, K, T, sigma, option_type)` - Basic Black-76 pricing
|
|
159
|
+
- `delta(F, K, T, sigma, option_type)` - Option delta
|
|
160
|
+
- `gamma(F, K, T, sigma)` - Option gamma
|
|
161
|
+
- `vega(F, K, T, sigma)` - Option vega
|
|
162
|
+
- `theta(F, K, T, sigma, option_type)` - Option theta
|
|
163
|
+
- `breakeven_price(K, premium, option_type)` - Breakeven calculation
|
|
164
|
+
|
|
165
|
+
### Advanced Classes
|
|
166
|
+
- `BlackScholesModel` - Advanced pricing with coin-based support
|
|
167
|
+
- `GreeksCalculator` - Portfolio-level Greeks and risk analysis
|
|
168
|
+
- `OptionParameters` - Structured option parameters
|
|
169
|
+
- `PortfolioGreeks` - Portfolio Greeks aggregation
|
|
170
|
+
|
|
171
|
+
### Data Integration
|
|
172
|
+
- `get_btc_forward_price()` - BTC perpetual price from Deribit
|
|
173
|
+
- `get_option_data(instrument)` - Option data from Deribit
|
|
174
|
+
- `get_available_instruments()` - List available options
|
|
175
|
+
|
|
176
|
+
## โ
Validation Results
|
|
177
|
+
|
|
178
|
+
**Real Deribit Data Test (Sept 2025):**
|
|
179
|
+
- **Exchange Price**: 0.0535 BTC
|
|
180
|
+
- **Model Price**: 0.0542 BTC
|
|
181
|
+
- **Difference**: 0.0007 BTC (1.3% relative difference)
|
|
182
|
+
- **Implied Volatility Match**: Within market expectations
|
|
183
|
+
|
|
184
|
+
## ๐ฏ Use Cases
|
|
185
|
+
|
|
186
|
+
- **Crypto Options Trading**: Price and risk-manage BTC/ETH options
|
|
187
|
+
- **Portfolio Hedging**: Calculate Greeks for complex option portfolios
|
|
188
|
+
- **Risk Analysis**: Assess gamma exposure and pin risk
|
|
189
|
+
- **Model Validation**: Compare theoretical prices with exchange data
|
|
190
|
+
- **Breakeven Analysis**: Determine profitable exercise points
|
|
191
|
+
|
|
192
|
+
## ๐งช Testing
|
|
193
|
+
|
|
194
|
+
Run the test suite using pytest:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
# Using pytest (recommended)
|
|
198
|
+
pytest tests/ -v
|
|
199
|
+
|
|
200
|
+
# Or using the test runner script
|
|
201
|
+
python run_tests.py
|
|
202
|
+
|
|
203
|
+
# Or run tests directly
|
|
204
|
+
python tests/test_pricing.py
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
All 16 tests should pass, covering:
|
|
208
|
+
- Basic Black-76 pricing
|
|
209
|
+
- Greeks calculations
|
|
210
|
+
- Coin-based pricing
|
|
211
|
+
- Advanced portfolio analysis
|
|
212
|
+
- Breakeven calculations
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
**Built for cryptocurrency options traders who need accurate, coin-settled pricing models.**
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from .pricing import price_option
|
|
2
|
+
from .greeks import delta, gamma, vega, theta, rho
|
|
3
|
+
from .utils import breakeven_price, breakeven_price_coin_based
|
|
4
|
+
from .data_fetch import get_btc_forward_price, get_option_data, get_available_instruments, get_btc_price, get_btc_volatility
|
|
5
|
+
|
|
6
|
+
# Advanced implementations
|
|
7
|
+
from .black_scholes import (
|
|
8
|
+
BlackScholesModel, Black76Model, OptionParameters, OptionPricing,
|
|
9
|
+
OptionType, PricingModel, price_coin_based_option, validate_deribit_pricing
|
|
10
|
+
)
|
|
11
|
+
from .greeks_calculator import (
|
|
12
|
+
GreeksCalculator, GreeksProfile, PortfolioGreeks, RiskMetrics,
|
|
13
|
+
calculate_option_greeks, analyze_portfolio_risk
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
# Basic functions
|
|
18
|
+
'price_option', 'delta', 'gamma', 'vega', 'theta', 'rho', 'breakeven_price',
|
|
19
|
+
'breakeven_price_coin_based',
|
|
20
|
+
'get_btc_forward_price', 'get_option_data', 'get_available_instruments',
|
|
21
|
+
'get_btc_price', 'get_btc_volatility',
|
|
22
|
+
|
|
23
|
+
# Advanced classes
|
|
24
|
+
'BlackScholesModel', 'Black76Model', 'OptionParameters', 'OptionPricing',
|
|
25
|
+
'OptionType', 'PricingModel', 'GreeksCalculator', 'GreeksProfile',
|
|
26
|
+
'PortfolioGreeks', 'RiskMetrics',
|
|
27
|
+
|
|
28
|
+
# Advanced functions
|
|
29
|
+
'price_coin_based_option', 'validate_deribit_pricing',
|
|
30
|
+
'calculate_option_greeks', 'analyze_portfolio_risk'
|
|
31
|
+
]
|