-
-
Save willtejeda/3969b914f82cdbe44c676bb382d17e07 to your computer and use it in GitHub Desktop.
gets premarket prices from nasdaq
This file contains hidden or 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