Class: DhanHQ::Models::Position
- Defined in:
- lib/DhanHQ/models/position.rb
Overview
Model for managing intraday and carry-forward positions.
The Positions API lets you retrieve a list of all open positions for the day. This includes all F&O carryforward positions as well. You can also convert positions between product types (e.g., intraday to delivery or vice versa).
Constant Summary collapse
- HTTP_PATH =
Base path used by the positions resource.
"/v2/positions"
Constants included from ResponseHelper
ResponseHelper::STATUS_ERROR_FALLBACK
Instance Attribute Summary
Attributes inherited from BaseModel
Class Method Summary collapse
-
.active ⇒ Array<Position>
Filters the position list to return only active (open) positions.
-
.all ⇒ Array<Position>
Retrieves all positions for the current trading day.
-
.convert(params) ⇒ Hash, DhanHQ::ErrorObject
Converts an existing position from one product type to another.
-
.exit_all! ⇒ Hash{Symbol => String}
Exits all active positions and cancels all open orders for the current trading day.
-
.resource ⇒ DhanHQ::Resources::Positions
Provides a shared instance of the Positions resource.
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Returns true if this position is closed.
-
#derivatives? ⇒ Boolean
Returns true if this is an F&O position.
-
#futures? ⇒ Boolean
Returns true if this is a futures position.
-
#in_loss? ⇒ Boolean
Returns true if this position is in loss (unrealized).
-
#long? ⇒ Boolean
Returns true if this is a long position.
-
#open? ⇒ Boolean
Returns true if this position has an open quantity.
-
#options? ⇒ Boolean
Returns true if this is an options position.
-
#profitable? ⇒ Boolean
Returns true if this position is profitable (unrealized).
-
#short? ⇒ Boolean
Returns true if this is a short position.
-
#to_prompt ⇒ Object
Returns a concise prompt-friendly summary of the position.
-
#total_pnl ⇒ Object
Returns the total unrealized P&L including realized.
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, #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
.active ⇒ Array<Position>
Filters the position list to return only active (open) positions.
Removes positions with "CLOSED" status, returning only positions that are currently open (LONG or SHORT positions).
207 208 209 |
# File 'lib/DhanHQ/models/position.rb', line 207 def active all.reject { |position| position.position_type == DhanHQ::Constants::OrderStatus::CLOSED } end |
.all ⇒ Array<Position>
Retrieves all positions for the current trading day.
Fetches a list of all open positions including F&O carryforward positions. Returns both active (LONG/SHORT) and closed positions. Use active if you only need open positions.
177 178 179 180 181 182 183 184 |
# File 'lib/DhanHQ/models/position.rb', line 177 def all response = resource.all return [] unless response.is_a?(Array) response.map do |position| new(snake_case(position), skip_validation: true) end end |
.convert(params) ⇒ Hash, DhanHQ::ErrorObject
The API returns HTTP 202 Accepted on successful conversion
Converts an existing position from one product type to another.
Allows conversion between eligible product types, most commonly between intraday (INTRADAY) and delivery (CNC) positions. This is useful when you want to hold an intraday position overnight or convert a delivery position to intraday.
267 268 269 270 271 272 273 |
# File 'lib/DhanHQ/models/position.rb', line 267 def convert(params) formatted_params = camelize_keys(params) validate_params!(formatted_params, DhanHQ::Contracts::PositionConversionContract) response = resource.convert(formatted_params) success_response?(response) ? response : DhanHQ::ErrorObject.new(response) end |
.exit_all! ⇒ Hash{Symbol => String}
Exits all active positions and cancels all open orders for the current trading day.
This is a safety endpoint for emergency position closure. It sends a DELETE request to close all positions and cancel all pending orders in one call.
293 294 295 |
# File 'lib/DhanHQ/models/position.rb', line 293 def exit_all! resource.exit_all end |
.resource ⇒ DhanHQ::Resources::Positions
Provides a shared instance of the Positions resource.
118 119 120 |
# File 'lib/DhanHQ/models/position.rb', line 118 def resource @resource ||= DhanHQ::Resources::Positions.new end |
Instance Method Details
#closed? ⇒ Boolean
Returns true if this position is closed.
74 75 76 |
# File 'lib/DhanHQ/models/position.rb', line 74 def closed? position_type == DhanHQ::Constants::OrderStatus::CLOSED || net_qty.to_i.zero? end |
#derivatives? ⇒ Boolean
Returns true if this is an F&O position.
99 100 101 |
# File 'lib/DhanHQ/models/position.rb', line 99 def derivatives? drv_expiry_date.present? || drv_strike_price.to_f.positive? end |
#futures? ⇒ Boolean
Returns true if this is a futures position.
109 110 111 |
# File 'lib/DhanHQ/models/position.rb', line 109 def futures? derivatives? && drv_option_type.nil? end |
#in_loss? ⇒ Boolean
Returns true if this position is in loss (unrealized).
89 90 91 |
# File 'lib/DhanHQ/models/position.rb', line 89 def in_loss? unrealized_profit.to_f.negative? end |
#long? ⇒ Boolean
Returns true if this is a long position.
64 65 66 |
# File 'lib/DhanHQ/models/position.rb', line 64 def long? position_type == DhanHQ::Constants::PositionType::LONG end |
#open? ⇒ Boolean
Returns true if this position has an open quantity.
79 80 81 |
# File 'lib/DhanHQ/models/position.rb', line 79 def open? !closed? && net_qty.to_i != 0 end |
#options? ⇒ Boolean
Returns true if this is an options position.
104 105 106 |
# File 'lib/DhanHQ/models/position.rb', line 104 def derivatives? && drv_option_type.present? end |
#profitable? ⇒ Boolean
Returns true if this position is profitable (unrealized).
84 85 86 |
# File 'lib/DhanHQ/models/position.rb', line 84 def profitable? unrealized_profit.to_f.positive? end |
#short? ⇒ Boolean
Returns true if this is a short position.
69 70 71 |
# File 'lib/DhanHQ/models/position.rb', line 69 def short? position_type == DhanHQ::Constants::PositionType::SHORT end |
#to_prompt ⇒ Object
Returns a concise prompt-friendly summary of the position.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/DhanHQ/models/position.rb', line 48 def to_prompt parts = [ "#{position_type} #{net_qty}x #{trading_symbol || security_id}", "on #{exchange_segment}/#{product_type}" ] parts << "buy_avg=#{buy_avg}" if buy_avg&.positive? parts << "sell_avg=#{sell_avg}" if sell_avg&.positive? parts << "realized_pnl=#{realized_profit}" if realized_profit parts << "unrealized_pnl=#{unrealized_profit}" if unrealized_profit parts << "expiry=#{drv_expiry_date}" if drv_expiry_date parts << "strike=#{drv_strike_price}" if drv_strike_price&.positive? parts << "type=#{drv_option_type}" if drv_option_type parts.join(", ") end |
#total_pnl ⇒ Object
Returns the total unrealized P&L including realized.
94 95 96 |
# File 'lib/DhanHQ/models/position.rb', line 94 def total_pnl realized_profit.to_f + unrealized_profit.to_f end |