Class: DhanHQ::Skills::Builtin::Straddle

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

Overview

Skill to build a long straddle (buy ATM call + buy ATM put).

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

Examples:

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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/DhanHQ/skills/builtin/straddle.rb', line 66

def build_intent(ctx)
  ctx[:intent] = {
    trade_type: "STRADDLE",
    symbol: ctx[:symbol],
    expiry: ctx[:expiry],
    quantity: ctx[:quantity],
    legs: [
      { action: DhanHQ::Constants::TransactionType::BUY, option_type: "CE", strike: ctx[:atm_strike], security_id: ctx[:ce_security_id], premium: ctx[:ce_premium] },
      { action: DhanHQ::Constants::TransactionType::BUY, option_type: "PE", strike: ctx[:atm_strike], security_id: ctx[:pe_security_id], premium: ctx[:pe_premium] }
    ],
    total_premium: ctx[:total_premium],
    break_even_upside: ctx[:atm_strike].to_f + ctx[:total_premium],
    break_even_downside: ctx[:atm_strike].to_f - ctx[:total_premium],
    stop_loss: ctx[:stop_loss],
    target: ctx[:target],
    note: "Long straddle prepared. Await human confirmation."
  }
  ctx
end

#find_instrument(ctx) ⇒ Object



35
36
37
38
# File 'lib/DhanHQ/skills/builtin/straddle.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/straddle.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/straddle.rb', line 40

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

#select_atm_strikes(ctx) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/DhanHQ/skills/builtin/straddle.rb', line 50

def select_atm_strikes(ctx)
  spot = ctx[:spot_price].to_f
  chain = ctx[:chain]

  atm = nearest_strike(chain, spot)
  raise ArgumentError, "Could not find ATM strike" unless atm

  ctx[:atm_strike] = atm[:strike]
  ctx[:ce_security_id] = leg_security_id(atm, "CE")
  ctx[:pe_security_id] = leg_security_id(atm, "PE")
  ctx[:ce_premium] = leg_premium(atm, "CE")
  ctx[:pe_premium] = leg_premium(atm, "PE")
  ctx[:total_premium] = ctx[:ce_premium].to_f + ctx[:pe_premium].to_f
  ctx
end