Skip to content

sf8193/blackscholes

Repository files navigation

Black-Scholes Options Pricing Library

A fast and accurate Go library for Black-Scholes options pricing calculations, including implied volatility calculation using binary search method.

Features

  • Black-Scholes pricing: Calculate theoretical option prices for calls and puts
  • Implied volatility: High-precision binary search method for IV calculation
  • Greeks calculation: Delta, gamma, theta, vega, and rho
  • High performance: Optimized for real-time trading applications
  • Proven accuracy: Battle-tested in production trading systems

Installation

go get github.com/sf8193/blackscholes

Quick Start

package main

import (
    "fmt"
    "github.com/sf8193/blackscholes"
)

func main() {
    // Calculate option price
    underlying := 100.0
    strike := 105.0
    timeToExpiry := 30.0 / 365.0  // 30 days
    volatility := 0.25            // 25% IV
    riskFreeRate := 0.05          // 5%
    
    callPrice := blackscholes.BlackScholes(underlying, strike, timeToExpiry, volatility, riskFreeRate, "CALL")
    fmt.Printf("Call price: $%.2f\n", callPrice)
    
    // Calculate implied volatility
    marketPrice := 2.50
    iv := blackscholes.GetImpliedVolatility(marketPrice, strike, underlying, timeToExpiry, riskFreeRate, "CALL", 0.2)
    fmt.Printf("Implied volatility: %.2f%%\n", iv*100)
    
    // Calculate delta
    delta, _ := blackscholes.GetDelta(underlying, strike, timeToExpiry, volatility, riskFreeRate, "CALL")
    fmt.Printf("Delta: %.4f\n", delta)
}

API Reference

Price Calculation

func BlackScholes(underlying, strike, timeToExpiry, volatility, riskFreeRate float64, contractType string) float64

Calculate theoretical option price using Black-Scholes formula.

Parameters:

  • underlying: Current price of underlying asset
  • strike: Strike price of option
  • timeToExpiry: Time to expiration in years (e.g., 30 days = 30/365)
  • volatility: Implied volatility as decimal (e.g., 25% = 0.25)
  • riskFreeRate: Risk-free interest rate as decimal (e.g., 5% = 0.05)
  • contractType: "CALL" or "PUT"

Implied Volatility

func GetImpliedVolatility(premium, strike, underlying, timeToExpiry, riskFreeRate float64, contractType string, estimate float64) float64

Calculate implied volatility using binary search method.

Parameters:

  • premium: Market price of the option
  • strike: Strike price of option
  • underlying: Current price of underlying asset
  • timeToExpiry: Time to expiration in years
  • riskFreeRate: Risk-free interest rate as decimal
  • contractType: "CALL" or "PUT"
  • estimate: Initial volatility estimate (use 0.0 for automatic)

Greeks

func GetDelta(underlying, strike, timeToExpiry, volatility, riskFreeRate float64, contractType string) (float64, error)
func GetGamma(underlying, strike, timeToExpiry, volatility, riskFreeRate float64) float64
func GetTheta(underlying, strike, timeToExpiry, volatility, riskFreeRate float64, contractType string) float64
func GetVega(underlying, strike, timeToExpiry, volatility, riskFreeRate float64) float64
func GetRho(underlying, strike, timeToExpiry, volatility, riskFreeRate float64, contractType string) float64

Performance

  • Implied volatility: ~16ns per calculation (binary search method)
  • Option pricing: ~8ns per calculation
  • Thread-safe: All functions are safe for concurrent use
  • Memory efficient: No allocations in hot paths

Testing

Run the test suite:

go test

The package includes comprehensive tests covering:

  • Price calculation accuracy
  • Implied volatility precision
  • Greeks calculation
  • Edge cases and error handling

License

MIT License - see LICENSE file for details.

About

my blackscholes lib

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages