Class: DhanHQ::Skills::Builtin::SquareOffAll

Inherits:
DhanHQ::Skills::Base show all
Defined in:
lib/DhanHQ/skills/builtin/square_off_all.rb

Overview

Skill to exit all open positions at market price.

Steps: fetch positions → exit each position → summarize results.

Examples:

result = DhanHQ::Skills::Registry.call("square_off_all")
puts result[:exited_count]

Instance Method Summary collapse

Methods inherited from DhanHQ::Skills::Base

#call, description, #description, #name, param, #param_definitions, params, risk, scope, step, steps, validate_params!

Instance Method Details

#exit_positions(ctx) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/DhanHQ/skills/builtin/square_off_all.rb', line 27

def exit_positions(ctx)
  results = ctx[:positions].map do
    DhanHQ::Models::Position.exit_all!
  rescue StandardError => e
    { error: e.message }
  end

  ctx[:exit_results] = results
  ctx[:exited_count] = results.count { |r| !(r.is_a?(Hash) && r.key?(:error)) }
  ctx[:failed_count] = results.count { |r| r.is_a?(Hash) && r.key?(:error) }
  ctx
end

#fetch_positions(ctx) ⇒ Object



22
23
24
25
# File 'lib/DhanHQ/skills/builtin/square_off_all.rb', line 22

def fetch_positions(ctx)
  ctx[:positions] = DhanHQ::Models::Position.all.reject { |p| p.net_qty.to_i.zero? }
  ctx
end