Class: PureGreeks::Option
- Inherits:
-
Object
- Object
- PureGreeks::Option
- 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
-
#dividend_yield ⇒ Object
readonly
Returns the value of attribute dividend_yield.
-
#exercise_style ⇒ Object
readonly
Returns the value of attribute exercise_style.
-
#expiration ⇒ Object
readonly
Returns the value of attribute expiration.
-
#risk_free_rate ⇒ Object
readonly
Returns the value of attribute risk_free_rate.
-
#strike ⇒ Object
readonly
Returns the value of attribute strike.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#underlying_price ⇒ Object
readonly
Returns the value of attribute underlying_price.
-
#valuation_date ⇒ Object
readonly
Returns the value of attribute valuation_date.
Instance Method Summary collapse
- #calculation_model ⇒ Object
- #delta ⇒ Object
- #gamma ⇒ Object
- #greeks ⇒ Object
- #implied_volatility ⇒ Object
-
#initialize(exercise_style:, type:, strike:, expiration:, underlying_price:, risk_free_rate:, dividend_yield:, valuation_date:, implied_volatility: nil, market_price: nil) ⇒ Option
constructor
A new instance of Option.
- #price ⇒ Object
- #rho ⇒ Object
- #theta ⇒ Object
- #time_to_expiry ⇒ Object
- #vega ⇒ Object
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, , expiration, valuation_date) @exercise_style = exercise_style @type = type @strike = strike.to_f @expiration = expiration @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_yield ⇒ Object (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_style ⇒ Object (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 |
#expiration ⇒ Object (readonly)
Returns the value of attribute expiration.
15 16 17 |
# File 'lib/pure_greeks/option.rb', line 15 def expiration @expiration end |
#risk_free_rate ⇒ Object (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 |
#strike ⇒ Object (readonly)
Returns the value of attribute strike.
15 16 17 |
# File 'lib/pure_greeks/option.rb', line 15 def strike @strike end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/pure_greeks/option.rb', line 15 def type @type end |
#underlying_price ⇒ Object (readonly)
Returns the value of attribute underlying_price.
15 16 17 |
# File 'lib/pure_greeks/option.rb', line 15 def @underlying_price end |
#valuation_date ⇒ Object (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_model ⇒ Object
67 68 69 |
# File 'lib/pure_greeks/option.rb', line 67 def calculation_model greeks.model end |
#delta ⇒ Object
47 48 49 |
# File 'lib/pure_greeks/option.rb', line 47 def delta greeks.delta end |
#gamma ⇒ Object
51 52 53 |
# File 'lib/pure_greeks/option.rb', line 51 def gamma greeks.gamma end |
#greeks ⇒ Object
39 40 41 |
# File 'lib/pure_greeks/option.rb', line 39 def greeks @greeks ||= compute_greeks end |
#implied_volatility ⇒ Object
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 |
#price ⇒ Object
43 44 45 |
# File 'lib/pure_greeks/option.rb', line 43 def price greeks.price end |
#rho ⇒ Object
63 64 65 |
# File 'lib/pure_greeks/option.rb', line 63 def rho greeks.rho end |
#theta ⇒ Object
55 56 57 |
# File 'lib/pure_greeks/option.rb', line 55 def theta greeks.theta end |
#time_to_expiry ⇒ Object
35 36 37 |
# File 'lib/pure_greeks/option.rb', line 35 def time_to_expiry (@expiration - @valuation_date).to_f / DAYS_PER_YEAR end |
#vega ⇒ Object
59 60 61 |
# File 'lib/pure_greeks/option.rb', line 59 def vega greeks.vega end |