0

So I have this code which searches for an option with the highest volatility.

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 = 'F'
expirationDate = '2020-06-19'


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

#identify option with highest implied volatility

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("Symbol: {chain_symbol}, Strike Price: {strike_price}, Ask: {ask_price}, Bid: {bid_price}, Delta: {delta}, IV: {implied_volatility}".format(**search_option[highest_idx]))

It then prints out the following result: Symbol: 'F', Strike Price: 1.5000, Ask: 4.900000, Bid: 4.800000, Delta: 0.990127, IV: 9.900301

My question is, how can I save this output into separate variables? Note, the variables for 'strike_price', 'ask_price', 'bid_price', 'delta' and 'implied_volatility' have to be integers, but 'symbol', has to be a string.

This is the result that I need:

symbol_for_order = F
strike_price_for_order = 1.5000
ask_price_for_order = 4.900000
....
Kam Halil
  • 35
  • 7
  • I dont think that is a dictionary. I can't be certain as i have never used that library. But by the looks of it that is just a standard format print statement like. x = 'hello' print('{0} there'.format(x)) – Ben Jun 17 '20 at 22:23
  • It looks like it already _is_ separate variables, which you're calling in the f string. The variables are `chain_symbol, strike_price, ask_price`... from `**search_option[highest_idx]` – G. Anderson Jun 17 '20 at 22:50
  • Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – G. Anderson Jun 17 '20 at 22:50
  • @G.Anderson that's what I thought do, but when I try to turn them into variables by doing for example: strike_price_for_order = option.get('strike_price'), it spits out a range of different 'option trades' with strike, ask price etc. criteria, and not a single narrowed down option trade like the one I mentioned in the code. – Kam Halil Jun 17 '20 at 23:45
  • Unfortunately I haven't used this package personally, but here's the breakdown as I understand it. At the point you're using the fstring, the loop has set a value for `highest_idx`, then you're using that value to index into `search_option`. Then, you are using the `**` unpacking on the result of that, and getting the named variables. – G. Anderson Jun 18 '20 at 15:27
  • For better help, please provide a [mcve] in the body of your question, including a sample of `search_option`, `option`, and what `highest_IV ` and `highest_idx` are at the time your code produces the given output – G. Anderson Jun 18 '20 at 15:29

0 Answers0