Class: SchwabRb::DataObjects::PriceHistory::Candle

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Candle

Returns a new instance of Candle.



109
110
111
112
113
114
115
116
117
118
# File 'lib/schwab_rb/data_objects/price_history.rb', line 109

def initialize(data)
  # Convert string keys to symbols if needed
  data = data.transform_keys(&:to_sym) if data.respond_to?(:transform_keys)
  @open = data[:open]
  @high = data[:high]
  @low = data[:low]
  @close = data[:close]
  @volume = data[:volume]
  @datetime_ms = data[:datetime]
end

Instance Attribute Details

#closeObject (readonly)

Returns the value of attribute close.



107
108
109
# File 'lib/schwab_rb/data_objects/price_history.rb', line 107

def close
  @close
end

#datetime_msObject (readonly)

Returns the value of attribute datetime_ms.



107
108
109
# File 'lib/schwab_rb/data_objects/price_history.rb', line 107

def datetime_ms
  @datetime_ms
end

#highObject (readonly)

Returns the value of attribute high.



107
108
109
# File 'lib/schwab_rb/data_objects/price_history.rb', line 107

def high
  @high
end

#lowObject (readonly)

Returns the value of attribute low.



107
108
109
# File 'lib/schwab_rb/data_objects/price_history.rb', line 107

def low
  @low
end

#openObject (readonly)

Returns the value of attribute open.



107
108
109
# File 'lib/schwab_rb/data_objects/price_history.rb', line 107

def open
  @open
end

#volumeObject (readonly)

Returns the value of attribute volume.



107
108
109
# File 'lib/schwab_rb/data_objects/price_history.rb', line 107

def volume
  @volume
end

Instance Method Details

#body_sizeObject



161
162
163
# File 'lib/schwab_rb/data_objects/price_history.rb', line 161

def body_size
  (@close - @open).abs
end

#dateObject



135
136
137
# File 'lib/schwab_rb/data_objects/price_history.rb', line 135

def date
  datetime&.to_date
end

#datetimeObject



131
132
133
# File 'lib/schwab_rb/data_objects/price_history.rb', line 131

def datetime
  Time.at(@datetime_ms / 1000.0) if @datetime_ms
end

#is_doji?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/schwab_rb/data_objects/price_history.rb', line 157

def is_doji?
  @close == @open
end

#is_green?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/schwab_rb/data_objects/price_history.rb', line 149

def is_green?
  @close > @open
end

#is_red?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/schwab_rb/data_objects/price_history.rb', line 153

def is_red?
  @close < @open
end

#lower_wickObject



173
174
175
# File 'lib/schwab_rb/data_objects/price_history.rb', line 173

def lower_wick
  [@open, @close].min - @low
end

#ohlc_averageObject



185
186
187
# File 'lib/schwab_rb/data_objects/price_history.rb', line 185

def ohlc_average
  (@open + @high + @low + @close) / 4.0
end

#price_changeObject



139
140
141
# File 'lib/schwab_rb/data_objects/price_history.rb', line 139

def price_change
  @close - @open
end

#price_change_percentObject



143
144
145
146
147
# File 'lib/schwab_rb/data_objects/price_history.rb', line 143

def price_change_percent
  return 0 if @open.zero?

  ((price_change / @open) * 100).round(4)
end

#to_hObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/schwab_rb/data_objects/price_history.rb', line 120

def to_h
  {
    open: @open,
    high: @high,
    low: @low,
    close: @close,
    volume: @volume,
    datetime: @datetime_ms
  }
end

#true_rangeObject



177
178
179
180
181
182
183
# File 'lib/schwab_rb/data_objects/price_history.rb', line 177

def true_range
  [
    @high - @low,
    (@high - @close).abs,
    (@low - @close).abs
  ].max
end

#typical_priceObject



189
190
191
# File 'lib/schwab_rb/data_objects/price_history.rb', line 189

def typical_price
  (@high + @low + @close) / 3.0
end

#upper_wickObject



169
170
171
# File 'lib/schwab_rb/data_objects/price_history.rb', line 169

def upper_wick
  @high - [@open, @close].max
end

#weighted_priceObject



193
194
195
# File 'lib/schwab_rb/data_objects/price_history.rb', line 193

def weighted_price
  (@high + @low + @close + @close) / 4.0
end

#wick_sizeObject



165
166
167
# File 'lib/schwab_rb/data_objects/price_history.rb', line 165

def wick_size
  upper_wick + lower_wick
end