Class: DhanHQ::MarketData::OHLCSeries
- Inherits:
-
Object
- Object
- DhanHQ::MarketData::OHLCSeries
- Includes:
- Enumerable
- Defined in:
- lib/DhanHQ/market_data/ohlc_series.rb
Overview
A time series of OHLCV candles for a single instrument.
Wraps historical data responses into a convenient array-like structure with helper methods for analysis.
Defined Under Namespace
Classes: Candle
Instance Attribute Summary collapse
-
#candles ⇒ Object
readonly
Returns the value of attribute candles.
-
#exchange_segment ⇒ Object
readonly
Returns the value of attribute exchange_segment.
-
#security_id ⇒ Object
readonly
Returns the value of attribute security_id.
Class Method Summary collapse
-
.from_response(response) ⇒ Object
Build an OHLCSeries from a raw historical data API response.
Instance Method Summary collapse
-
#average_close ⇒ Object
Calculate the average close price.
-
#closes ⇒ Object
Get all close prices.
-
#date_range ⇒ Object
Get the date range of the series.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#first ⇒ Object
Get the oldest candle.
-
#highs ⇒ Object
Get all high prices.
-
#initialize(candles = [], metadata = {}) ⇒ OHLCSeries
constructor
A new instance of OHLCSeries.
-
#last ⇒ Object
Get the most recent candle.
-
#lows ⇒ Object
Get all low prices.
-
#opens ⇒ Object
Get all open prices.
-
#price_range ⇒ Object
Calculate the price range (highest high - lowest low).
- #size ⇒ Object
-
#slice_range(from_timestamp, to_timestamp) ⇒ Object
Slice the series by date range (requires timestamps).
-
#tail(count) ⇒ Object
Take the last N candles.
-
#total_volume ⇒ Object
Calculate the total volume across all candles.
-
#volumes ⇒ Object
Get all volumes.
Constructor Details
#initialize(candles = [], metadata = {}) ⇒ OHLCSeries
Returns a new instance of OHLCSeries.
53 54 55 56 57 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 53 def initialize(candles = [], = {}) @candles = candles @security_id = [:security_id] @exchange_segment = [:exchange_segment] end |
Instance Attribute Details
#candles ⇒ Object (readonly)
Returns the value of attribute candles.
51 52 53 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 51 def candles @candles end |
#exchange_segment ⇒ Object (readonly)
Returns the value of attribute exchange_segment.
51 52 53 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 51 def exchange_segment @exchange_segment end |
#security_id ⇒ Object (readonly)
Returns the value of attribute security_id.
51 52 53 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 51 def security_id @security_id end |
Class Method Details
.from_response(response) ⇒ Object
Build an OHLCSeries from a raw historical data API response.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 60 def self.from_response(response) data = response.is_a?(Hash) ? (response[:data] || response["data"] || response) : response data = [data] unless data.is_a?(Array) candles = data.map do |candle| Candle.new( timestamp: candle[:timestamp] || candle["timestamp"], open: (candle[:open] || candle["open"]).to_f, high: (candle[:high] || candle["high"]).to_f, low: (candle[:low] || candle["low"]).to_f, close: (candle[:close] || candle["close"]).to_f, volume: (candle[:volume] || candle["volume"]).to_i, open_interest: candle[:open_interest] || candle["open_interest"] ) end new(candles) end |
Instance Method Details
#average_close ⇒ Object
Calculate the average close price.
139 140 141 142 143 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 139 def average_close return nil if empty? closes.sum / size end |
#closes ⇒ Object
Get all close prices.
92 93 94 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 92 def closes @candles.map(&:close) end |
#date_range ⇒ Object
Get the date range of the series.
127 128 129 130 131 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 127 def date_range return nil if empty? [first., last.] end |
#each ⇒ Object
79 80 81 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 79 def each(&) @candles.each(&) end |
#empty? ⇒ Boolean
87 88 89 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 87 def empty? @candles.empty? end |
#first ⇒ Object
Get the oldest candle.
122 123 124 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 122 def first @candles.first end |
#highs ⇒ Object
Get all high prices.
102 103 104 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 102 def highs @candles.map(&:high) end |
#last ⇒ Object
Get the most recent candle.
117 118 119 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 117 def last @candles.last end |
#lows ⇒ Object
Get all low prices.
107 108 109 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 107 def lows @candles.map(&:low) end |
#opens ⇒ Object
Get all open prices.
97 98 99 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 97 def opens @candles.map(&:open) end |
#price_range ⇒ Object
Calculate the price range (highest high - lowest low).
146 147 148 149 150 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 146 def price_range return nil if empty? highs.max - lows.min end |
#size ⇒ Object
83 84 85 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 83 def size @candles.size end |
#slice_range(from_timestamp, to_timestamp) ⇒ Object
Slice the series by date range (requires timestamps).
153 154 155 156 157 158 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 153 def slice_range(, ) self.class.new( @candles.select { |c| c..between?(, ) }, { security_id: @security_id, exchange_segment: @exchange_segment } ) end |
#tail(count) ⇒ Object
Take the last N candles.
161 162 163 164 165 166 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 161 def tail(count) self.class.new( @candles.last(count), { security_id: @security_id, exchange_segment: @exchange_segment } ) end |
#total_volume ⇒ Object
Calculate the total volume across all candles.
134 135 136 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 134 def total_volume @candles.sum(&:volume) end |
#volumes ⇒ Object
Get all volumes.
112 113 114 |
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 112 def volumes @candles.map(&:volume) end |