Class: DhanHQ::Models::KillSwitch

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/DhanHQ/models/kill_switch.rb

Overview

Note:

Important: Before activating the kill switch, you must ensure that all your positions are closed and there are no pending orders in your account. The kill switch will not activate if there are open positions or pending orders.

Model for managing the trading kill switch feature.

The Kill Switch API lets you activate or deactivate the kill switch for your account, which will disable trading for the current trading day. This is a safety feature that can be used to prevent further trading activity when needed.

Examples:

Activate kill switch

response = DhanHQ::Models::KillSwitch.activate
puts response[:kill_switch_status]
# => "Kill Switch has been successfully activated"

Deactivate kill switch

response = DhanHQ::Models::KillSwitch.deactivate
puts response[:kill_switch_status]

Update kill switch with custom status

response = DhanHQ::Models::KillSwitch.update("ACTIVATE")
if response[:kill_switch_status].include?("successfully")
  puts "Kill switch activated"
end

Constant Summary collapse

HTTP_PATH =

Base path used by the kill switch resource.

"/v2/killswitch"

Constants included from ResponseHelper

ResponseHelper::STATUS_ERROR_FALLBACK

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes, #errors

Class Method Summary collapse

Methods inherited from BaseModel

all, 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

#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

.activateHash{Symbol => String}

Note:

Prerequisites: All positions must be closed and there must be no pending orders in your account before activation. The API will reject the activation request if these conditions are not met.

Activates the kill switch for your trading account.

Disables trading for the current trading day. All trading operations will be blocked until the kill switch is deactivated. This is a safety feature to prevent further trading activity.

Examples:

Activate kill switch

response = DhanHQ::Models::KillSwitch.activate
puts response[:kill_switch_status]
# => "Kill Switch has been successfully activated"

Activate with error handling

begin
  response = DhanHQ::Models::KillSwitch.activate
  if response[:kill_switch_status].include?("successfully")
    puts "✓ Kill switch activated - trading disabled"
  end
rescue => e
  puts "Failed to activate kill switch: #{e.message}"
  puts "Ensure all positions are closed and no orders are pending"
end

Returns:

  • (Hash{Symbol => String})

    Response hash containing kill switch activation result. Response structure (keys normalized to snake_case):

    • :dhan_client_id [String] User-specific identification generated by Dhan
    • :kill_switch_status [String] Status message, typically: "Kill Switch has been successfully activated"


107
108
109
# File 'lib/DhanHQ/models/kill_switch.rb', line 107

def activate
  update("ACTIVATE")
end

.deactivateHash{Symbol => String}

Deactivates the kill switch for your trading account.

Re-enables trading for your account. After deactivation, you can resume placing orders and executing trades normally.

Examples:

Deactivate kill switch

response = DhanHQ::Models::KillSwitch.deactivate
puts response[:kill_switch_status]

Re-enable trading

response = DhanHQ::Models::KillSwitch.deactivate
if response[:kill_switch_status].include?("successfully")
  puts "✓ Trading re-enabled"
end

Returns:

  • (Hash{Symbol => String})

    Response hash containing kill switch deactivation result. Response structure (keys normalized to snake_case):

    • :dhan_client_id [String] User-specific identification generated by Dhan
    • :kill_switch_status [String] Status message indicating deactivation result


132
133
134
# File 'lib/DhanHQ/models/kill_switch.rb', line 132

def deactivate
  update("DEACTIVATE")
end

.resourceDhanHQ::Resources::KillSwitch

Provides a shared instance of the KillSwitch resource.

Returns:



40
41
42
# File 'lib/DhanHQ/models/kill_switch.rb', line 40

def resource
  @resource ||= DhanHQ::Resources::KillSwitch.new
end

.statusHash{Symbol => String}

Fetches the current kill switch status for your account.

Checks whether the kill switch is currently active or inactive for the current trading day.

Examples:

Check if kill switch is active

response = DhanHQ::Models::KillSwitch.status
if response[:kill_switch_status] == "ACTIVATE"
  puts "Kill switch is active — trading disabled"
else
  puts "Kill switch is inactive — trading enabled"
end

Returns:

  • (Hash{Symbol => String})

    Response hash containing kill switch status.

    • :dhan_client_id [String] User-specific identification generated by Dhan
    • :kill_switch_status [String] Current status: "ACTIVATE" or "DEACTIVATE"


154
155
156
# File 'lib/DhanHQ/models/kill_switch.rb', line 154

def status
  resource.status
end

.update(status) ⇒ Hash{Symbol => String}

Updates the kill switch status with the specified action.

Allows you to set the kill switch to either "ACTIVATE" or "DEACTIVATE" state. The status is passed as a query parameter to the API endpoint.

Examples:

Update kill switch status

response = DhanHQ::Models::KillSwitch.update("ACTIVATE")
puts response[:kill_switch_status]

Check if activation was successful

response = DhanHQ::Models::KillSwitch.update("ACTIVATE")
if response[:kill_switch_status].include?("successfully")
  puts "Kill switch is now active"
end

Parameters:

  • status (String)

    Kill switch action to perform. Valid values: "ACTIVATE", "DEACTIVATE"

Returns:

  • (Hash{Symbol => String})

    Response hash containing kill switch operation result. Response structure (keys normalized to snake_case):

    • :dhan_client_id [String] User-specific identification generated by Dhan
    • :kill_switch_status [String] Status message indicating the result of the operation. For activation: "Kill Switch has been successfully activated" For deactivation: Status message for deactivation


70
71
72
# File 'lib/DhanHQ/models/kill_switch.rb', line 70

def update(status)
  resource.update(status)
end