1

So the dictionary titled 'option' spits out the result (tradeable options) below by strike_price, ask_price, delta and implied_volatility. But I don't need it to spit out all of the tradeable options. I only want the output to show me 1 tradeable option that has the highest implied_volatility (IV), so for example, the result should only show the option with the highest IV:

Strike Price: 43.0000, Ask: 0.030000, Bid: 0.000000, Delta: 0.008705, IV: 1.449510 - because IV here is the highest from the entire output below.

How can I do this?

import config 
import robin_stocks as r 

r.login(config.USERNAME,config.PASSWORD)


#specify criteria to search for options of a given symbol and its exp date
symbol = 'GDX'
expirationDate = '2020-06-19'


search_option = r.find_options_for_stock_by_expiration(symbol,expirationDate,optionType='call')


for option in search_option:

        print("Strike Price: {}, Ask: {}, Bid: {}, Delta: {}, IV: {}".format(option['strike_price'],option['ask_price'],option['bid_price'],option['delta'], option['implied_volatility']))


**OUTPUT**:
Strike Price: 42.0000, Ask: 0.030000, Bid: 0.000000, Delta: 0.009354, IV: 1.335719
Strike Price: 43.0000, Ask: 0.030000, Bid: 0.000000, Delta: 0.008705, IV: 1.449510
Strike Price: 35.5000, Ask: 0.060000, Bid: 0.040000, Delta: 0.073395, IV: 0.634361
Strike Price: 36.5000, Ask: 0.030000, Bid: 0.020000, Delta: 0.041370, IV: 0.743600
Kam Halil
  • 35
  • 7

2 Answers2

1

In the place of for loop of your code replace this one.

highest_IV, highest_idx = 0, None
for idx, option in enumerate(search_option):
    if option['implied_volatility'] and highest_IV < float(option['implied_volatility']):
        highest_IV = float(option['implied_volatility'])
        highest_idx = idx
if highest_idx is not None:
    print("Strike Price: {strike_price}, Ask: {ask_price}, Bid: {bid_price}, Delta: {delta}, IV: {implied_volatility}".format(**search_option[highest_idx]))

Here, you may need to consider the case that search_option is empty.

I hope this would help you.

Perfect
  • 1,616
  • 1
  • 19
  • 27
  • I got this error: if highest['implied_volatility'] < option['implied_volatility']: TypeError: '<' not supported between instances of 'str' and 'NoneType' – Kam Halil Jun 17 '20 at 18:05
  • 1
    Is there any case that `implied_volatility` can be `None`? – Perfect Jun 17 '20 at 18:06
  • 1
    Ah yes! I completely forgot about the fact that I also wanted to exclude the Nones! My apologies on that one – Kam Halil Jun 17 '20 at 18:08
  • I updated the code but not sure if this has any syntax or bug case. But would you give it a shot and let me know. – Perfect Jun 17 '20 at 18:12
  • So I fixed the syntax and I thought it would all work until it spat this out: print("Strike Price: {strike_price}, Ask: {ask_price}, Bid: {bid_price}, Delta: {delta}, IV: {implied_volatility}".format(search_option[highest_idx])) KeyError: 'strike_price' – Kam Halil Jun 17 '20 at 18:21
  • But I double checked and 'strike_price' is the correct key in the dictionary. What the hell! – Kam Halil Jun 17 '20 at 18:22
  • Ah, just a moment, my code had a small bug. Let me fix it shortly. – Perfect Jun 17 '20 at 18:22
  • Corrected. Please try again. – Perfect Jun 17 '20 at 18:23
  • Oh wait it gives me a full list of these options now haha with high IV I guess! Strike Price: 28.5000, Ask: 4.450000, Bid: 4.300000, Delta: 0.987135, IV: 0.876913 Strike Price: 42.0000, Ask: 0.030000, Bid: 0.000000, Delta: 0.009185, IV: 1.374517 Strike Price: 43.0000, Ask: 0.030000, Bid: 0.000000, Delta: 0.008567, IV: 1.487969 Strike Price: 50.0000, Ask: 0.010000, Bid: 0.000000, Delta: 0.006106, IV: 2.190367 Strike Price: 8.0000, Ask: 24.950000, Bid: 24.800000, Delta: 0.998303, IV: 7.164647 – Kam Halil Jun 17 '20 at 18:27
  • I've literally been trying to fix this myself for the past 6 hours so I'm still super grateful for your code haha! – Kam Halil Jun 17 '20 at 18:28
  • I guess you might have written the last if statement within the `for` loop? – Perfect Jun 17 '20 at 18:29
  • It works fully now! You have no idea how much time and sweat you've saved me from, buddy :D! Super vote! – Kam Halil Jun 17 '20 at 18:36
  • Happy to hear that. – Perfect Jun 17 '20 at 18:40
0

You can return the option with the highest IV doing something like this:

def find_highest_iv(search_option):
    max_iv = max([option['implied_volatility'] for option in search_option ])
    for option in search_option:
        for k,v in option.items():
            if k == 'implied_volatility' and v == max_iv:
                return option

If there are two options with the same IV this will return the first one found in search_options. There's probably a more pythonic shorthand way to do this, but this should work.

ncasale
  • 769
  • 6
  • 10