options_library 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest +5 -0
- data/Rakefile +12 -0
- data/lib/options_library/option_calculator.rb +125 -0
- data/lib/options_library.rb +5 -0
- data/options_library.gemspec +29 -0
- metadata +64 -0
data/Manifest
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('options_library', '1.0.0') do |p|
|
6
|
+
p.description = 'A gem used to calc the price of an option.'
|
7
|
+
p.url = 'http://github.com/codertrader/options_library'
|
8
|
+
p.author = 'Dan Tylenda-Emmons'
|
9
|
+
p.email = 'jrubyist@gmail.com'
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# Author Dan Tylenda-Emmons
|
2
|
+
# Since Feb 13, 2011
|
3
|
+
# Based on Black-Scholes forumla for pricing options
|
4
|
+
|
5
|
+
module Option
|
6
|
+
class Calculator
|
7
|
+
class << self
|
8
|
+
|
9
|
+
include Math
|
10
|
+
|
11
|
+
# computes the call price sensitivity to a change in underlying price
|
12
|
+
def delta_call( underlying, strike, time, interest, sigma, dividend )
|
13
|
+
norm_sdist( d_one( underlying, strike, time, interest, sigma, dividend ) )
|
14
|
+
end
|
15
|
+
|
16
|
+
# computes the put price sensitivity to a change in underlying price
|
17
|
+
def delta_put( underlying, strike, time, interest, sigma, dividend )
|
18
|
+
call_delta( underlying, strike, time, interest, sigma, dividend ) - 1
|
19
|
+
end
|
20
|
+
|
21
|
+
# computes the option price sensitivity to a change in delta
|
22
|
+
def gamma( underlying, strike, time, interest, sigma, dividend )
|
23
|
+
phi( d_one( underlying, strike, time, interest, sigma, dividend ) ) / ( underlying * sigma * sqrt(time) )
|
24
|
+
end
|
25
|
+
|
26
|
+
# computes the option price sensitivity to a change in volatility
|
27
|
+
def vega( underlying, strike, time, interest, sigma, dividend )
|
28
|
+
0.01 * underlying * sqrt(time) * phi(d_one(underlying, strike, time, interest, sigma, dividend))
|
29
|
+
end
|
30
|
+
|
31
|
+
# computes the fair value of the call based on the knowns and assumed volatility (sigma)
|
32
|
+
def price_call( underlying, strike, time, interest, sigma, dividend )
|
33
|
+
d1 = d_one( underlying, strike, time, interest, sigma, dividend )
|
34
|
+
discounted_underlying = exp(-1.0 * dividend * time) * underlying
|
35
|
+
probability_weighted_value_of_being_exercised = discounted_underlying * norm_sdist( d1 )
|
36
|
+
|
37
|
+
d2 = d1 - ( sigma * sqrt(time) )
|
38
|
+
discounted_strike = exp(-1.0 * interest * time) * strike
|
39
|
+
probability_weighted_value_of_discounted_strike = discounted_strike * norm_sdist( d2 )
|
40
|
+
|
41
|
+
expected_value = probability_weighted_value_of_being_exercised - probability_weighted_value_of_discounted_strike
|
42
|
+
end
|
43
|
+
|
44
|
+
# computes the fair value of the put based on the knowns and assumed volatility (sigma)
|
45
|
+
def price_put( underlying, strike, time, interest, sigma, dividend )
|
46
|
+
d2 = d_two( underlying, strike, time, interest, sigma, dividend )
|
47
|
+
discounted_strike = strike * exp(-1.0 * interest * time)
|
48
|
+
probabiltity_weighted_value_of_discounted_strike = discounted_strike * norm_sdist( -1.0 * d2 )
|
49
|
+
|
50
|
+
d1 = d2 + ( sigma * sqrt(time) )
|
51
|
+
discounted_underlying = underlying * exp(-1.0 * dividend * time)
|
52
|
+
probability_weighted_value_of_being_exercised = discounted_underlying * norm_sdist( -1.0 * d1 )
|
53
|
+
|
54
|
+
expected_value = probabiltity_weighted_value_of_discounted_strike - probability_weighted_value_of_being_exercised
|
55
|
+
end
|
56
|
+
|
57
|
+
# finds the implied volatility based on the target_price passed in.
|
58
|
+
def implied_vol_call( underlying, strike, time, interest, target_price, dividend )
|
59
|
+
low, high = 0, 5
|
60
|
+
|
61
|
+
while( high - low > 0.0001 )
|
62
|
+
if( call_option( underlying, strike, time, interest, (high+low)/2.0, dividend ) > target_price )
|
63
|
+
high = (high + low) / 2.0
|
64
|
+
else
|
65
|
+
low = (high + low) / 2.0
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
(high + low) / 2.0
|
70
|
+
end
|
71
|
+
|
72
|
+
# finds the implied volatility based on the target_price passed in.
|
73
|
+
def implied_vol_put( underlying, strike, time, interest, target_price, dividend )
|
74
|
+
low, high = 0, 5
|
75
|
+
|
76
|
+
while( high - low > 0.0001 )
|
77
|
+
if( put_option( underlying, strike, time, interest, (high+low)/2.0, dividend ) > target_price )
|
78
|
+
high = (high + low) / 2.0
|
79
|
+
else
|
80
|
+
low = (high + low) / 2.0
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
(high + low) / 2.0
|
85
|
+
end
|
86
|
+
|
87
|
+
# probability of being exercised at maturity (must be greater than d2 by (sigma*sqrt(time)) if exercised)
|
88
|
+
def d_one( underlying, strike, time, interest, sigma, dividend )
|
89
|
+
numerator = ( log(underlying / strike) + (interest - dividend + 0.5 * sigma ** 2.0 ) * time)
|
90
|
+
denominator = ( sigma * sqrt(time) )
|
91
|
+
numerator / denominator
|
92
|
+
end
|
93
|
+
|
94
|
+
# probability of underlying reaching the strike price (must be smaller than d1 by (sigma*sqrt(time)) if exercised.
|
95
|
+
def d_two( underlying, strike, time, interest, sigma, dividend )
|
96
|
+
d_one( underlying, strike, time, interest, sigma, dividend ) - ( sigma * sqrt(time) )
|
97
|
+
end
|
98
|
+
|
99
|
+
# Normal Standard Distribution
|
100
|
+
# using Taylor's approximation
|
101
|
+
def norm_sdist( z )
|
102
|
+
return 0.0 if z < -8.0
|
103
|
+
return 1.0 if z > +8.0
|
104
|
+
|
105
|
+
i, sum, term = 3.0, 0.0, z
|
106
|
+
|
107
|
+
while( sum + term != sum )
|
108
|
+
sum = sum + term
|
109
|
+
term = term * z * z / i
|
110
|
+
i += 2.0
|
111
|
+
end
|
112
|
+
|
113
|
+
0.5 + sum * phi(z)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Standard Gaussian pdf
|
117
|
+
def phi(x)
|
118
|
+
numerator = exp(-1.0 * x*x / 2.0)
|
119
|
+
denominator = sqrt(2.0 * PI)
|
120
|
+
numerator / denominator
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{options_library}
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Dan Tylenda-Emmons"]
|
9
|
+
s.date = %q{2011-02-13}
|
10
|
+
s.description = %q{A gem used to calc the price of an option.}
|
11
|
+
s.email = %q{jrubyist@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["lib/options_library.rb", "lib/options_library/option_calculator.rb"]
|
13
|
+
s.files = ["Rakefile", "lib/options_library.rb", "lib/options_library/option_calculator.rb", "options_library.gemspec", "Manifest"]
|
14
|
+
s.homepage = %q{http://github.com/codertrader/options_library}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Options_library"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{options_library}
|
18
|
+
s.rubygems_version = %q{1.5.2}
|
19
|
+
s.summary = %q{A gem used to calc the price of an option.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
s.specification_version = 3
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
+
else
|
26
|
+
end
|
27
|
+
else
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: options_library
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dan Tylenda-Emmons
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-13 00:00:00 -06:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A gem used to calc the price of an option.
|
18
|
+
email: jrubyist@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- lib/options_library.rb
|
25
|
+
- lib/options_library/option_calculator.rb
|
26
|
+
files:
|
27
|
+
- Rakefile
|
28
|
+
- lib/options_library.rb
|
29
|
+
- lib/options_library/option_calculator.rb
|
30
|
+
- options_library.gemspec
|
31
|
+
- Manifest
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/codertrader/options_library
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --line-numbers
|
39
|
+
- --inline-source
|
40
|
+
- --title
|
41
|
+
- Options_library
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "1.2"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project: options_library
|
59
|
+
rubygems_version: 1.5.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: A gem used to calc the price of an option.
|
63
|
+
test_files: []
|
64
|
+
|