Module: TA::Candles
- Defined in:
- lib/ta/candles.rb
Overview
Candle data processing and resampling utilities
Class Method Summary collapse
Class Method Details
.from_series(series) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ta/candles.rb', line 19 def from_series(series) ts = series["timestamp"] || series[:timestamp] open = series["open"] || series[:open] high = series["high"] || series[:high] low = series["low"] || series[:low] close = series["close"] || series[:close] vol = series["volume"] || series[:volume] return [] unless ts && open && high && low && close && vol return [] if close.empty? (0...close.size).map do |i| { t: parse_time_like(ts[i]), o: open[i].to_f, h: high[i].to_f, l: low[i].to_f, c: close[i].to_f, v: vol[i].to_f } end rescue StandardError [] end |
.parse_time_like(val) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/ta/candles.rb', line 10 def parse_time_like(val) return Time.at(val) if val.is_a?(Numeric) s = val.to_s return Time.at(s.to_i) if /\A\d+\z/.match?(s) && s.length >= 10 && s.length <= 13 Time.parse(s) end |
.resample(candles, minutes) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ta/candles.rb', line 37 def resample(candles, minutes) return candles if minutes.to_i == 1 grouped = {} candles.each do |c| key = Time.at((c[:t].to_i / 60) / minutes * minutes * 60) b = (grouped[key] ||= { t: key, o: c[:o], h: c[:h], l: c[:l], c: c[:c], v: 0.0 }) b[:h] = [b[:h], c[:h]].max b[:l] = [b[:l], c[:l]].min b[:c] = c[:c] b[:v] += c[:v] end grouped.keys.sort.map { |k| grouped[k] } end |