Skip to content

Instantly share code, notes, and snippets.

@willtejeda
Forked from ranaroussi/nasdaq_premarket.py
Created August 30, 2020 09:02
Show Gist options
  • Save willtejeda/3969b914f82cdbe44c676bb382d17e07 to your computer and use it in GitHub Desktop.
Save willtejeda/3969b914f82cdbe44c676bb382d17e07 to your computer and use it in GitHub Desktop.
gets premarket prices from nasdaq
import requests
def get_premarket(ticker):
"""
gets premarket prices from nasdaq
usage:
>> price, volume = get_premarket('spy')
"""
url = 'https://api.nasdaq.com/api/quote/%s/extended-trading?assetclass=etf&markettype=pre'
data = requests.get(url % ticker.upper()).json()['data']
if data['infoTable']['rows']:
volume = int(data['infoTable']['rows'][0]['volume'].replace(',', ''))
price = float(data['tradeDetailTable']['rows'][0]['price'].replace('$', '').replace(',', ''))
return price, volume
# no premarket trading, return last price with 0 volume
return float(data['previousInfo'].split('$')[1]), 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment