Class: DhanHQ::Skills::Builtin::BearCallSpread

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

Overview

Skill to build a bear call spread (sell OTM call, buy further OTM call).

Steps: find instrument → spot price → option chain → select strikes → build intent.

Examples:

result = DhanHQ::Skills::Registry.call("bear_call_spread",
  symbol: "NIFTY",
  expiry: "2026-01-30",
  quantity: 50
)

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

#build_intent(ctx) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/DhanHQ/skills/builtin/bear_call_spread.rb', line 71

def build_intent(ctx)
  ctx[:intent] = {
    trade_type: "BEAR_CALL_SPREAD",
    symbol: ctx[:symbol],
    expiry: ctx[:expiry],
    quantity: ctx[:quantity],
    spread_width: ctx[:spread_width],
    max_loss: ctx[:max_loss],
    legs: ctx[:legs],
    note: "Bear call spread prepared. Await human confirmation before execution."
  }
  ctx
end

#find_instrument(ctx) ⇒ Object



35
36
37
38
# File 'lib/DhanHQ/skills/builtin/bear_call_spread.rb', line 35

def find_instrument(ctx)
  ctx[:instrument] = DhanHQ::Models::Instrument.find(DhanHQ::Constants::ExchangeSegment::IDX_I, ctx[:symbol])
  ctx
end

#get_option_chain(ctx) ⇒ Object



45
46
47
48
# File 'lib/DhanHQ/skills/builtin/bear_call_spread.rb', line 45

def get_option_chain(ctx)
  ctx[:chain] = ctx[:instrument].option_chain(expiry: ctx[:expiry])
  ctx
end

#get_spot_price(ctx) ⇒ Object



40
41
42
43
# File 'lib/DhanHQ/skills/builtin/bear_call_spread.rb', line 40

def get_spot_price(ctx)
  ctx[:spot_price] = ctx[:instrument].ltp
  ctx
end

#select_strikes(ctx) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/DhanHQ/skills/builtin/bear_call_spread.rb', line 50

def select_strikes(ctx)
  spot = ctx[:spot_price].to_f
  chain = ctx[:chain]
  spread = ctx[:spread_width].to_f

  atm_strike_price = nearest_strike(chain, spot)[:strike].to_f

  short_call = find_strike(chain, atm_strike_price + spread)
  long_call = find_strike(chain, atm_strike_price + (spread * 2))

  raise ArgumentError, "Could not build bear call spread — insufficient strikes in chain" unless short_call && long_call

  ctx[:legs] = [
    { action: DhanHQ::Constants::TransactionType::SELL, option_type: "CE", strike: short_call[:strike],
      security_id: leg_security_id(short_call, "CE") },
    { action: DhanHQ::Constants::TransactionType::BUY, option_type: "CE", strike: long_call[:strike],
      security_id: leg_security_id(long_call, "CE") }
  ]
  ctx
end