Class: DhanHQ::Models::ExpiredOptionsData
- Defined in:
- lib/DhanHQ/models/expired_options_data.rb
Overview
Model for fetching expired options contract data on a rolling basis.
This API provides pre-processed expired options data for up to the last 5 years. Data is available on a minute-level basis, organized by strike price relative to spot (e.g., ATM, ATM+1, ATM-1, etc.). You can fetch up to 31 days of data in a single API call.
Available data includes:
-
OHLC (Open, High, Low, Close) prices
-
Volume and Open Interest
-
Implied Volatility (IV)
-
Strike prices
-
Spot prices
-
Timestamps
Strike ranges:
-
Index Options (near expiry): Up to ATM+10 / ATM-10
-
All other contracts: Up to ATM+3 / ATM-3
rubocop:disable Metrics/ClassLength
Instance Attribute Summary
Attributes inherited from BaseModel
Class Method Summary collapse
-
.fetch(params) ⇒ ExpiredOptionsData
Fetches expired options data for rolling contracts on a minute-level basis.
Instance Method Summary collapse
-
#at_the_money? ⇒ Boolean
Checks if the strike is at the money (ATM).
-
#average_implied_volatility(option_type = nil) ⇒ Float
Calculates the average implied volatility for the specified option type.
-
#average_open_interest(option_type = nil) ⇒ Float
Calculates the average open interest for the specified option type.
-
#average_volume(option_type = nil) ⇒ Float
Calculates the average volume for the specified option type.
-
#call_data ⇒ Hash{Symbol => Array<Float, Integer>}?
Gets call option data from the response.
-
#call_option? ⇒ Boolean
Checks if this is call option data.
-
#data_for_type(option_type) ⇒ Hash{Symbol => Array<Float, Integer>}?
Gets data for the specified option type.
-
#data_points_count(option_type = nil) ⇒ Integer
Gets the number of data points available for the specified option type.
-
#implied_volatility_data(option_type = nil) ⇒ Array<Float>
Gets implied volatility (IV) data for the specified option type.
-
#index_options? ⇒ Boolean
Checks if this is index options data.
-
#monthly_expiry? ⇒ Boolean
Checks if this is monthly expiry data.
-
#ohlc_data(option_type = nil) ⇒ Hash{Symbol => Array<Float>}, Hash{Symbol => Array}
Gets OHLC (Open, High, Low, Close) data for the specified option type.
-
#open_interest_data(option_type = nil) ⇒ Array<Float>
Gets open interest (OI) data for the specified option type.
-
#price_ranges(option_type = nil) ⇒ Array<Float>
Calculates price range (high - low) for each timeframe of the specified option type.
-
#put_data ⇒ Hash{Symbol => Array<Float, Integer>}?
Gets put option data from the response.
-
#put_option? ⇒ Boolean
Checks if this is put option data.
-
#spot_data(option_type = nil) ⇒ Array<Float>
Gets spot price data for the specified option type.
-
#stock_options? ⇒ Boolean
Checks if this is stock options data.
-
#strike_data(option_type = nil) ⇒ Array<Float>
Gets strike price data for the specified option type.
-
#strike_offset ⇒ Integer
Calculates the strike offset from ATM (At The Money).
-
#summary_stats(option_type = nil) ⇒ Hash{Symbol => Integer, Float, Array, Boolean}
Gets comprehensive summary statistics for the specified option type.
-
#timestamp_data(option_type = nil) ⇒ Array<Integer>
Gets timestamp data for the specified option type.
-
#validation_contract ⇒ Object
ExpiredOptionsData objects are read-only, so no validation contract needed.
-
#volume_data(option_type = nil) ⇒ Array<Integer>
Gets volume data for the specified option type.
-
#weekly_expiry? ⇒ Boolean
Checks if this is weekly expiry data.
Methods inherited from BaseModel
all, api, api_type, #assign_attributes, attributes, create, #delete, #destroy, find, #id, #initialize, #new_record?, #optionchain_api?, parse_collection_response, #persisted?, resource, resource_path, #save, #save!, #to_request_params, #update, #valid?, validate_attributes, validation_contract, where
Methods included from APIHelper
Methods included from AttributeHelper
#camelize_keys, #inspect, #normalize_keys, #snake_case, #titleize_keys
Methods included from ValidationHelper
#valid?, #validate!, #validate_params!
Methods included from RequestHelper
Constructor Details
This class inherits a constructor from DhanHQ::BaseModel
Class Method Details
.fetch(params) ⇒ ExpiredOptionsData
Fetches expired options data for rolling contracts on a minute-level basis.
Data is organized by strike price relative to spot and can be fetched for up to 31 days in a single request. Historical data is available for up to the last 5 years.
116 117 118 119 120 121 122 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 116 def fetch(params) normalized = normalize_params(params) validate_params(normalized) response = .fetch(normalized) new(response.merge(normalized), skip_validation: true) end |
Instance Method Details
#at_the_money? ⇒ Boolean
Checks if the strike is at the money (ATM).
521 522 523 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 521 def at_the_money? strike == "ATM" end |
#average_implied_volatility(option_type = nil) ⇒ Float
Calculates the average implied volatility for the specified option type.
408 409 410 411 412 413 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 408 def average_implied_volatility(option_type = nil) iv_data = implied_volatility_data(option_type) return 0.0 if iv_data.empty? iv_data.sum.to_f / iv_data.size end |
#average_open_interest(option_type = nil) ⇒ Float
Calculates the average open interest for the specified option type.
395 396 397 398 399 400 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 395 def average_open_interest(option_type = nil) oi_data = open_interest_data(option_type) return 0.0 if oi_data.empty? oi_data.sum.to_f / oi_data.size end |
#average_volume(option_type = nil) ⇒ Float
Calculates the average volume for the specified option type.
382 383 384 385 386 387 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 382 def average_volume(option_type = nil) volumes = volume_data(option_type) return 0.0 if volumes.empty? volumes.sum.to_f / volumes.size end |
#call_data ⇒ Hash{Symbol => Array<Float, Integer>}?
Gets call option data from the response.
206 207 208 209 210 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 206 def call_data return nil unless data.is_a?(Hash) data["ce"] || data[:ce] end |
#call_option? ⇒ Boolean
Checks if this is call option data.
505 506 507 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 505 def call_option? drv_option_type == "CALL" end |
#data_for_type(option_type) ⇒ Hash{Symbol => Array<Float, Integer>}?
Gets data for the specified option type.
240 241 242 243 244 245 246 247 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 240 def data_for_type(option_type) case option_type.upcase when "CALL" call_data when "PUT" put_data end end |
#data_points_count(option_type = nil) ⇒ Integer
Gets the number of data points available for the specified option type.
371 372 373 374 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 371 def data_points_count(option_type = nil) = (option_type) .size end |
#implied_volatility_data(option_type = nil) ⇒ Array<Float>
Gets implied volatility (IV) data for the specified option type.
312 313 314 315 316 317 318 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 312 def implied_volatility_data(option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return [] unless option_data option_data["iv"] || option_data[:iv] || [] end |
#index_options? ⇒ Boolean
Checks if this is index options data.
473 474 475 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 473 def instrument == "OPTIDX" end |
#monthly_expiry? ⇒ Boolean
Checks if this is monthly expiry data.
497 498 499 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 497 def monthly_expiry? expiry_flag == "MONTH" end |
#ohlc_data(option_type = nil) ⇒ Hash{Symbol => Array<Float>}, Hash{Symbol => Array}
Gets OHLC (Open, High, Low, Close) data for the specified option type.
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 261 def ohlc_data(option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return {} unless option_data { open: option_data["open"] || option_data[:open] || [], high: option_data["high"] || option_data[:high] || [], low: option_data["low"] || option_data[:low] || [], close: option_data["close"] || option_data[:close] || [] } end |
#open_interest_data(option_type = nil) ⇒ Array<Float>
Gets open interest (OI) data for the specified option type.
297 298 299 300 301 302 303 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 297 def open_interest_data(option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return [] unless option_data option_data["oi"] || option_data[:oi] || [] end |
#price_ranges(option_type = nil) ⇒ Array<Float>
Calculates price range (high - low) for each timeframe of the specified option type.
422 423 424 425 426 427 428 429 430 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 422 def price_ranges(option_type = nil) ohlc = ohlc_data(option_type) highs = ohlc[:high] lows = ohlc[:low] return [] if highs.empty? || lows.empty? highs.zip(lows).map { |high, low| high - low } end |
#put_data ⇒ Hash{Symbol => Array<Float, Integer>}?
Gets put option data from the response.
228 229 230 231 232 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 228 def put_data return nil unless data.is_a?(Hash) data["pe"] || data[:pe] end |
#put_option? ⇒ Boolean
Checks if this is put option data.
513 514 515 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 513 def put_option? drv_option_type == "PUT" end |
#spot_data(option_type = nil) ⇒ Array<Float>
Gets spot price data for the specified option type.
342 343 344 345 346 347 348 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 342 def spot_data(option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return [] unless option_data option_data["spot"] || option_data[:spot] || [] end |
#stock_options? ⇒ Boolean
Checks if this is stock options data.
481 482 483 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 481 def instrument == "OPTSTK" end |
#strike_data(option_type = nil) ⇒ Array<Float>
Gets strike price data for the specified option type.
327 328 329 330 331 332 333 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 327 def strike_data(option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return [] unless option_data option_data["strike"] || option_data[:strike] || [] end |
#strike_offset ⇒ Integer
Calculates the strike offset from ATM (At The Money).
543 544 545 546 547 548 549 550 551 552 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 543 def strike_offset return 0 if at_the_money? match = strike.match(/\AATM(\+|-)?(\d+)\z/) return 0 unless match sign = match[1] == "-" ? -1 : 1 offset = match[2].to_i sign * offset end |
#summary_stats(option_type = nil) ⇒ Hash{Symbol => Integer, Float, Array, Boolean}
Gets comprehensive summary statistics for the specified option type.
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 448 def summary_stats(option_type = nil) option_type ||= drv_option_type ohlc = ohlc_data(option_type) volumes = volume_data(option_type) oi_data = open_interest_data(option_type) iv_data = implied_volatility_data(option_type) { data_points: data_points_count(option_type), avg_volume: average_volume(option_type), avg_open_interest: average_open_interest(option_type), avg_implied_volatility: average_implied_volatility(option_type), price_ranges: price_ranges(option_type), has_ohlc: !ohlc[:open].empty?, has_volume: !volumes.empty?, has_open_interest: !oi_data.empty?, has_implied_volatility: !iv_data.empty? } end |
#timestamp_data(option_type = nil) ⇒ Array<Integer>
Gets timestamp data for the specified option type.
357 358 359 360 361 362 363 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 357 def (option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return [] unless option_data option_data["timestamp"] || option_data[:timestamp] || [] end |
#validation_contract ⇒ Object
ExpiredOptionsData objects are read-only, so no validation contract needed
186 187 188 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 186 def validation_contract nil end |
#volume_data(option_type = nil) ⇒ Array<Integer>
Gets volume data for the specified option type.
282 283 284 285 286 287 288 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 282 def volume_data(option_type = nil) option_type ||= drv_option_type option_data = data_for_type(option_type) return [] unless option_data option_data["volume"] || option_data[:volume] || [] end |
#weekly_expiry? ⇒ Boolean
Checks if this is weekly expiry data.
489 490 491 |
# File 'lib/DhanHQ/models/expired_options_data.rb', line 489 def weekly_expiry? expiry_flag == "WEEK" end |