Class: DhanHQ::Skills::Builtin::Strangle
Overview
Skill to build a long strangle (buy OTM CE + buy OTM PE).
Steps: find instrument → spot price → option chain → select strikes → build intent.
Instance Method Summary
collapse
#call, description, #description, #name, param, #param_definitions, params, risk, scope, step, steps, validate_params!
Instance Method Details
#build_intent(ctx) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/DhanHQ/skills/builtin/strangle.rb', line 74
def build_intent(ctx)
ctx[:intent] = {
trade_type: "STRANGLE",
symbol: ctx[:symbol],
expiry: ctx[:expiry],
quantity: ctx[:quantity],
legs: [
{ action: DhanHQ::Constants::TransactionType::BUY, option_type: "CE", strike: ctx[:ce_strike], security_id: ctx[:ce_security_id], premium: ctx[:ce_premium] },
{ action: DhanHQ::Constants::TransactionType::BUY, option_type: "PE", strike: ctx[:pe_strike], security_id: ctx[:pe_security_id], premium: ctx[:pe_premium] }
],
stop_loss: ctx[:stop_loss],
target: ctx[:target],
note: "Long strangle prepared. Await human confirmation."
}
ctx
end
|
#find_instrument(ctx) ⇒ Object
#get_option_chain(ctx) ⇒ Object
46
47
48
49
|
# File 'lib/DhanHQ/skills/builtin/strangle.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/strangle.rb', line 41
def get_spot_price(ctx)
ctx[:spot_price] = ctx[:instrument].ltp
ctx
end
|
#select_strikes(ctx) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/DhanHQ/skills/builtin/strangle.rb', line 51
def select_strikes(ctx)
spot = ctx[:spot_price].to_f
chain = ctx[:chain]
offset = ctx[:offset_pct] / 100.0
ce_strike = spot * (1 + offset)
pe_strike = spot * (1 - offset)
long_ce = nearest_strike(chain, ce_strike)
long_pe = nearest_strike(chain, pe_strike)
raise ArgumentError, "Could not find suitable CE strike near #{ce_strike}" unless long_ce
raise ArgumentError, "Could not find suitable PE strike near #{pe_strike}" unless long_pe
ctx[:ce_strike] = long_ce[:strike]
ctx[:pe_strike] = long_pe[:strike]
ctx[:ce_security_id] = leg_security_id(long_ce, "CE")
ctx[:pe_security_id] = leg_security_id(long_pe, "PE")
ctx[:ce_premium] = leg_premium(long_ce, "CE")
ctx[:pe_premium] = leg_premium(long_pe, "PE")
ctx
end
|