pikachelos
pikachelos

Reputation: 23

Python API to access Stock Market information

I would like to know if there is a place from where I can download metadata of a given stock. I was studying sometime back about REST API and I though I could maybe use something like this:

stock_code = "GME" 
base_url = "https://somestockmarkekpage.com/api/stock?code={}" 
resp = requests.get(base_url.format(stock_code))
print(resp.json()['short_ratio'])

The problem is I dont know any base_url from where I can download this data, dont even know if it exist for free. However any other API or service you could provide is very welcome

Upvotes: 2

Views: 2463

Answers (2)

Pranbir Sarkar
Pranbir Sarkar

Reputation: 131

You can use the free Yahoo Finance API and their most popular Python library yfinance. Link: https://pypi.org/project/yfinance/

Sample Code:

import yfinance as yf

GME_data = yf.Ticker("GME")

# get stock info
GME_data.info

Other than that you can also use many other API. You can search in RapidAPI and search "Stock".

Upvotes: 1

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

There is a free API provided by Yahoo that contains up to date data related with several tickets. You can see the API details here. One example to extract metadata from a ticket would be:

import yfinance as yf
stock_obj = yf.Ticker("GME")
# Here are some fixs on the JSON it returns
validated = str(stock_obj.info).replace("'","\"").replace("None", "\"NULL\"").replace("False", "\"FALSE\"").replace("True", "\"TRUE\"")
# Parsing the JSON here
meta_obj = json.loads(validated)

# Some of the short fields
print("sharesShort: "+str( meta_obj['sharesShort']))
print("shortRatio: "+str( meta_obj['shortRatio']))
print("shortPercentOfFloat: "+str( meta_obj['shortPercentOfFloat']))

The output for the ticket you are interested in would be:

sharesShort: 61782730
shortRatio: 2.81 
shortPercentOfFloat: 2.2642

Upvotes: 5

Related Questions