Class: Iro::Option
- Inherits:
-
Object
- Object
- Iro::Option
- Includes:
- Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
- Defined in:
- app/models/iro/option.rb
Overview
require ‘business_time’
Instance Attribute Summary collapse
-
#recompute ⇒ Object
Returns the value of attribute recompute.
Class Method Summary collapse
Instance Method Summary collapse
- #call_price ⇒ Object
-
#d1 ⇒ Object
## ## ## annual to daily:.
- #d2 ⇒ Object
- #put_price ⇒ Object
- #stdev ⇒ Object
- #symbol ⇒ Object
- #t ⇒ Object
Instance Attribute Details
#recompute ⇒ Object
Returns the value of attribute recompute.
12 13 14 |
# File 'app/models/iro/option.rb', line 12 def recompute @recompute end |
Class Method Details
.rate_annual ⇒ Object
170 171 172 |
# File 'app/models/iro/option.rb', line 170 def self.rate_annual 0.05 end |
.rate_daily ⇒ Object
# test
inn = 100
n.times { inn = inn*(1.0+out) }
inn
182 183 184 185 186 187 188 189 |
# File 'app/models/iro/option.rb', line 182 def self.rate_daily n = 250.0 # days # n = 12 # months out = (1.0+self.rate_annual)**(1.0/n) - 1.0 puts! out, 'rate_daily' return out end |
Instance Method Details
#call_price ⇒ Object
153 154 155 156 157 158 159 |
# File 'app/models/iro/option.rb', line 153 def call_price last = stock.last r = self.class.rate_annual out = N.cdf( d1 ) * last - N.cdf( d2 ) * strike * Math::E**( -1 * r * t ) return out end |
#d1 ⇒ Object
##
##
##
annual to daily:
AR = ((DR + 1)^365 – 1) x 100
##
##
##
From: https://www.investopedia.com/articles/optioninvestor/07/options_beat_market.asp
K :: strike price
S_t :: last
r :: risk-free rate
t :: time to maturity
C = S_t N( d1 ) - K e^-rt N( d2 )
d1 = ln( St / K ) + (r + theta**2 / 2 )t
/{ theta_s * sqrt( t ) }
d2 = d1 - theta_s sqrt( t )
##
## From: https://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model
##
D :: e^(rt) # discount factor
F :: e^(rt) S # forward price of underlying
C(F,t) = D[ N(d1)F - N(d2)K ]
d1 = ln(F/K) + stdev**2 t / 2
/{ stdev sqrt(t) }
d2 = d1 - stdev sqrt(t)
##
## From: https://www.daytrading.com/options-pricing-models
##
C0 = S0N(d1) – Xe-rtN(d2)
C0 = current call premium
S0 = current stock price
N(d1) = the probability that a value in a normal distribution will be less than d
N(d2) = the probability that the option will be in the money by expiration
X = strike price of the option
T = time until expiration (expressed in years)
r = risk-free interest rate
e = 2.71828, the base of the natural logarithm
ln = natural logarithm function
σ = standard deviation of the stock’s annualized rate of return (compounded continuously)
d1 = ln(S0/X) + (r + σ2/2)Tσ√T
d2 = d1 – σ√T
Note that:
Xe-rt = X/ert = the present value of the strike price using a continuously compounded interest rate
##
## From: https://www.wallstreetmojo.com/black-scholes-model/
##
## init
require 'distribution'
N = Distribution::Normal
stock = Iro::Stock.find_by ticker: 'NVDA'
strike = 910.0
r = Iro::Option.rate_daily
stdev = 91.0
t = 7.0
expires_on = '2024-03-22'
130 131 132 133 134 135 136 137 |
# File 'app/models/iro/option.rb', line 130 def d1 last = stock.last r = self.class.rate_annual out = Math.log( last / strike ) + ( r + stdev**2 / 2 ) * t out = out /( stdev * Math.sqrt(t) ) return out end |
#d2 ⇒ Object
138 139 140 141 142 143 144 |
# File 'app/models/iro/option.rb', line 138 def d2 last = stock.last r = self.class.rate_annual out = d1 - stdev * Math.sqrt( t ) return out end |
#put_price ⇒ Object
161 162 163 164 165 166 167 |
# File 'app/models/iro/option.rb', line 161 def put_price last = stock.last r = self.class.rate_annual out = N.cdf(-d2) * strike * exp(-r*t) - N.cdf(-d1) * last return out end |
#stdev ⇒ Object
149 150 151 152 |
# File 'app/models/iro/option.rb', line 149 def stdev recompute = nil stock.stdev( recompute: recompute ) end |
#symbol ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/iro/option.rb', line 38 def symbol if !self[:symbol] p_c_ = put_call == 'PUT' ? 'P' : 'C' strike_ = strike.to_i == strike ? strike.to_i : strike sym = "#{stock.ticker}_#{expires_on.strftime("%m%d%y")}#{p_c_}#{strike_}" # XYZ_011819P45 self[:symbol] = sym save end self[:symbol] end |
#t ⇒ Object
145 146 147 148 |
# File 'app/models/iro/option.rb', line 145 def t # t = 1.0 / 365 * Date.today.business_days_until( expires_on ) t = 1.0 / 365 * (expires_on - Date.today).to_i end |