Class: DhanHQ::Models::Funds

Inherits:
BaseModel show all
Defined in:
lib/DhanHQ/models/funds.rb

Overview

Note:

The API response contains a typo in the field name (availabelBalance). The model automatically maps this to the correctly spelled available_balance attribute for convenience.

Model for retrieving trading account fund information including balance, margin utilized, collateral, etc.

This endpoint provides comprehensive information about your trading account including available balance, start of day limit, collateral amounts, utilized amounts, and withdrawable balance.

Examples:

Fetch complete fund details

funds = DhanHQ::Models::Funds.fetch
puts "Available Balance: #{funds.available_balance}"
puts "Start of Day Limit: #{funds.sod_limit}"
puts "Utilized Amount: #{funds.utilized_amount}"
puts "Withdrawable Balance: #{funds.withdrawable_balance}"

Fetch only available balance

balance = DhanHQ::Models::Funds.balance
puts "Available to trade: #{balance}"

Constant Summary collapse

HTTP_PATH =

Base path used by the funds resource.

"/v2/fundlimit"

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, 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, #validation_contract, validation_contract, where

Methods included from APIHelper

#handle_response

Methods included from AttributeHelper

#camelize_keys, #deep_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

.balanceFloat

Convenience method to fetch only the available balance.

This method calls fetch internally and returns just the available_balance attribute, which is the amount available to trade.

Examples:

Quick balance check

balance = DhanHQ::Models::Funds.balance
puts "Available to trade: ₹#{balance}"

Use in conditional logic

if DhanHQ::Models::Funds.balance > 50000
  # Proceed with large order
end

Returns:

  • (Float)

    Available balance in the trading account (amount available to trade). Returns 0.0 if unavailable or on error.



122
123
124
# File 'lib/DhanHQ/models/funds.rb', line 122

def balance
  fetch.available_balance
end

.fetchFunds

Note:

This is a GET request with no body parameters required

Fetches all fund limit details for your trading account.

Retrieves comprehensive information about account balance, margin utilized, collateral, receivable amounts, and withdrawable balance. No request parameters are required.

Examples:

Fetch complete fund details

funds = DhanHQ::Models::Funds.fetch
puts "Available Balance: ₹#{funds.available_balance}"
puts "SOD Limit: ₹#{funds.sod_limit}"
puts "Utilized: ₹#{funds.utilized_amount}"
puts "Withdrawable: ₹#{funds.withdrawable_balance}"

Check if sufficient balance for trading

funds = DhanHQ::Models::Funds.fetch
if funds.available_balance >= 10000.0
  puts "Sufficient balance for trading"
else
  puts "Low balance: ₹#{funds.available_balance}"
end

Returns:

  • (Funds)

    Funds object containing all account fund information. Response structure (keys normalized to snake_case):

    • :dhan_client_id [String] User-specific identification generated by Dhan
    • :available_balance [Float] Available amount to trade
    • :sod_limit [Float] Start of the day balance in account
    • :collateral_amount [Float] Amount received against collateral
    • :receiveable_amount [Float] Amount available against selling deliveries
    • :utilized_amount [Float] Amount utilised in the day
    • :blocked_payout_amount [Float] Amount blocked against payout request
    • :withdrawable_balance [Float] Amount available to withdraw in bank account


100
101
102
103
# File 'lib/DhanHQ/models/funds.rb', line 100

def fetch
  response = resource.fetch
  new(response, skip_validation: true)
end

.resourceDhanHQ::Resources::Funds

Provides a shared instance of the Funds resource.

Returns:



63
64
65
# File 'lib/DhanHQ/models/funds.rb', line 63

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

Instance Method Details

#assign_attributesvoid

This method returns an undefined value.

Normalizes the typo'd availabelBalance key from the API response to available_balance.

The API currently returns availabelBalance (note the typo). This method ensures backwards compatibility while exposing a correctly spelled attribute accessor.



51
52
53
54
55
56
# File 'lib/DhanHQ/models/funds.rb', line 51

def assign_attributes
  if @attributes.key?(:availabel_balance) && !@attributes.key?(:available_balance)
    @attributes[:available_balance] = @attributes[:availabel_balance]
  end
  super
end

#to_promptObject

Returns a concise prompt-friendly summary of funds.



33
34
35
36
37
38
39
40
41
42
# File 'lib/DhanHQ/models/funds.rb', line 33

def to_prompt
  parts = []
  parts << "available=₹#{available_balance}" if available_balance
  parts << "utilized=₹#{utilized_amount}" if utilized_amount
  parts << "withdrawable=₹#{withdrawable_balance}" if withdrawable_balance
  parts << "collateral=₹#{collateral_amount}" if collateral_amount&.positive?
  parts << "sod_limit=₹#{sod_limit}" if sod_limit
  parts << "blocked_payout=₹#{blocked_payout_amount}" if blocked_payout_amount&.positive?
  parts.join(", ")
end