Help us shape the future of Go! Take the 2026 Go Developer Survey

calculator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 2 Imported by: 0

README

stock-options

Documentation

Overview

Package calculator provides basic constants and mathematical functions for stock options.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidStrike       = errors.New("calculator: strike should be >= 0")
	ErrInvalidStoke        = errors.New("calculator: stoke should be >= 0")
	ErrInvalidPremium      = errors.New("calculator: premium should be >= 0")
	ErrInvalidInterestRate = errors.New("calculator: interest rate should be >= 0 and <= 1")
	ErrInvalidVolatility   = errors.New("calculator: volatility should be >= 0 and <= 1")
	ErrInvalidTimeToExpire = errors.New("calculator: time to expire should be >= 0")
	ErrInvalidDividend     = errors.New("calculator: dividend should be >= 0 and <= 1")
)

Validation errors

Functions

func BreakEvenPoint

func BreakEvenPoint(opt Option, str, pr float64) (float64, error)

BreakEvenPoint calculates price in the underlying asset at which exercise/dispose of the contract without incurring a loss

It takes strike price (str) and premium (pr) in $$$ per share and returns break-even point regarding to the option type (opt)

Example (Call)
var strike, premium float64 = 50, 10
bep, err := BreakEvenPoint(Call, strike, premium)
if err != nil {
	// handle error
}
fmt.Printf("%.2f", bep)
Output:
60.00
Example (Put)
var strike, premium float64 = 50, 10
bep, err := BreakEvenPoint(Put, strike, premium)
if err != nil {
	// handle error
}
fmt.Printf("%.2f", bep)
Output:
40.00

func PayoffFromBuying

func PayoffFromBuying(opt Option, str, st, pr float64) (float64, error)

PayoffFromBuying calculates current market price of an option ($$$ per share) from buying

It takes strike price (str), stock (st) and premium (pr) in $$$ per share and returns profit/lose from buying options regarding to the option type (opt)

Example (Call)
var strike, stock, premium float64 = 50, 70, 10
payoff, err := PayoffFromBuying(Call, strike, stock, premium)
if err != nil {
	// handle error
}
fmt.Printf("%.2f", payoff)
Output:
10.00
Example (Put)
var strike, stock, premium float64 = 50, 30, 10
payoff, err := PayoffFromBuying(Put, strike, stock, premium)
if err != nil {
	// handle error
}
fmt.Printf("%.2f", payoff)
Output:
10.00

func PayoffFromSelling

func PayoffFromSelling(opt Option, str, st, pr float64) (float64, error)

PayoffFromSelling calculates current market price of an option ($$$ per share) from selling

It takes strike price (str), stock (st) and premium (pr) in $$$ per share and returns profit/lose from selling options regarding to the option type (opt)

Example (Call)
var strike, stock, premium float64 = 50, 40, 10
payoff, err := PayoffFromSelling(Call, strike, stock, premium)
if err != nil {
	// handle error
}
fmt.Printf("%.2f", payoff)
Output:
10.00
Example (Put)
var strike, stock, premium float64 = 50, 70, 10
payoff, err := PayoffFromSelling(Put, strike, stock, premium)
if err != nil {
	// handle error
}
fmt.Printf("%.2f", payoff)
Output:
10.00

Types

type BlackScholesModel

type BlackScholesModel struct {
	Option               // option type (call or put)
	Strike       float64 // strike price ($$$ per share)
	Stock        float64 // underlying price ($$$ per share)
	InterestRate float64 // continuously compounded risk-free interest rate (% p.a.)
	Volatility   float64 // volatility (% p.a.)
	TimeToExpire float64 // time to expiration (% of year)
	Dividend     float64 // continuously compounded dividend yield (% p.a.)
}

func (*BlackScholesModel) Price

func (bsm *BlackScholesModel) Price() (float64, error)

Price calculates by solving the equation for the corresponding terminal and boundary conditions for more details see https://en.wikipedia.org/wiki/Black-Scholes_model

Example (Call)
bsm := BlackScholesModel{
	Option:       Call,
	Strike:       58,
	Stock:        60,
	InterestRate: 0.035,
	Volatility:   0.2,
	TimeToExpire: 0.5,
	Dividend:     0.0125,
}
price, err := bsm.Price()
if err != nil {
	// handle error
}
fmt.Printf("%.4f", price)
Output:
4.6245
Example (Put)
bsm := BlackScholesModel{
	Option:       Put,
	Strike:       58,
	Stock:        60,
	InterestRate: 0.035,
	Volatility:   0.2,
	TimeToExpire: 0.5,
	Dividend:     0.0125,
}
price, err := bsm.Price()
if err != nil {
	// handle error
}
fmt.Printf("%.4f", price)
Output:
1.9922

type Option

type Option int
const (
	Call Option = iota
	Put
)

Option types

Jump to

Keyboard shortcuts

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