optionstratlib 0.1.1

OptionStratLib is a comprehensive Rust library for options trading and strategy development across multiple asset classes.
Documentation

Dual License Crates.io Downloads Stars

Build Status Coverage Dependencies

OptionStratLib v0.1.0: Financial Options Library

Table of Contents

  1. Introduction
  2. Features
  3. Project Structure
  4. Setup Instructions
  5. Library Usage
  6. Usage Examples
  7. Testing
  8. 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: Implements Black-Scholes and binomial models for option pricing.
  2. Greeks Calculation: Calculates delta, gamma, theta, vega, and rho for sensitivity analysis.
  3. Option Types: Supports European and American options, both calls and puts.
  4. Risk Analysis: Includes VaR (Value at Risk) calculations and other risk metrics. TODO!
  5. Simulations: Allows Monte Carlo simulations for scenario analysis.
  6. Exotic Options: Supports some types of exotic options such as Asian and lookback options. TODO!
  7. Strategy Development: Provides tools for creating and backtesting option trading strategies.
  8. Performance Visualization: Generates payoff diagrams and risk profiles for visual analysis.
  9. Multi-Asset Support: TODO!
  10. Risk Management: TODO!
  11. Backtesting: TODO!
  12. Performance Metrics: Generates performance metrics for strategy evaluation and Positions.

Project Structure

The project is structured as follows:

  1. Options Module (option.rs): This module contains the core implementation of option-related structures and methods. It includes the Options struct that represents an option contract, along with methods for pricing, calculating Greeks, and managing the profit and loss (P&L) of the option.

  2. Pricing Models (pricing/):

    • binomial_model.rs: Implements the binomial model for pricing options.
    • black_scholes_model.rs: Implements the Black-Scholes model for pricing options.
    • monte_carlo.rs: Provides tools for running Monte Carlo simulations to price options.
    • payoff.rs: Defines the payoff functions for different option types.
    • telegraph.rs: Implements the Telegraphic method for pricing options.
    • utils.rs: Utility functions related to option pricing.
  3. Greeks Calculation (greeks/):

    • equations.rs: Contains the mathematical equations for calculating various Greeks (delta, gamma, theta, vega, rho).
    • utils.rs: Utility functions for working with Greeks.
  4. Profit and Loss (P&L) (pnl/):

    • utils.rs: Implements functions for calculating the P&L of option positions.
  5. Risk Management (risk/):

    • This module will contain implementations of risk metrics and management strategies, such as Value-at-Risk (VaR), Expected Shortfall, and risk-based portfolio optimization.
  6. Strategies (strategies/):

    • base.rs: Defines the base traits and structures for option trading strategies.
    • bear_put_spread.rs, bull_call_spread.rs, butterfly_spread.rs, collar.rs, covered_call.rs, iron_condor.rs, protective_put.rs, straddle.rs, strangle.rs: Implementations of various option trading strategies.
    • utils.rs: Utility functions for working with option trading strategies.
  7. Curves and Surfaces (curves/, surfaces/):

    • These modules will contain functionality for constructing, analyzing, and visualizing yield curves, volatility surfaces, and other financial curves and surfaces.
  8. Visualization (visualization/):

    • binomial_tree.rs: Visualization of binomial option pricing trees.
    • strategy.rs: Visualization of option trading strategies.
    • utils.rs: Utility functions for creating visualizations.
  9. Volatility (volatility/):

    • utils.rs: Utility functions for working with volatility.
  10. Backtesting (backtesting/):

    • This module will contain the necessary functionality for performing backtesting of trading strategies.
  11. Utility Modules:

    • constants.rs: Defines common constants used throughout the project.
    • model/: Contains structs and types used to represent various financial concepts, such as options, positions, and formats.

Setup Instructions

  1. Clone the repository:
git clone https://github.com/joaquinbejar/OptionStratLib.git
cd OptionStratLib
  1. Build the project:
make build
  1. Run tests:
make test
  1. Format the code:
make fmt
  1. Run linting:
make lint
  1. Clean the project:
make clean
  1. Run the project:
make run
  1. Fix issues:
make fix
  1. Run pre-push checks:
make pre-push
  1. Generate documentation:
make doc
  1. Publish the package:
make publish
  1. Generate coverage report:
make coverage

Library Usage

To use the library in your project, add the following to your Cargo.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:

use optionstratlib::model::option::Options;
use optionstratlib::model::types::{ExpirationDate, OptionStyle, OptionType, Side};
use optionstratlib::visualization::utils::Graph;
use std::error::Error;
use optionstratlib::greeks::equations::Greeks;

fn create_sample_option() -> Options {
   Options::new(
      OptionType::European,
      Side::Short,
      "AAPL".to_string(),
      100.0,
      ExpirationDate::Days(30.0),
      0.2,
      1,
      105.0,
      0.05,
      OptionStyle::Call,
      0.0,
      None,
   )
}
fn main() -> Result<(), Box<dyn Error>> {
   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<f64> = (50..150).map(|x| x as f64).collect();

   // Generate the intrinsic value graph
   option.graph(&price_range,
                "Draws/Options/intrinsic_value_chart.png",
                25,
                (1400, 933),
                (10, 30),
                10
   )?;

   Ok(())
}
use optionstratlib::model::types::ExpirationDate;
use optionstratlib::strategies::base::Strategies;
use optionstratlib::strategies::bull_call_spread::BullCallSpread;
use optionstratlib::visualization::utils::Graph;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
   let strategy = BullCallSpread::new(
      "GOLD".to_string(),
      2505.8,
      2460.0,
      2515.0,
      ExpirationDate::Days(30.0),
      0.2,
      0.05,
      0.0,
      1,
      27.26,
      5.33,
      0.58,
      0.58,
      0.55,
      0.55,
   );
   let price_range: Vec<f64> = (2400..2600).map(|x| x as f64).collect();
   info!("Title: {}", strategy.title());
   info!("Break Even {}", strategy.break_even());
   info!("Net Premium Received: {}", strategy.net_premium_received());
   info!("Max Profit: {}", strategy.max_profit());
   info!("Max Loss: {}", strategy.max_loss());
   info!("Total Cost: {}", strategy.total_cost());

   // Generate the intrinsic value graph
   strategy.graph(
      &price_range,
      "Draws/Strategy/bull_call_spread_value_chart.png",
      20,
      (1400, 933),
      (10, 30),
      15
   )?;
   Ok(())
}

Testing

To run unit tests:

make test

To run tests with coverage:

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

We appreciate your interest and look forward to your contributions!