Skip to content

Instantly share code, notes, and snippets.

@ranaroussi
Last active November 18, 2021 20:18
Show Gist options
  • Save ranaroussi/4010b0bd32d7f4712705177f455ab016 to your computer and use it in GitHub Desktop.
Save ranaroussi/4010b0bd32d7f4712705177f455ab016 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