Skip to content

Instantly share code, notes, and snippets.

@ranaroussi
Last active November 4, 2024 07:32
Show Gist options
  • Save ranaroussi/36d66652111bc6f34f41 to your computer and use it in GitHub Desktop.
Save ranaroussi/36d66652111bc6f34f41 to your computer and use it in GitHub Desktop.
Retrieve Pre-Market data from Google Finance (python 3.x)
"""
Retrieve Pre-Market data from Google Finance
Ported to Python 3.x
Original code by Dr. Pawel Lachowicz
http://www.quantatrisk.com/2015/05/07/hacking-google-finance-in-pre-market-trading-python/
"""
import requests
import json
from time import sleep
def fetchPreMarket(symbol, exchange):
link = "http://finance.google.com/finance/info?client=ig&q="
url = link + "%s:%s" % (exchange, symbol)
content = requests.get(url).text
info = json.loads(content[3:])[0]
ts = str(info["elt"]) # time stamp
last = float(info["l"]) # close price (previous trading day)
pre = float(info["el"]) # stock price in pre-market (after-hours)
return (ts, last, pre)
if __name__ == '__main__':
prev = 0
while True:
ts, last, pre = fetchPreMarket("AAPL", "NASDAQ")
if (pre != prev):
prev = pre
print(("%s\t%.2f\t%.2f\t%+.2f\t%+.2f%%" %
(ts, last, pre, pre - last, (pre / last - 1) * 100.)))
sleep(60)
@marcusademola
Copy link

this is great idea, any ideas how to fetch pre-market data on google sheets?

@mabdurrafey1
Copy link

this is great idea, any ideas how to fetch pre-market data on google sheets?

@leoprz606
Copy link

We'd need this in the form of javascript so that I can be used in Google Sheet's App Scripts.

@frankmedia
Copy link

not working for me.

JSONDecodeError Traceback (most recent call last)
in
17 prev = 0
18 while True:
---> 19 ts, last, pre = fetchPreMarket("AAPL", "NASDAQ")
20 if (pre != prev):
21 prev = pre

in fetchPreMarket(symbol, exchange)
8 url = link + "%s:%s" % (exchange, symbol)
9 content = requests.get(url).text
---> 10 info = json.loads(content[3:])[0]
11 ts = str(info["elt"]) # time stamp
12 last = float(info["l"]) # close price (previous trading day)

~/opt/anaconda3/lib/python3.8/json/init.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None and not kw):
--> 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder

~/opt/anaconda3/lib/python3.8/json/decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):

~/opt/anaconda3/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@richard-zhang-tfs
Copy link

Not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment