Class: DhanHQ::Models::TwapOrder

Inherits:
BaseModel show all
Includes:
Concerns::ApiResponseHandler
Defined in:
lib/DhanHQ/models/twap_order.rb

Overview

Note:

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.

Examples:

Create a TWAP order

order = DhanHQ::Models::TwapOrder.create(
  dhan_client_id: "1000000003",
  transaction_type: "SELL",
  exchange_segment: "NSE_EQ",
  product_type: "INTRADAY",
  order_type: "MARKET",
  validity: "DAY",
  security_id: "11536",
  quantity: 500,
  price: 0,                    # MARKET order
  slice_interval: 300,         # 5 minutes
  start_time: "09:30:00",
  end_time: "15:00:00"
)
puts "TWAP Order ID: #{order.order_id} - #{order.order_status}"

Modify a TWAP order (extend window)

order = DhanHQ::Models::TwapOrder.find(order_id)
order.modify(
  dhan_client_id: "1000000003",
  order_id: order_id,
  end_time: "15:30:00",
  slice_interval: 600
)

Cancel a TWAP order

order = DhanHQ::Models::TwapOrder.find(order_id)
order.cancel

Constant Summary

Constants included from ResponseHelper

ResponseHelper::STATUS_ERROR_FALLBACK

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes, #errors

Class Method Summary collapse

Instance Method Summary collapse

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

#handle_response

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

#build_from_response

Constructor Details

This class inherits a constructor from DhanHQ::BaseModel

Class Method Details

.allArray<TwapOrder>

Retrieves all TWAP orders for the day.

Returns:



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.

Parameters:

  • params (Hash{Symbol => String, Integer, Float})

Returns:



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.

Parameters:

  • order_id (String)

Returns:



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

.resourceDhanHQ::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

#cancelBoolean

Cancels the TWAP order.

Returns:

  • (Boolean)

    true when cancelled successfully



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.

Parameters:

  • new_params (Hash{Symbol => String, Integer, Float})

Returns:



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