Class: SchwabRb::DataObjects::PriceHistory::Candle
- Inherits:
-
Object
- Object
- SchwabRb::DataObjects::PriceHistory::Candle
- Defined in:
- lib/schwab_rb/data_objects/price_history.rb
Instance Attribute Summary collapse
-
#close ⇒ Object
readonly
Returns the value of attribute close.
-
#datetime_ms ⇒ Object
readonly
Returns the value of attribute datetime_ms.
-
#high ⇒ Object
readonly
Returns the value of attribute high.
-
#low ⇒ Object
readonly
Returns the value of attribute low.
-
#open ⇒ Object
readonly
Returns the value of attribute open.
-
#volume ⇒ Object
readonly
Returns the value of attribute volume.
Instance Method Summary collapse
- #body_size ⇒ Object
- #date ⇒ Object
- #datetime ⇒ Object
-
#initialize(data) ⇒ Candle
constructor
A new instance of Candle.
- #is_doji? ⇒ Boolean
- #is_green? ⇒ Boolean
- #is_red? ⇒ Boolean
- #lower_wick ⇒ Object
- #ohlc_average ⇒ Object
- #price_change ⇒ Object
- #price_change_percent ⇒ Object
- #to_h ⇒ Object
- #true_range ⇒ Object
- #typical_price ⇒ Object
- #upper_wick ⇒ Object
- #weighted_price ⇒ Object
- #wick_size ⇒ Object
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
#close ⇒ Object (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_ms ⇒ Object (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 |
#high ⇒ Object (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 |
#low ⇒ Object (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 |
#open ⇒ Object (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 |
#volume ⇒ Object (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_size ⇒ Object
161 162 163 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 161 def body_size (@close - @open).abs end |
#date ⇒ Object
135 136 137 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 135 def date datetime&.to_date end |
#datetime ⇒ Object
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
157 158 159 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 157 def is_doji? @close == @open end |
#is_green? ⇒ Boolean
149 150 151 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 149 def is_green? @close > @open end |
#is_red? ⇒ Boolean
153 154 155 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 153 def is_red? @close < @open end |
#lower_wick ⇒ Object
173 174 175 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 173 def lower_wick [@open, @close].min - @low end |
#ohlc_average ⇒ Object
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_change ⇒ Object
139 140 141 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 139 def price_change @close - @open end |
#price_change_percent ⇒ Object
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_h ⇒ Object
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_range ⇒ Object
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_price ⇒ Object
189 190 191 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 189 def typical_price (@high + @low + @close) / 3.0 end |
#upper_wick ⇒ Object
169 170 171 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 169 def upper_wick @high - [@open, @close].max end |
#weighted_price ⇒ Object
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_size ⇒ Object
165 166 167 |
# File 'lib/schwab_rb/data_objects/price_history.rb', line 165 def wick_size upper_wick + lower_wick end |