Class: DhanHQ::Models::OptionChain

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/DhanHQ/models/option_chain.rb

Overview

Note:

Rate Limits: You can call the Option Chain API once every 3 seconds. This rate limit is enforced because OI data updates slowly compared to LTP or other data parameters. The client's internal rate limiter automatically throttles calls to prevent exceeding limits.

Note:

Data Filtering: The model automatically filters out strikes where both Call (CE) and Put (PE) options have zero last_price, keeping the payload compact and focused on actively traded strikes.

Model for fetching option chain data for any option instrument across exchanges.

The Option Chain API provides the entire option chain for any underlying instrument across NSE, BSE, and MCX exchanges. For each strike price, you get Open Interest (OI), Greeks (Delta, Theta, Gamma, Vega), Volume, Last Traded Price, Best Bid/Ask prices, Implied Volatility (IV), and other option analytics.

Examples:

Fetch option chain for NIFTY index options

chain = DhanHQ::Models::OptionChain.fetch(
  underlying_scrip: 13,
  underlying_seg: "IDX_I",
  expiry: "2024-10-31"
)
puts "Underlying LTP: ₹#{chain[:last_price]}"
nifty_first_strike = chain[:strikes].first
puts "Strike: #{nifty_first_strike[:strike]}"
puts "Call LTP: ₹#{nifty_first_strike[:call][:last_price]}"

Fetch expiry list for an underlying

expiries = DhanHQ::Models::OptionChain.expiry_list(
  underlying_scrip: 13,
  underlying_seg: "IDX_I"
)
expiries.each { |expiry| puts expiry }

Access Greeks for a strike

chain = DhanHQ::Models::OptionChain.fetch(
  underlying_scrip: 1333,
  underlying_seg: "NSE_FNO",
  expiry: "2024-12-26"
)
strike_data = chain[:strikes].find { |s| s[:strike] == 25000.0 }
ce_greeks = strike_data[:call][:greeks]
puts "Delta: #{ce_greeks[:delta]}"

Constant Summary

Constants included from ResponseHelper

ResponseHelper::STATUS_ERROR_FALLBACK

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes, #errors

Class Method Summary collapse

Instance Method Summary collapse

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_path, #save, #save!, #to_request_params, #update, #valid?, validate_attributes, where

Methods included from APIHelper

#handle_response

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

#build_from_response

Constructor Details

This class inherits a constructor from DhanHQ::BaseModel

Class Method Details

.fetch(params) ⇒ HashWithIndifferentAccess

Fetches the entire option chain for a specified underlying instrument and expiry.

Retrieves real-time option chain data across all strikes for the given underlying. The response includes Open Interest (OI), Greeks, Volume, Last Traded Price, Best Bid/Ask prices, Implied Volatility (IV), and other option analytics for both Call (CE) and Put (PE) options at each strike price.

Parameters:

  • params (Hash{Symbol => Integer, String})

    Request parameters for option chain @option params [Integer] :underlying_scrip (required) Security ID of the underlying instrument. @option params [String] :underlying_seg (required) Exchange and segment of underlying. @option params [String] :expiry (required) Expiry date in "YYYY-MM-DD" format.

Returns:

  • (HashWithIndifferentAccess)

    Normalized option chain data. Response structure:

    • :last_price [Float] Last Traded Price (LTP) of the underlying instrument
    • :strikes [Array] Sorted array of strike data:
      • :strike [Float] The strike price
      • :call [Hash] Call Option (CE) data for this strike
      • :put [Hash] Put Option (PE) data for this strike

Raises:



92
93
94
95
96
97
98
99
# File 'lib/DhanHQ/models/option_chain.rb', line 92

def fetch(params)
  validate_params!(params, DhanHQ::Contracts::OptionChainContract)

  response = resource.fetch(params)
  return {}.with_indifferent_access unless response[:status] == "success"

  normalize_chain(response[:data]).with_indifferent_access
end

.fetch_expiry_list(params) ⇒ Array<String> Also known as: expiry_list

Fetches the list of active expiry dates for an underlying instrument.

Parameters:

  • params (Hash{Symbol => Integer, String})

    Request parameters for expiry list @option params [Integer] :underlying_scrip (required) Security ID of the underlying instrument. @option params [String] :underlying_seg (required) Exchange and segment of underlying.

Returns:

  • (Array<String>)

    Array of expiry dates in "YYYY-MM-DD" format.



109
110
111
112
113
114
# File 'lib/DhanHQ/models/option_chain.rb', line 109

def fetch_expiry_list(params)
  validate_params!(params, DhanHQ::Contracts::OptionChainExpiryListContract)

  response = resource.expirylist(params)
  response[:status] == "success" ? response[:data] : []
end

.resourceDhanHQ::Resources::OptionChain

Provides a shared instance of the OptionChain resource.

Returns:



62
63
64
# File 'lib/DhanHQ/models/option_chain.rb', line 62

def resource
  @resource ||= DhanHQ::Resources::OptionChain.new
end

.validation_contractObject



66
67
68
# File 'lib/DhanHQ/models/option_chain.rb', line 66

def validation_contract
  @validation_contract ||= DhanHQ::Contracts::OptionChainContract.new
end

Instance Method Details

#validation_contractObject



53
54
55
# File 'lib/DhanHQ/models/option_chain.rb', line 53

def validation_contract
  self.class.validation_contract
end