Metadata-Version: 2.4
Name: crypto_bs
Version: 0.3.0
Summary: Python library for pricing coin-settled crypto options
Author-email: Seyed Mohammad Hossein Fasihi <mhmd.fasihi@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Seyed Mohammad Hossein Fasihi
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
        
Project-URL: Homepage, https://github.com/MhmdFasihi/crypto_black_scholes
Project-URL: Repository, https://github.com/MhmdFasihi/crypto_black_scholes
Project-URL: Issues, https://github.com/MhmdFasihi/crypto_black_scholes/issues
Project-URL: Changelog, https://github.com/MhmdFasihi/crypto_black_scholes/blob/main/CHANGELOG.md
Keywords: black-scholes,black-76,options,greeks,implied-volatility,crypto,bitcoin,deribit,pricing,risk
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Topic :: Office/Business :: Financial :: Investment
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21.0
Requires-Dist: scipy>=1.7.0
Requires-Dist: requests>=2.25.0
Requires-Dist: pandas>=1.3.0
Dynamic: license-file

# Crypto Black-Scholes

**Version 0.3.0** — Python library for pricing **coin-settled** cryptocurrency options with Black-76 and Black-Scholes-style models, Greeks, portfolio aggregation, Deribit-oriented helpers, and historical volatility estimators.

See **[CHANGELOG.md](CHANGELOG.md)** for release notes and breaking changes.

## Features

- **Pricing** — Black-76 (forward / coin premium), enhanced Black-Scholes with USD vs coin-denominated premium
- **Greeks** — Delta (USD vs coin premium), gamma, theta, vega, rho; second-order Greeks (speed, charm, vanna, vomma) via finite differences in `GreeksCalculator`
- **Portfolio** — Multi-position Greeks, risk metrics, gamma exposure profile
- **Data** — Deribit REST helpers (timeouts on HTTP calls)
- **IV** — Implied vol from market price (Brent’s method), intrinsic and bounds checks
- **Vectorized chain pricing** — `price_options_vectorized` for many strikes / expiries at once
- **Breakeven** — USD and coin-settled breakeven helpers
- **Historical volatility** — Close-to-close, Parkinson, Rogers-Satchell, Yang-Zhang estimators

## Model overview

1. **Black-76 (module-level `price_option`)** — Forward `F`, undiscounted coin premium; suited to Deribit-style quoting.
2. **`BlackScholesModel`** — Spot-based Merton BS; **`is_coin_based=True`** for premium as fraction of spot; exposes **`delta_usd`** (hedge delta) and **`delta_coin`** (premium sensitivity to spot).
3. **Portfolio** — Aggregations use **USD delta** per position for `total_delta` and dollar exposures.

## Installation

```bash
pip install -U crypto-bs
```

Import as:

```python
import crypto_bs
```

Requires Python ≥3.10, `numpy`, `scipy`, `pandas`, `requests` (see `pyproject.toml`).

## Upgrading from 0.1.x

- Replace **`result.delta`** with **`result.delta_usd`** (hedging / notional delta) and/or **`result.delta_coin`** (academic / premium-in-coin sensitivity).
- Dicts from **`price_coin_based_option`** / **`validate_deribit_pricing`**: use **`delta_usd`** / **`delta_coin`** instead of **`delta`**.
- **`get_btc_volatility()`** remains unimplemented in `data_fetch`; use `crypto_bs.historical_vol` functions for realized volatility from OHLC data.
- Optional: add **`pytest`** to your environment for the test suite (`pip install pytest`).

## Quick start

```python
from crypto_bs import price_option, delta, gamma, price_options_vectorized
import numpy as np

# Black-76 coin premium
price = price_option(F=110000, K=105000, T=1/365, sigma=0.6, option_type='call')
print(f"Premium (coin): {price:.6f}")

d = delta(110000, 105000, 1/365, 0.6, 'call')
g = gamma(110000, 105000, 1/365, 0.6)

# Whole chain in one call
K = np.array([100000.0, 105000.0, 110000.0])
T = np.full(3, 1 / 365)
sig = np.full(3, 0.6)
types = np.array(["call", "call", "put"])
premiums = price_options_vectorized(110000, K, T, sig, types)
```

### Breakeven

```python
from crypto_bs import breakeven_price, breakeven_price_coin_based

be_usd = breakeven_price(105000, 500, 'call')
be_coin = breakeven_price_coin_based(105000, price, 'call')
```

## Advanced usage

### Coin-based `BlackScholesModel`

```python
from crypto_bs import BlackScholesModel, OptionParameters, OptionType

bs_model = BlackScholesModel()
params = OptionParameters(
    spot_price=110000,
    strike_price=105000,
    time_to_maturity=1/365,
    volatility=0.6,
    option_type=OptionType.CALL,
    is_coin_based=True,
)

result = bs_model.calculate_option_price(params)
print(f"Coin premium: {result.coin_based_price:.6f}")
print(f"USD equivalent: ${result.usd_price:.2f}")
print(f"Delta USD (hedge): {result.delta_usd:.6f}")
print(f"Delta coin premium: {result.delta_coin:.9f}")
```

### Portfolio risk

```python
from crypto_bs import analyze_portfolio_risk

portfolio = [
    {
        "quantity": 10,
        "spot_price": 110000,
        "strike_price": 105000,
        "time_to_maturity": 1/365,
        "volatility": 0.6,
        "option_type": "call",
        "underlying": "BTC",
        "is_coin_based": True,
    }
]

risk = analyze_portfolio_risk(portfolio)
print("Portfolio delta (USD):", risk["portfolio_summary"]["total_delta"])
print("Gamma exposure:", risk["risk_metrics"]["gamma_exposure"])
```

### Deribit helpers

```python
from crypto_bs import get_btc_forward_price, get_option_data, validate_deribit_pricing

F = get_btc_forward_price()
option_data = get_option_data("BTC-3SEP25-105000-C")

validation = validate_deribit_pricing(
    deribit_price_btc=option_data["mark_price"],
    spot=F,
    strike=105000,
    time_to_maturity=1/365,
    option_type="call",
)
print("IV:", validation["implied_volatility"])
print("Delta USD:", validation["delta_usd"])
```

Requests use a **10-second timeout**; failures surface as `requests` exceptions or `ValueError` from the API helpers.

### Historical volatility

```python
import pandas as pd
from crypto_bs import close_to_close_hv, parkinson_hv, yang_zhang_hv

# Example OHLC arrays as pandas Series
close = pd.Series([100, 102, 101, 104, 103, 106, 108])
high = close * 1.01
low = close * 0.99
open_ = close.shift(1).fillna(close.iloc[0])

hv_cc = close_to_close_hv(close, window=5)
hv_parkinson = parkinson_hv(high, low, window=5)
hv_yz = yang_zhang_hv(open_, high, low, close, window=5)
```

## API reference (summary)

| Symbol | Role |
|--------|------|
| `price_option`, `black_76_call`, `black_76_put` | Scalar Black-76 coin premium |
| `price_options_vectorized` | Vectorized chain pricing |
| `delta`, `gamma`, `vega`, `theta`, `rho` | Black-76 Greeks on forward `F`; `rho(..., risk_free_rate=0)` |
| `breakeven_price`, `breakeven_price_coin_based` | Breakeven spot levels |
| `BlackScholesModel`, `OptionParameters`, `OptionPricing` | Full pricing + Greeks; **`OptionPricing.delta_usd` / `delta_coin`** |
| `calculate_implied_volatility` | IV search; default max vol **20.0**; validates vs intrinsic |
| `GreeksCalculator`, `calculate_option_greeks`, `analyze_portfolio_risk` | Profiles and portfolio |
| `get_btc_forward_price`, `get_option_data`, `get_available_instruments`, `get_btc_price` | Market data |
| `get_btc_volatility` | **Not implemented** — raises `NotImplementedError` |
| `close_to_close_hv`, `parkinson_hv`, `rogers_satchell_hv`, `yang_zhang_hv`, `vol_premium` | Historical volatility analytics |

Full signatures and defaults are in the source docstrings.

## Validation (example)

Using real Deribit-style numbers, model vs mark can be on the order of **~1%** in BTC premium depending on mark, forward, and IV; use `validate_deribit_pricing` and live `get_option_data` for your own checks.

## Testing

```bash
# From repo root (recommended)
pytest tests/ -v

# With conda
conda activate crypto-option
cd /path/to/crypto_bs_project
PYTHONPATH=. pytest tests/ -v

# Wrapper script
python run_tests.py
```

The suite includes **29** tests (pricing, Greeks, coin gamma, IV, vectorized parity, historical vol estimators, and parity/data checks). Install **pytest** if it is not already in the environment.

## License and links

- License: see **LICENSE** in the repository.
- Repository: [github.com/MhmdFasihi/crypto_black_scholes](https://github.com/MhmdFasihi/crypto_black_scholes)
- **Changes / migration:** [CHANGELOG.md](CHANGELOG.md)

---

Built for cryptocurrency options workflows that need **coin-settled** premiums, explicit **hedge delta vs coin-premium delta**, and **portfolio** Greeks.
