Class: DhanHQ::Models::Trade
- Defined in:
- lib/DhanHQ/models/trade.rb
Overview
Model for retrieving trade execution details.
The Trade Book API lets you retrieve an array of all trades executed in a day. You can also fetch trade details for a specific order ID, which is useful during partial trades or Bracket/Cover Orders. Additionally, you can retrieve detailed trade history for all orders within a particular time frame.
Constant Summary collapse
- HTTP_PATH =
"/v2/trades"
Constants included from ResponseHelper
ResponseHelper::STATUS_ERROR_FALLBACK
Instance Attribute Summary
Attributes inherited from BaseModel
Class Method Summary collapse
-
.find_by_order_id(order_id) ⇒ Trade?
Retrieves trade details for a specific order ID (current day).
-
.history(from_date:, to_date:, page: 0) ⇒ Array<Trade>
(also: all)
Retrieves detailed trade history for all orders within a specified time frame.
-
.resource ⇒ DhanHQ::Resources::Trades
Provides a shared instance of the Trades resource for current day tradebook APIs.
-
.statements_resource ⇒ DhanHQ::Resources::Statements
Provides a shared instance of the Statements resource for historical trade data.
-
.today ⇒ Array<Trade>
Retrieves all trades executed during the current trading day.
Instance Method Summary collapse
-
#buy? ⇒ Boolean
Checks if the trade is a BUY transaction.
-
#call_option? ⇒ Boolean
Checks if the trade is a CALL option.
-
#derivative? ⇒ Boolean
Checks if the trade instrument is DERIVATIVES.
-
#equity? ⇒ Boolean
Checks if the trade instrument is EQUITY.
-
#net_value ⇒ Float
Calculates the net trade value after deducting all charges.
-
#option? ⇒ Boolean
Checks if the trade is an option (CALL or PUT).
-
#put_option? ⇒ Boolean
Checks if the trade is a PUT option.
-
#sell? ⇒ Boolean
Checks if the trade is a SELL transaction.
-
#to_prompt ⇒ Object
Returns a concise prompt-friendly summary of the trade.
-
#total_charges ⇒ Float
Calculates the total charges for the trade.
-
#total_value ⇒ Float
Calculates the total trade value (quantity × price).
Methods inherited from BaseModel
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, #validation_contract, validation_contract, where
Methods included from APIHelper
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
Constructor Details
This class inherits a constructor from DhanHQ::BaseModel
Class Method Details
.find_by_order_id(order_id) ⇒ Trade?
Retrieves trade details for a specific order ID (current day).
Fetches all trades generated for a particular order ID. This is especially useful during partial trades or Bracket/Cover Orders where traders may get confused reading trades from the tradebook. The response includes all trades generated for the specified order ID.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/DhanHQ/models/trade.rb', line 150 def find_by_order_id(order_id) # Validate input contract = DhanHQ::Contracts::TradeByOrderIdContract.new validation_result = contract.call(order_id: order_id) unless validation_result.success? raise DhanHQ::ValidationError, "Invalid order_id: #{validation_result.errors.to_h}" end response = resource.find(order_id) return nil unless response.is_a?(Hash) || (response.is_a?(Array) && response.any?) data = response.is_a?(Array) ? response.first : response new(data, skip_validation: true) end |
.history(from_date:, to_date:, page: 0) ⇒ Array<Trade> Also known as: all
Retrieves detailed trade history for all orders within a specified time frame.
Fetches paginated trade history data with comprehensive charge breakdowns. The response includes detailed information about SEBI tax, STT, brokerage charges, service tax, exchange transaction charges, and stamp duty for each trade.
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/DhanHQ/models/trade.rb', line 222 def history(from_date:, to_date:, page: 0) validate_history_params(from_date, to_date, page) response = statements_resource.trade_history( from_date: from_date, to_date: to_date, page: page ) return [] unless response.is_a?(Array) response.map { |trade_data| new(trade_data, skip_validation: true) } end |
.resource ⇒ DhanHQ::Resources::Trades
Provides a shared instance of the Trades resource for current day tradebook APIs.
65 66 67 |
# File 'lib/DhanHQ/models/trade.rb', line 65 def resource @resource ||= DhanHQ::Resources::Trades.new end |
.statements_resource ⇒ DhanHQ::Resources::Statements
Provides a shared instance of the Statements resource for historical trade data.
73 74 75 |
# File 'lib/DhanHQ/models/trade.rb', line 73 def statements_resource @statements_resource ||= DhanHQ::Resources::Statements.new end |
.today ⇒ Array<Trade>
Retrieves all trades executed during the current trading day.
Fetches an array of all trades executed in the day. This is useful for tracking daily execution activity and analyzing trade performance.
122 123 124 125 |
# File 'lib/DhanHQ/models/trade.rb', line 122 def today response = resource.all parse_collection_response(response) end |
Instance Method Details
#buy? ⇒ Boolean
Checks if the trade is a BUY transaction.
273 274 275 |
# File 'lib/DhanHQ/models/trade.rb', line 273 def buy? transaction_type == DhanHQ::Constants::TransactionType::BUY end |
#call_option? ⇒ Boolean
Checks if the trade is a CALL option.
344 345 346 |
# File 'lib/DhanHQ/models/trade.rb', line 344 def call_option? drv_option_type == DhanHQ::Constants::OptionType::CALL end |
#derivative? ⇒ Boolean
Checks if the trade instrument is DERIVATIVES.
316 317 318 |
# File 'lib/DhanHQ/models/trade.rb', line 316 def derivative? instrument == "DERIVATIVES" end |
#equity? ⇒ Boolean
Checks if the trade instrument is EQUITY.
302 303 304 |
# File 'lib/DhanHQ/models/trade.rb', line 302 def equity? instrument == DhanHQ::Constants::InstrumentType::EQUITY end |
#net_value ⇒ Float
Calculates the net trade value after deducting all charges.
415 416 417 |
# File 'lib/DhanHQ/models/trade.rb', line 415 def net_value total_value - total_charges end |
#option? ⇒ Boolean
Checks if the trade is an option (CALL or PUT).
330 331 332 |
# File 'lib/DhanHQ/models/trade.rb', line 330 def option? [DhanHQ::Constants::OptionType::CALL, DhanHQ::Constants::OptionType::PUT].include?(drv_option_type) end |
#put_option? ⇒ Boolean
Checks if the trade is a PUT option.
358 359 360 |
# File 'lib/DhanHQ/models/trade.rb', line 358 def put_option? drv_option_type == DhanHQ::Constants::OptionType::PUT end |
#sell? ⇒ Boolean
Checks if the trade is a SELL transaction.
288 289 290 |
# File 'lib/DhanHQ/models/trade.rb', line 288 def sell? transaction_type == DhanHQ::Constants::TransactionType::SELL end |
#to_prompt ⇒ Object
Returns a concise prompt-friendly summary of the trade.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/DhanHQ/models/trade.rb', line 48 def to_prompt parts = [ "#{transaction_type} #{traded_quantity}x #{trading_symbol || security_id}", "@ ₹#{traded_price}", "on #{exchange_segment}/#{product_type}" ] parts << "order=#{order_id}" if order_id parts << "charges=₹#{total_charges}" if respond_to?(:total_charges) && total_charges parts << "time=#{create_time}" if create_time parts.join(", ") end |
#total_charges ⇒ Float
Calculates the total charges for the trade.
Sums all applicable charges including SEBI tax, STT, brokerage charges, service tax, exchange transaction charges, and stamp duty.
395 396 397 398 399 |
# File 'lib/DhanHQ/models/trade.rb', line 395 def total_charges charges = [sebi_tax, stt, brokerage_charges, service_tax, exchange_transaction_charges, stamp_duty].compact charges.sum(&:to_f) end |
#total_value ⇒ Float
Calculates the total trade value (quantity × price).
372 373 374 375 376 |
# File 'lib/DhanHQ/models/trade.rb', line 372 def total_value return 0 unless traded_quantity && traded_price traded_quantity * traded_price end |