Class: DhanHQ::MarketData::OHLCSeries

Inherits:
Object
  • Object
show all
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.

Examples:

Build a series from historical data response

response = DhanHQ::Models::HistoricalData.daily(
  security_id: "11536",
  exchange_segment: "NSE_EQ",
  instrument: "EQUITY",
  from_date: "2024-01-01",
  to_date: "2024-12-31"
)
series = DhanHQ::MarketData::OHLCSeries.from_response(response)
series.closes #=> [2800.0, 2810.5, ...]
series.volumes #=> [123456, 234567, ...]

Defined Under Namespace

Classes: Candle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#candlesObject (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_segmentObject (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_idObject (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_closeObject

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

#closesObject

Get all close prices.



92
93
94
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 92

def closes
  @candles.map(&:close)
end

#date_rangeObject

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.timestamp, last.timestamp]
end

#eachObject



79
80
81
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 79

def each(&)
  @candles.each(&)
end

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 87

def empty?
  @candles.empty?
end

#firstObject

Get the oldest candle.



122
123
124
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 122

def first
  @candles.first
end

#highsObject

Get all high prices.



102
103
104
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 102

def highs
  @candles.map(&:high)
end

#lastObject

Get the most recent candle.



117
118
119
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 117

def last
  @candles.last
end

#lowsObject

Get all low prices.



107
108
109
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 107

def lows
  @candles.map(&:low)
end

#opensObject

Get all open prices.



97
98
99
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 97

def opens
  @candles.map(&:open)
end

#price_rangeObject

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

#sizeObject



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(from_timestamp, to_timestamp)
  self.class.new(
    @candles.select { |c| c.timestamp.between?(from_timestamp, to_timestamp) },
    { 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_volumeObject

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

#volumesObject

Get all volumes.



112
113
114
# File 'lib/DhanHQ/market_data/ohlc_series.rb', line 112

def volumes
  @candles.map(&:volume)
end