pyoptionpricing 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.
- pyoptionpricing-0.1.0/LICENSE +21 -0
- pyoptionpricing-0.1.0/PKG-INFO +295 -0
- pyoptionpricing-0.1.0/README.md +257 -0
- pyoptionpricing-0.1.0/pyoptionpricing/__init__.py +23 -0
- pyoptionpricing-0.1.0/pyoptionpricing/american.py +4 -0
- pyoptionpricing-0.1.0/pyoptionpricing/analysis.py +112 -0
- pyoptionpricing-0.1.0/pyoptionpricing/asian.py +20 -0
- pyoptionpricing-0.1.0/pyoptionpricing/barrier.py +83 -0
- pyoptionpricing-0.1.0/pyoptionpricing/base.py +66 -0
- pyoptionpricing-0.1.0/pyoptionpricing/binomial.py +115 -0
- pyoptionpricing-0.1.0/pyoptionpricing/black_scholes.py +54 -0
- pyoptionpricing-0.1.0/pyoptionpricing/engine.py +31 -0
- pyoptionpricing-0.1.0/pyoptionpricing/european.py +4 -0
- pyoptionpricing-0.1.0/pyoptionpricing/factory.py +61 -0
- pyoptionpricing-0.1.0/pyoptionpricing/implied_vol.py +140 -0
- pyoptionpricing-0.1.0/pyoptionpricing/monte_carlo.py +71 -0
- pyoptionpricing-0.1.0/pyoptionpricing/path_dependent.py +77 -0
- pyoptionpricing-0.1.0/pyoptionpricing/paths.py +36 -0
- pyoptionpricing-0.1.0/pyoptionpricing.egg-info/PKG-INFO +295 -0
- pyoptionpricing-0.1.0/pyoptionpricing.egg-info/SOURCES.txt +34 -0
- pyoptionpricing-0.1.0/pyoptionpricing.egg-info/dependency_links.txt +1 -0
- pyoptionpricing-0.1.0/pyoptionpricing.egg-info/requires.txt +15 -0
- pyoptionpricing-0.1.0/pyoptionpricing.egg-info/top_level.txt +1 -0
- pyoptionpricing-0.1.0/pyproject.toml +62 -0
- pyoptionpricing-0.1.0/setup.cfg +4 -0
- pyoptionpricing-0.1.0/tests/test_analysis.py +87 -0
- pyoptionpricing-0.1.0/tests/test_api.py +211 -0
- pyoptionpricing-0.1.0/tests/test_asian.py +65 -0
- pyoptionpricing-0.1.0/tests/test_barrier.py +93 -0
- pyoptionpricing-0.1.0/tests/test_base.py +73 -0
- pyoptionpricing-0.1.0/tests/test_binomial.py +150 -0
- pyoptionpricing-0.1.0/tests/test_black_scholes.py +130 -0
- pyoptionpricing-0.1.0/tests/test_engine.py +51 -0
- pyoptionpricing-0.1.0/tests/test_factory.py +107 -0
- pyoptionpricing-0.1.0/tests/test_implied_vol.py +118 -0
- pyoptionpricing-0.1.0/tests/test_monte_carlo.py +80 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MaadhiniNarayanaKumar
|
|
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,295 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyoptionpricing
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Object-oriented option pricing: Black-Scholes, binomial tree and Monte Carlo behind one interface, with Asian and barrier options and implied volatility.
|
|
5
|
+
Author: Maadhini Narayanakumar
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/MaadhiniNarayanaKumar/pyoptionpricing
|
|
8
|
+
Project-URL: Demo, https://pyoptionpricingui.vercel.app
|
|
9
|
+
Project-URL: API docs, https://pyoptionpricing.vercel.app/docs
|
|
10
|
+
Keywords: options,derivatives,black-scholes,binomial-tree,monte-carlo,implied-volatility,quantitative-finance
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: numpy>=1.24
|
|
25
|
+
Requires-Dist: scipy>=1.10
|
|
26
|
+
Provides-Extra: api
|
|
27
|
+
Requires-Dist: fastapi; extra == "api"
|
|
28
|
+
Requires-Dist: pydantic; extra == "api"
|
|
29
|
+
Requires-Dist: uvicorn; extra == "api"
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: fastapi; extra == "dev"
|
|
32
|
+
Requires-Dist: pydantic; extra == "dev"
|
|
33
|
+
Requires-Dist: uvicorn; extra == "dev"
|
|
34
|
+
Requires-Dist: httpx; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest; extra == "dev"
|
|
36
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
37
|
+
Dynamic: license-file
|
|
38
|
+
|
|
39
|
+
# OptionPricing
|
|
40
|
+
|
|
41
|
+
An object-oriented Python library and web platform for interactive option valuation and Greeks analysis.
|
|
42
|
+
|
|
43
|
+
**Course:** Object-Oriented Programming II (22:839:615:40), Master of Quantitative Finance, Rutgers University
|
|
44
|
+
**Professor:** Dr. Hseu-Ming Chen
|
|
45
|
+
**Author:** Maadhini Narayanakumar
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Overview
|
|
50
|
+
|
|
51
|
+
**Live demo:** https://pyoptionpricingui.vercel.app · **API docs:** https://pyoptionpricing.vercel.app/docs
|
|
52
|
+
|
|
53
|
+
`pyoptionpricing` is a modular Python library that prices vanilla and path-dependent options through a single, model-independent interface. Client code can price any option with `price()` and `greeks()` without knowing whether the underlying method is Black-Scholes, a Binomial Tree or Monte Carlo.
|
|
54
|
+
|
|
55
|
+
The library is exposed through a FastAPI backend and explored through a Next.js frontend, where you can enter parameters once, compare pricing models side by side, and visualize Greeks, payoffs, convergence and implied volatility.
|
|
56
|
+
|
|
57
|
+
> Live market data feeds are out of scope. The volatility surface is built from synthetic market prices.
|
|
58
|
+
|
|
59
|
+
## Features
|
|
60
|
+
|
|
61
|
+
- Black-Scholes pricing with analytical Greeks (Delta, Gamma, Theta, Vega, Rho) for European calls and puts
|
|
62
|
+
- Binomial Tree (CRR) pricing for European and American options
|
|
63
|
+
- Monte Carlo pricing for European options, with standard error estimation
|
|
64
|
+
- Monte Carlo pricing for path-dependent options (Asian and Barrier)
|
|
65
|
+
- Implied volatility via root finding (Brent's method), working across all pricing models
|
|
66
|
+
- Implied volatility smile/surface across strikes
|
|
67
|
+
- Side-by-side model comparison (Black-Scholes vs Binomial vs Monte Carlo)
|
|
68
|
+
- American vs European price comparison
|
|
69
|
+
- Binomial convergence chart against the Black-Scholes price
|
|
70
|
+
- Payoff diagrams and simulated price path visualization
|
|
71
|
+
- Input validation with meaningful error messages
|
|
72
|
+
- Swagger/OpenAPI documentation
|
|
73
|
+
|
|
74
|
+
## OOP Design
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
Option (abstract)
|
|
78
|
+
├── EuropeanOption (abstract)
|
|
79
|
+
│ ├── BlackScholesOption
|
|
80
|
+
│ ├── BinomialEuropeanOption
|
|
81
|
+
│ └── MonteCarloEuropeanOption
|
|
82
|
+
├── AmericanOption (abstract)
|
|
83
|
+
│ └── BinomialAmericanOption
|
|
84
|
+
└── PathDependentOption (abstract)
|
|
85
|
+
├── AsianOption
|
|
86
|
+
└── BarrierOption
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| Principle | How it is demonstrated |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| Abstraction | `Option` and its abstract subclasses define a common `price()` / `greeks()` interface and hide implementation details |
|
|
92
|
+
| Inheritance | Concrete option classes extend the shared behavior of their parents |
|
|
93
|
+
| Polymorphism | `PricingEngine` prices any `Option` object through the same interface |
|
|
94
|
+
| Encapsulation | Tree nodes, probabilities, simulated paths and intermediate math are internal to each class |
|
|
95
|
+
| Factory pattern | `OptionFactory` builds the right option object from model, exercise style, option type and extra parameters (e.g. barrier level) |
|
|
96
|
+
|
|
97
|
+
## Tech Stack
|
|
98
|
+
|
|
99
|
+
| Layer | Technology |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| Core library | Python, NumPy, SciPy |
|
|
102
|
+
| Backend | FastAPI, Pydantic, Swagger/OpenAPI |
|
|
103
|
+
| Frontend | Next.js, React, TypeScript, Recharts |
|
|
104
|
+
| Testing | pytest, pytest-cov |
|
|
105
|
+
| Deployment | Vercel (backend and frontend, as two projects) |
|
|
106
|
+
|
|
107
|
+
## Project Structure
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
.
|
|
111
|
+
├── pyoptionpricing/ # Core OOP library
|
|
112
|
+
├── backend/ # FastAPI application
|
|
113
|
+
├── frontend/ # Next.js application
|
|
114
|
+
├── tests/ # pytest test suite
|
|
115
|
+
├── docs/ # UML and architecture diagrams, report
|
|
116
|
+
├── pyproject.toml
|
|
117
|
+
└── README.md
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Getting Started
|
|
121
|
+
|
|
122
|
+
### Install the package
|
|
123
|
+
|
|
124
|
+
pip install git+https://github.com/MaadhiniNarayanaKumar/pyoptionpricing.git
|
|
125
|
+
|
|
126
|
+
To run the API as well: `pip install "pyoptionpricing[api] @ git+https://github.com/MaadhiniNarayanaKumar/pyoptionpricing.git"`
|
|
127
|
+
|
|
128
|
+
### Prerequisites
|
|
129
|
+
|
|
130
|
+
- Python 3.9+
|
|
131
|
+
- Node.js 18+
|
|
132
|
+
- Git
|
|
133
|
+
|
|
134
|
+
### 1. Clone the repository
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
git clone https://github.com/<your-username>/<your-repo>.git
|
|
138
|
+
cd <your-repo>
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 2. Set up the Python environment
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
python -m venv .venv
|
|
145
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
146
|
+
pip install -e ".[dev]" # or: pip install -r requirements-dev.txt
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The library itself depends only on NumPy and SciPy, so `pip install .` is enough to use it from Python. The `api` extra (`pip install ".[api]"`) adds FastAPI, Pydantic and Uvicorn for the backend, and `dev` adds the test tools.
|
|
150
|
+
|
|
151
|
+
### 3. Run the backend
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
uvicorn backend.main:app --reload
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
- API: http://localhost:8000
|
|
158
|
+
- Swagger docs: http://localhost:8000/docs
|
|
159
|
+
|
|
160
|
+
### 4. Run the frontend
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
cd frontend
|
|
164
|
+
npm install
|
|
165
|
+
cp .env.example .env.local # set NEXT_PUBLIC_API_URL=http://localhost:8000
|
|
166
|
+
npm run dev
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- App: http://localhost:3000
|
|
170
|
+
|
|
171
|
+
## Usage Example
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from pyoptionpricing import OptionFactory, PricingEngine
|
|
175
|
+
|
|
176
|
+
option = OptionFactory.create(
|
|
177
|
+
model="black_scholes",
|
|
178
|
+
style="european",
|
|
179
|
+
option_type="call",
|
|
180
|
+
spot=100, strike=100, maturity=1.0, rate=0.05, volatility=0.20,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
engine = PricingEngine(option)
|
|
184
|
+
print(engine.price()) # ~10.4506
|
|
185
|
+
print(engine.greeks()) # delta, gamma, theta, vega, rho
|
|
186
|
+
print(engine.run()) # model name, price, Greeks (+ standard error for Monte Carlo)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Swapping `model="binomial"` or `model="monte_carlo"` requires no other change to the client code.
|
|
190
|
+
|
|
191
|
+
### Demo script
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
python demo.py
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
This walks through one example per feature: Black-Scholes and parity, validation, every model through the factory and engine, binomial convergence, American vs European, Monte Carlo error, Asian and Barrier, implied volatility, and the smile.
|
|
198
|
+
|
|
199
|
+
## API Endpoints
|
|
200
|
+
|
|
201
|
+
| Method | Endpoint | Description |
|
|
202
|
+
| --- | --- | --- |
|
|
203
|
+
| POST | `/price` | Price an option with the chosen model (plus standard error for simulations) |
|
|
204
|
+
| POST | `/greeks` | Compute Greeks |
|
|
205
|
+
| POST | `/compare` | Price the same contract with every model side by side |
|
|
206
|
+
| POST | `/convergence` | Binomial price by tree depth and Monte Carlo price by path count, against Black-Scholes |
|
|
207
|
+
| POST | `/payoff` | Payoff and profit at maturity across terminal spot prices |
|
|
208
|
+
| POST | `/paths` | Sample simulated GBM price paths |
|
|
209
|
+
| POST | `/implied-vol` | Implied volatility from a market price, with any model |
|
|
210
|
+
| POST | `/vol-surface` | Implied volatility across strikes and maturities from synthetic skewed prices |
|
|
211
|
+
| GET | `/models` | Supported (model, style) pairs and the extra inputs each needs |
|
|
212
|
+
| GET | `/health` | Health check |
|
|
213
|
+
|
|
214
|
+
Every request takes `spot`, `strike`, `maturity`, `rate`, `volatility`, `option_type`, `model` and `style`. Model-specific inputs are optional (`steps`; `n_paths`, `n_steps`, `seed`; `barrier`, `barrier_type`) and are ignored by models that do not use them. Invalid inputs return HTTP 422 with a readable message.
|
|
215
|
+
|
|
216
|
+
Full request and response schemas are available in Swagger at `/docs`. Allowed frontend origins can be set with `FRONTEND_ORIGINS` (comma-separated, default `http://localhost:3000`).
|
|
217
|
+
|
|
218
|
+
## Testing
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
pytest --cov=pyoptionpricing --cov=backend --cov-report=term-missing
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
The suite covers the library and every API endpoint. Current coverage is about 99%.
|
|
225
|
+
|
|
226
|
+
Correctness checks include:
|
|
227
|
+
|
|
228
|
+
- Put-call parity
|
|
229
|
+
- Binomial Tree convergence to Black-Scholes
|
|
230
|
+
- Greeks validated against finite-difference bumps
|
|
231
|
+
- Monte Carlo prices within standard error of Black-Scholes
|
|
232
|
+
- American put price at least the European put price
|
|
233
|
+
- Barrier knock-in plus knock-out equals the vanilla price
|
|
234
|
+
- Asian call price at most the European call price
|
|
235
|
+
|
|
236
|
+
Target coverage: 80%+
|
|
237
|
+
|
|
238
|
+
## Documentation
|
|
239
|
+
|
|
240
|
+
- [UML class diagram](docs/uml.md)
|
|
241
|
+
- [Architecture and request flow](docs/architecture.md)
|
|
242
|
+
- [Final report](docs/report.md): OOP principles mapped to classes and files, results, challenges
|
|
243
|
+
|
|
244
|
+
## Deployment
|
|
245
|
+
|
|
246
|
+
Both parts run on Vercel as two separate projects from this one repository.
|
|
247
|
+
|
|
248
|
+
### Backend (FastAPI on Vercel)
|
|
249
|
+
|
|
250
|
+
Create a Vercel project from the repository with **Root Directory** left at the repository root and **Framework Preset** set to FastAPI (or Other). No build command is needed.
|
|
251
|
+
|
|
252
|
+
- `pyproject.toml` sets `[tool.vercel] entrypoint = "backend.main:app"`. Vercel installs from `pyproject.toml` and ignores `requirements.txt` when both exist, and its `uv sync --no-dev` skips optional extras. So the API packages are also listed in the `api` dependency group, which `[tool.uv] default-groups` installs. Keep that group in step with the `api` extra.
|
|
253
|
+
- `vercel.json` sets the function timeout and leaves the frontend, tests and docs out of the bundle.
|
|
254
|
+
- Set the environment variable `FRONTEND_ORIGINS` to the frontend's URL (comma-separated for more than one), for example `https://your-frontend.vercel.app`. Redeploy after changing it.
|
|
255
|
+
- Vercel gives each deployment its own URL, so you can also set `FRONTEND_ORIGIN_REGEX` to a pattern that matches all of them, for example `https://pyoptionpricing-.*-maadhinis-projects\.vercel\.app`.
|
|
256
|
+
|
|
257
|
+
With the Vercel CLI instead of Git:
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
npm i -g vercel
|
|
261
|
+
vercel # preview deployment
|
|
262
|
+
vercel --prod # production deployment
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Check it with `https://<your-backend>.vercel.app/health` and `https://<your-backend>.vercel.app/docs`.
|
|
266
|
+
|
|
267
|
+
### Frontend (Next.js on Vercel)
|
|
268
|
+
|
|
269
|
+
Create a second project with **Root Directory** set to `frontend`, and set `NEXT_PUBLIC_API_URL` to the backend URL.
|
|
270
|
+
|
|
271
|
+
### Links
|
|
272
|
+
|
|
273
|
+
- **Backend:** https://pyoptionpricing.vercel.app (Swagger docs at https://pyoptionpricing.vercel.app/docs)
|
|
274
|
+
- **Frontend / demo:** https://pyoptionpricingui.vercel.app
|
|
275
|
+
|
|
276
|
+
## Deliverables
|
|
277
|
+
|
|
278
|
+
- `pyoptionpricing` package with the full option hierarchy
|
|
279
|
+
- Black-Scholes, Binomial and Monte Carlo pricing models plus Asian/Barrier options
|
|
280
|
+
- Implied volatility and volatility surface utilities
|
|
281
|
+
- FastAPI backend with Swagger documentation
|
|
282
|
+
- Next.js frontend with convergence and volatility charts
|
|
283
|
+
- Test suite with coverage report
|
|
284
|
+
- UML and system architecture diagrams (`docs/`)
|
|
285
|
+
- Final report
|
|
286
|
+
|
|
287
|
+
## Roadmap
|
|
288
|
+
|
|
289
|
+
- Variance reduction for Monte Carlo (antithetic and control variates)
|
|
290
|
+
- Volatility surface across maturities
|
|
291
|
+
- Scheduled data ingestion and calibration (e.g. Airflow)
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
Released under the MIT License. See [LICENSE](LICENSE). Developed as coursework at Rutgers University.
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# OptionPricing
|
|
2
|
+
|
|
3
|
+
An object-oriented Python library and web platform for interactive option valuation and Greeks analysis.
|
|
4
|
+
|
|
5
|
+
**Course:** Object-Oriented Programming II (22:839:615:40), Master of Quantitative Finance, Rutgers University
|
|
6
|
+
**Professor:** Dr. Hseu-Ming Chen
|
|
7
|
+
**Author:** Maadhini Narayanakumar
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
**Live demo:** https://pyoptionpricingui.vercel.app · **API docs:** https://pyoptionpricing.vercel.app/docs
|
|
14
|
+
|
|
15
|
+
`pyoptionpricing` is a modular Python library that prices vanilla and path-dependent options through a single, model-independent interface. Client code can price any option with `price()` and `greeks()` without knowing whether the underlying method is Black-Scholes, a Binomial Tree or Monte Carlo.
|
|
16
|
+
|
|
17
|
+
The library is exposed through a FastAPI backend and explored through a Next.js frontend, where you can enter parameters once, compare pricing models side by side, and visualize Greeks, payoffs, convergence and implied volatility.
|
|
18
|
+
|
|
19
|
+
> Live market data feeds are out of scope. The volatility surface is built from synthetic market prices.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- Black-Scholes pricing with analytical Greeks (Delta, Gamma, Theta, Vega, Rho) for European calls and puts
|
|
24
|
+
- Binomial Tree (CRR) pricing for European and American options
|
|
25
|
+
- Monte Carlo pricing for European options, with standard error estimation
|
|
26
|
+
- Monte Carlo pricing for path-dependent options (Asian and Barrier)
|
|
27
|
+
- Implied volatility via root finding (Brent's method), working across all pricing models
|
|
28
|
+
- Implied volatility smile/surface across strikes
|
|
29
|
+
- Side-by-side model comparison (Black-Scholes vs Binomial vs Monte Carlo)
|
|
30
|
+
- American vs European price comparison
|
|
31
|
+
- Binomial convergence chart against the Black-Scholes price
|
|
32
|
+
- Payoff diagrams and simulated price path visualization
|
|
33
|
+
- Input validation with meaningful error messages
|
|
34
|
+
- Swagger/OpenAPI documentation
|
|
35
|
+
|
|
36
|
+
## OOP Design
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
Option (abstract)
|
|
40
|
+
├── EuropeanOption (abstract)
|
|
41
|
+
│ ├── BlackScholesOption
|
|
42
|
+
│ ├── BinomialEuropeanOption
|
|
43
|
+
│ └── MonteCarloEuropeanOption
|
|
44
|
+
├── AmericanOption (abstract)
|
|
45
|
+
│ └── BinomialAmericanOption
|
|
46
|
+
└── PathDependentOption (abstract)
|
|
47
|
+
├── AsianOption
|
|
48
|
+
└── BarrierOption
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
| Principle | How it is demonstrated |
|
|
52
|
+
| --- | --- |
|
|
53
|
+
| Abstraction | `Option` and its abstract subclasses define a common `price()` / `greeks()` interface and hide implementation details |
|
|
54
|
+
| Inheritance | Concrete option classes extend the shared behavior of their parents |
|
|
55
|
+
| Polymorphism | `PricingEngine` prices any `Option` object through the same interface |
|
|
56
|
+
| Encapsulation | Tree nodes, probabilities, simulated paths and intermediate math are internal to each class |
|
|
57
|
+
| Factory pattern | `OptionFactory` builds the right option object from model, exercise style, option type and extra parameters (e.g. barrier level) |
|
|
58
|
+
|
|
59
|
+
## Tech Stack
|
|
60
|
+
|
|
61
|
+
| Layer | Technology |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| Core library | Python, NumPy, SciPy |
|
|
64
|
+
| Backend | FastAPI, Pydantic, Swagger/OpenAPI |
|
|
65
|
+
| Frontend | Next.js, React, TypeScript, Recharts |
|
|
66
|
+
| Testing | pytest, pytest-cov |
|
|
67
|
+
| Deployment | Vercel (backend and frontend, as two projects) |
|
|
68
|
+
|
|
69
|
+
## Project Structure
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
.
|
|
73
|
+
├── pyoptionpricing/ # Core OOP library
|
|
74
|
+
├── backend/ # FastAPI application
|
|
75
|
+
├── frontend/ # Next.js application
|
|
76
|
+
├── tests/ # pytest test suite
|
|
77
|
+
├── docs/ # UML and architecture diagrams, report
|
|
78
|
+
├── pyproject.toml
|
|
79
|
+
└── README.md
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Getting Started
|
|
83
|
+
|
|
84
|
+
### Install the package
|
|
85
|
+
|
|
86
|
+
pip install git+https://github.com/MaadhiniNarayanaKumar/pyoptionpricing.git
|
|
87
|
+
|
|
88
|
+
To run the API as well: `pip install "pyoptionpricing[api] @ git+https://github.com/MaadhiniNarayanaKumar/pyoptionpricing.git"`
|
|
89
|
+
|
|
90
|
+
### Prerequisites
|
|
91
|
+
|
|
92
|
+
- Python 3.9+
|
|
93
|
+
- Node.js 18+
|
|
94
|
+
- Git
|
|
95
|
+
|
|
96
|
+
### 1. Clone the repository
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
git clone https://github.com/<your-username>/<your-repo>.git
|
|
100
|
+
cd <your-repo>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 2. Set up the Python environment
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
python -m venv .venv
|
|
107
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
108
|
+
pip install -e ".[dev]" # or: pip install -r requirements-dev.txt
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The library itself depends only on NumPy and SciPy, so `pip install .` is enough to use it from Python. The `api` extra (`pip install ".[api]"`) adds FastAPI, Pydantic and Uvicorn for the backend, and `dev` adds the test tools.
|
|
112
|
+
|
|
113
|
+
### 3. Run the backend
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
uvicorn backend.main:app --reload
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
- API: http://localhost:8000
|
|
120
|
+
- Swagger docs: http://localhost:8000/docs
|
|
121
|
+
|
|
122
|
+
### 4. Run the frontend
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
cd frontend
|
|
126
|
+
npm install
|
|
127
|
+
cp .env.example .env.local # set NEXT_PUBLIC_API_URL=http://localhost:8000
|
|
128
|
+
npm run dev
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
- App: http://localhost:3000
|
|
132
|
+
|
|
133
|
+
## Usage Example
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from pyoptionpricing import OptionFactory, PricingEngine
|
|
137
|
+
|
|
138
|
+
option = OptionFactory.create(
|
|
139
|
+
model="black_scholes",
|
|
140
|
+
style="european",
|
|
141
|
+
option_type="call",
|
|
142
|
+
spot=100, strike=100, maturity=1.0, rate=0.05, volatility=0.20,
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
engine = PricingEngine(option)
|
|
146
|
+
print(engine.price()) # ~10.4506
|
|
147
|
+
print(engine.greeks()) # delta, gamma, theta, vega, rho
|
|
148
|
+
print(engine.run()) # model name, price, Greeks (+ standard error for Monte Carlo)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Swapping `model="binomial"` or `model="monte_carlo"` requires no other change to the client code.
|
|
152
|
+
|
|
153
|
+
### Demo script
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
python demo.py
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
This walks through one example per feature: Black-Scholes and parity, validation, every model through the factory and engine, binomial convergence, American vs European, Monte Carlo error, Asian and Barrier, implied volatility, and the smile.
|
|
160
|
+
|
|
161
|
+
## API Endpoints
|
|
162
|
+
|
|
163
|
+
| Method | Endpoint | Description |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| POST | `/price` | Price an option with the chosen model (plus standard error for simulations) |
|
|
166
|
+
| POST | `/greeks` | Compute Greeks |
|
|
167
|
+
| POST | `/compare` | Price the same contract with every model side by side |
|
|
168
|
+
| POST | `/convergence` | Binomial price by tree depth and Monte Carlo price by path count, against Black-Scholes |
|
|
169
|
+
| POST | `/payoff` | Payoff and profit at maturity across terminal spot prices |
|
|
170
|
+
| POST | `/paths` | Sample simulated GBM price paths |
|
|
171
|
+
| POST | `/implied-vol` | Implied volatility from a market price, with any model |
|
|
172
|
+
| POST | `/vol-surface` | Implied volatility across strikes and maturities from synthetic skewed prices |
|
|
173
|
+
| GET | `/models` | Supported (model, style) pairs and the extra inputs each needs |
|
|
174
|
+
| GET | `/health` | Health check |
|
|
175
|
+
|
|
176
|
+
Every request takes `spot`, `strike`, `maturity`, `rate`, `volatility`, `option_type`, `model` and `style`. Model-specific inputs are optional (`steps`; `n_paths`, `n_steps`, `seed`; `barrier`, `barrier_type`) and are ignored by models that do not use them. Invalid inputs return HTTP 422 with a readable message.
|
|
177
|
+
|
|
178
|
+
Full request and response schemas are available in Swagger at `/docs`. Allowed frontend origins can be set with `FRONTEND_ORIGINS` (comma-separated, default `http://localhost:3000`).
|
|
179
|
+
|
|
180
|
+
## Testing
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
pytest --cov=pyoptionpricing --cov=backend --cov-report=term-missing
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The suite covers the library and every API endpoint. Current coverage is about 99%.
|
|
187
|
+
|
|
188
|
+
Correctness checks include:
|
|
189
|
+
|
|
190
|
+
- Put-call parity
|
|
191
|
+
- Binomial Tree convergence to Black-Scholes
|
|
192
|
+
- Greeks validated against finite-difference bumps
|
|
193
|
+
- Monte Carlo prices within standard error of Black-Scholes
|
|
194
|
+
- American put price at least the European put price
|
|
195
|
+
- Barrier knock-in plus knock-out equals the vanilla price
|
|
196
|
+
- Asian call price at most the European call price
|
|
197
|
+
|
|
198
|
+
Target coverage: 80%+
|
|
199
|
+
|
|
200
|
+
## Documentation
|
|
201
|
+
|
|
202
|
+
- [UML class diagram](docs/uml.md)
|
|
203
|
+
- [Architecture and request flow](docs/architecture.md)
|
|
204
|
+
- [Final report](docs/report.md): OOP principles mapped to classes and files, results, challenges
|
|
205
|
+
|
|
206
|
+
## Deployment
|
|
207
|
+
|
|
208
|
+
Both parts run on Vercel as two separate projects from this one repository.
|
|
209
|
+
|
|
210
|
+
### Backend (FastAPI on Vercel)
|
|
211
|
+
|
|
212
|
+
Create a Vercel project from the repository with **Root Directory** left at the repository root and **Framework Preset** set to FastAPI (or Other). No build command is needed.
|
|
213
|
+
|
|
214
|
+
- `pyproject.toml` sets `[tool.vercel] entrypoint = "backend.main:app"`. Vercel installs from `pyproject.toml` and ignores `requirements.txt` when both exist, and its `uv sync --no-dev` skips optional extras. So the API packages are also listed in the `api` dependency group, which `[tool.uv] default-groups` installs. Keep that group in step with the `api` extra.
|
|
215
|
+
- `vercel.json` sets the function timeout and leaves the frontend, tests and docs out of the bundle.
|
|
216
|
+
- Set the environment variable `FRONTEND_ORIGINS` to the frontend's URL (comma-separated for more than one), for example `https://your-frontend.vercel.app`. Redeploy after changing it.
|
|
217
|
+
- Vercel gives each deployment its own URL, so you can also set `FRONTEND_ORIGIN_REGEX` to a pattern that matches all of them, for example `https://pyoptionpricing-.*-maadhinis-projects\.vercel\.app`.
|
|
218
|
+
|
|
219
|
+
With the Vercel CLI instead of Git:
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
npm i -g vercel
|
|
223
|
+
vercel # preview deployment
|
|
224
|
+
vercel --prod # production deployment
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Check it with `https://<your-backend>.vercel.app/health` and `https://<your-backend>.vercel.app/docs`.
|
|
228
|
+
|
|
229
|
+
### Frontend (Next.js on Vercel)
|
|
230
|
+
|
|
231
|
+
Create a second project with **Root Directory** set to `frontend`, and set `NEXT_PUBLIC_API_URL` to the backend URL.
|
|
232
|
+
|
|
233
|
+
### Links
|
|
234
|
+
|
|
235
|
+
- **Backend:** https://pyoptionpricing.vercel.app (Swagger docs at https://pyoptionpricing.vercel.app/docs)
|
|
236
|
+
- **Frontend / demo:** https://pyoptionpricingui.vercel.app
|
|
237
|
+
|
|
238
|
+
## Deliverables
|
|
239
|
+
|
|
240
|
+
- `pyoptionpricing` package with the full option hierarchy
|
|
241
|
+
- Black-Scholes, Binomial and Monte Carlo pricing models plus Asian/Barrier options
|
|
242
|
+
- Implied volatility and volatility surface utilities
|
|
243
|
+
- FastAPI backend with Swagger documentation
|
|
244
|
+
- Next.js frontend with convergence and volatility charts
|
|
245
|
+
- Test suite with coverage report
|
|
246
|
+
- UML and system architecture diagrams (`docs/`)
|
|
247
|
+
- Final report
|
|
248
|
+
|
|
249
|
+
## Roadmap
|
|
250
|
+
|
|
251
|
+
- Variance reduction for Monte Carlo (antithetic and control variates)
|
|
252
|
+
- Volatility surface across maturities
|
|
253
|
+
- Scheduled data ingestion and calibration (e.g. Airflow)
|
|
254
|
+
|
|
255
|
+
## License
|
|
256
|
+
|
|
257
|
+
Released under the MIT License. See [LICENSE](LICENSE). Developed as coursework at Rutgers University.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from .base import Option # abstract base: inputs, validation, price()/greeks()
|
|
2
|
+
from .european import EuropeanOption # exercise only at maturity
|
|
3
|
+
from .american import AmericanOption # exercise any time
|
|
4
|
+
|
|
5
|
+
from .path_dependent import PathDependentOption # payoff depends on price path
|
|
6
|
+
from .black_scholes import BlackScholesOption # closed form price and Greeks
|
|
7
|
+
|
|
8
|
+
from .binomial import BinomialEuropeanOption, BinomialAmericanOption # CRR tree
|
|
9
|
+
from .monte_carlo import MonteCarloEuropeanOption # simulation
|
|
10
|
+
|
|
11
|
+
from .asian import AsianOption # average price payoff
|
|
12
|
+
from .barrier import BarrierOption # barrier-based payoff
|
|
13
|
+
from .factory import OptionFactory # creates an option from model/style/type
|
|
14
|
+
from .engine import PricingEngine # prices any Option through one interface
|
|
15
|
+
from .implied_vol import implied_volatility, volatility_smile # vol from market price; smile
|
|
16
|
+
|
|
17
|
+
from .analysis import (
|
|
18
|
+
binomial_convergence, # tree price vs number of steps
|
|
19
|
+
monte_carlo_convergence, # mc price vs number of paths
|
|
20
|
+
payoff_curve, # payoff vs spot price at maturity
|
|
21
|
+
sample_paths, # sample paths
|
|
22
|
+
volatility_surface, # volatility surface
|
|
23
|
+
)
|