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.
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).
141 142 143 |
# File 'lib/DhanHQ/models/position.rb', line 141 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.
111 112 113 114 115 116 117 118 |
# File 'lib/DhanHQ/models/position.rb', line 111 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.
201 202 203 204 205 206 207 |
# File 'lib/DhanHQ/models/position.rb', line 201 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.
227 228 229 |
# File 'lib/DhanHQ/models/position.rb', line 227 def exit_all! resource.exit_all end |
.resource ⇒ DhanHQ::Resources::Positions
Provides a shared instance of the Positions resource.
52 53 54 |
# File 'lib/DhanHQ/models/position.rb', line 52 def resource @resource ||= DhanHQ::Resources::Positions.new end |