Class: SchwabRb::DataObjects::PriceHistory

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/schwab_rb/data_objects/price_history.rb

Defined Under Namespace

Classes: Candle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#candlesObject (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

#emptyObject (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

#symbolObject (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_priceObject



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_timestamp = start_date.to_time.to_i * 1000
  end_timestamp = (end_date.to_time + 24 * 60 * 60 - 1).to_i * 1000

  @candles.select do |candle|
    candle.datetime_ms >= start_timestamp && candle.datetime_ms <= end_timestamp
  end
end

#countObject 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

Returns:

  • (Boolean)


30
31
32
# File 'lib/schwab_rb/data_objects/price_history.rb', line 30

def empty?
  @empty == true || @candles.empty?
end

#first_candleObject



40
41
42
# File 'lib/schwab_rb/data_objects/price_history.rb', line 40

def first_candle
  @candles.first
end

#highest_priceObject



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_volumeObject



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_candleObject



44
45
46
# File 'lib/schwab_rb/data_objects/price_history.rb', line 44

def last_candle
  @candles.last
end

#lowest_priceObject



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_rangeObject



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_hObject



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_volumeObject



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