Skip to content

Commit f08072d

Browse files
author
Ayushman Chhabra
committed
Made the requested changes and then another
1 parent 5b97f85 commit f08072d

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

01 API Tutorials/04 Using Options in QuantConnect/04 Select Contracts.html

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@
6363
<div class="section-example-container">
6464

6565
<pre class="python">def OnData(self,slice):
66-
for i in slice.OptionChains:
67-
if i.Key != self.symbol: continue
68-
optionchain = i.Value
69-
self.Log("underlying price:" + str(optionchain.Underlying.Price))
66+
for kvp in slice.OptionChains:
67+
if kvp.Key != self.symbol:
68+
continue
69+
optionchain = kvp.Value
7070
df = pd.DataFrame([[x.Right,float(x.Strike),x.Expiry,float(x.BidPrice),float(x.AskPrice)] for x in optionchain],
7171
index=[x.Symbol.Value for x in optionchain],
7272
columns=['type(call 0, put 1)', 'strike', 'expiry', 'ask price', 'bid price'])
73-
self.Log(str(df))
73+
self.Log(f"Underlying price: {optionchain.Underlying.Price}\n{df}")
7474
</pre>
7575
</div>
7676
<table class="table qc-table">
@@ -158,20 +158,22 @@
158158

159159
<div class="section-example-container">
160160

161-
<pre class="python">for i in slice.OptionChains:
162-
if i.Key != self.symbol: continue
163-
optionchain = i.Value
161+
<pre class="python">for kvp in slice.OptionChains:
162+
if kvp.Key != self.symbol:
163+
continue
164+
optionchain = kvp.Value
164165
# differentiate the call and put options
165-
call = [x for x in optionchain if x.Right == 0]
166-
put = [x for x in optionchain if x.Right == 1]
166+
call = [x for x in optionchain if x.Right == OptionRight.Call]
167+
put = [x for x in optionchain if x.Right == OptionRight.Put]
168+
price = optionchain.Underlying.Price
167169
# choose ITM contracts
168-
contracts = [x for x in call if call.UnderlyingLastPrice - x.Strike &gt; 0]
170+
contracts = [x for x in call if price - x.Strike > 0]
169171
# or choose ATM contracts
170-
contracts = sorted(optionchain, key = lambda x: abs(optionchain.Underlying.Price - x.Strike))[0]
172+
contracts = [x for x in call if price - x.Strike == 0]
171173
# or choose OTM contracts
172-
contracts = [x for x in call if call.UnderlyingLastPrice - x.Strike &lt; 0]
174+
contracts = [x for x in call if price - x.Strike < 0]
173175
# sort the contracts by their expiration dates
174-
contracts = sorted(contracts, key = lambda x:x.Expiry, reverse = True)
176+
contracts = sorted(contracts, key = lambda x: x.Expiry, reverse = True)
175177
</pre>
176178
</div>
177179
<p>

0 commit comments

Comments
 (0)