Last active
November 18, 2021 20:18
-
-
Save ranaroussi/4010b0bd32d7f4712705177f455ab016 to your computer and use it in GitHub Desktop.
gets premarket prices from nasdaq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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