Class: DhanHQ::Models::SuperOrder
- Extended by:
- Concerns::BangWrites, Concerns::TrackedWrites
- Includes:
- Concerns::ApiResponseHandler
- Defined in:
- lib/DhanHQ/models/super_order.rb
Overview
Static IP Whitelisting: Super order creation, modification, and cancellation APIs require Static IP whitelisting. Ensure your IP is whitelisted before using these endpoints.
Model for managing multi-leg super orders with smart execution.
Super orders are built for smart execution of trades. They are a collection of orders clubbed into a single order request, which includes an entry leg, target leg, and stop loss leg along with the option to add trailing stop loss. This allows for server-side risk management immediately after entry.
Super orders can be placed across all exchanges and segments, supporting intraday, carry forward, or MTF orders.
Constant Summary
Constants included from ResponseHelper
ResponseHelper::STATUS_ERROR_FALLBACK
Instance Attribute Summary
Attributes inherited from BaseModel
Class Method Summary collapse
-
.all ⇒ Array<SuperOrder>
Retrieves all super orders placed during the current trading day.
-
.create(params) ⇒ SuperOrder?
Creates a new super order with entry, target, and stop loss legs.
-
.resource ⇒ DhanHQ::Resources::SuperOrders
Provides a shared instance of the SuperOrders resource.
Instance Method Summary collapse
-
#cancel(leg_name = DhanHQ::Constants::LegName::ENTRY_LEG) ⇒ Boolean
Cancels a specific leg of a super order, or the entry leg by default.
-
#modify(new_params) ⇒ Boolean
Modifies any leg of a Super Order while it is in PENDING or PART_TRADED state.
Methods included from Concerns::BangWrites
bang_class_writes, bang_writes
Methods included from Concerns::TrackedWrites
track_class_writes, track_writes
Methods inherited from BaseModel
api, api_type, #assign_attributes, attributes, #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
.all ⇒ Array<SuperOrder>
Order Status: "CLOSED" is used when the ENTRY_LEG and one of either TARGET_LEG or STOP_LOSS_LEG is triggered for entire quantity. "TRIGGERED" status is present for TARGET_LEG and STOP_LOSS_LEG indicating which leg is actually triggered.
Retrieves all super orders placed during the current trading day.
Fetches a special order book that only consists of Super Orders, where the target and stop loss orders are nested under the main entry order leg. Individual legs of each super order can also be found in the main order book with their Order ID.
144 145 146 147 148 149 |
# File 'lib/DhanHQ/models/super_order.rb', line 144 def all response = resource.all return [] unless response.is_a?(Array) response.map { |o| new(o, skip_validation: true) } end |
.create(params) ⇒ SuperOrder?
Creates a new super order with entry, target, and stop loss legs.
Places a super order that combines entry, target, and stop-loss legs into one atomic instruction. Supports an optional trailing jump for server-side risk management immediately after entry. Available across all exchanges and segments, supporting intraday, carry-forward, or MTF orders.
218 219 220 221 222 223 224 225 226 |
# File 'lib/DhanHQ/models/super_order.rb', line 218 def create(params) normalized_params = snake_case(params) config = DhanHQ.configuration normalized_params[:dhan_client_id] ||= config.client_id if config&.client_id response = resource.create(normalized_params) return nil unless response.is_a?(Hash) && response["orderId"] new(order_id: response["orderId"], order_status: response["orderStatus"], skip_validation: true) end |
.resource ⇒ DhanHQ::Resources::SuperOrders
Provides a shared instance of the SuperOrders resource.
76 77 78 |
# File 'lib/DhanHQ/models/super_order.rb', line 76 def resource @resource ||= DhanHQ::Resources::SuperOrders.new end |
Instance Method Details
#cancel(leg_name = DhanHQ::Constants::LegName::ENTRY_LEG) ⇒ Boolean
The API returns HTTP 202 Accepted on successful cancellation.
Cancels a specific leg of a super order, or the entry leg by default.
Cancels a pending/active super order leg using the order ID and leg name. Cancelling the main entry order ID (ENTRY_LEG) cancels all legs. If a particular target or stop loss leg is cancelled, it cannot be added again.
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/DhanHQ/models/super_order.rb', line 336 def cancel(leg_name = DhanHQ::Constants::LegName::ENTRY_LEG) raise "Order ID is required to cancel a super order" unless id DhanHQ.logger&.info("[DhanHQ::Models::SuperOrder] Cancelling super order #{id} leg #{leg_name}") response = self.class.resource.cancel(id, leg_name) return false unless response.is_a?(Hash) && response["orderStatus"] == DhanHQ::Constants::OrderStatus::CANCELLED DhanHQ.logger&.info("[DhanHQ::Models::SuperOrder] Super order #{id} leg #{leg_name} cancelled successfully") true end |
#modify(new_params) ⇒ Boolean
Modifies any leg of a Super Order while it is in PENDING or PART_TRADED state.
The ENTRY_LEG can modify the entire super order and can only be modified when the order status is PENDING or PART_TRADED. Once the entry order status is TRADED, only TARGET_LEG and STOP_LOSS_LEG price and trail jump can be modified.
295 296 297 298 299 300 301 302 303 304 |
# File 'lib/DhanHQ/models/super_order.rb', line 295 def modify(new_params) raise "Order ID is required to modify a super order" unless id DhanHQ.logger&.info("[DhanHQ::Models::SuperOrder] Modifying super order #{id}") response = self.class.resource.update(id, new_params) return false unless response.is_a?(Hash) && response["orderId"] == id DhanHQ.logger&.info("[DhanHQ::Models::SuperOrder] Super order #{id} modified successfully") true end |