Free realtime stocks data with python(finnhub)
disclaimer : I am not an expert i am just getting you familiar with api available and showing basic code as few months before i did’nt knew what a websocket is
theres an api called Finnhub , Go ahead and sign up and get your api keys from dashboard
don’t worry you can’t use my keys i will change it as soon as this article is posted
once you have your api keys copy them and fire up your text editor IDE of your choice and i am assuming if your reading this you alredy have python and pip setuped
install 2 libraris ( use pip3 if your using linux or mac )
pip install websocket
pip install websocket-client
once you’ve installed this libraries import them
import websocket
write code to get started ! yay (its really basic but enough to get started with utilizing data)
in line 13 you can change the symbol acordingly for stocks data you wanna subscribe
if you run this file with (python3 if you have multiple python vesions installed)
python yourfilename.py
you’ll get a output like this :
note that if you get output type ping its because market is closed at the time maybe try cheaking day this confuses a lot of peoples includeing me
then you can easily use this data for whatever you want like this:
when you run this you’ll get much more cleaner output like this:
according to documentation at Finnhub attributes of a resposnse are:
Response Attributes:type
Message type.data
List of trades or price updates.s
Symbol.p
Last price.t
UNIX milliseconds timestamp.v
Volume.c
List of trade conditions. A comprehensive list of trade conditions code can be found here (redirects to google docs).
and yea according to docs if your using a free api key you can only
1 API key can only open 1 connection at a time.
do take a note about that.
Okay so basically thats it i hoped it helped you even a little you free to use this data now in your projetcs do let me know if you’ve liked what i am doing it kinda helps me do more of this with motivation
Thanks for reading :)