blackscholes

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 5 Imported by: 0

README

go-blackscholes

Go implementation of the basic Black Scholes formulas for European option prices, greeks and implied volatility.

Edge cases such as e.g. zero volatility implemented exactly.

Use at your own risk.

Install
go get github.com/uscott/go-blackscholes@latest
Usage

Refer to test cases for more examples

package main

import (
    "fmt"
    bs "github.com/uscott/go-blackscholes"
)

func main() {

    vol := 0.2
    timeToExpiry := 1.0
    spot := 100.0
    strike := 100.0
    interestRate := 0.02
    dividendYield := 0.01
    optionType := blackscholes.Straddle

    price, err := blackscholes.Price(vol, timeToExpiry, spot, strike, interestRate, dividendYield, optionType)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Price: %.2f\n", price)
}

Documentation

Index

Constants

View Source
const (
	Call     = OptionType('c')
	Put      = OptionType('p')
	Straddle = OptionType('s')
)
View Source
const InvSqrt2PI float64 = 1.0 / math.Sqrt2 / math.SqrtPi

Variables

View Source
var (
	ErrNegVol            = errors.New("Negative volatility")
	ErrNegTimeToExp      = errors.New("Negative time to expiry")
	ErrNegPremium        = errors.New("Negative option premium")
	ErrNegPrice          = errors.New("Negative underlying price")
	ErrNegStrike         = errors.New("Negative strike")
	ErrUnknownOptionType = errors.New("Unknown option type")
	ErrNilPtrArg         = errors.New("Nil pointer argument")
	ErrNoncovergence     = errors.New("Did not converge")
)
View Source
var ErrMaxIterations = errors.New("max iterations exceeded")
View Source
var ErrNegativeEpsilon = errors.New("epsilon must be positive")

Functions

func AtmApprox

func AtmApprox(
	vol, timeToExpiry, spot, dividendYield float64,
	optionType OptionType,
) (price float64, err error)

AtmApprox approximates the option price when the spot discounted by the dividend yield is equal to the strike discounted by the interest rate.

func CheckPriceParams

func CheckPriceParams(timeToExpiry, spot, strike float64, optionType OptionType) error

CheckPriceParams checks whether timeToExpiry, spot, and strike are non-negative, and optionType is one of the defined OptionType constants

func CorrectVolSign

func CorrectVolSign(extrinsic float64, vol float64) float64

func Delta

func Delta(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
) (delta float64, err error)

func DeltaNumeric added in v0.2.0

func DeltaNumeric(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
	epsilon ...float64,
) (delta float64, err error)

func DeltaZeroSpot added in v0.2.0

func DeltaZeroSpot(t, q float64, o OptionType) float64

func DeltaZeroStrike added in v0.2.0

func DeltaZeroStrike(t, q float64, o OptionType) float64

func DeltaZeroVol added in v0.2.0

func DeltaZeroVol(t, x, k, r, q float64, o OptionType) float64

func Gamma

func Gamma(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
) (gamma float64, err error)

func GammaNumeric added in v0.2.0

func GammaNumeric(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
	epsilon ...float64,
) (gamma float64, err error)

func GammaZeroVol added in v0.2.0

func GammaZeroVol(t, x, k, r, q float64) float64

func ImpliedVol

func ImpliedVol(
	premium, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
	params ...ImpliedVolParams,
) (vol float64, err error)

func Intrinsic

func Intrinsic(
	timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
) float64

func NormCDF

func NormCDF(x float64) float64

func NormCDFInverse

func NormCDFInverse(q float64) float64

func NormPDF added in v0.2.0

func NormPDF(x float64) float64

func Price

func Price(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
) (price float64, err error)

func PriceSim added in v0.2.0

func PriceSim(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
	numPaths ...uint,
) (price float64, err error)

func PriceZeroSpot added in v0.2.0

func PriceZeroSpot(t, k, r float64, o OptionType) float64

func PriceZeroStrike added in v0.2.0

func PriceZeroStrike(t, x, q float64, o OptionType) float64

func Theta

func Theta(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
) (theta float64, err error)

func ThetaNumeric added in v0.2.0

func ThetaNumeric(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
	epsilon ...float64,
) (theta float64, err error)

func ThetaZeroSpot added in v0.2.0

func ThetaZeroSpot(t, k, r float64, o OptionType) float64

func ThetaZeroStrike added in v0.2.0

func ThetaZeroStrike(t, x, q float64, o OptionType) float64

func ThetaZeroVol added in v0.2.0

func ThetaZeroVol(t, x, k, r, q float64, o OptionType) float64

func ValidOptionType

func ValidOptionType(optionType OptionType) bool

func Vega

func Vega(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
) (vega float64, err error)

func VegaNumeric added in v0.2.0

func VegaNumeric(
	vol, timeToExpiry, spot, strike, interestRate, dividendYield float64,
	optionType OptionType,
	epsilon ...float64,
) (vega float64, err error)

Types

type ImpliedVolParams

type ImpliedVolParams struct {
	LowerBound    *float64
	UpperBound    *float64
	Tolerance     *float64
	MaxIterations *int
}

type OptionType

type OptionType rune

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL