0

So I have a list of dicts with different strike, bid prices and implied_volatility for 4 different potential option trades.

search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' }, 
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None}, 
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]

And here, the code searches for the option with the highest implied_volatility and gives me an output.

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}, Bid: {bid_price}, IV: {implied_volatility}".format(**search_option[highest_idx]))

I then save them separately as variables in order to initiate trades.

order_strike_price = search_option[highest_idx].get('strike_price')
order_bid_price = search_option[highest_idx].get('bid_price')
....
....

I don't really need the 'highest implied volatility' code any more.

My task now is, how can I search for an option with a strike price > 3 and a bid price < 0.25?

I then need to save all of the keys of the matching dictionary (strike, bid, implied_volatility) as separate variables like I did with the ones above.

Kam Halil
  • 35
  • 7

1 Answers1

1

Try this:

search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' }, 
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None}, 
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]

sort = [i for i in search_option if float(i["strike_price"]) > 3 and float(i["bid_price"]) < 0.25]

print(sort)

Output:

[{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'}]
Max
  • 126
  • 5
  • Question, can you please show how to save each of the keys as separate variables? – Kam Halil Jun 22 '20 at 11:17
  • Something like this? `x = {'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'}` `strike = x["strike_price"]` `bid= x["bid_price"]` `vol= x["implied_volatility"]` – Max Jun 22 '20 at 11:21
  • Yes precisely. But any chance I can save them as floats instead of strings? – Kam Halil Jun 22 '20 at 11:24
  • Yes. You can use the `float()` function to achieve this. Instead of `strike = x["strike_price"]` you could use `strike = float(x["strike_price"])`. Here is a useful link to explain [Python Casting](https://www.w3schools.com/python/python_casting.asp) – Max Jun 22 '20 at 11:27