finance_engine 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a461095fc7ba2a9fe186d3e9ad0d97b17578cc2f
4
- data.tar.gz: 58b5fc4581f833b153a1c1a0b71c2bc38d6f99b4
3
+ metadata.gz: 403e3f9b415c340a85e6ff725393e532720ea17b
4
+ data.tar.gz: a19bbe00cd3ef4ed21a8e567c2e6b60a57c0f677
5
5
  SHA512:
6
- metadata.gz: 478421efb129d5123c87911e6f76cc6253aa77b8b1c76e5ed7aeaef5b227ae766d70fb9115b3895d3b2799fc068072224f45e54808052fbf224bc7c6afa475e6
7
- data.tar.gz: 7480b5610dbb0d07d28266e3b5dc50855282c6a36e0186a29290734dc340d78fc224e422fd208a0deb1fa58f29bf3823b4bbd42f5b0e0822f93a27939763c164
6
+ metadata.gz: e15d712f3965f2078ead43e10b216c1965f2a187f93e966881ad3e344a50343c3b3fbb85b4aa644c665a181b1e8ab2ac8517222a105af15c642d51d996156084
7
+ data.tar.gz: 7eac3d8c0890b42cd453069b9684eae71fe3e9105fe178742ba0d683556df3ca42dce8c921c192bba20c00da38d2b0716a8b10dcc48982db25e66cec52709a82
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- finance_engine (0.0.1)
4
+ finance_engine (0.0.2)
5
5
  distribution
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -44,6 +44,8 @@ At the moment there are only five limited modules. Here we will try to demonstra
44
44
 
45
45
  FE.gordon_growth_model({ :rate => 0.05, :dividend => 100, :growth => 0.02, :value => 3333.33 }) - Using 3 of the 4 input values the Gordon Growth Model method will calculate the 4th input.
46
46
 
47
+ FE.gordon_growth_model({ :rate => 0.05, :dividend => 100, :growth => 0.02 }) - Will return a value of 3333.33.
48
+
47
49
  4) FBS = FinanceEngine::Black_Scholes
48
50
 
49
51
  FBS.new({ :current_stock_price => 100, :time => 5, :strike_price => 95, :risk_free_rate => 0.05, :volatility => 0.25 }) - After initializing a new instance of Black_Scholes run the build options method. This will create and store the put call and price data on the instance of Black Scholes. This is different from the other modules in that it will store the variables in the instance.
@@ -54,14 +56,22 @@ At the moment there are only five limited modules. Here we will try to demonstra
54
56
 
55
57
  FTVM.future_value_cash_flows(years, cashflows, rates) - Years, cashflows and rates are all arrays matching in length. This method will return the future value of a series of cash flows when given the time, amount and effective interest rates.
56
58
 
59
+ 6) AO = FinanceEngine::American_Options
60
+
61
+ AO.new(Price, Volatility, Risk Free Rate, Strike Price) - Initializes a new instance of the american options storing the price, volatility, rf rate and strike price.
62
+ AO.build_american_options(years to expiration, number of periods within that year to exercise options) - this method will build out a binary tree to calculate the current option prices based on the future stock value and strike price.
63
+ AO.current_call_price - returns the calculated current call price.
64
+ AO.current_put_price - returns the calculated current put price.
57
65
 
58
66
 
59
67
 
60
68
 
61
69
  ## Contributing
62
70
 
63
- 1. Fork it ( https://github.com/[my-github-username]/finance_engine/fork )
71
+ 1. Fork it ( https://github.com/penkar/finance_engine/fork )
64
72
  2. Create your feature branch (`git checkout -b my-new-feature`)
65
73
  3. Commit your changes (`git commit -am 'Add some feature'`)
66
74
  4. Push to the branch (`git push origin my-new-feature`)
67
75
  5. Create a new Pull Request
76
+
77
+ What would be useful? Methods in Time value of money to calculate the rate, and nper.
Binary file
@@ -0,0 +1,72 @@
1
+ require 'distribution'
2
+ module FinanceEngine
3
+ class American_Options
4
+ attr_accessor :price, :vol, :rf, :strike, :time, :tree, :up_factor, :down_factor, :probability
5
+ def initialize(price, volatility, risk_free_rate, strike)
6
+ @price = price
7
+ @vol = volatility
8
+ @rf = risk_free_rate
9
+ @strike = strike
10
+ @tree = {}
11
+ end
12
+
13
+ def build_american_options(years_to_expiration, periods_in_year)
14
+ up_fac(periods_in_year**-1)
15
+ down_fac(periods_in_year**-1)
16
+ probability_increase_price(periods_in_year**-1)
17
+ create_tree_for_years(years_to_expiration, periods_in_year**-1)
18
+ end
19
+
20
+ def create_tree_for_years(years_to_expiration, periods_in_year, node='original_', price=@price)
21
+ @tree[node]={'price'=>price.round(4)}
22
+ if years_to_expiration.round(3) == 0
23
+ @tree[node]['call'] = call_value(price)
24
+ @tree[node]['put'] = put_value(price)
25
+ elsif years_to_expiration.round(3) > 0
26
+ create_tree_for_years((years_to_expiration - periods_in_year), periods_in_year, node+'u', price * @up_factor )
27
+ create_tree_for_years((years_to_expiration - periods_in_year), periods_in_year, node+'d', price * @down_factor)
28
+ get_put_value(node, periods_in_year)
29
+ get_call_value(node, periods_in_year)
30
+ end
31
+ end
32
+
33
+ def get_put_value(node, periods_in_year)
34
+ up = @tree[node+'u']['put']
35
+ down = @tree[node+'d']['put']
36
+ @tree[node]['put'] = (Math::E**(-@rf* periods_in_year)*(@probability * up)+Math::E**(-@rf* periods_in_year)*((1-@probability) * down)).round(4)
37
+ end
38
+
39
+ def get_call_value(node, periods_in_year)
40
+ up = @tree[node+'u']['call']
41
+ down = @tree[node+'d']['call']
42
+ @tree[node]['call'] = (Math::E**(-@rf* periods_in_year)*(@probability * up)+Math::E**(-@rf* periods_in_year)*((1-@probability) * down)).round(4)
43
+ end
44
+
45
+ def put_value(price)
46
+ [0,price - strike].max.round(4)
47
+ end
48
+
49
+ def call_value(price)
50
+ [0,strike - price].max.round(4)
51
+ end
52
+
53
+ def up_fac(time)
54
+ @up_factor = Math::E**(@vol*(time)**(0.5))
55
+ end
56
+
57
+ def down_fac(time)
58
+ @down_factor = Math::E**(-@vol*(time)**(0.5))
59
+ end
60
+
61
+ def probability_increase_price(time)
62
+ @probability = (Math::E**(time*@rf)-@down_factor)/(@up_factor - @down_factor)
63
+ end
64
+
65
+ def current_call_price
66
+ @tree['original_']['call']
67
+ end
68
+ def current_put_price
69
+ @tree['original_']['put']
70
+ end
71
+ end
72
+ end
@@ -47,20 +47,12 @@ module FinanceEngine
47
47
  #Calculates the present value of an annuity. Both formulas below, non-loop in use.
48
48
  def self.pv_annuity(pmt,time,rate)
49
49
  pv = pmt*(1-(1+rate)**-time)/rate
50
- # pv = 0
51
- # 1.upto(time) do |x|
52
- # pv += pmt / ((1+rate)**x)
53
- # end
54
50
  return pv
55
51
  end
56
52
 
57
53
  #Calculates the future value of an annuity. Both formulas below, non-loop in use.
58
54
  def self.fv_annuity(pmt,time,rate)
59
55
  fv = pmt*((1+rate)**time -1)/rate
60
- # fv = 0
61
- # 1.upto(time) do |x|
62
- # fv += pmt * ((1+rate)**(time-x))
63
- # end
64
56
  return fv
65
57
  end
66
58
 
@@ -1,3 +1,3 @@
1
1
  module FinanceEngine
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "finance_engine/version"
2
+ require_relative 'finance_engine/americanoptionpricing.rb'
2
3
  require_relative 'finance_engine/optionpricing.rb'
3
4
  require_relative 'finance_engine/tvm.rb'
4
5
  require_relative 'finance_engine/eareay.rb'
@@ -0,0 +1,42 @@
1
+ require_relative '../spec_helper.rb'
2
+
3
+ describe FinanceEngine::American_Options do
4
+ it 'It should be able to initialize with price, volatility and rate.' do
5
+ first = FinanceEngine::American_Options.new(100,0.2,0.05,90)
6
+ expect(first.rf).to eq(0.05)
7
+ expect(first.vol).to eq(0.2)
8
+ expect(first.price).to eq(100)
9
+ expect(first.strike).to eq(90)
10
+ end
11
+
12
+ it 'Can calc the % up and down a stock will go.' do
13
+ first = FinanceEngine::American_Options.new(32,0.2,0.10,90)
14
+ ans = first.up_fac(1.0/12)
15
+ ans = first.down_fac(1.0/12)
16
+ expect(first.up_factor.round(4)).to be_within(0.0005).of(1.0594)
17
+ expect(first.down_factor.round(4)).to be_within(0.0005).of(0.9439)
18
+ end
19
+
20
+ it 'It should be able calculate the correct probability of an increase of price for a 1 month period.' do
21
+ first = FinanceEngine::American_Options.new(32,0.2,0.10,90)
22
+ expect(first.price).to eq(32)
23
+ first.up_fac(1.0/12)
24
+ first.down_fac(1.0/12)
25
+ first.probability_increase_price(1.0/12)
26
+ expect(first.probability.round(4)).to be_within(0.0005).of(0.5576)
27
+ end
28
+
29
+ it 'Be able to create tree.' do
30
+ first = FinanceEngine::American_Options.new(32,0.2,0.10,35)
31
+ first.build_american_options(2, 1)
32
+ expect(first.tree['original_dd']['price']).to be_within(0.0005).of(21.450)
33
+ expect(first.tree['original_uu']['price']).to be_within(0.0005).of(47.7384)
34
+ end
35
+
36
+ it 'Get american put and call option values.' do
37
+ first = FinanceEngine::American_Options.new(100,0.3,0.05,100)
38
+ first.build_american_options(1, 4)
39
+ expect(first.tree['original_']['put']).to be_within(0.0005).of(13.5240)
40
+ expect(first.tree['original_']['call']).to be_within(0.0005).of(8.6469)
41
+ end
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finance_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey Penkar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,14 +95,17 @@ files:
95
95
  - README.md
96
96
  - README.md~
97
97
  - Rakefile
98
+ - finance_engine-0.0.1.gem
98
99
  - finance_engine.gemspec
99
100
  - lib/finance_engine.rb
101
+ - lib/finance_engine/americanoptionpricing.rb
100
102
  - lib/finance_engine/annuity.rb
101
103
  - lib/finance_engine/eareay.rb
102
104
  - lib/finance_engine/gordon_growth_model.rb
103
105
  - lib/finance_engine/optionpricing.rb
104
106
  - lib/finance_engine/tvm.rb
105
107
  - lib/finance_engine/version.rb
108
+ - spec/entities/americanoptionpricing_spec.rb
106
109
  - spec/entities/annuity_spec.rb
107
110
  - spec/entities/eareay_spec.rb
108
111
  - spec/entities/gordon_growth_model_spec.rb
@@ -134,6 +137,7 @@ signing_key:
134
137
  specification_version: 4
135
138
  summary: Financial Engine Gem. Financial methods and tools.
136
139
  test_files:
140
+ - spec/entities/americanoptionpricing_spec.rb
137
141
  - spec/entities/annuity_spec.rb
138
142
  - spec/entities/eareay_spec.rb
139
143
  - spec/entities/gordon_growth_model_spec.rb