Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exchange Quotes
#1
Quotes from iextrading.com:

#!/usr/bin/python3
import requests

tic = "ibm"
url = "https://api.iextrading.com/1.0/stock/" + tic + "/quote"

rsp = requests.get(url)
if rsp.status_code in (200,):
    ustr = rsp.text.strip()
    print(ustr)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
Output:
{"symbol":"IBM","companyName":"International Business Machines Corporation","primaryExchange":"New York Stock Exchange","sector":"Technology","calculationPrice":"close","open":135,"openTime":1558618233304,"close":132.39,"closeTime":1558641720610,"high":135.13,"low":130.44,"latestPrice":132.39,"latestSource":"Close","latestTime":"May 23, 2019","latestUpdate":1558641720610,"latestVolume":5236513,"iexRealtimePrice":132.41,"iexRealtimeSize":35,"iexLastUpdated":1558641594136,"delayedPrice":132.39,"delayedPriceTime":1558641720610,"extendedPrice":132.28,"extendedChange":-0.11,"extendedChangePercent":-0.00083,"extendedPriceTime":1558644319071,"previousClose":136.35,"change":-3.96,"changePercent":-0.02904,"iexMarketPercent":0.04697,"iexVolume":245959,"avgTotalVolume":3859335,"iexBidPrice":0,"iexBidSize":0,"iexAskPrice":0,"iexAskSize":0,"marketCap":117382649956,"peRatio":9.58,"week52High":154.36,"week52Low":105.94,"ytdChange":0.14727588066422426}
Reply
#2
Quotes from stooq.com:

#!/usr/bin/python3
import requests

tic = "ibm.us"
url = "https://stooq.com/q/l/?f=sd2t2ohlcv&h&e=csv&s=" + tic

rsp = requests.get(url)
if rsp.status_code in (200,):
    ustr = rsp.text.strip()
    print(ustr)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
Output:
Symbol,Date,Time,Open,High,Low,Close,Volume IBM.US,2019-05-23,22:02:00,135,135.13,130.44,132.39,5343437
Reply
#3
Quotes from alphavantage.co:

#!/usr/bin/python3
import requests
key = "xxxxxxxxx" # key is free (5 requests per minute, 500 requests per day)

tic = "ibm"
url = "https://www.alphavantage.co/query?apikey="+key+"&function=TIME_SERIES_DAILY&symbol="+ tic

rsp = requests.get(url)
if rsp.status_code in (200,):
    ustr = rsp.text.strip()
    print(ustr)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
Output:
{ "Meta Data": { "1. Information": "Daily Prices (open, high, low, close) and Volumes", "2. Symbol": "ibm", "3. Last Refreshed": "2019-05-23", "4. Output Size": "Compact", "5. Time Zone": "US/Eastern" }, "Time Series (Daily)": { "2019-05-23": { "1. open": "135.1300", "2. high": "135.1300", "3. low": "130.4400", "4. close": "132.3900", "5. volume": "5342570" }, "2019-05-22": { "1. open": "136.0000", "2. high": "136.7500", "3. low": "135.7116", "4. close": "136.3500", "5. volume": "1849821" }, "2019-05-21": { "1. open": "136.1200", "2. high": "137.0600", "3. low": "135.7900", "4. close": "136.4500", "5. volume": "2503504" } } }
Reply
#4
Quotes from quandl.com:

#!/usr/bin/python3
import requests
key = "xxxxxxxxx" # key is free
 
tic = "euronext/ibma"
url = "https://www.quandl.com/api/v3/datasets/"+tic+"?rows=1&api_key="+key

rsp = requests.get(url)
if rsp.status_code in (200,):
    ustr = rsp.text.strip()
    print(ustr)
else:
    print("ERROR:", rsp.status_code)
    print(rsp.text)
Output:
{"dataset":{"id":9819986,"dataset_code":"IBMA","database_code":"EURONEXT","name":"Ibm (IBMA)","description":"Stock Prices for Ibm from the Euronext Stock Exchange. Currency: USD. Market: Traded not listed Brussels","refreshed_at":"2019-05-24T17:15:31.540Z","newest_available_date":"2019-02-28","oldest_available_date":"2014-01-22","column_names":["Date","Open","High","Low","Last","Volume","Turnover"],"frequency":"daily","type":"Time Series","premium":false,"limit":1,"transform":null,"column_index":null,"start_date":"2014-01-22","end_date":"2019-02-28","data":[["2019-02-28",141.0,141.0,141.0,141.0,20.0,2470.22]],"collapse":null,"order":null,"database_id":5628}}
Reply
#5
It looks like the data you are receiving is in json form. Using import json gives you access to a number of useful features, including several methods of pretty-printing them to console.
Reply
#6
We are in forum "Scripts/Snippets".

If you know where to get free quotes, let me know,
because it gets more and more difficult
to get free quotes since Yahoo API or Google API have been down.
Reply
#7
(May-26-2019, 07:58 AM)OstermanA Wrote: It looks like the data you are receiving is in json form. Using import json gives you access to a number of useful features, including several methods of pretty-printing them to console.
There is no need to use import json as Requests that @heiner55 use has a build in json parser/encoder.

@heiner55 could you a little description about code,don't need to long like not all now what Requests is.
I can also add a example of using the data which is json,i know if just need Quotes text output then it's okay.
But often when using these API want to do stuff with the json data.
import requests
from pprint import pprint

key = "xxxxxxx" # key is free (5 requests per minute, 500 requests per day)
tic = "ibm"
url = f"https://www.alphavantage.co/query?apikey={key}&function=TIME_SERIES_DAILY&symbol={tic}"

rsp = requests.get(url)
if rsp.status_code == 200:
    json_data = rsp.json()
    pprint(json_data)
else:
    print(f"ERROR: {rsp.status_code}")
    print(rsp.text)
Now the output the same,but it's json that can used.
# Meta Data index
>>> json_data['Meta Data']
{'1. Information': 'Daily Prices (open, high, low, close) and Volumes',
 '2. Symbol': 'ibm',
 '3. Last Refreshed': '2019-05-24',
 '4. Output Size': 'Compact',
 '5. Time Zone': 'US/Eastern'}

>>> # Get data for a single day
>>> json_data['Time Series (Daily)']['2019-01-02']
{'1. open': '112.0100',
 '2. high': '115.9800',
 '3. low': '111.6900',
 '4. close': '115.2100',
 '5. volume': '4239924'}

>>> # Get data for a single day
>>> json_data['Time Series (Daily)']['2019-05-22']
{'1. open': '136.0000',
 '2. high': '136.7500',
 '3. low': '135.7116',
 '4. close': '136.3500',
 '5. volume': '1849821'}
Reply
#8
Yes, you are right.
I was a little bit lazy.
My goal was only to show where you can get quotes
with a tiny code example.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020