1

I am trying to get the bid and ask price for an options contract using the yahoo finance API, however, the JSON output is difficult to navigate.

import requests
url = requests.get("https://query1.finance.yahoo.com/v7/finance/options/AAPL").json()
url

There a multiple contractSymbol such as:

'contractSymbol': 'AAPL190503C00150000'
'contractSymbol': 'AAPL190503C00155000'

and it returns this associated data for each contractSymbol.

{'contractSymbol': 'AAPL190503C00150000',
        'strike': 150.0,
        'currency': 'USD',
        'lastPrice': 54.31,
        'change': -1.579998,
        'percentChange': -2.826978,
        'volume': 105,
        'openInterest': 35,
        'bid': 52.25,
        'ask': 54.85,
        'contractSize': 'REGULAR',
        'expiration': 1556841600,
        'lastTradeDate': 1556306875,
        'impliedVolatility': 1.4033232958984376,
        'inTheMoney': True}

I am only trying to recover the 'bid and 'ask' for each contractSymbol, however, it seems to be nested very far into the json and I am having a lot of trouble.

2 Answers 2

1

You can take apart the dict that results from parsing the JSON and store the information of interest in a list. The following is an example:

import requests

r = requests.get("https://query1.finance.yahoo.com/v7/finance/options/AAPL")
data = r.json()

# "option_chain" is a list holding everything.
option_chain = data['optionChain']

# "result" is a dictionary, the first item in "option_chain".
result = option_chain['result'][0]

# These are the components of the "result" dictionary.
underlyingSymbol = result['underlyingSymbol']
expirationDates = result['expirationDates']
strikes = result['strikes']
hasMiniOptions = result['hasMiniOptions']
quote = result['quote']
options = result['options'][0]

# This is the list of dictionaries that interest you.
calls = options['calls']

con_symbs = []
bids = []
asks = []

for call_dict in calls:
    contract_symbol = call_dict['contractSymbol']
    bid = call_dict['bid']
    ask = call_dict['ask']

    con_symbs.append(contract_symbol)
    bids.append(bid)
    asks.append(ask)

for (cs,b,a) in zip(con_symbs,bids,asks):
    print('[INFO] {0}: bid: {1} ask: {2}'.format(cs,b,a))

This will print, for your reference, the following:

[INFO] AAPL190503C00150000: bid: 52.25 ask: 54.85
[INFO] AAPL190503C00155000: bid: 47.25 ask: 51.25
[INFO] AAPL190503C00160000: bid: 44.05 ask: 44.9
[INFO] AAPL190503C00165000: bid: 37.25 ask: 41.5
[INFO] AAPL190503C00167500: bid: 34.8 ask: 39.0
[INFO] AAPL190503C00170000: bid: 33.6 ask: 34.9
[INFO] AAPL190503C00172500: bid: 29.95 ask: 34.05
[INFO] AAPL190503C00175000: bid: 29.3 ask: 29.95
[INFO] AAPL190503C00177500: bid: 25.0 ask: 28.85
[INFO] AAPL190503C00180000: bid: 24.2 ask: 25.05
[INFO] AAPL190503C00182500: bid: 22.0 ask: 22.4
[INFO] AAPL190503C00185000: bid: 19.6 ask: 19.85
[INFO] AAPL190503C00187500: bid: 17.2 ask: 17.5
[INFO] AAPL190503C00190000: bid: 14.95 ask: 15.2
[INFO] AAPL190503C00192500: bid: 12.75 ask: 13.05
[INFO] AAPL190503C00195000: bid: 10.75 ask: 11.0
[INFO] AAPL190503C00197500: bid: 8.9 ask: 9.05
[INFO] AAPL190503C00200000: bid: 7.2 ask: 7.35
[INFO] AAPL190503C00202500: bid: 5.7 ask: 5.85
[INFO] AAPL190503C00205000: bid: 4.35 ask: 4.45
[INFO] AAPL190503C00207500: bid: 3.2 ask: 3.35
[INFO] AAPL190503C00210000: bid: 2.25 ask: 2.3
[INFO] AAPL190503C00212500: bid: 1.49 ask: 1.54
[INFO] AAPL190503C00215000: bid: 0.94 ask: 0.99
[INFO] AAPL190503C00217500: bid: 0.59 ask: 0.62
[INFO] AAPL190503C00220000: bid: 0.35 ask: 0.39
[INFO] AAPL190503C00222500: bid: 0.23 ask: 0.25
[INFO] AAPL190503C00225000: bid: 0.14 ask: 0.16
[INFO] AAPL190503C00230000: bid: 0.06 ask: 0.07
[INFO] AAPL190503C00235000: bid: 0.03 ask: 0.05
[INFO] AAPL190503C00240000: bid: 0.02 ask: 0.04
Sign up to request clarification or add additional context in comments.

Comments

0
import requests
data = requests.get("https://query1.finance.yahoo.com/v7/finance/options/AAPL").json()
calls = data["optionChain"]["result"][0]["options"][0]["calls"]
for el in calls:
    print(el["contractSymbol"], el["bid"], el["ask"])

And I think @Dodge's answer is confusing for new python users

4 Comments

Thanks for another method of getting the results I needed! Makes a lot of sense now!
This now returns all the calls. What would be the next step to just get bid and ask for 1 contractSymbol for example 'contractSymbol': 'AAPL190503C00230000'
@ 饶伟健 Our answers are fundamentally the same. I don't understand why you think data["optionChain"]["result"][0]["options"][0]["calls"] is easy for a new user to understand. Zen of Python: Explicit is better than implicit.
Because there are too many intermediate variables. In my personal opinion, data["optionChain"]["result"][0]["options"][0]["calls"] tells user how to get deep data in json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.