Module: OptionLab

Defined in:
lib/option_lab.rb,
lib/option_lab/utils.rb,
lib/option_lab/engine.rb,
lib/option_lab/models.rb,
lib/option_lab/support.rb,
lib/option_lab/version.rb,
lib/option_lab/plotting.rb,
lib/option_lab/binomial_tree.rb,
lib/option_lab/black_scholes.rb,
lib/option_lab/configuration.rb,
lib/option_lab/bjerksund_stensland.rb

Overview

Main module for OptionLab

Defined Under Namespace

Modules: BinomialTree, BjerksundStensland, BlackScholes, Engine, Models, Plotting, Support, Utils Classes: Configuration, Error

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Get the current configuration

Returns:



30
31
32
# File 'lib/option_lab/configuration.rb', line 30

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Configure the library

Yields:

  • (config)

    Configuration object that can be modified



36
37
38
# File 'lib/option_lab/configuration.rb', line 36

def configure
  yield configuration if block_given?
end

.create_price_array(inputs_data, n: 100_000, seed: nil) ⇒ Numo::DFloat

Create price array

Parameters:

Returns:

  • (Numo::DFloat)

    Array of prices



39
40
41
# File 'lib/option_lab.rb', line 39

def create_price_array(inputs_data, n: 100_000, seed: nil)
  Support.create_price_array(inputs_data, n: n, seed: seed)
end

.get_american_greeks(option_type, s0, x, r, volatility, years_to_maturity, dividend_yield = 0.0) ⇒ Hash

Calculate option Greeks using the Bjerksund-Stensland model

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Hash)

    Option Greeks (delta, gamma, theta, vega, rho)



127
128
129
# File 'lib/option_lab.rb', line 127

def get_american_greeks(option_type, s0, x, r, volatility, years_to_maturity, dividend_yield = 0.0)
  BjerksundStensland.get_greeks(option_type, s0, x, r, volatility, years_to_maturity, dividend_yield)
end

.get_binomial_greeks(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0) ⇒ Hash

Calculate option Greeks using the CRR binomial tree model

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • n_steps (Integer) (defaults to: 100)

    Number of time steps

  • is_american (Boolean) (defaults to: true)

    True for American options, false for European

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Hash)

    Option Greeks (delta, gamma, theta, vega, rho)



101
102
103
# File 'lib/option_lab.rb', line 101

def get_binomial_greeks(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0)
  BinomialTree.get_greeks(option_type, s0, x, r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)
end

.get_binomial_tree(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 15, is_american = true, dividend_yield = 0.0) ⇒ Hash

Get binomial tree data for visualization and analysis

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • n_steps (Integer) (defaults to: 15)

    Number of time steps

  • is_american (Boolean) (defaults to: true)

    True for American options, false for European

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Hash)

    Tree structure with stock prices and option values



86
87
88
# File 'lib/option_lab.rb', line 86

def get_binomial_tree(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 15, is_american = true, dividend_yield = 0.0)
  BinomialTree.get_tree(option_type, s0, x, r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)
end

.get_pl(outputs, leg = nil) ⇒ Array<Numo::DFloat, Numo::DFloat>

Get profit/loss data

Parameters:

  • outputs (Models::Outputs)

    Output data from a strategy calculation

  • leg (Integer, nil) (defaults to: nil)

    Index of a strategy leg

Returns:

  • (Array<Numo::DFloat, Numo::DFloat>)

    Array of stock prices and profits/losses



47
48
49
# File 'lib/option_lab.rb', line 47

def get_pl(outputs, leg = nil)
  Utils.get_pl(outputs, leg)
end

.pl_to_csv(outputs, filename = 'pl.csv', leg = nil) ⇒ void

This method returns an undefined value.

Save profit/loss data to CSV

Parameters:

  • outputs (Models::Outputs)

    Output data from a strategy calculation

  • filename (String) (defaults to: 'pl.csv')

    Name of the CSV file

  • leg (Integer, nil) (defaults to: nil)

    Index of a strategy leg



56
57
58
# File 'lib/option_lab.rb', line 56

def pl_to_csv(outputs, filename = 'pl.csv', leg = nil)
  Utils.pl_to_csv(outputs, filename, leg)
end

.plot_pl(outputs) ⇒ void

This method returns an undefined value.

Plot profit/loss diagram

Parameters:



30
31
32
# File 'lib/option_lab.rb', line 30

def plot_pl(outputs)
  Plotting.plot_pl(outputs)
end

.price_american(option_type, s0, x, r, volatility, years_to_maturity, dividend_yield = 0.0) ⇒ Float

Price an option using the Bjerksund-Stensland model

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Float)

    Option price



114
115
116
# File 'lib/option_lab.rb', line 114

def price_american(option_type, s0, x, r, volatility, years_to_maturity, dividend_yield = 0.0)
  BjerksundStensland.price_option(option_type, s0, x, r, volatility, years_to_maturity, dividend_yield)
end

.price_binomial(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0) ⇒ Float

Price an option using the Cox-Ross-Rubinstein binomial tree model

Parameters:

  • option_type (String)

    'call' or 'put'

  • s0 (Float)

    Spot price

  • x (Float)

    Strike price

  • r (Float)

    Risk-free interest rate

  • volatility (Float)

    Volatility

  • years_to_maturity (Float)

    Time to maturity in years

  • n_steps (Integer) (defaults to: 100)

    Number of time steps

  • is_american (Boolean) (defaults to: true)

    True for American options, false for European

  • dividend_yield (Float) (defaults to: 0.0)

    Continuous dividend yield

Returns:

  • (Float)

    Option price



71
72
73
# File 'lib/option_lab.rb', line 71

def price_binomial(option_type, s0, x, r, volatility, years_to_maturity, n_steps = 100, is_american = true, dividend_yield = 0.0)
  BinomialTree.price_option(option_type, s0, x, r, volatility, years_to_maturity, n_steps, is_american, dividend_yield)
end

.reset_configurationObject

Reset configuration to defaults



41
42
43
# File 'lib/option_lab/configuration.rb', line 41

def reset_configuration
  @configuration = Configuration.new
end

.run_strategy(inputs) ⇒ Models::Outputs

Run a strategy calculation

Parameters:

  • inputs (Hash, Models::Inputs)

    Input data for the strategy calculation

Returns:



23
24
25
# File 'lib/option_lab.rb', line 23

def run_strategy(inputs)
  Engine.run_strategy(inputs)
end