bondie 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff581c38e3f0f86a771e43cdd6c584c257b6da07
4
+ data.tar.gz: 994a7c8a080ef137bb24323235af6f4f9c8d5a96
5
+ SHA512:
6
+ metadata.gz: e41f6717415f6903ddd3ea0c256100b7270f18e2ef1b4ce08fde18086508c65232dd4c90612826dde7fc3f5055500ab3565c39f7bab3a513f62a6779d3fa24a9
7
+ data.tar.gz: 34a19436ae93c544a245fb04304c8e35f3be4ae4f72d917bc6e9e65bf238e6495c9deceefd4615b83bce03edfd43127fce02ad1b89df3ff51e1876ab79de9eb5
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bondie.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 kobi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Bondie
2
+
3
+ Bondie is a simple calculator for the bond properties.
4
+ It can calculate Yield To Maturity (YTM), interest payments dates or price based on YTM.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'bondie'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install bondie
21
+
22
+ ## Usage
23
+
24
+ Calculation YTM based on price and date:
25
+
26
+ ```ruby
27
+ issue = Bondie::Issue.new(coupon: 0.0622, maturity_date: Date.parse('2016-08-22'), coupon_frequency: 4)
28
+ issue.ytm(Date.parse('2015-09-07'), price: 100.8)
29
+ ```
30
+
31
+ It will not count exact yield, but it will approximate it - you can set maximum approximation error by adding `approximation_error` parameter to the method.
32
+
33
+ You can also check interest payments dates:
34
+
35
+ ```ruby
36
+ issue = Bondie::Issue.new(coupon: 0.0622, maturity_date: Date.parse('2016-08-22'), coupon_frequency: 4)
37
+ issue.interest_payments(Date.parse('2015-09-07'))
38
+ ```
39
+
40
+ Currently it will not skip weekends or other days without bond quotations, so it can be inaccurate.
41
+
42
+ Also it's possible to check price on which it will generate given yield:
43
+
44
+ ```ruby
45
+ issue = Bondie::Issue.new(coupon: 0.0622, maturity_date: Date.parse('2016-08-22'), coupon_frequency: 4)
46
+ issue.price(0.05156, Date.parse('2015-09-07'))
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bondie.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bondie/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bondie"
8
+ spec.version = Bondie::VERSION
9
+ spec.authors = ["bewon"]
10
+ spec.email = ["strong.neon@gmail.com"]
11
+ spec.summary = 'Bondie is a simple calculator for the bond properties - e.g. yield (YTM).'
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/bewon/bondie"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.3"
24
+
25
+ spec.required_ruby_version = '>= 2.0.0'
26
+ end
data/lib/bondie.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "bondie/version"
2
+ require "bondie/issue"
3
+
4
+ module Bondie
5
+
6
+ end
@@ -0,0 +1,69 @@
1
+ require 'date'
2
+
3
+ module Bondie
4
+ class Issue
5
+
6
+ DEFAULT_APPROXIMATION_ERROR = 0.001
7
+
8
+ def initialize(coupon: nil, maturity_date: nil, coupon_frequency: nil)
9
+ @coupon = coupon
10
+ @maturity_date = maturity_date
11
+ @coupon_frequency = coupon_frequency
12
+ end
13
+
14
+ def price(ytm, date, fees: 0)
15
+ price_for_irr(irr_from_ytm(ytm), date, fees: fees)
16
+ end
17
+
18
+ def interest_payments(from_date)
19
+ payments = []
20
+ date = @maturity_date
21
+ while date >= from_date
22
+ payments << date
23
+ date = date.prev_month(12/@coupon_frequency)
24
+ end
25
+ payments.sort
26
+ end
27
+
28
+ def ytm(date, price: 100, fees: 0, approximation_error: DEFAULT_APPROXIMATION_ERROR)
29
+ ytm_down, ytm_up = ytm_limits(price, date, fees: fees)
30
+ approximate_ytm(ytm_down, ytm_up, price, date, fees: fees, approximation_error: approximation_error)
31
+ end
32
+
33
+
34
+ private
35
+
36
+ def irr_from_ytm(ytm)
37
+ (ytm/@coupon_frequency.to_f + 1) ** @coupon_frequency - 1
38
+ end
39
+
40
+
41
+ def approximate_ytm(ytm_down, ytm_up, price, date, fees: 0, approximation_error: DEFAULT_APPROXIMATION_ERROR)
42
+ approx_ytm = (ytm_up + ytm_down) / 2.0
43
+ return approx_ytm if ((ytm_up - approx_ytm)/approx_ytm).abs <= approximation_error
44
+ p = price(approx_ytm, date, fees: fees)
45
+ ytm_down, ytm_up = p < price ? [ytm_down, approx_ytm] : [approx_ytm, ytm_up]
46
+ approximate_ytm(ytm_down, ytm_up, price, date, fees: fees, approximation_error: approximation_error)
47
+ end
48
+
49
+ def ytm_limits(price, date, fees: 0)
50
+ ytm_up = 0.1
51
+ ytm_up *= 10 while price(ytm_up, date, fees: fees) > price
52
+ # IRR will be never lower than -1, so YTM should be never lower than -coupon_frequency (see irr_from_ytm)
53
+ ytm_down = price(0, date, fees: fees) > price ? 0.0 : -@coupon_frequency.to_f
54
+ [ytm_down, ytm_up]
55
+ end
56
+
57
+ def price_for_irr(irr, date, fees: 0)
58
+ raise "Date is after maturity_date!" if date > @maturity_date
59
+ last = date
60
+ interest_payments(date).map do |payday|
61
+ interest = @coupon * 100 * ((payday-last)/365)
62
+ interest += 100 if payday == @maturity_date
63
+ last = payday
64
+ interest / ((1+irr) ** ((payday-date)/365))
65
+ end.inject(:+) / (1+fees)
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Bondie
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bondie::Issue do
4
+ let(:issue) { Bondie::Issue.new(coupon: 0.0622, maturity_date: Date.parse('2016-08-22'), coupon_frequency: 4) }
5
+
6
+ describe 'price' do
7
+ it 'should return price for which bond should be bought to make given YTM' do
8
+ price = issue.price(0.05156, Date.parse('2015-09-07'), fees: 0.0019)
9
+ expect(price).to be_within(0.01).of(100.8)
10
+ end
11
+ end
12
+
13
+ describe 'interest_payments' do
14
+ it 'should return dates of interest payments' do
15
+ dates = %w(2015-11-22 2016-02-22 2016-05-22 2016-08-22).map { |d| Date.parse(d) }
16
+ expect(issue.interest_payments(Date.parse('2015-09-07'))).to match_array(dates)
17
+ end
18
+ it 'should return also from_date if it\'s one of the payments date' do
19
+ date = Date.parse('2016-05-22')
20
+ expect(issue.interest_payments(date)).to include(date)
21
+ end
22
+ end
23
+
24
+ describe 'ytm' do
25
+ it 'should calculate YTM for given date and price' do
26
+ result = issue.ytm(Date.parse('2015-09-07'), price: 100.8, fees: 0.0019)
27
+ expect(result).to be_within(0.0001).of(0.05156)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,98 @@
1
+ require 'bondie'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # The `.rspec` file also contains a few flags that are not defaults but that
18
+ # users commonly want.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+ RSpec.configure do |config|
22
+ # rspec-expectations config goes here. You can use an alternate
23
+ # assertion/expectation library such as wrong or the stdlib/minitest
24
+ # assertions if you prefer.
25
+ config.expect_with :rspec do |expectations|
26
+ # This option will default to `true` in RSpec 4. It makes the `description`
27
+ # and `failure_message` of custom matchers include text for helper methods
28
+ # defined using `chain`, e.g.:
29
+ # be_bigger_than(2).and_smaller_than(4).description
30
+ # # => "be bigger than 2 and smaller than 4"
31
+ # ...rather than:
32
+ # # => "be bigger than 2"
33
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34
+ end
35
+
36
+ # rspec-mocks config goes here. You can use an alternate test double
37
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
38
+ config.mock_with :rspec do |mocks|
39
+ # Prevents you from mocking or stubbing a method that does not exist on
40
+ # a real object. This is generally recommended, and will default to
41
+ # `true` in RSpec 4.
42
+ mocks.verify_partial_doubles = true
43
+ end
44
+
45
+ # The settings below are suggested to provide a good initial experience
46
+ # with RSpec, but feel free to customize to your heart's content.
47
+ =begin
48
+ # These two settings work together to allow you to limit a spec run
49
+ # to individual examples or groups you care about by tagging them with
50
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # get run.
52
+ config.filter_run :focus
53
+ config.run_all_when_everything_filtered = true
54
+
55
+ # Allows RSpec to persist some state between runs in order to support
56
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
57
+ # you configure your source control system to ignore this file.
58
+ config.example_status_persistence_file_path = "spec/examples.txt"
59
+
60
+ # Limits the available syntax to the non-monkey patched syntax that is
61
+ # recommended. For more details, see:
62
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
64
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = 'doc'
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bondie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - bewon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ description: Bondie is a simple calculator for the bond properties - e.g. yield (YTM).
56
+ email:
57
+ - strong.neon@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bondie.gemspec
69
+ - lib/bondie.rb
70
+ - lib/bondie/issue.rb
71
+ - lib/bondie/version.rb
72
+ - spec/bondie/issue_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/bewon/bondie
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.0.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Bondie is a simple calculator for the bond properties - e.g. yield (YTM).
98
+ test_files:
99
+ - spec/bondie/issue_spec.rb
100
+ - spec/spec_helper.rb