Class: Edgar::TTMCalculator
- Inherits:
-
Object
- Object
- Edgar::TTMCalculator
- Defined in:
- lib/edgar/ttm_calculator.rb
Overview
Calculates Trailing Twelve Months (TTM) values from quarterly financial facts.
Matches Python edgartools' TTMCalculator algorithm:
1. Classify facts by period duration (quarter: 70-120d, YTD_6M: 140-229d,
YTD_9M: 230-329d, annual: 330-420d)
2. Derive Q2=YTD_6M-Q1, Q3=YTD_9M-YTD_6M, Q4=FY-YTD_9M
3. Take 4 most recent quarters and sum them.
Falls back to fp-based quarterly selection when values lack start dates.
Constant Summary collapse
- QUARTER_MIN_DAYS =
70- QUARTER_MAX_DAYS =
120- YTD_6M_MIN_DAYS =
140- YTD_6M_MAX_DAYS =
229- YTD_9M_MIN_DAYS =
230- YTD_9M_MAX_DAYS =
329- ANNUAL_MIN_DAYS =
330- ANNUAL_MAX_DAYS =
420- PERIODIC_FORMS =
%w[10-K 10-K/A 10-Q 10-Q/A 20-F 20-F/A 40-F 40-F/A 6-K 6-K/A].freeze
Instance Method Summary collapse
-
#calculate ⇒ Object
Returns a hash with "end" and "val" keys, or nil if insufficient data.
-
#initialize(values) ⇒ TTMCalculator
constructor
A new instance of TTMCalculator.
Constructor Details
#initialize(values) ⇒ TTMCalculator
Returns a new instance of TTMCalculator.
24 25 26 |
# File 'lib/edgar/ttm_calculator.rb', line 24 def initialize(values) @values = values end |
Instance Method Details
#calculate ⇒ Object
Returns a hash with "end" and "val" keys, or nil if insufficient data.
29 30 31 32 33 34 35 36 |
# File 'lib/edgar/ttm_calculator.rb', line 29 def calculate all_quarters = quarterize(@values) return nil if all_quarters.length < 4 ttm_qs = all_quarters.last(4) sum = ttm_qs.sum { |v| v["val"].to_f } { "end" => ttm_qs.last["end"], "val" => sum } end |