Pumpernickel
Pumpernickel

Reputation: 47

Creating dict from item in a list

I'm using the robinhood.options.get_option_market_data API from the robin_stocks library to read information about stock options into my Python 3 program.

The documentation for this API indicates it returns a dictionary of key/value pairs

robin_stocks.robinhood.options.get_option_market_data_by_id(id,info=None)

Returns the option market data for a stock, including the greeks, open interest, change of profit, and adjusted mark price.Parameters•id(str) – The id of the stock.•info(Optional[str]) – Will filter the results to get a specific value. Returns a dictionary of key/value pairs for the stock. If info parameter is provided, the value of the key that matches info is extracted. Complete Docs - https://readthedocs.org/projects/robin-stocks/downloads/pdf/latest/

I use the following code to retrieve information about this particular option.

price = rs.robinhood.options.get_option_market_data(ticker,'2021-06-11','260','call',info=None)
print(price[0])

Printing out this data will return:

[{'adjusted_mark_price': '0.400000', 'ask_price': '0.430000', 'ask_size': 49, 'bid_price': '0.370000', 'bid_size': 37, 'break_even_price': '260.400000', 'high_price': '0.710000', 'instrument': 'https://api.robinhood.com/options/instruments/70b3f0a9-3747-4cde-817e-07850f2d1a16/', 'instrument_id': '70b3f0a9-3747-4cde-817e-07850f2d1a16', 'last_trade_price': '0.390000', 'last_trade_size': 1, 'low_price': '0.380000', 'mark_price': '0.400000', 'open_interest': 2409, 'previous_close_date': '2021-05-27', 'previous_close_price': '0.470000', 'volume': 747, 'symbol': 'MSFT', 'occ_symbol': 'MSFT  210611C00260000', 'chance_of_profit_long': '0.092005', 'chance_of_profit_short': '0.907995', 'delta': '0.105930', 'gamma': '0.022910', 'implied_volatility': '0.177756', 'rho': '0.008438', 'theta': '-0.061925', 'vega': '0.082234', 'high_fill_rate_buy_price': '0.420000', 'high_fill_rate_sell_price': '0.370000', 'low_fill_rate_buy_price': '0.390000', 'low_fill_rate_sell_price': '0.400000'}]

However, this code seems to be returning this data in the form of a string that is inside the first entry of a list, not a dictionary as the docs suggest. Since it is not a dictionary, I'm not able to issue a .get() to retrieve the key/value pair I'm interested in. Any attempt to get a key/value pair in this way results in the error "Attribute Error: 'list' object has no attribute 'get'"

I'm pretty new to using Python. What is the best way of converting this string to a dictionary of key/value pairs? I tried defining price as price = dict() before calling the robin_stocks API, but that did not seem to do the trick. Changing the API to use info='ask_price' or any other field also results in an error message - "Error: The keyword "ask_price" is not a key in the dictionary."

Any help would be greatly appreciated!

Upvotes: 0

Views: 169

Answers (1)

Joe Thor
Joe Thor

Reputation: 1260

It is returning a list of a list with a dictionary in it, albeit it is a list of a list with only one value and then one value, at least from the sample you provided in the comments.


price= [[{'adjusted_mark_price': '0.400000', 'ask_price': '0.430000', 'ask_size': 49, 'bid_price': '0.370000', 'bid_size': 37, 'break_even_price': '260.400000', 'high_price': '0.710000', 'instrument': 'https://api.robinhood.com/options/instruments/70b3f0a9-3747-4cde-817e-07850f2d1a16/', 'instrument_id': '70b3f0a9-3747-4cde-817e-07850f2d1a16', 'last_trade_price': '0.390000', 'last_trade_size': 1, 'low_price': '0.380000', 'mark_price': '0.400000', 'open_interest': 2409, 'previous_close_date': '2021-05-27', 'previous_close_price': '0.470000', 'volume': 747, 'symbol': 'MSFT', 'occ_symbol': 'MSFT  210611C00260000', 'chance_of_profit_long': '0.092005', 'chance_of_profit_short': '0.907995', 'delta': '0.105930', 'gamma': '0.022910', 'implied_volatility': '0.177756', 'rho': '0.008438', 'theta': '-0.061925', 'vega': '0.082234', 'high_fill_rate_buy_price': '0.420000', 'high_fill_rate_sell_price': '0.370000', 'low_fill_rate_buy_price': '0.390000', 'low_fill_rate_sell_price': '0.400000'}]]

So ask_price can be accessed by referencing the index value of the list and then the key value of the dictionary

In this case

price[0][0]['ask_price']

if that is printed

print(price[0][0]['ask_price'])

it will display

49

Upvotes: 1

Related Questions