DhanHQ 2.1.8 → 2.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +50 -0
- data/lib/DhanHQ/contracts/expired_options_data_contract.rb +6 -6
- data/lib/DhanHQ/core/base_model.rb +9 -1
- data/lib/DhanHQ/models/edis.rb +150 -14
- data/lib/DhanHQ/models/expired_options_data.rb +307 -82
- data/lib/DhanHQ/models/forever_order.rb +261 -22
- data/lib/DhanHQ/models/funds.rb +76 -10
- data/lib/DhanHQ/models/historical_data.rb +148 -31
- data/lib/DhanHQ/models/holding.rb +82 -6
- data/lib/DhanHQ/models/kill_switch.rb +113 -11
- data/lib/DhanHQ/models/ledger_entry.rb +101 -13
- data/lib/DhanHQ/models/margin.rb +133 -8
- data/lib/DhanHQ/models/market_feed.rb +181 -17
- data/lib/DhanHQ/models/option_chain.rb +184 -12
- data/lib/DhanHQ/models/order.rb +399 -34
- data/lib/DhanHQ/models/position.rb +161 -10
- data/lib/DhanHQ/models/profile.rb +103 -7
- data/lib/DhanHQ/models/super_order.rb +275 -15
- data/lib/DhanHQ/models/trade.rb +279 -26
- data/lib/DhanHQ/resources/expired_options_data.rb +1 -1
- data/lib/DhanHQ/version.rb +1 -1
- data/lib/DhanHQ/ws/orders/client.rb +2 -1
- data/lib/DhanHQ/ws/orders.rb +2 -1
- metadata +1 -1
|
@@ -2,7 +2,67 @@
|
|
|
2
2
|
|
|
3
3
|
module DhanHQ
|
|
4
4
|
module Models
|
|
5
|
-
|
|
5
|
+
##
|
|
6
|
+
# Model for creating and managing Forever Orders (also known as Good Till Triggered or GTT orders).
|
|
7
|
+
#
|
|
8
|
+
# Forever Orders are conditional orders that remain active until triggered, modified, or cancelled.
|
|
9
|
+
# They can be configured as:
|
|
10
|
+
# - **SINGLE**: A single order that triggers when the trigger price is reached
|
|
11
|
+
# - **OCO** (One-Cancels-Other): Two linked orders where execution of one cancels the other
|
|
12
|
+
#
|
|
13
|
+
# Order placement, modification, and cancellation APIs require Static IP whitelisting.
|
|
14
|
+
#
|
|
15
|
+
# @example Create a single forever order
|
|
16
|
+
# order = DhanHQ::Models::ForeverOrder.create(
|
|
17
|
+
# order_flag: "SINGLE",
|
|
18
|
+
# transaction_type: "BUY",
|
|
19
|
+
# exchange_segment: "NSE_EQ",
|
|
20
|
+
# product_type: "CNC",
|
|
21
|
+
# order_type: "LIMIT",
|
|
22
|
+
# validity: "DAY",
|
|
23
|
+
# security_id: "1333",
|
|
24
|
+
# quantity: 5,
|
|
25
|
+
# price: 1428.0,
|
|
26
|
+
# trigger_price: 1427.0
|
|
27
|
+
# )
|
|
28
|
+
#
|
|
29
|
+
# @example Create an OCO forever order
|
|
30
|
+
# order = DhanHQ::Models::ForeverOrder.create(
|
|
31
|
+
# order_flag: "OCO",
|
|
32
|
+
# transaction_type: "BUY",
|
|
33
|
+
# exchange_segment: "NSE_EQ",
|
|
34
|
+
# product_type: "CNC",
|
|
35
|
+
# order_type: "LIMIT",
|
|
36
|
+
# validity: "DAY",
|
|
37
|
+
# security_id: "1333",
|
|
38
|
+
# quantity: 5,
|
|
39
|
+
# price: 1428.0,
|
|
40
|
+
# trigger_price: 1427.0,
|
|
41
|
+
# price1: 1420.0,
|
|
42
|
+
# trigger_price1: 1419.0,
|
|
43
|
+
# quantity1: 10
|
|
44
|
+
# )
|
|
45
|
+
#
|
|
46
|
+
# @example Modify a forever order
|
|
47
|
+
# order = DhanHQ::Models::ForeverOrder.find("5132208051112")
|
|
48
|
+
# order.modify(
|
|
49
|
+
# order_flag: "SINGLE",
|
|
50
|
+
# order_type: "LIMIT",
|
|
51
|
+
# leg_name: "TARGET_LEG",
|
|
52
|
+
# quantity: 15,
|
|
53
|
+
# price: 1421.0,
|
|
54
|
+
# trigger_price: 1420.0,
|
|
55
|
+
# validity: "DAY"
|
|
56
|
+
# )
|
|
57
|
+
#
|
|
58
|
+
# @example Cancel a forever order
|
|
59
|
+
# order = DhanHQ::Models::ForeverOrder.find("5132208051112")
|
|
60
|
+
# order.cancel # => true
|
|
61
|
+
#
|
|
62
|
+
# @example List all forever orders
|
|
63
|
+
# orders = DhanHQ::Models::ForeverOrder.all
|
|
64
|
+
# orders.each { |order| puts order.order_status }
|
|
65
|
+
#
|
|
6
66
|
class ForeverOrder < BaseModel
|
|
7
67
|
attributes :dhan_client_id, :order_id, :correlation_id, :order_status,
|
|
8
68
|
:transaction_type, :exchange_segment, :product_type, :order_flag,
|
|
@@ -13,17 +73,46 @@ module DhanHQ
|
|
|
13
73
|
:drv_strike_price
|
|
14
74
|
|
|
15
75
|
class << self
|
|
16
|
-
|
|
76
|
+
##
|
|
77
|
+
# Provides a shared instance of the ForeverOrders resource.
|
|
17
78
|
#
|
|
18
|
-
# @return [DhanHQ::Resources::ForeverOrders]
|
|
79
|
+
# @return [DhanHQ::Resources::ForeverOrders] The ForeverOrders resource client instance
|
|
19
80
|
def resource
|
|
20
81
|
@resource ||= DhanHQ::Resources::ForeverOrders.new
|
|
21
82
|
end
|
|
22
83
|
|
|
23
84
|
##
|
|
24
|
-
#
|
|
85
|
+
# Retrieves an array of all existing Forever Orders in the account.
|
|
86
|
+
#
|
|
87
|
+
# @return [Array<ForeverOrder>] Array of ForeverOrder objects. Returns empty array if no orders exist.
|
|
88
|
+
# Each ForeverOrder object contains (keys normalized to snake_case):
|
|
89
|
+
# - **:dhan_client_id** [String] User-specific identification generated by Dhan
|
|
90
|
+
# - **:order_id** [String] Order-specific identification generated by Dhan
|
|
91
|
+
# - **:order_status** [String] Last updated status of the order.
|
|
92
|
+
# Valid values: "TRANSIT", "PENDING", "REJECTED", "CANCELLED", "TRADED", "EXPIRED", "CONFIRM"
|
|
93
|
+
# - **:transaction_type** [String] The trading side of transaction. Valid values: "BUY", "SELL"
|
|
94
|
+
# - **:exchange_segment** [String] Exchange and segment. Valid values: "NSE_EQ", "NSE_FNO", "BSE_EQ", "MCX_COMM"
|
|
95
|
+
# - **:product_type** [String] Product type of trade. Valid values: "CNC", "INTRADAY", "MARGIN", "CO", "BO"
|
|
96
|
+
# - **:order_type** [String] Order type. Valid values: "SINGLE", "OCO"
|
|
97
|
+
# - **:trading_symbol** [String] Symbol of the instrument in which forever order is placed
|
|
98
|
+
# - **:security_id** [String] Exchange standard ID for each scrip
|
|
99
|
+
# - **:quantity** [Integer] Number of shares for the order
|
|
100
|
+
# - **:price** [Float] Price at which order is placed
|
|
101
|
+
# - **:trigger_price** [Float] Price at which order is to be triggered
|
|
102
|
+
# - **:leg_name** [String] Order leg of Forever Order.
|
|
103
|
+
# Valid values: "TARGET_LEG" (for Single and First leg of OCO), "STOP_LOSS_LEG" (for Second leg of OCO)
|
|
104
|
+
# - **:create_time** [String] Time at which the Forever Order is created
|
|
105
|
+
# - **:update_time** [String, nil] Time at which the Forever Order is updated
|
|
106
|
+
# - **:exchange_time** [String, nil] Time at which order reached at exchange end
|
|
107
|
+
# - **:drv_expiry_date** [String, nil] Contract Expiry Date for Derivative contract
|
|
108
|
+
# - **:drv_option_type** [String, nil] Type of Option. Valid values: "CALL", "PUT"
|
|
109
|
+
# - **:drv_strike_price** [Float] Strike Price in case of Option Contract
|
|
25
110
|
#
|
|
26
|
-
# @
|
|
111
|
+
# @example List all forever orders
|
|
112
|
+
# orders = DhanHQ::Models::ForeverOrder.all
|
|
113
|
+
# orders.each do |order|
|
|
114
|
+
# puts "#{order.order_id}: #{order.order_status} - #{order.trading_symbol}"
|
|
115
|
+
# end
|
|
27
116
|
def all
|
|
28
117
|
response = resource.all
|
|
29
118
|
return [] unless response.is_a?(Array)
|
|
@@ -32,10 +121,17 @@ module DhanHQ
|
|
|
32
121
|
end
|
|
33
122
|
|
|
34
123
|
##
|
|
35
|
-
#
|
|
124
|
+
# Retrieves a specific Forever Order by its order ID.
|
|
36
125
|
#
|
|
37
|
-
# @param order_id [String]
|
|
38
|
-
# @return [ForeverOrder, nil]
|
|
126
|
+
# @param order_id [String] Order-specific identification generated by Dhan
|
|
127
|
+
# @return [ForeverOrder, nil] ForeverOrder object if found, nil otherwise
|
|
128
|
+
#
|
|
129
|
+
# @example Find a specific forever order
|
|
130
|
+
# order = DhanHQ::Models::ForeverOrder.find("5132208051112")
|
|
131
|
+
# if order
|
|
132
|
+
# puts "Status: #{order.order_status}"
|
|
133
|
+
# puts "Symbol: #{order.trading_symbol}"
|
|
134
|
+
# end
|
|
39
135
|
def find(order_id)
|
|
40
136
|
response = resource.find(order_id)
|
|
41
137
|
return nil unless response.is_a?(Hash) && response.any?
|
|
@@ -44,12 +140,86 @@ module DhanHQ
|
|
|
44
140
|
end
|
|
45
141
|
|
|
46
142
|
##
|
|
47
|
-
#
|
|
143
|
+
# Creates a new Forever Order (Good Till Triggered order).
|
|
144
|
+
#
|
|
145
|
+
# Forever Orders can be configured as SINGLE (one order) or OCO (One-Cancels-Other with two linked orders).
|
|
146
|
+
# The order remains active until triggered, modified, or cancelled.
|
|
147
|
+
#
|
|
148
|
+
# @param params [Hash{Symbol => String, Integer, Float}] Forever order creation parameters
|
|
149
|
+
# @option params [String] :dhan_client_id (required) User-specific identification generated by Dhan.
|
|
150
|
+
# Must be explicitly provided in the params hash
|
|
151
|
+
# @option params [String] :correlation_id (optional) User/partner generated ID for tracking purposes
|
|
152
|
+
# @option params [String] :order_flag (required) Order flag type.
|
|
153
|
+
# Valid values: "OCO" for OCO order, "SINGLE" for Forever Order
|
|
154
|
+
# @option params [String] :transaction_type (required) The trading side of transaction.
|
|
155
|
+
# Valid values: "BUY", "SELL"
|
|
156
|
+
# @option params [String] :exchange_segment (required) Exchange and segment identifier.
|
|
157
|
+
# Valid values: "NSE_EQ", "NSE_FNO", "BSE_EQ", "BSE_FNO"
|
|
158
|
+
# @option params [String] :product_type (required) Product type.
|
|
159
|
+
# Valid values: "CNC", "MTF"
|
|
160
|
+
# @option params [String] :order_type (required) Order type.
|
|
161
|
+
# Valid values: "LIMIT", "MARKET"
|
|
162
|
+
# @option params [String] :validity (required) Validity of order for execution.
|
|
163
|
+
# Valid values: "DAY", "IOC"
|
|
164
|
+
# @option params [String] :security_id (required) Exchange standard ID for each scrip
|
|
165
|
+
# @option params [Integer] :quantity (required) Number of shares for the order
|
|
166
|
+
# @option params [Integer] :disclosed_quantity (optional) Number of shares visible to market.
|
|
167
|
+
# Keep more than 30% of quantity
|
|
168
|
+
# @option params [Float] :price (required) Price at which order is placed
|
|
169
|
+
# @option params [Float] :trigger_price (required) Price at which order is to be triggered
|
|
170
|
+
# @option params [Float] :price1 (conditionally required) Target price for OCO order.
|
|
171
|
+
# Required when order_flag is "OCO"
|
|
172
|
+
# @option params [Float] :trigger_price1 (conditionally required) Target trigger price for OCO order.
|
|
173
|
+
# Required when order_flag is "OCO"
|
|
174
|
+
# @option params [Integer] :quantity1 (conditionally required) Target quantity for OCO order.
|
|
175
|
+
# Required when order_flag is "OCO"
|
|
176
|
+
#
|
|
177
|
+
# @return [ForeverOrder, nil] ForeverOrder object with order details if creation succeeds, nil otherwise.
|
|
178
|
+
# Response structure includes:
|
|
179
|
+
# - **:order_id** [String] Order-specific identification generated by Dhan
|
|
180
|
+
# - **:order_status** [String] Order status. Valid values: "TRANSIT", "PENDING", "REJECTED",
|
|
181
|
+
# "CANCELLED", "TRADED", "EXPIRED", "CONFIRM"
|
|
182
|
+
#
|
|
183
|
+
# @example Create a single forever order
|
|
184
|
+
# order = DhanHQ::Models::ForeverOrder.create(
|
|
185
|
+
# dhan_client_id: "1000000132",
|
|
186
|
+
# order_flag: "SINGLE",
|
|
187
|
+
# transaction_type: "BUY",
|
|
188
|
+
# exchange_segment: "NSE_EQ",
|
|
189
|
+
# product_type: "CNC",
|
|
190
|
+
# order_type: "LIMIT",
|
|
191
|
+
# validity: "DAY",
|
|
192
|
+
# security_id: "1333",
|
|
193
|
+
# quantity: 5,
|
|
194
|
+
# disclosed_quantity: 1,
|
|
195
|
+
# price: 1428.0,
|
|
196
|
+
# trigger_price: 1427.0
|
|
197
|
+
# )
|
|
198
|
+
#
|
|
199
|
+
# @example Create an OCO forever order
|
|
200
|
+
# order = DhanHQ::Models::ForeverOrder.create(
|
|
201
|
+
# dhan_client_id: "1000000132",
|
|
202
|
+
# order_flag: "OCO",
|
|
203
|
+
# transaction_type: "BUY",
|
|
204
|
+
# exchange_segment: "NSE_EQ",
|
|
205
|
+
# product_type: "CNC",
|
|
206
|
+
# order_type: "LIMIT",
|
|
207
|
+
# validity: "DAY",
|
|
208
|
+
# security_id: "1333",
|
|
209
|
+
# quantity: 5,
|
|
210
|
+
# price: 1428.0,
|
|
211
|
+
# trigger_price: 1427.0,
|
|
212
|
+
# price1: 1420.0,
|
|
213
|
+
# trigger_price1: 1419.0,
|
|
214
|
+
# quantity1: 10
|
|
215
|
+
# )
|
|
48
216
|
#
|
|
49
|
-
# @
|
|
50
|
-
# @return [ForeverOrder, nil]
|
|
217
|
+
# @note Order placement APIs require Static IP whitelisting
|
|
51
218
|
def create(params)
|
|
52
|
-
|
|
219
|
+
# Normalize params and auto-inject dhan_client_id from configuration if not provided
|
|
220
|
+
normalized_params = snake_case(params)
|
|
221
|
+
normalized_params[:dhan_client_id] ||= DhanHQ.configuration.client_id if DhanHQ.configuration.client_id
|
|
222
|
+
response = resource.create(normalized_params)
|
|
53
223
|
return nil unless response.is_a?(Hash) && response["orderId"]
|
|
54
224
|
|
|
55
225
|
find(response["orderId"])
|
|
@@ -57,27 +227,96 @@ module DhanHQ
|
|
|
57
227
|
end
|
|
58
228
|
|
|
59
229
|
##
|
|
60
|
-
#
|
|
230
|
+
# Modifies an existing Forever Order.
|
|
61
231
|
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
232
|
+
# The variables that can be modified are: price, quantity, order_type, disclosed_quantity,
|
|
233
|
+
# trigger_price, and validity. For OCO orders, you can specify which leg to modify using leg_name.
|
|
234
|
+
#
|
|
235
|
+
# @param new_params [Hash{Symbol => String, Integer, Float}] Modification parameters
|
|
236
|
+
# @option new_params [String] :dhan_client_id (required) User-specific identification generated by Dhan.
|
|
237
|
+
# Must be explicitly provided in the params hash
|
|
238
|
+
# @option new_params [String] :order_id (required) Order-specific identification generated by Dhan.
|
|
239
|
+
# Must be explicitly provided in the params hash
|
|
240
|
+
# @option new_params [String] :order_flag (required) Order flag type.
|
|
241
|
+
# Valid values: "OCO" for OCO order, "SINGLE" for Forever Order
|
|
242
|
+
# @option new_params [String] :order_type (required) Order type.
|
|
243
|
+
# Valid values: "LIMIT", "MARKET", "STOP_LOSS", "STOP_LOSS_MARKET"
|
|
244
|
+
# @option new_params [String] :leg_name (required) Order leg of Forever Order where modification is to be done.
|
|
245
|
+
# Valid values: "TARGET_LEG" (for Single and First leg of OCO), "STOP_LOSS_LEG" (for Second leg of OCO)
|
|
246
|
+
# @option new_params [Integer] :quantity (required) Number of shares for the order
|
|
247
|
+
# @option new_params [Float] :price (required) Price at which order is placed
|
|
248
|
+
# @option new_params [Integer] :disclosed_quantity (optional) Number of shares visible to market.
|
|
249
|
+
# Keep more than 30% of quantity
|
|
250
|
+
# @option new_params [Float] :trigger_price (required) Price at which order is to be triggered
|
|
251
|
+
# @option new_params [String] :validity (required) Validity of order for execution.
|
|
252
|
+
# Valid values: "DAY", "IOC"
|
|
253
|
+
#
|
|
254
|
+
# @return [ForeverOrder, nil] Updated ForeverOrder object if modification succeeds, nil otherwise.
|
|
255
|
+
# Response structure includes:
|
|
256
|
+
# - **:order_id** [String] Order-specific identification generated by Dhan
|
|
257
|
+
# - **:order_status** [String] Last updated status of the order. Valid values: "TRANSIT", "PENDING",
|
|
258
|
+
# "REJECTED", "CANCELLED", "TRADED", "EXPIRED", "CONFIRM"
|
|
259
|
+
#
|
|
260
|
+
# @example Modify a single forever order
|
|
261
|
+
# order = DhanHQ::Models::ForeverOrder.find("5132208051112")
|
|
262
|
+
# order.modify(
|
|
263
|
+
# dhan_client_id: "1000000132",
|
|
264
|
+
# order_id: "5132208051112",
|
|
265
|
+
# order_flag: "SINGLE",
|
|
266
|
+
# order_type: "LIMIT",
|
|
267
|
+
# leg_name: "TARGET_LEG",
|
|
268
|
+
# quantity: 15,
|
|
269
|
+
# price: 1421.0,
|
|
270
|
+
# disclosed_quantity: 1,
|
|
271
|
+
# trigger_price: 1420.0,
|
|
272
|
+
# validity: "DAY"
|
|
273
|
+
# )
|
|
274
|
+
#
|
|
275
|
+
# @example Modify the stop loss leg of an OCO order
|
|
276
|
+
# order.modify(
|
|
277
|
+
# dhan_client_id: "1000000132",
|
|
278
|
+
# order_id: "5132208051112",
|
|
279
|
+
# order_flag: "OCO",
|
|
280
|
+
# order_type: "LIMIT",
|
|
281
|
+
# leg_name: "STOP_LOSS_LEG",
|
|
282
|
+
# quantity: 10,
|
|
283
|
+
# price: 1400.0,
|
|
284
|
+
# trigger_price: 1399.0,
|
|
285
|
+
# validity: "DAY"
|
|
286
|
+
# )
|
|
287
|
+
#
|
|
288
|
+
# @raise [RuntimeError] If order_id is not available
|
|
289
|
+
# @note Order modification APIs require Static IP whitelisting
|
|
64
290
|
def modify(new_params)
|
|
65
|
-
raise "Order ID is required to modify a forever order" unless
|
|
291
|
+
raise "Order ID is required to modify a forever order" unless order_id
|
|
66
292
|
|
|
67
|
-
response = self.class.resource.update(
|
|
68
|
-
return self.class.find(
|
|
293
|
+
response = self.class.resource.update(order_id, new_params)
|
|
294
|
+
return self.class.find(order_id) if self.class.send(:success_response?, response)
|
|
69
295
|
|
|
70
296
|
nil
|
|
71
297
|
end
|
|
72
298
|
|
|
73
299
|
##
|
|
74
|
-
#
|
|
300
|
+
# Cancels a pending Forever Order using the Order ID.
|
|
301
|
+
#
|
|
302
|
+
# @return [Boolean] true if cancellation succeeds (order_status becomes "CANCELLED"), false otherwise.
|
|
303
|
+
# Response structure includes:
|
|
304
|
+
# - **:order_id** [String] Order-specific identification generated by Dhan
|
|
305
|
+
# - **:order_status** [String] Order status after cancellation. Valid values: "TRANSIT", "PENDING",
|
|
306
|
+
# "REJECTED", "CANCELLED", "TRADED", "EXPIRED", "CONFIRM"
|
|
307
|
+
#
|
|
308
|
+
# @example Cancel a forever order
|
|
309
|
+
# order = DhanHQ::Models::ForeverOrder.find("5132208051112")
|
|
310
|
+
# if order.cancel
|
|
311
|
+
# puts "Order #{order.order_id} cancelled successfully"
|
|
312
|
+
# end
|
|
75
313
|
#
|
|
76
|
-
# @
|
|
314
|
+
# @raise [RuntimeError] If order_id is not available
|
|
315
|
+
# @note Order cancellation APIs require Static IP whitelisting
|
|
77
316
|
def cancel
|
|
78
|
-
raise "Order ID is required to cancel a forever order" unless
|
|
317
|
+
raise "Order ID is required to cancel a forever order" unless order_id
|
|
79
318
|
|
|
80
|
-
response = self.class.resource.cancel(
|
|
319
|
+
response = self.class.resource.cancel(order_id)
|
|
81
320
|
response["orderStatus"] == "CANCELLED"
|
|
82
321
|
end
|
|
83
322
|
end
|
data/lib/DhanHQ/models/funds.rb
CHANGED
|
@@ -2,7 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
module DhanHQ
|
|
4
4
|
module Models
|
|
5
|
-
|
|
5
|
+
##
|
|
6
|
+
# Model for retrieving trading account fund information including balance, margin utilized, collateral, etc.
|
|
7
|
+
#
|
|
8
|
+
# This endpoint provides comprehensive information about your trading account including available balance,
|
|
9
|
+
# start of day limit, collateral amounts, utilized amounts, and withdrawable balance.
|
|
10
|
+
#
|
|
11
|
+
# @example Fetch complete fund details
|
|
12
|
+
# funds = DhanHQ::Models::Funds.fetch
|
|
13
|
+
# puts "Available Balance: #{funds.available_balance}"
|
|
14
|
+
# puts "Start of Day Limit: #{funds.sod_limit}"
|
|
15
|
+
# puts "Utilized Amount: #{funds.utilized_amount}"
|
|
16
|
+
# puts "Withdrawable Balance: #{funds.withdrawable_balance}"
|
|
17
|
+
#
|
|
18
|
+
# @example Fetch only available balance
|
|
19
|
+
# balance = DhanHQ::Models::Funds.balance
|
|
20
|
+
# puts "Available to trade: #{balance}"
|
|
21
|
+
#
|
|
22
|
+
# @note The API response contains a typo in the field name (`availabelBalance`). The model automatically
|
|
23
|
+
# maps this to the correctly spelled `available_balance` attribute for convenience.
|
|
24
|
+
#
|
|
6
25
|
class Funds < BaseModel
|
|
7
26
|
# Base path used by the funds resource.
|
|
8
27
|
HTTP_PATH = "/v2/fundlimit"
|
|
@@ -10,37 +29,84 @@ module DhanHQ
|
|
|
10
29
|
attributes :available_balance, :sod_limit, :collateral_amount, :receiveable_amount, :utilized_amount,
|
|
11
30
|
:blocked_payout_amount, :withdrawable_balance
|
|
12
31
|
|
|
13
|
-
|
|
14
|
-
#
|
|
15
|
-
#
|
|
32
|
+
##
|
|
33
|
+
# Normalizes the typo'd `availabelBalance` key from the API response to `available_balance`.
|
|
34
|
+
#
|
|
35
|
+
# The API currently returns `availabelBalance` (note the typo). This method ensures backwards
|
|
36
|
+
# compatibility while exposing a correctly spelled attribute accessor.
|
|
37
|
+
#
|
|
38
|
+
# @return [void]
|
|
16
39
|
def assign_attributes
|
|
17
40
|
if @attributes.key?(:availabel_balance) && !@attributes.key?(:available_balance)
|
|
18
41
|
@attributes[:available_balance] = @attributes[:availabel_balance]
|
|
19
42
|
end
|
|
20
43
|
super
|
|
21
44
|
end
|
|
45
|
+
|
|
22
46
|
class << self
|
|
23
47
|
##
|
|
24
|
-
# Provides a
|
|
48
|
+
# Provides a shared instance of the Funds resource.
|
|
25
49
|
#
|
|
26
|
-
# @return [DhanHQ::Resources::Funds]
|
|
50
|
+
# @return [DhanHQ::Resources::Funds] The Funds resource client instance
|
|
27
51
|
def resource
|
|
28
52
|
@resource ||= DhanHQ::Resources::Funds.new
|
|
29
53
|
end
|
|
30
54
|
|
|
31
55
|
##
|
|
32
|
-
#
|
|
56
|
+
# Fetches all fund limit details for your trading account.
|
|
57
|
+
#
|
|
58
|
+
# Retrieves comprehensive information about account balance, margin utilized, collateral,
|
|
59
|
+
# receivable amounts, and withdrawable balance. No request parameters are required.
|
|
60
|
+
#
|
|
61
|
+
# @return [Funds] Funds object containing all account fund information.
|
|
62
|
+
# Response structure (keys normalized to snake_case):
|
|
63
|
+
# - **:dhan_client_id** [String] User-specific identification generated by Dhan
|
|
64
|
+
# - **:available_balance** [Float] Available amount to trade
|
|
65
|
+
# - **:sod_limit** [Float] Start of the day balance in account
|
|
66
|
+
# - **:collateral_amount** [Float] Amount received against collateral
|
|
67
|
+
# - **:receiveable_amount** [Float] Amount available against selling deliveries
|
|
68
|
+
# - **:utilized_amount** [Float] Amount utilised in the day
|
|
69
|
+
# - **:blocked_payout_amount** [Float] Amount blocked against payout request
|
|
70
|
+
# - **:withdrawable_balance** [Float] Amount available to withdraw in bank account
|
|
71
|
+
#
|
|
72
|
+
# @example Fetch complete fund details
|
|
73
|
+
# funds = DhanHQ::Models::Funds.fetch
|
|
74
|
+
# puts "Available Balance: ₹#{funds.available_balance}"
|
|
75
|
+
# puts "SOD Limit: ₹#{funds.sod_limit}"
|
|
76
|
+
# puts "Utilized: ₹#{funds.utilized_amount}"
|
|
77
|
+
# puts "Withdrawable: ₹#{funds.withdrawable_balance}"
|
|
33
78
|
#
|
|
34
|
-
# @
|
|
79
|
+
# @example Check if sufficient balance for trading
|
|
80
|
+
# funds = DhanHQ::Models::Funds.fetch
|
|
81
|
+
# if funds.available_balance >= 10000.0
|
|
82
|
+
# puts "Sufficient balance for trading"
|
|
83
|
+
# else
|
|
84
|
+
# puts "Low balance: ₹#{funds.available_balance}"
|
|
85
|
+
# end
|
|
86
|
+
#
|
|
87
|
+
# @note This is a GET request with no body parameters required
|
|
35
88
|
def fetch
|
|
36
89
|
response = resource.fetch
|
|
37
90
|
new(response, skip_validation: true)
|
|
38
91
|
end
|
|
39
92
|
|
|
40
93
|
##
|
|
41
|
-
#
|
|
94
|
+
# Convenience method to fetch only the available balance.
|
|
95
|
+
#
|
|
96
|
+
# This method calls {fetch} internally and returns just the `available_balance` attribute,
|
|
97
|
+
# which is the amount available to trade.
|
|
98
|
+
#
|
|
99
|
+
# @return [Float] Available balance in the trading account (amount available to trade).
|
|
100
|
+
# Returns 0.0 if unavailable or on error.
|
|
101
|
+
#
|
|
102
|
+
# @example Quick balance check
|
|
103
|
+
# balance = DhanHQ::Models::Funds.balance
|
|
104
|
+
# puts "Available to trade: ₹#{balance}"
|
|
42
105
|
#
|
|
43
|
-
# @
|
|
106
|
+
# @example Use in conditional logic
|
|
107
|
+
# if DhanHQ::Models::Funds.balance > 50000
|
|
108
|
+
# # Proceed with large order
|
|
109
|
+
# end
|
|
44
110
|
def balance
|
|
45
111
|
fetch.available_balance
|
|
46
112
|
end
|