|
63 | 63 | <div class="section-example-container"> |
64 | 64 |
|
65 | 65 | <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 |
70 | 70 | df = pd.DataFrame([[x.Right,float(x.Strike),x.Expiry,float(x.BidPrice),float(x.AskPrice)] for x in optionchain], |
71 | 71 | index=[x.Symbol.Value for x in optionchain], |
72 | 72 | 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}") |
74 | 74 | </pre> |
75 | 75 | </div> |
76 | 76 | <table class="table qc-table"> |
|
158 | 158 |
|
159 | 159 | <div class="section-example-container"> |
160 | 160 |
|
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 |
164 | 165 | # 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 |
167 | 169 | # choose ITM contracts |
168 | | - contracts = [x for x in call if call.UnderlyingLastPrice - x.Strike > 0] |
| 170 | + contracts = [x for x in call if price - x.Strike > 0] |
169 | 171 | # 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] |
171 | 173 | # or choose OTM contracts |
172 | | - contracts = [x for x in call if call.UnderlyingLastPrice - x.Strike < 0] |
| 174 | + contracts = [x for x in call if price - x.Strike < 0] |
173 | 175 | # 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) |
175 | 177 | </pre> |
176 | 178 | </div> |
177 | 179 | <p> |
|
0 commit comments