Module: TA::MarketCalendar
- Defined in:
- lib/ta/market_calendar.rb
Overview
Market calendar utilities for trading day calculations
Constant Summary collapse
- MARKET_HOLIDAYS =
[ Date.new(2025, 8, 15), Date.new(2025, 10, 2), Date.new(2025, 8, 27) ].freeze
Class Method Summary collapse
- .last_trading_day(from: Date.today) ⇒ Object
- .prev_trading_day(from: Date.today) ⇒ Object
- .today_or_last_trading_day ⇒ Object
- .trading_day?(date) ⇒ Boolean
- .trading_days_ago(date, days) ⇒ Object
- .weekday?(date) ⇒ Boolean
Class Method Details
.last_trading_day(from: Date.today) ⇒ Object
23 24 25 26 27 |
# File 'lib/ta/market_calendar.rb', line 23 def self.last_trading_day(from: Date.today) d = from d -= 1 until trading_day?(d) d end |
.prev_trading_day(from: Date.today) ⇒ Object
29 30 31 32 33 |
# File 'lib/ta/market_calendar.rb', line 29 def self.prev_trading_day(from: Date.today) d = from - 1 d -= 1 until trading_day?(d) d end |
.today_or_last_trading_day ⇒ Object
35 36 37 |
# File 'lib/ta/market_calendar.rb', line 35 def self.today_or_last_trading_day trading_day?(Date.today) ? Date.today : last_trading_day(from: Date.today) end |
.trading_day?(date) ⇒ Boolean
19 20 21 |
# File 'lib/ta/market_calendar.rb', line 19 def self.trading_day?(date) weekday?(date) && !MARKET_HOLIDAYS.include?(date) end |
.trading_days_ago(date, days) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ta/market_calendar.rb', line 39 def self.trading_days_ago(date, days) raise ArgumentError, "days must be >= 0" if days.to_i.negative? d = trading_day?(date) ? date : today_or_last_trading_day count = 0 while count < days d = prev_trading_day(from: d) count += 1 end d end |
.weekday?(date) ⇒ Boolean
14 15 16 17 |
# File 'lib/ta/market_calendar.rb', line 14 def self.weekday?(date) w = date.wday w.between?(1, 5) end |