controller

package
v0.0.0-...-3e0d6b8 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpiryToInstrumentName

func ExpiryToInstrumentName(
	underlying shared.Underlying,
	expiry uint64,
	strike decimal.Decimal,
	isCall bool,
) (name string)

ExpiryToInstrumentName - Convert an expiry timestamp to instrument name Arguments:

expiry - Timestamp for expiry

Returns:

name - Name of the instrument on Deribit

func GetAlternativeMinimum

func GetAlternativeMinimum(spot float64, percent float64) float64

GetAlternativeMinimum - Compute alternative minimum for 1 naked calls / puts.

func GetAmountOTM

func GetAmountOTM(spot float64, strike float64, isCall bool) (amount float64)

GetAmountOTM - Compute the amount out the money Cannot be a negative amount, if ITM, returns 0

func GetCallPrice

func GetCallPrice(
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetCallPrice - Compute Black Scholes price of call Formula:

C = SN(d1)-Ke^{-rt}N(d2)

func GetDelta

func GetDelta(
	isCall bool,
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetDelta - Compute delta of an option Formula:

delta = N(d1) if call
delta = N(d1) - 1 if put

Notes:

https://en.wikipedia.org/wiki/Greeks_(finance)#Vega
http://www.columbia.edu/~mh2078/FoundationsFE/BlackScholes.pdf

Arguments:

isCall - whether this is a call or a put
spot - Spot price
strike - Strike price
sigma - Annualized implied volatility
tau - Time to expiry (annualized)
rate - The risk-free rate

Returns:

delta - The delta for an option

func GetDeribitSigma

func GetDeribitSigma(name string) (
	askSigma float64,
	bidSigma float64,
	markSigma float64,
	err error,
)

GetDeribitSigma Get IV From Instrument ID Arguments:

ID - Instrument ID. See `InstrumentNameToID`

Returns:

askSigma - Implied volatility of the best ask price
bidSigma - Implied volatility of the best bid price
markSigma - Implied volatility of the best mark price
err - Error. Nil if no error

func GetGamma

func GetGamma(
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetGamma - Compute gamma of an option Formula:

gamma = N'(d1) / (sigma * spot * sqrt{tau})

Notes:

https://en.wikipedia.org/wiki/Greeks_(finance)#Vega
http://www.columbia.edu/~mh2078/FoundationsFE/BlackScholes.pdf
Call and put gamma are equal

Arguments:

spot - Spot price
strike - Strike price
sigma - Annualized implied volatility
tau - Time to expiry (annualized)
rate - The risk-free rate

Returns:

gamma - The gamma for an option

func GetInitialMargin

func GetInitialMargin(
	isBuy bool,
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
	isCall bool,
	minMarginPerc float64,
) (margin float64)

GetInitialMargin - Compute the initial margin requirements Arguments:

isBuy - If the margin is for buyer or seller
spot - Spot price
strike - Strike price (not level)
sigma - Annualized implied volatility
tau - Time to expiry (annualiezd)
rate - Risk-free interest rate
isCall - If the option is a call or put
minMarginPerc - Alternative minimum percentage

func GetMarkPrice

func GetMarkPrice(
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
	isCall bool,
) float64

GetMarkPrice - Helper function to return call or put prices Arguments:

See `GetCallPrice` and `GetPutPrice`
isCall - Boolean that is true if call else put

func GetProbabilityFactors

func GetProbabilityFactors(
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) (float64, float64)

GetProbabilityFactors - Compute d1 and d2 in Black Scholes Formula:

d1 = (log(S/K) + (r + sigma^2/2*tau)) / (sigma*sqrt(tau))
tau = T - t = time to maturity
r = rate of return
S = spot price
K = strike price
sigma = implied volatility

Arguments:

spot - Spot price
strike - Strike price
sigma - Annualized implied volatility
tau - Time to expiry (annualized)
rate - The risk-free rate

Returns:

d1 Probability factor one
d2 Probability factor one

func GetPutPrice

func GetPutPrice(
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetPutPrice - Compute Black Scholes price of put Formula:

P = Ke^{-rt}N(-d2)-SN(-d1)

func GetRho

func GetRho(
	isCall bool,
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetRho - Compute rho of an option Notes:

https://en.wikipedia.org/wiki/Greeks_(finance)#Vega
http://www.columbia.edu/~mh2078/FoundationsFE/BlackScholes.pdf

Arguments:

isCall - whether this is a call or a put
spot - Spot price
strike - Strike price
sigma - Annualized implied volatility
tau - Time to expiry (annualized)
rate - The risk-free rate

Returns:

rho - The rho for an option

func GetSigmaByBisection

func GetSigmaByBisection(
	spot float64,
	strike float64,
	tau float64,
	rate float64,
	price float64,
	isCall bool,
	maxIter int,
	tolerance float64,
) (float64, error)

GetSigmaByBisection - Solve for volatility from call price iteratively using Bisection method Resources:

https://en.wikipedia.org/wiki/Bisection_method

Notes:

We found this to be more stable and efficient than Newton Raphson
which struggles with Vega -> 0 for strikes from the spot

Arguments:

isCall - boolean that is true if option is a call
maxIter - Maximum number of iterations for convergence
tolerance - Maximum discrepancy to define convergence

func GetTheta

func GetTheta(
	isCall bool,
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetTheta - Compute rho of an option Notes:

https://en.wikipedia.org/wiki/Greeks_(finance)#Theta
http://www.columbia.edu/~mh2078/FoundationsFE/BlackScholes.pdf

Arguments:

isCall - whether this is a call or a put
spot - Spot price
strike - Strike price
sigma - Annualized implied volatility
tau - Time to expiry (annualized)
rate - The risk-free rate

Returns:

theta - The theta for an option

func GetVega

func GetVega(
	spot float64,
	strike float64,
	sigma float64,
	tau float64,
	rate float64,
) float64

GetVega - Compute vega of an option Formula:

vega = S * sqrt{tau} * N'(d1)

Notes:

https://en.wikipedia.org/wiki/Greeks_(finance)#Vega
http://www.columbia.edu/~mh2078/FoundationsFE/BlackScholes.pdf
Call and put vega are equal

Arguments:

spot - Spot price
strike - Strike price
sigma - Annualized implied volatility
tau - Time to expiry (annualized)
rate - The risk-free rate

Returns:

vega - The vega for an option

func InstrumentNameToID

func InstrumentNameToID(name string) (uint64, error)

InstrumentNameToID - Get Instrument ID from Name using deribits API

Types

type VolatilityManager

type VolatilityManager struct {
	// A single surface will be for a single underlying
	Underlying shared.Underlying
	// A single surface will be for a single expiry
	Expiry uint64
	// A surface is stored as N moneyness points
	// And N points for time until expiry (or tau)
	Surface [20][20]float64
	// Moneyness points (sorted)
	MoneynessPoints [20]float64
	// Minimum moneyness
	MinMoneyness float64
	// Maximum moneyness
	MaxMoneyness float64
	// Time to expiry points (sorted & annualized)
	TauPoints [20]float64
	// Minimum time to expiry (annualized)
	MinTau float64
	// Max time to expiry (annualized)
	MaxTau float64
}

VolatilityManager - A struct to store volatility surfaces

func CreateVolatilityManager

func CreateVolatilityManager(
	underlying shared.Underlying,
	expiry uint64,
	initSigma float64,
	minMoneyness float64,
	maxMoneyness float64,
) (*VolatilityManager, error)

CreateVolatilityManager - Create `VolatilityManager` instance Arguments:

underlying - Address for the underlying asset
expiry - Expiry timestamp since epoch; unique identifiser
initSigma - Value to initialize the surface to (e.g. 0)
minMoneyness - Minimum moneyness in surface
maxMoneyness - Maximum moneyness in surface

func (*VolatilityManager) Backstop

func (manager *VolatilityManager) Backstop(strike decimal.Decimal) (float64, error)

Backstop - Get deribits implied volatility so we don't end up serving something bad Arguments:

strike - Current strike price
isCall - Whether option is a call or put. This doesn't matter that much

Returns:

sigma - Implied volatility (annualized) as used in Black-Scholes

func (*VolatilityManager) FindClosestMoneyness

func (manager *VolatilityManager) FindClosestMoneyness(query float64) (index int)

FindClosestMoneyness - Find the closest index to a query value for moneyness

func (*VolatilityManager) FindClosestTau

func (manager *VolatilityManager) FindClosestTau(query float64) (index int)

FindClosestTau - Find the closest index to a query value for time-to-expiry

func (*VolatilityManager) FindClosestTwoMoneyness

func (manager *VolatilityManager) FindClosestTwoMoneyness(query float64) (int, int)

FindClosestTwoMoneyness - Find the index of the two closest moneyness Notes:

If query is exactly one of the moneyness points, then return the same index twice
Otherwise return two indices of the closest neighbors

func (*VolatilityManager) FindClosestTwoTau

func (manager *VolatilityManager) FindClosestTwoTau(query float64) (int, int)

FindClosestTwoTau - Find the index of the (two) closest times to expiries Notes:

If query is exactly one of the tau points, then return the same index twice
Otherwise return two indices of the closest neighbors
All tau terms below are annualilzed

func (*VolatilityManager) Interpolate

func (manager *VolatilityManager) Interpolate(
	moneyness float64,
	tau float64,
) float64

Interpolate - Find the weighted average between the four nearest points Resources:

https://en.wikipedia.org/wiki/Bilinear_interpolation

func (*VolatilityManager) IsEqual

func (manager *VolatilityManager) IsEqual(other *VolatilityManager) bool

IsEqual - Check if two volatility manager are equal

func (*VolatilityManager) Query

func (manager *VolatilityManager) Query(
	spot decimal.Decimal,
	strike decimal.Decimal,
	tauAnnualized float64,
) float64

Query - Given a moneyness, and time until expiry, query for IV Arguments:

	spot - Current spot price
 strike - Current strike price
	tauAnnualized - Current time to expiry in years
	useBackstop - If true, use Deribit's API as a backstop. This will update our own surface

Returns:

sigma - Implied volatility (annualized) as used in Black-Scholes

Notes:

The IV is computed as a weighted average over the four closest points

func (*VolatilityManager) Tau

func (manager *VolatilityManager) Tau() uint64

Tau - Return the time to expiry If < 0, returns 0

func (*VolatilityManager) TauAnnualized

func (manager *VolatilityManager) TauAnnualized() float64

TauAnnualized - Return time to expiry in years If < 0, returns 0

func (*VolatilityManager) Update

func (manager *VolatilityManager) Update(
	spot decimal.Decimal,
	strike decimal.Decimal,
	sigma float64,
	weight float64,
) error

Update - Given a new order, update the surface Notes:

This will perform a local smoothing operation rather than just update a single point

Arguments:

underlying - Enum for underlying
spot - Spot price in decimals
strike - Strike price in decimals
sigma - Implied volatility (annualized) as used in Black-Scholes
weight - Weight to give new value. DEfault choice is 0.8

Jump to

Keyboard shortcuts

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