Class: PureGreeks::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/pure_greeks/option.rb

Constant Summary collapse

VALID_EXERCISE_STYLES =
%i[american european].freeze
VALID_TYPES =
%i[call put].freeze
DAYS_PER_YEAR =
365.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exercise_style:, type:, strike:, expiration:, underlying_price:, risk_free_rate:, dividend_yield:, valuation_date:, implied_volatility: nil, market_price: nil) ⇒ Option

Returns a new instance of Option.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pure_greeks/option.rb', line 18

def initialize(exercise_style:, type:, strike:, expiration:, underlying_price:,
               risk_free_rate:, dividend_yield:, valuation_date:,
               implied_volatility: nil, market_price: nil)
  validate!(exercise_style, type, strike, underlying_price, expiration, valuation_date)

  @exercise_style = exercise_style
  @type = type
  @strike = strike.to_f
  @expiration = expiration
  @underlying_price = underlying_price.to_f
  @implied_volatility = implied_volatility&.to_f
  @market_price = market_price&.to_f
  @risk_free_rate = risk_free_rate.to_f
  @dividend_yield = dividend_yield.to_f
  @valuation_date = valuation_date
end

Instance Attribute Details

#dividend_yieldObject (readonly)

Returns the value of attribute dividend_yield.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def dividend_yield
  @dividend_yield
end

#exercise_styleObject (readonly)

Returns the value of attribute exercise_style.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def exercise_style
  @exercise_style
end

#expirationObject (readonly)

Returns the value of attribute expiration.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def expiration
  @expiration
end

#risk_free_rateObject (readonly)

Returns the value of attribute risk_free_rate.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def risk_free_rate
  @risk_free_rate
end

#strikeObject (readonly)

Returns the value of attribute strike.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def strike
  @strike
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def type
  @type
end

#underlying_priceObject (readonly)

Returns the value of attribute underlying_price.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def underlying_price
  @underlying_price
end

#valuation_dateObject (readonly)

Returns the value of attribute valuation_date.



15
16
17
# File 'lib/pure_greeks/option.rb', line 15

def valuation_date
  @valuation_date
end

Instance Method Details

#calculation_modelObject



67
68
69
# File 'lib/pure_greeks/option.rb', line 67

def calculation_model
  greeks.model
end

#deltaObject



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

def delta
  greeks.delta
end

#gammaObject



51
52
53
# File 'lib/pure_greeks/option.rb', line 51

def gamma
  greeks.gamma
end

#greeksObject



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

def greeks
  @greeks ||= compute_greeks
end

#implied_volatilityObject

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pure_greeks/option.rb', line 71

def implied_volatility
  return @implied_volatility if @implied_volatility
  raise InvalidInputError, "market_price required to solve for implied_volatility" unless @market_price

  @implied_volatility = ImpliedVolatility::BrentSolver.find_root(lower: 1e-6, upper: 5.0, tolerance: 1e-6) do |sigma|
    Engines::BlackScholesEuropean.price(
      type: @type,
      strike: @strike,
      underlying_price: @underlying_price,
      time_to_expiry: time_to_expiry,
      implied_volatility: sigma,
      risk_free_rate: @risk_free_rate,
      dividend_yield: @dividend_yield
    ) - @market_price
  end
end

#priceObject



43
44
45
# File 'lib/pure_greeks/option.rb', line 43

def price
  greeks.price
end

#rhoObject



63
64
65
# File 'lib/pure_greeks/option.rb', line 63

def rho
  greeks.rho
end

#thetaObject



55
56
57
# File 'lib/pure_greeks/option.rb', line 55

def theta
  greeks.theta
end

#time_to_expiryObject



35
36
37
# File 'lib/pure_greeks/option.rb', line 35

def time_to_expiry
  (@expiration - @valuation_date).to_f / DAYS_PER_YEAR
end

#vegaObject



59
60
61
# File 'lib/pure_greeks/option.rb', line 59

def vega
  greeks.vega
end