How to make dictionary to output by max value only (implied_volatility)?

51 views Asked by At

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
2

There are 2 answers

13
Perfect On BEST ANSWER

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.

0
ncasale On

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.