optionstratlib 0.4.5

OptionStratLib is a comprehensive Rust library for options trading and strategy development across multiple asset classes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<div style="text-align: center;">
<img src="https://raw.githubusercontent.com/joaquinbejar/OptionStratLib/refs/heads/main/doc/images/logo.png" alt="OptionStratLib" style="width: 100%; height: 100%;">
</div>

[![Dual License](https://img.shields.io/badge/license-MIT%20and%20Apache%202.0-blue)](./LICENSE)
[![Crates.io](https://img.shields.io/crates/v/optionstratlib.svg)](https://crates.io/crates/optionstratlib)
[![Downloads](https://img.shields.io/crates/d/optionstratlib.svg)](https://crates.io/crates/optionstratlib)
[![Stars](https://img.shields.io/github/stars/joaquinbejar/OptionStratLib.svg)](https://github.com/joaquinbejar/OptionStratLib/stargazers)
[![Issues](https://img.shields.io/github/issues/joaquinbejar/OptionStratLib.svg)](https://github.com/joaquinbejar/OptionStratLib/issues)
[![PRs](https://img.shields.io/github/issues-pr/joaquinbejar/OptionStratLib.svg)](https://github.com/joaquinbejar/OptionStratLib/pulls)
[![Build Status](https://img.shields.io/github/workflow/status/joaquinbejar/OptionStratLib/CI)](https://github.com/joaquinbejar/OptionStratLib/actions)
[![Coverage](https://img.shields.io/codecov/c/github/joaquinbejar/OptionStratLib)](https://codecov.io/gh/joaquinbejar/OptionStratLib)
[![Dependencies](https://img.shields.io/librariesio/github/joaquinbejar/OptionStratLib)](https://libraries.io/github/joaquinbejar/OptionStratLib)
[![Documentation](https://img.shields.io/badge/docs-latest-blue.svg)](https://docs.rs/optionstratlib)



 # OptionStratLib v0.4.5: Financial Options Library

 ## Table of Contents
 1. [Introduction]#introduction
 2. [Features]#features
 3. [Project Structure]#project-structure
 4. [Setup Instructions]#setup-instructions
 5. [Library Usage]#library-usage
 6. [Usage Examples]#usage-examples
 7. [Testing]#testing
 8. [Contribution and Contact]#contribution-and-contact

 ## Introduction

 OptionStratLib is a comprehensive Rust library for options trading and strategy development across multiple asset classes. This versatile toolkit enables traders, quants, and developers to:

 ## Features

 1. **Valuation Models**:
 - Black-Scholes model
 - Binomial model
 - Monte Carlo simulations
 - Telegraph process model

 2. **Greeks Calculation**:
 - Delta, gamma, theta, vega, and rho
 - Custom Greeks implementation
 - Greeks visualization

 3. **Option Types**:
 - European and American options
 - Calls and puts
 - Support for exotic options (Asian, Barrier, etc.)

 4. **Volatility Models**:
 - Constant volatility
 - EWMA (Exponentially Weighted Moving Average)
 - GARCH implementation
 - Heston stochastic volatility
 - Volatility surface interpolation

 5. **Option Chain Management**:
 - Chain construction and analysis
 - Strike price generation
 - Chain data import/export (CSV/JSON)

 6. **Trading Strategies**:
 - Bull Call Spread
 - Bear Put Spread
 - Call Butterfly
 - Strategy optimization
 - Custom strategy development

 7. **Risk Management**:
 - SPAN margin calculation
 - Position tracking
 - Break-even analysis
 - Profit/Loss calculations

 8. **Simulation Tools**:
 - Random Walk simulation
 - Telegraph process
 - Monte Carlo methods
 - Custom simulation frameworks

 9. **Visualization**:
 - Strategy payoff diagrams
 - Greeks visualization
 - Binomial trees
 - Risk profiles
 - Interactive charts

 10. **Data Management**:
 - CSV/JSON import/export
 - Option chain data handling
 - Historical data analysis
 - Price series management

 11. **Backtesting**: **TODO!**
 12. **Performance Metrics**: **TODO!**


 ## Project Structure

 The project is organized into the following key modules:

 1. **Core Options** (`options/`):
 - `option.rs`: Core option structures and methods
 - `position.rs`: Position management
 - `chain.rs`: Option chain handling

 2. **Pricing Models** (`pricing/`):
 - `binomial_model.rs`: Binomial tree implementation
 - `black_scholes_model.rs`: Black-Scholes pricing
 - `monte_carlo.rs`: Monte Carlo simulations
 - `telegraph.rs`: Telegraph process model

 3. **Greeks** (`greeks/`):
 - `equations.rs`: Greeks calculations
 - `utils.rs`: Greek utilities

 4. **Volatility** (`volatility/`):
 - `constant.rs`: Constant volatility model
 - `ewma.rs`: EWMA implementation
 - `garch.rs`: GARCH model
 - `heston.rs`: Heston model
 - `surface.rs`: Volatility surface handling

 5. **Strategies** (`strategies/`):
 - `base.rs`: Strategy base traits
 - `bear_put_spread.rs`:
 - `bull_call_spread.rs`:
 - `butterfly_spread.rs`:
 - `call_butterfly.rs`:
 - `collar.rs`:
 - `covered_call.rs`:
 - `custom.rs`: Custom strategy framework
 - `iron_condor.rs`:
 - `poor_mans_covered_call.rs`:
 - `protective_put.rs`:
 - `straddle.rs`:
 - `strangle.rs`:
 - `utils.rs`: Strategy utilities

 6. **Risk Management** (`risk/`):
 - `span.rs`: SPAN margin calculation
 - `margin.rs`: Margin requirements
 - `position.rs`: Position risk metrics

 7. **Simulation** (`simulation/`):
 - `random_walk.rs`
 - `telegraph.rs`
 - `monte_carlo.rs`

 8. **Visualization** (`visualization/`):
 - `binomial_tree.rs`
 - `strategy.rs`
 - `utils.rs`

 9. **Data Management** (`data/`):
 - `chain.rs`: Chain data structures
 - `import.rs`: Data import utilities
 - `export.rs`: Data export utilities

 ## Relationships

 ### Base Structure
 ```mermaid
 classDiagram
 class Options {
 +option_type: OptionType
 +side: Side
 +underlying_symbol: String
 +strike_price: Positive
 +expiration_date: ExpirationDate
 +implied_volatility: Positive
 +quantity: Positive
 +underlying_price: Positive
 +risk_free_rate: Decimal
 +option_style: OptionStyle
 +dividend_yield: Positive
 +exotic_params: Option~ExoticParams~
 +calculate_price_black_scholes()
 +calculate_price_binomial()
 +time_to_expiration()
 +is_long()
 +is_short()
 +validate()
 }

 class OptionType {
 <<enumeration>>
 European
 American
 Bermuda
 Asian
 Barrier
 Binary
 Lookback
 Compound
 Chooser
 Cliquet
 Rainbow
 Spread
 Quanto
 Exchange
 Power
 }

 class Side {
 <<enumeration>>
 Long
 Short
 }

 class OptionStyle {
 <<enumeration>>
 Call
 Put
 }

 class Position {
 +option: Options
 +premium: f64
 +date: DateTime
 +open_fee: f64
 +close_fee: f64
 +total_cost()
 +unrealized_pnl()
 +days_held()
 +days_to_expiration()
 +is_long()
 +is_short()
 +break_even()
 }

 class Strategies {
 <<interface>>
 +add_leg()
 +get_legs()
 +break_even()
 +max_profit()
 +max_loss()
 +total_cost()
 +net_premium_received()
 +fees()
 }

 class BullCallSpread {
 +long_call: Position
 +short_call: Position
 +break_even()
 +max_profit()
 +max_loss()
 }

 class CallButterfly {
 +long_call_itm: Position
 +short_call: Position
 +long_call_otm: Position
 +break_even()
 +max_profit()
 +max_loss()
 }

 class Greeks {
 <<interface>>
 +delta()
 +gamma()
 +theta()
 +vega()
 +rho()
 +rho_d()
 }

 class Profit {
 <<interface>>
 +calculate_profit_at()
 }

 class Graph {
 <<interface>>
 +graph()
 +title()
 +get_values()
 }

 class PnLCalculator {
 <<interface>>
 +calculate_pnl()
 +calculate_pnl_at_expiration()
 }

 Options --|> Greeks
 Options --|> Profit
 Options --|> Graph
 Position *-- Options
 Position --|> Greeks
 Position --|> Profit
 Position --|> Graph
 Position --|> PnLCalculator
 BullCallSpread --|> Strategies
 BullCallSpread --|> Profit
 BullCallSpread --|> Graph
 CallButterfly --|> Strategies
 CallButterfly --|> Profit
 CallButterfly --|> Graph
 Options o-- OptionType
 Options o-- Side
 Options o-- OptionStyle
 ```

 ### Strategy Structure
 ```mermaid
 classDiagram
 class Options {
 +option_type: OptionType
 +side: Side
 +strike_price: Positive
 +expiration_date: ExpirationDate
 +implied_volatility: f64
 +calculate_price_black_scholes()
 +calculate_price_binomial()
 +calculate_delta()
 +payoff()
 }

 class Position {
 +option: Options
 +premium: f64
 +date: DateTime
 +open_fee: f64
 +close_fee: f64
 +total_cost()
 +unrealized_pnl()
 +days_held()
 }

 class OptionChain {
 +symbol: String
 +underlying_price: Positive
 +options: BTreeSet<OptionData>
 +build_chain()
 +add_option()
 +save_to_csv()
 +load_from_csv()
 }

 class Strategy {
 <<Interface>>
 +add_leg()
 +get_legs()
 +break_even()
 +max_profit()
 +max_loss()
 +total_cost()
 }

 class BullCallSpread {
 +long_call: Position
 +short_call: Position
 +break_even_points: Vec<f64>
 +calculate_profit_at()
 }

 class CallButterfly {
 +long_call_itm: Position
 +long_call_otm: Position
 +short_call: Position
 +break_even_points: Vec<f64>
 }

 class Graph {
 <<Interface>>
 +title()
 +get_values()
 +get_vertical_lines()
 +get_points()
 }

 class Profit {
 <<Interface>>
 +calculate_profit_at()
 }

 class Greeks {
 <<Interface>>
 +delta()
 +gamma()
 +theta()
 +vega()
 }

 Position o-- Options
 Strategy <|.. BullCallSpread
 Strategy <|.. CallButterfly
 Graph <|.. Options
 Graph <|.. Position
 Graph <|.. Strategy
 Profit <|.. Options
 Profit <|.. Position
 Profit <|.. Strategy
 Greeks <|.. Options
 OptionChain o-- Options
 BullCallSpread o-- Position
 CallButterfly o-- Position
 ```

 ## Strategies Classifications

 ```mermaid
 ---
 config:
 layout: fixed
 ---
 flowchart TD
 start["Options Strategies"] --> bullish["Bullish"] & bearish["Bearish"] & neutral["Neutral"]
 bullish --> bull_high["High Volatility"] & bull_low["Low Volatility"]
 bull_high --> bull_high_lim["Limited Risk"] & bull_high_unlim["Unlimited Risk"]
 bull_low --> bull_low_lim["Limited Risk"] & bull_low_unlim["Unlimited Risk"]
 bull_high_lim --> bull_high_lim_opt["Options Only"] & bull_high_lim_stock["With Underlying"]
 bull_high_unlim --> bull_high_unlim_opt["Options Only"] & bull_high_unlim_stock["With Underlying"]
 bull_low_lim --> bull_low_lim_opt["Options Only"] & bull_low_lim_stock["With Underlying"]
 bull_low_unlim --> bull_low_unlim_opt["Options Only"] & bull_low_unlim_stock["With Underlying"]
 bull_high_lim_opt --> bull_call(("Bull Call Spread"))
 bull_high_lim_stock --> protective_put(("Protective Put"))
 bull_high_unlim_opt --> long_call(("Long Call"))
 bull_high_unlim_stock --> pmcc((Poor Man's Covered Call))
 style pmcc shape:circle
 bull_low_lim_opt --> bull_put(("Bull Put Spread"))
 bull_low_lim_stock --> collar(("Collar"))
 bull_low_unlim_opt --> naked_put(("Naked Put"))
 bull_low_unlim_stock --> covered_call(("Covered Call"))
 bearish --> bear_high["High Volatility"] & bear_low["Low Volatility"]
 bear_high --> bear_high_lim["Limited Risk"] & bear_high_unlim["Unlimited Risk"]
 bear_low --> bear_low_lim["Limited Risk"] & bear_low_unlim["Unlimited Risk"]
 bear_high_lim --> bear_high_lim_opt["Options Only"] & bear_high_lim_stock["With Underlying"]
 bear_high_unlim --> bear_high_unlim_opt["Options Only"] & bear_high_unlim_stock["With Underlying"]
 bear_low_lim --> bear_low_lim_opt["Options Only"] & bear_low_lim_stock["With Underlying"]
 bear_low_unlim --> bear_low_unlim_opt["Options Only"] & bear_low_unlim_stock["With Underlying"]
 bear_high_lim_opt --> bear_put(("Bear Put Spread"))
 bear_high_lim_stock --> synthetic_put(("Synthetic Put"))
 bear_high_unlim_opt --> long_put(("Long Put"))
 bear_high_unlim_stock --> covered_put(("Covered Put"))
 bear_low_lim_opt --> bear_call(("Bear Call Spread"))
 bear_low_lim_stock --> reverse_collar(("Reverse Collar"))
 bear_low_unlim_opt --> naked_call(("Naked Call"))
 bear_low_unlim_stock --> protective_call(("Protective Call"))
 neutral --> neut_high["High Volatility"] & neut_low["Low Volatility"]
 neut_high --> neut_high_lim["Limited Risk"] & neut_high_unlim["Unlimited Risk"]
 neut_low --> neut_low_lim["Limited Risk"] & neut_low_unlim["Unlimited Risk"]
 neut_high_lim --> neut_high_lim_opt["Options Only"] & neut_high_lim_stock["With Underlying"]
 neut_high_unlim --> neut_high_unlim_opt["Options Only"] & neut_high_unlim_stock["With Underlying"]
 neut_low_lim --> neut_low_lim_opt["Options Only"] & neut_low_lim_stock["With Underlying"]
 neut_low_unlim --> neut_low_unlim_opt["Options Only"] & neut_low_unlim_stock["With Underlying"]
 neut_high_lim_opt --> call_butterfly(("Call Butterfly")) & butterfly_spread(("Butterfly Spread")) & long_straddle(("Long Straddle")) & long_strangle(("Long Strangle"))
 neut_high_unlim_opt --> short_straddle(("Short Straddle")) & short_strangle(("Short Strangle"))
 neut_low_lim_opt --> iron_butterfly(("Iron Butterfly")) & iron_condor(("Iron Condor"))
 neut_low_unlim_opt --> calendar_spread(("Calendar Spread")) & box_spread(("Box Spread"))
 neut_high_lim_stock --> conversion(("Conversion"))
 neut_high_unlim_stock --> reversal(("Reversal"))
 neut_low_lim_stock --> married_combo(("Married Combo"))
 neut_low_unlim_stock --> ratio_spread(("Ratio Spread"))
 ```

 ## Setup Instructions

 1. Clone the repository:
 ```shell
 git clone https://github.com/joaquinbejar/OptionStratLib.git
 cd OptionStratLib
 ```

 2. Build the project:
 ```shell
 make build
 ```

 3. Run tests:
 ```shell
 make test
 ```

 4. Format the code:
 ```shell
 make fmt
 ```

 5. Run linting:
 ```shell
 make lint
 ```

 6. Clean the project:
 ```shell
 make clean
 ```

 7. Run the project:
 ```shell
 make run
 ```

 8. Fix issues:
 ```shell
 make fix
 ```

 9. Run pre-push checks:
 ```shell
 make pre-push
 ```

 10. Generate documentation:
 ```shell
 make doc
 ```

 11. Publish the package:
 ```shell
 make publish
 ```

 12. Generate coverage report:
 ```shell
 make coverage
 ```



 ## Library Usage

 To use the library in your project, add the following to your `Cargo.toml`:

 ```toml
 [dependencies]
 optionstratlib = { git = "https://github.com/joaquinbejar/OptionStratLib.git" }
 ```

 ## Usage Examples

 Here are some examples of how to use the library for option pricing and analysis:

```rust
use optionstratlib::greeks::Greeks;
use optionstratlib::Options;
use optionstratlib::Positive;
use optionstratlib::model::types::{ExpirationDate, OptionStyle, OptionType, Side};
use optionstratlib::pos;
use optionstratlib::utils::setup_logger;
use optionstratlib::visualization::utils::Graph;
use optionstratlib::visualization::utils::GraphBackend;
use std::error::Error;
use tracing::info;

fn create_sample_option() -> Options {
    use rust_decimal_macros::dec;
use optionstratlib::pos;Options::new(
        OptionType::European,
        Side::Long,
        "AAPL".to_string(),
        pos!(100.0),
        ExpirationDate::Days(pos!(30.0)),
        pos!(0.2),
        pos!(1.0),
        pos!(105.0),
        dec!(0.05),
        OptionStyle::Call,
        Positive::ZERO,
        None,
    )
}
fn main() -> Result<(), Box<dyn Error>> {
    setup_logger();
    let option = create_sample_option();
    info!("Title: {}", option.title());
    info!("Greeks: {:?}", option.greeks());

    // Define a range of prices for the graph
    let price_range: Vec<Positive> = (50..150)
        .map(|x| pos!(x as f64))
        .collect();

    // Generate the intrinsic value graph
    option.graph(
        GraphBackend::Bitmap {
            file_path: "Draws/Options/intrinsic_value_chart.png",
            size: (1400, 933),
        },
        25,
    )?;

    Ok(())
}
 ```

```rust
use optionstratlib::Positive;
use optionstratlib::ExpirationDate;
use optionstratlib::pos;
use optionstratlib::strategies::Strategies;
use optionstratlib::strategies::bull_call_spread::BullCallSpread;
use optionstratlib::utils::setup_logger;
use optionstratlib::visualization::utils::Graph;
use optionstratlib::visualization::utils::GraphBackend;
use std::error::Error;
use tracing::info;

fn main() -> Result<(), Box<dyn Error>> {
    use rust_decimal_macros::dec;
setup_logger();

    let underlying_price = pos!(5781.88);

    let strategy = BullCallSpread::new(
        "SP500".to_string(),
        underlying_price,   // underlying_price
        pos!(5750.0),   // long_strike_itm
        pos!(5820.0),   // short_strike
        ExpirationDate::Days(pos!(2.0)),
        pos!(0.18),   // implied_volatility
        dec!(0.05),   // risk_free_rate
        Positive::ZERO,   // dividend_yield
        pos!(2.0),   // long quantity
        pos!(85.04),   // premium_long
        pos!(29.85),   // premium_short
        pos!(0.78),   // open_fee_long
        pos!(0.78),   // open_fee_long
        pos!(0.73),   // close_fee_long
        pos!(0.73),   // close_fee_short
    );

    let price_range = strategy.best_range_to_show(pos!(1.0)).unwrap();

    info!("Title: {}", strategy.title());
    info!("Break Even Points: {:?}", strategy.break_even_points);
    info!(
        "Net Premium Received: ${:.2}",
        strategy.net_premium_received()?
    );
    info!("Max Profit: ${:.2}", strategy.max_profit().unwrap_or(Positive::ZERO));
    info!("Max Loss: ${:0.2}", strategy.max_loss().unwrap_or(Positive::ZERO));
    info!("Total Fees: ${:.2}", strategy.fees()?);
    info!("Profit Area: {:.2}%", strategy.profit_area()?);
    info!("Profit Ratio: {:.2}%", strategy.profit_ratio()?);

    // Generate the profit/loss graph
    strategy.graph(
        GraphBackend::Bitmap {
            file_path: "Draws/Strategy/bull_call_spread_profit_loss_chart.png",
            size: (1400, 933),
        },
        20,
    )?;

    Ok(())
}
```

 ## Testing

 To run unit tests:
 ```shell
 make test
 ```

 To run tests with coverage:
 ```shell
 make coverage
 ```




## Contribution and Contact

We welcome contributions to this project! If you would like to contribute, please follow these steps:

1. Fork the repository.
2. Create a new branch for your feature or bug fix.
3. Make your changes and ensure that the project still builds and all tests pass.
4. Commit your changes and push your branch to your forked repository.
5. Submit a pull request to the main repository.

If you have any questions, issues, or would like to provide feedback, please feel free to contact the project maintainer:

**Joaquín Béjar García**
- Email: jb@taunais.com
- GitHub: [joaquinbejar]https://github.com/joaquinbejar

We appreciate your interest and look forward to your contributions!

## ✍️ License

Licensed under MIT license