stock_market_days 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ require 'date'
2
+ require 'stock_market_days/calculator'
3
+
4
+ module StockMarketDays
5
+ module_function
6
+ MARKET_DAYS_FILE=File.join(File.dirname(__FILE__), 'market_open_days', 'nyse_market_days.csv')
7
+
8
+ @@default_calculator = StockMarketDays::Calculator.new(MARKET_DAYS_FILE)
9
+
10
+ def is_market_day?(date = Date.today)
11
+ @@default_calculator.is_market_day?(date)
12
+ end
13
+
14
+ def market_days_between(begin_date, end_date)
15
+ @@default_calculator.market_days_between(begin_date, end_date)
16
+ end
17
+
18
+ def market_days_from(begin_date, days)
19
+ @@default_calculator.market_days_from(begin_date, days)
20
+ end
21
+
22
+ def next_market_day(from_date=Date.today)
23
+ @@default_calculator.market_days_from(from_date, 1)
24
+ end
25
+
26
+ end
@@ -0,0 +1,49 @@
1
+ require 'stock_market_days/utility_methods'
2
+
3
+ module StockMarketDays
4
+ class Calculator
5
+ include UtilityMethods
6
+
7
+ attr_reader :market_days_list
8
+
9
+ def initialize(market_days_file)
10
+ file_contents = File.open(market_days_file).read
11
+ @market_days_list = file_contents.split("\n").map { |date_s| Date.parse(date_s) }
12
+ end
13
+
14
+ def is_market_day?(date=Date.today)
15
+ market_days_list.include?(date)
16
+ end
17
+
18
+ # gets number of market days between begin_day (excluding) and end_day (including)
19
+ def market_days_between(begin_date, end_date)
20
+ unless (begin_date < end_date) && (end_date <= market_days_list.max)
21
+ raise "Please enter a begin date before the end date, prior to #{market_days_list.max}"
22
+ end
23
+
24
+ days_between=0
25
+ market_days_list.any? do |date|
26
+ if date > begin_date && date <= end_date
27
+ days_between += 1
28
+ end
29
+ return days_between if date > end_date
30
+ end
31
+ days_between
32
+ end
33
+
34
+ def market_days_from(begin_day, days)
35
+ begin_index = market_days_list.index(
36
+ market_days_list.find { |md| md >= begin_day }
37
+ )
38
+ if market_days_list[begin_index] == begin_day
39
+ market_days_list[begin_index + days]
40
+ elsif market_days_list[begin_index] > begin_day
41
+ market_days_list[begin_index - 1 + days]
42
+ else
43
+ raise "Calculator Error - This shouldn't happen in StockMarketDays#market_days_from"
44
+ end
45
+ end
46
+
47
+
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ module StockMarketDays
2
+ module UtilityMethods
3
+
4
+ def time_strip_date(time)
5
+ time.to_r / 86400 % 1
6
+ end
7
+
8
+ def time_strip_time_zone(time)
9
+ Time.parse(time.strftime('%a, %d %b %Y %H:%M:%S'))
10
+ end
11
+
12
+ def compare_time(time1, time2)
13
+ (time1.to_r / 86400) % 1 <=> (time2.to_r / 86400) % 1
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module StockMarketDays
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,107 @@
1
+ require 'pry'
2
+
3
+ Dir[File.join(File.dirname(__FILE__), 'support', '/**/*.rb')].each { |f| require(f) }
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
8
+ # this file to always be loaded, without a need to explicitly require it in any
9
+ # files.
10
+ #
11
+ # Given that it is always loaded, you are encouraged to keep this file as
12
+ # light-weight as possible. Requiring heavyweight dependencies from this file
13
+ # will add to the boot time of your test suite on EVERY test run, even for an
14
+ # individual file that may not need all of that loaded. Instead, consider making
15
+ # a separate helper file that requires the additional dependencies and performs
16
+ # the additional setup, and require it from the spec files that actually need
17
+ # it.
18
+ #
19
+ # The `.rspec` file also contains a few flags that are not defaults but that
20
+ # users commonly want.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
48
+ # have no way to turn it off -- the option exists only for backwards
49
+ # compatibility in RSpec 3). It causes shared context metadata to be
50
+ # inherited by the metadata hash of host groups and examples, rather than
51
+ # triggering implicit auto-inclusion in groups with matching metadata.
52
+ config.shared_context_metadata_behavior = :apply_to_host_groups
53
+
54
+ # The settings below are suggested to provide a good initial experience
55
+ # with RSpec, but feel free to customize to your heart's content.
56
+ =begin
57
+ # This allows you to limit a spec run to individual examples or groups
58
+ # you care about by tagging them with `:focus` metadata. When nothing
59
+ # is tagged with `:focus`, all examples get run. RSpec also provides
60
+ # aliases for `it`, `describe`, and `context` that include `:focus`
61
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
62
+ config.filter_run_when_matching :focus
63
+
64
+ # Allows RSpec to persist some state between runs in order to support
65
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
66
+ # you configure your source control system to ignore this file.
67
+ config.example_status_persistence_file_path = "spec/examples.txt"
68
+
69
+ # Limits the available syntax to the non-monkey patched syntax that is
70
+ # recommended. For more details, see:
71
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
72
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
73
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
74
+ config.disable_monkey_patching!
75
+
76
+ # This setting enables warnings. It's recommended, but in some cases may
77
+ # be too noisy due to issues in dependencies.
78
+ config.warnings = true
79
+
80
+ # Many RSpec users commonly either run the entire suite or an individual
81
+ # file, and it's useful to allow more verbose output when running an
82
+ # individual spec file.
83
+ if config.files_to_run.one?
84
+ # Use the documentation formatter for detailed output,
85
+ # unless a formatter has already been configured
86
+ # (e.g. via a command-line flag).
87
+ config.default_formatter = 'doc'
88
+ end
89
+
90
+ # Print the 10 slowest examples and example groups at the
91
+ # end of the spec run, to help surface which specs are running
92
+ # particularly slow.
93
+ config.profile_examples = 10
94
+
95
+ # Run specs in random order to surface order dependencies. If you find an
96
+ # order dependency and want to debug it, you can fix the order by providing
97
+ # the seed, which is printed after each run.
98
+ # --seed 1234
99
+ config.order = :random
100
+
101
+ # Seed global randomization in this process using the `--seed` CLI option.
102
+ # Setting this allows you to use `--seed` to deterministically reproduce
103
+ # test failures related to randomization by passing the same `--seed` value
104
+ # as the one that triggered the failure.
105
+ Kernel.srand config.seed
106
+ =end
107
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+ require 'stock_market_days'
3
+
4
+ describe StockMarketDays do
5
+
6
+ context 'included in a class' do
7
+ let(:dummy_class) do
8
+ class Dummy
9
+ include StockMarketDays
10
+
11
+ def is_market_day_delegator(date)
12
+ is_market_day?(date)
13
+ end
14
+ end
15
+
16
+ Dummy.new
17
+ end
18
+
19
+ context '#is_market_day?' do
20
+ subject { dummy_class.is_market_day_delegator(date) }
21
+ let(:date) { Date.new(2019,12,30) }
22
+
23
+ it { is_expected.to be_truthy }
24
+ end
25
+ end
26
+
27
+ context '#is_market_day?' do
28
+ subject { described_class.is_market_day?(date) }
29
+
30
+ context 'is a market day' do
31
+ let(:date) { Date.new(2022,1,5) }
32
+
33
+ it { is_expected.to be_truthy }
34
+ end
35
+
36
+ context 'is Martin Luther King Day' do
37
+ let(:date) { Date.new(2022,1,17) }
38
+
39
+ it { is_expected.to be_falsey }
40
+ end
41
+
42
+ context 'is Presidents Day' do
43
+ let(:date) { Date.new(2023,2,20) }
44
+
45
+ it { is_expected.to be_falsey }
46
+ end
47
+
48
+ context 'is Thanksgiving' do
49
+ let(:date) { Date.new(2023,11,23) }
50
+
51
+ it { is_expected.to be_falsey }
52
+ end
53
+
54
+ context 'is Christmas (observed)' do
55
+ let(:date) { Date.new(2027,12,24) }
56
+
57
+ it { is_expected.to be_falsey }
58
+ end
59
+ end
60
+
61
+ context '#market_days_between' do
62
+ subject { described_class.market_days_between(begin_date, end_date) }
63
+
64
+ context 'pure trading days' do
65
+ let(:begin_date) { Date.new(2020,4,20) }
66
+ let(:end_date) { Date.new(2020,5,27) }
67
+
68
+ it { is_expected.to eql(26) }
69
+ end
70
+
71
+ context 'end date on a weekend' do
72
+ let(:begin_date) { Date.new(2020,4,20) }
73
+ let(:end_date) { Date.new(2020,5,31) }
74
+
75
+ it { is_expected.to eql(28) }
76
+ end
77
+
78
+ context 'end date is less than begin date' do
79
+ let(:begin_date) { Date.new(2020,5,31) }
80
+ let(:end_date) { Date.new(2020,4,20) }
81
+
82
+ it { expect { subject }.to raise_error(RuntimeError) }
83
+ end
84
+ end
85
+
86
+ context '#market_days_from' do
87
+ subject { described_class.market_days_from(begin_date, count_days) }
88
+
89
+ context 'on a holiday weekend' do
90
+ context '0 days' do
91
+ let(:begin_date) { Date.new(2019,9,1) }
92
+ let(:count_days) { 0 }
93
+
94
+ it { is_expected.to eql(Date.new(2019,8,30)) } # returns most recent trading day
95
+ end
96
+
97
+ context '1 day' do
98
+ let(:begin_date) { Date.new(2019,9,1) }
99
+ let(:count_days) { 1 }
100
+
101
+ it { is_expected.to eql(Date.new(2019,9,3)) } # returns next trading day
102
+ end
103
+
104
+ context '2 days' do
105
+ let(:begin_date) { Date.new(2019,9,1) }
106
+ let(:count_days) { 2 }
107
+
108
+ it { is_expected.to eql(Date.new(2019,9,4)) }
109
+ end
110
+ end
111
+
112
+ context 'over a weekend' do
113
+ context '0 days' do
114
+ let(:begin_date) { Date.new(2019,8,30) }
115
+ let(:count_days) { 0 }
116
+
117
+ it { is_expected.to eql(Date.new(2019,8,30)) }
118
+ end
119
+
120
+ context '1 day' do
121
+ let(:begin_date) { Date.new(2019,8,30) }
122
+ let(:count_days) { 1 }
123
+
124
+ it { is_expected.to eql(Date.new(2019,9,3)) }
125
+ end
126
+
127
+ context '2 days' do
128
+ let(:begin_date) { Date.new(2019,8,30) }
129
+ let(:count_days) { 2 }
130
+
131
+ it { is_expected.to eql(Date.new(2019,9,4)) }
132
+ end
133
+ end
134
+
135
+ context 'midweek' do
136
+ let(:begin_date) { Date.new(2019,9,3) }
137
+ let(:count_days) { 2 }
138
+
139
+ it { is_expected.to eql(Date.new(2019,9,5)) }
140
+ end
141
+ end
142
+
143
+ context '#next_market_day' do
144
+ subject { described_class.next_market_day(from_date) }
145
+
146
+ context 'on a holiday weekend' do
147
+ let(:from_date) { Date.new(2019,9,1) }
148
+
149
+ it { is_expected.to eql(Date.new(2019,9,3)) }
150
+ end
151
+
152
+ context 'on a regular trading day' do
153
+ let(:from_date) { Date.new(2019,9,5) }
154
+
155
+ it { is_expected.to eql(Date.new(2019,9,6)) }
156
+ end
157
+
158
+ context 'on a Friday' do
159
+ let(:from_date) { Date.new(2019,8,30) }
160
+
161
+ it { is_expected.to eql(Date.new(2019,9,3)) }
162
+ end
163
+ end
164
+
165
+ end
File without changes
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stock_market_days/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stock_market_days"
8
+ spec.version = StockMarketDays::VERSION
9
+ spec.authors = ["Winston Kotzan"]
10
+ spec.email = ["wak@wakproductions.com"]
11
+ spec.summary = %q{A gem for determining the days that the stock market is open}
12
+ spec.description = "Tells you whether the US stock market is open on a given day, up to 2059"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = [`git ls-files`.split($/)] + Dir["lib/**/*"]
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "bundler", ">= 1.7"
22
+ spec.add_dependency "rake"
23
+
24
+ spec.add_development_dependency "clipboard"
25
+ spec.add_development_dependency "rspec", ">= 3.2"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "webmock"
28
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stock_market_days
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Winston Kotzan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-22 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: :runtime
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: '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: clipboard
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
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Tells you whether the US stock market is open on a given day, up to 2059
98
+ email:
99
+ - wak@wakproductions.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".rspec"
105
+ - CHANGELOG.md
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - README.md
109
+ - Rakefile
110
+ - lib/manually_invoked_build/holiday_calculator.rb
111
+ - lib/market_open_days/easter_dates.csv
112
+ - lib/market_open_days/nyse_market_days.csv
113
+ - lib/stock_market_days.rb
114
+ - lib/stock_market_days/calculator.rb
115
+ - lib/stock_market_days/utility_methods.rb
116
+ - lib/stock_market_days/version.rb
117
+ - spec/spec_helper.rb
118
+ - spec/stock_market_days/stock_market_days_spec.rb
119
+ - spec/support/.keep
120
+ - stock_market_days.gemspec
121
+ homepage: ''
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.1.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A gem for determining the days that the stock market is open
144
+ test_files:
145
+ - spec/spec_helper.rb
146
+ - spec/stock_market_days/stock_market_days_spec.rb
147
+ - spec/support/.keep