Class: DhanHQ::Skills::Builtin::BuyAtmCall
Overview
Skill to buy an ATM call option on an index.
Steps: find instrument → get spot price → get option chain →
select ATM strike → prepare trade intent.
Instance Method Summary
collapse
#call, description, #description, #name, param, #param_definitions, params, risk, scope, step, steps, validate_params!
Instance Method Details
#find_instrument(ctx) ⇒ Object
36
37
38
39
|
# File 'lib/DhanHQ/skills/builtin/buy_atm_call.rb', line 36
def find_instrument(ctx)
ctx[:instrument] = DhanHQ::Models::Instrument.find(DhanHQ::Constants::ExchangeSegment::IDX_I, ctx[:symbol])
ctx
end
|
#get_option_chain(ctx) ⇒ Object
46
47
48
49
|
# File 'lib/DhanHQ/skills/builtin/buy_atm_call.rb', line 46
def get_option_chain(ctx)
ctx[:chain] = ctx[:instrument].option_chain(expiry: ctx[:expiry])
ctx
end
|
#get_spot_price(ctx) ⇒ Object
41
42
43
44
|
# File 'lib/DhanHQ/skills/builtin/buy_atm_call.rb', line 41
def get_spot_price(ctx)
ctx[:spot_price] = ctx[:instrument].ltp
ctx
end
|
#prepare_intent(ctx) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/DhanHQ/skills/builtin/buy_atm_call.rb', line 65
def prepare_intent(ctx)
ctx[:intent] = {
trade_type: "OPTIONS_BUY",
instrument: "#{ctx[:symbol]} #{ctx[:strike]} CE",
security_id: ctx[:security_id],
strike: ctx[:strike],
expiry: ctx[:expiry],
option_type: "CE",
quantity: ctx[:quantity],
premium: ctx[:premium],
stop_loss: ctx[:stop_loss],
target: ctx[:target],
note: "Prepared ATM call buy. Await human confirmation."
}
ctx
end
|
#select_atm_strike(ctx) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/DhanHQ/skills/builtin/buy_atm_call.rb', line 51
def select_atm_strike(ctx)
spot = ctx[:spot_price]
chain = ctx[:chain]
atm = nearest_strike(chain, spot)
raise ArgumentError, "Could not find ATM strike" unless atm
ctx[:selected_option] = atm
ctx[:security_id] = leg_security_id(atm, "CE")
ctx[:strike] = atm[:strike]
ctx[:premium] = leg_premium(atm, "CE")
ctx
end
|