0
votes

I am trying to extract the current value of the 200-day moving average from Yahoo Finance (so far, I have been parsing finviz.com, Yahoo seems to be faster because of the JSON output).

For example, the 200 day MA can be found at https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL -> Trading information -> Stock price history -> 200-day moving average

This solution is very helpful: Using BeautifulSoup to Search Through Yahoo Finance

The request URL looks like this: https://query2.finance.yahoo.com/v10/finance/quoteSummary/AAPL?formatted=true&crumb=8ldhetOu7RJ&lang=en-US&region=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com'

I tried different modules (found a list of the modules here: Yahoo Finance URL not working)

However, I can't find the 200 day MA in any of the modules.

I'd kindly appreciate any hints or suggestions!

2

2 Answers

1
votes

I think I figured it out! There's another module on Yahoo finance that returns the information I was looking for: summaryDetail

It's really simple:

from bs4 import BeautifulSoup
import requests

r = requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/GLW?formatted=true&crumb=8ldhetOu7RJ&lang=en-US&region=US&modules=summaryDetail&corsDomain=finance.yahoo.com')
data = r.json()
financial_data=data['quoteSummary']['result'][0]['summaryDetail']
twoHundredMA_dict = financial_data['twoHundredDayAverage']
print(twoHundredMA_dict['fmt'])

In this case, using JSON instead of lxml is way faster!

0
votes

Note that the 200-day moving average on Yahoo's Key Statistics page is based on 200 calendar days and not the traditional definition of 200 trading days.