Class: DhanHQ::Models::TwapOrder
- Includes:
- Concerns::ApiResponseHandler
- Defined in:
- lib/DhanHQ/models/twap_order.rb
Overview
Static IP Whitelisting: TWAP order creation, modification, and cancellation APIs require Static IP whitelisting.
Model for creating and managing TWAP Orders.
TWAP (Time-Weighted Average Price) orders split a large order into smaller slices distributed evenly across a defined time window. This minimizes market impact and achieves execution closer to the period's average price.
Constant Summary
Constants included from ResponseHelper
ResponseHelper::STATUS_ERROR_FALLBACK
Instance Attribute Summary
Attributes inherited from BaseModel
Class Method Summary collapse
-
.all ⇒ Array<TwapOrder>
Retrieves all TWAP orders for the day.
-
.create(params) ⇒ TwapOrder?
Creates a new TWAP order.
-
.find(order_id) ⇒ TwapOrder?
Retrieves a specific TWAP order by ID.
- .resource ⇒ DhanHQ::Resources::TwapOrders
Instance Method Summary collapse
-
#cancel ⇒ Boolean
Cancels the TWAP order.
-
#modify(new_params) ⇒ TwapOrder?
Modifies an existing TWAP order.
Methods inherited from BaseModel
api, api_type, #assign_attributes, attributes, #delete, #destroy, #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
.all ⇒ Array<TwapOrder>
Retrieves all TWAP orders for the day.
68 69 70 71 72 73 |
# File 'lib/DhanHQ/models/twap_order.rb', line 68 def all response = resource.all return [] unless response.is_a?(Array) response.map { |o| new(o, skip_validation: true) } end |
.create(params) ⇒ TwapOrder?
Creates a new TWAP order.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/DhanHQ/models/twap_order.rb', line 90 def create(params) normalized = snake_case(params) config = DhanHQ.configuration normalized[:dhan_client_id] ||= config.client_id if config&.client_id validate_params!(normalized, DhanHQ::Contracts::TwapOrderCreateContract) formatted = camelize_keys(normalized) response = resource.create(formatted) return nil unless response.is_a?(Hash) && response["orderId"] new(order_id: response["orderId"], order_status: response["orderStatus"], skip_validation: true) end |
.find(order_id) ⇒ TwapOrder?
Retrieves a specific TWAP order by ID.
79 80 81 82 83 84 |
# File 'lib/DhanHQ/models/twap_order.rb', line 79 def find(order_id) response = resource.find(order_id) return nil unless response.is_a?(Hash) && response.any? new(response, skip_validation: true) end |
.resource ⇒ DhanHQ::Resources::TwapOrders
61 62 63 |
# File 'lib/DhanHQ/models/twap_order.rb', line 61 def resource @resource ||= DhanHQ::Resources::TwapOrders.new end |
Instance Method Details
#cancel ⇒ Boolean
Cancels the TWAP order.
128 129 130 131 132 133 |
# File 'lib/DhanHQ/models/twap_order.rb', line 128 def cancel raise "Order ID is required to cancel a TWAP order" unless order_id response = self.class.resource.cancel(order_id) response.is_a?(Hash) && response["orderStatus"] == DhanHQ::Constants::OrderStatus::CANCELLED end |
#modify(new_params) ⇒ TwapOrder?
Modifies an existing TWAP order.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/DhanHQ/models/twap_order.rb', line 107 def modify(new_params) raise "Order ID is required to modify a TWAP order" unless order_id DhanHQ.logger&.info("[DhanHQ::Models::TwapOrder] Modifying order #{order_id}") full_params = snake_case(new_params) config = DhanHQ.configuration full_params[:dhan_client_id] ||= config.client_id if config&.client_id full_params[:order_id] = order_id validate_params!(full_params, DhanHQ::Contracts::TwapOrderModifyContract) formatted = camelize_keys(full_params) response = self.class.resource.update(order_id, formatted) success = handle_api_response(response, success_key: "orderId", context: "[DhanHQ::Models::TwapOrder] Modification") return self.class.find(order_id) if success nil end |