greeks 1.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.
@@ -0,0 +1,79 @@
1
+ require File.expand_path("../../spec_helper.rb", File.dirname(__FILE__))
2
+
3
+ describe "Math::GreekCalculations::misc_price_ratio_log_less_rates" do
4
+ include Math
5
+ include Math::GreekCalculations
6
+ include Math::GreekCalculationHelpers
7
+
8
+ let(:stock_price) { 10.00 }
9
+ let(:stock_dividend_rate_f) { 0.00 }
10
+ let(:option_expires_pct_year) { 1.00 }
11
+
12
+ context "exactly at the money" do
13
+ let(:option_strike) { 10.00 }
14
+
15
+ context "0% interest" do
16
+ let(:federal_reserve_interest_rate_f) { 0.00 }
17
+ let(:expected) { 0.00 }
18
+
19
+ it { var_price_ratio_log_less_rates().should === expected }
20
+ end
21
+
22
+ context "0.02% interest" do
23
+ let(:federal_reserve_interest_rate_f) { 0.0002 }
24
+ let(:expected) { 0.0002 }
25
+
26
+ it { var_price_ratio_log_less_rates().should === expected }
27
+ end
28
+ end
29
+
30
+ context "out of the money" do
31
+ let(:option_strike) { 15.00 }
32
+
33
+ context "0% interest" do
34
+ let(:federal_reserve_interest_rate_f) { 0.00 }
35
+ let(:expected) { -0.40546510810816444 }
36
+
37
+ it { var_price_ratio_log_less_rates().should === expected }
38
+ end
39
+
40
+ context "0.02% interest" do
41
+ let(:federal_reserve_interest_rate_f) { 0.0002 }
42
+ let(:expected) { -0.40526510810816446 }
43
+
44
+ it { var_price_ratio_log_less_rates().should === expected }
45
+ end
46
+ end
47
+
48
+ context "in of the money" do
49
+ let(:option_strike) { 5.00 }
50
+
51
+ context "0% interest" do
52
+ let(:federal_reserve_interest_rate_f) { 0.00 }
53
+ let(:expected) { 0.6931471805599453 }
54
+
55
+ it { var_price_ratio_log_less_rates().should === expected }
56
+ end
57
+
58
+ context "0.02% interest" do
59
+ let(:federal_reserve_interest_rate_f) { 0.0002 }
60
+ let(:expected) { 0.6933471805599453 }
61
+
62
+ it { var_price_ratio_log_less_rates().should === expected }
63
+ end
64
+ end
65
+ end
66
+
67
+
68
+ describe "Math::GreekCalculations::misc_price_vs_rate_vs_expires" do
69
+ include Math
70
+ include Math::GreekCalculations
71
+
72
+ let(:stock_price) { 10.00 }
73
+ let(:option_expires_pct_year) { 1.00 }
74
+ let(:stock_dividend_rate_f) { 0.05 }
75
+ let(:expected) { 9.51229424500714 }
76
+ subject { misc_price_vs_rate_vs_expires(:stock_price => stock_price, :option_expires_pct_year => option_expires_pct_year, :stock_dividend_rate_f => stock_dividend_rate_f) }
77
+
78
+ it { should === expected }
79
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path("../../spec_helper.rb", File.dirname(__FILE__))
2
+
3
+ describe "Math::GreekCalculations::vega" do
4
+ extend Math::GreekCalculations
5
+ include Math::GreekCalculations
6
+
7
+ it { vega(:price_vs_rate_vs_expires => 1.0, :nd1 => 1.0, :option_expires_pct_year_sqrt => 1.0, :iv => 'any value').should === 0.01 }
8
+ it { vega(:price_vs_rate_vs_expires => 3.0, :nd1 => 2.0, :option_expires_pct_year_sqrt => 1.0, :iv => 'any value').should === 0.06 }
9
+ it { vega(:price_vs_rate_vs_expires => 10.0, :nd1 => 10.0, :option_expires_pct_year_sqrt => 5.0, :iv => 'any value').should === 5.0 }
10
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path("../spec_helper.rb", File.dirname(__FILE__))
2
+
3
+ describe Math::Greeks::Calculator do
4
+ let(:entity) do
5
+ {
6
+ :symbol => 'AMD',
7
+ :type => :stock,
8
+ :date => '2013-05-19',
9
+ }
10
+ end
11
+
12
+ let(:date) { '2013-06-21' }
13
+ let(:days) { 35.0 }
14
+
15
+ let(:call) do
16
+ {
17
+ :strike => 4.50,
18
+ :bid => 0.16,
19
+ :iv => 61.92,
20
+ :delta => 8.59,
21
+ :gamma => 5.57,
22
+ :vega => 1.81,
23
+ :rho => 0.75,
24
+ :theta => -2.51,
25
+ :break_even => 21.38,
26
+ }
27
+ end
28
+
29
+ let(:put) do
30
+ {
31
+ :strike => 4.50,
32
+ :bid => 0.59,
33
+ :iv => 61.93,
34
+ :delta => -4.57,
35
+ :gamma => -2.84,
36
+ :vega => 0.49,
37
+ :rho => -0.55,
38
+ :theta => -0.68,
39
+ :break_even => 45.66,
40
+ }
41
+ end
42
+
43
+ let(:base_opts) do
44
+ {
45
+ :stock_price => 4.07,
46
+ :stock_dividend_rate => 0.00,
47
+ :option_expires_in_days => 35.0,
48
+ :federal_reserve_interest_rate => 0.01,
49
+ :option_type => nil,
50
+ :option_price => nil,
51
+ :option_strike => nil,
52
+ }
53
+ end
54
+
55
+ context "call option" do
56
+ let(:opts) do
57
+ base_opts.merge(
58
+ :option_type => :call,
59
+ :option_price => call[:bid],
60
+ :option_strike => call[:strike],
61
+ )
62
+ end
63
+
64
+ subject(:calc) { Math::Greeks::Calculator.new(opts) }
65
+
66
+ it { calc.stock_price.should === opts[:stock_price] }
67
+ it { calc.stock_dividend_rate.should === opts[:stock_dividend_rate] }
68
+ it { calc.option_type.should === opts[:option_type] }
69
+ it { calc.option_price.should === opts[:option_price] }
70
+ it { calc.option_strike.should === opts[:option_strike] }
71
+ it { calc.option_expires_in_days.should === opts[:option_expires_in_days] }
72
+ it { calc.federal_reserve_interest_rate.should === opts[:federal_reserve_interest_rate] }
73
+
74
+ it { calc.to_hash[:iv].should === call[:iv] }
75
+ it { calc.to_hash[:delta].should === call[:delta] }
76
+ it { calc.to_hash[:gamma].should === call[:gamma] }
77
+ it { calc.to_hash[:vega].should === call[:vega] }
78
+ it { calc.to_hash[:theta].should === call[:theta] }
79
+ it { calc.to_hash[:rho].should === call[:rho] }
80
+ it { calc.to_hash[:break_even].round(2).should === call[:break_even] }
81
+ end
82
+
83
+ context "put option" do
84
+ let(:opts) do
85
+ base_opts.merge(
86
+ :option_type => :put,
87
+ :option_price => put[:bid],
88
+ :option_strike => put[:strike],
89
+ )
90
+ end
91
+
92
+ subject(:calc) { Math::Greeks::Calculator.new(opts) }
93
+
94
+ it { calc.stock_price.should === opts[:stock_price] }
95
+ it { calc.stock_dividend_rate.should === opts[:stock_dividend_rate] }
96
+ it { calc.option_type.should === opts[:option_type] }
97
+ it { calc.option_price.should === opts[:option_price] }
98
+ it { calc.option_strike.should === opts[:option_strike] }
99
+ it { calc.option_expires_in_days.should === opts[:option_expires_in_days] }
100
+ it { calc.federal_reserve_interest_rate.should === opts[:federal_reserve_interest_rate] }
101
+
102
+ it { calc.to_hash[:iv].should === put[:iv] }
103
+ it { calc.to_hash[:delta].should === put[:delta] }
104
+ it { calc.to_hash[:gamma].should === put[:gamma] }
105
+ it { calc.to_hash[:vega].should === put[:vega] }
106
+ it { calc.to_hash[:theta].should === put[:theta] }
107
+ it { calc.to_hash[:rho].should === put[:rho] }
108
+ it { calc.to_hash[:break_even].round(2).should === put[:break_even] }
109
+ end
110
+ end
@@ -0,0 +1,89 @@
1
+ require 'rubygems'
2
+
3
+ require 'rspec'
4
+ require 'rspec-expectations'
5
+ require 'benchmark'
6
+
7
+ $:.push File.expand_path("../lib", File.dirname(__FILE__))
8
+ require 'greeks'
9
+
10
+ $spec_root = File.dirname(__FILE__)
11
+
12
+ RSpec.configure do |config|
13
+ old_verbose, $VERBOSE = $VERBOSE, nil
14
+
15
+ def puts(s)
16
+ file = File.basename(caller(1).first)
17
+ super("puts() from #{file}: #{s}")
18
+ end
19
+
20
+ def print(s)
21
+ file = File.basename(caller(1).first)
22
+ super("print() from #{file}: #{s}")
23
+ end
24
+
25
+ def p(s)
26
+ file = File.basename(caller(1).first)
27
+ super("p() from #{file}: #{s}")
28
+ end
29
+
30
+ def test_speed(x_speed, x_times = 1000)
31
+ time = Benchmark.realtime { x_times.times { |n| @result = yield } }
32
+ (time / x_times).should < x_speed
33
+ @result
34
+ end
35
+ end
36
+
37
+ module Math
38
+ module GreekCalculationHelpers
39
+ include Math
40
+ include Math::GreekCalculations
41
+
42
+ def var_price_ratio_log_less_rates
43
+ misc_price_ratio_log_less_rates(
44
+ :stock_price => stock_price,
45
+ :option_strike => option_strike,
46
+ :option_expires_pct_year => option_expires_pct_year,
47
+ :federal_reserve_interest_rate_f => federal_reserve_interest_rate_f,
48
+ :stock_dividend_rate_f => stock_dividend_rate_f
49
+ )
50
+ end
51
+
52
+ def var_price_vs_rate_vs_expires
53
+ misc_price_vs_rate_vs_expires(
54
+ :stock_price => stock_price,
55
+ :option_expires_pct_year => option_expires_pct_year,
56
+ :stock_dividend_rate_f => stock_dividend_rate_f
57
+ )
58
+ end
59
+
60
+ def var_vega
61
+ iv_vega(stock_price, option_strike, option_expires_pct_year, volatility_guess, federal_reserve_interest_rate_f, stock_dividend_rate_f, var_price_ratio_log_less_rates, var_price_vs_rate_vs_expires)
62
+ end
63
+
64
+ def var_vega
65
+ iv_vega(stock_price, option_strike, option_expires_pct_year, Math::sqrt(option_expires_pct_year), volatility_guess, federal_reserve_interest_rate_f, stock_dividend_rate_f, var_price_ratio_log_less_rates, var_price_vs_rate_vs_expires)
66
+ end
67
+
68
+ def var_option_price
69
+ iv_option_price(stock_price, option_strike, option_expires_pct_year, Math::sqrt(option_expires_pct_year), volatility_guess, federal_reserve_interest_rate_f, stock_dividend_rate_f, option_type, var_price_ratio_log_less_rates, var_price_vs_rate_vs_expires, misc_strike_vs_fed_vs_expires(:option_strike => option_strike, :option_expires_pct_year => option_expires_pct_year, :federal_reserve_interest_rate_f => federal_reserve_interest_rate_f))
70
+ end
71
+
72
+ def var_iv
73
+ iv(
74
+ :stock_price => stock_price,
75
+ :option_strike => option_strike,
76
+ :option_expires_pct_year => option_expires_pct_year,
77
+ :option_expires_pct_year_sqrt => Math::sqrt(option_expires_pct_year),
78
+ :federal_reserve_interest_rate_f => federal_reserve_interest_rate_f,
79
+ :stock_dividend_rate_f => stock_dividend_rate_f,
80
+ :option_type => option_type,
81
+ :option_price => option_price,
82
+ :rate_vs_expires => misc_rate_vs_expires(:option_expires_pct_year => option_expires_pct_year, :stock_dividend_rate_f => stock_dividend_rate_f),
83
+ :price_vs_rate_vs_expires => var_price_vs_rate_vs_expires,
84
+ :strike_vs_fed_vs_expires => misc_strike_vs_fed_vs_expires(:option_strike => option_strike, :option_expires_pct_year => option_expires_pct_year, :federal_reserve_interest_rate_f => federal_reserve_interest_rate_f),
85
+ :price_ratio_log_less_rates => var_price_ratio_log_less_rates
86
+ )
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: greeks
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Glenn Nagel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: require_all
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hash_plus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-expectations
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Calculate greeks (iv, delta, gamma, vega, rho, theta)
70
+ email:
71
+ - glenn@mercury-wireless.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - README.md
80
+ - greeks.gemspec
81
+ - lib/greeks.rb
82
+ - lib/greeks/calculations/delta.rb
83
+ - lib/greeks/calculations/gamma.rb
84
+ - lib/greeks/calculations/iv.rb
85
+ - lib/greeks/calculations/normal_distribution.rb
86
+ - lib/greeks/calculations/rho.rb
87
+ - lib/greeks/calculations/theta.rb
88
+ - lib/greeks/calculations/time_values.rb
89
+ - lib/greeks/calculations/vega.rb
90
+ - lib/greeks/version.rb
91
+ - spec/greeks/calculations/delta_spec.rb
92
+ - spec/greeks/calculations/gamma_spec.rb
93
+ - spec/greeks/calculations/iv_option_price_spec.rb
94
+ - spec/greeks/calculations/iv_spec.rb
95
+ - spec/greeks/calculations/iv_vega_spec.rb
96
+ - spec/greeks/calculations/normal_distribution_spec.rb
97
+ - spec/greeks/calculations/rho_spec.rb
98
+ - spec/greeks/calculations/theta_spec.rb
99
+ - spec/greeks/calculations/time_values_spec.rb
100
+ - spec/greeks/calculations/vega_spec.rb
101
+ - spec/greeks/greeks_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/gnagel/greeks
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ - tasks
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.0.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Calculate greeks for options trading (Implied Volatility, Delta, Gamma, Vega,
128
+ Rho, and Theta)
129
+ test_files:
130
+ - spec/greeks/calculations/delta_spec.rb
131
+ - spec/greeks/calculations/gamma_spec.rb
132
+ - spec/greeks/calculations/iv_option_price_spec.rb
133
+ - spec/greeks/calculations/iv_spec.rb
134
+ - spec/greeks/calculations/iv_vega_spec.rb
135
+ - spec/greeks/calculations/normal_distribution_spec.rb
136
+ - spec/greeks/calculations/rho_spec.rb
137
+ - spec/greeks/calculations/theta_spec.rb
138
+ - spec/greeks/calculations/time_values_spec.rb
139
+ - spec/greeks/calculations/vega_spec.rb
140
+ - spec/greeks/greeks_spec.rb
141
+ - spec/spec_helper.rb