Class: SchwabRb::DataObjects::PriceHistory
- Inherits:
-
Object
- Object
- SchwabRb::DataObjects::PriceHistory
- Includes:
- Enumerable
- Defined in:
- lib/schwab_rb/data_objects/price_history.rb
Defined Under Namespace
Classes: Candle
Instance Attribute Summary collapse
-
#candles ⇒ Object
readonly
Returns the value of attribute candles.
-
#empty ⇒ Object
readonly
Returns the value of attribute empty.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
Instance Method Summary collapse
- #average_price ⇒ Object
- #candles_for_date_range(start_date, end_date) ⇒ Object
- #count ⇒ Object (also: #size, #length)
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first_candle ⇒ Object
- #highest_price ⇒ Object
- #highest_volume ⇒ Object
-
#initialize(data) ⇒ PriceHistory
constructor
A new instance of PriceHistory.
- #last_candle ⇒ Object
- #lowest_price ⇒ Object
- #price_range ⇒ Object
- #to_h ⇒ Object
- #total_volume ⇒ Object
Constructor Details
#initialize(data) ⇒ PriceHistory
Returns a new instance of PriceHistory.
14 15 16 17 18 19 20 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 14 def initialize(data) # Convert string keys to symbols if needed data = data.transform_keys(&:to_sym) if data.respond_to?(:transform_keys) @symbol = data[:symbol] @empty = data[:empty] @candles = data[:candles]&.map { |candle_data| Candle.new(candle_data) } || [] end |
Instance Attribute Details
#candles ⇒ Object (readonly)
Returns the value of attribute candles.
6 7 8 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 6 def candles @candles end |
#empty ⇒ Object (readonly)
Returns the value of attribute empty.
6 7 8 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 6 def empty @empty end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
6 7 8 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 6 def symbol @symbol end |
Class Method Details
.build(data) ⇒ Object
9 10 11 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 9 def build(data) new(data) end |
Instance Method Details
#average_price ⇒ Object
81 82 83 84 85 86 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 81 def average_price return nil if @candles.empty? total_price = @candles.map(&:close).sum total_price / @candles.length.to_f end |
#candles_for_date_range(start_date, end_date) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 48 def candles_for_date_range(start_date, end_date) = start_date.to_time.to_i * 1000 = (end_date.to_time + 24 * 60 * 60 - 1).to_i * 1000 @candles.select do |candle| candle.datetime_ms >= && candle.datetime_ms <= end end |
#count ⇒ Object Also known as: size, length
34 35 36 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 34 def count @candles.length end |
#each(&block) ⇒ Object
98 99 100 101 102 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 98 def each(&block) return enum_for(:each) unless block_given? @candles.each(&block) end |
#empty? ⇒ Boolean
30 31 32 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 30 def empty? @empty == true || @candles.empty? end |
#first_candle ⇒ Object
40 41 42 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 40 def first_candle @candles.first end |
#highest_price ⇒ Object
57 58 59 60 61 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 57 def highest_price return nil if @candles.empty? @candles.map(&:high).max end |
#highest_volume ⇒ Object
69 70 71 72 73 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 69 def highest_volume return nil if @candles.empty? @candles.map(&:volume).max end |
#last_candle ⇒ Object
44 45 46 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 44 def last_candle @candles.last end |
#lowest_price ⇒ Object
63 64 65 66 67 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 63 def lowest_price return nil if @candles.empty? @candles.map(&:low).min end |
#price_range ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 88 def price_range return nil if @candles.empty? { high: highest_price, low: lowest_price, range: highest_price - lowest_price } end |
#to_h ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 22 def to_h { symbol: @symbol, empty: @empty, candles: @candles.map(&:to_h) } end |
#total_volume ⇒ Object
75 76 77 78 79 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 75 def total_volume return 0 if @candles.empty? @candles.map(&:volume).sum end |