Python: How to Get Live Market Data (Less Than 0.1-Second Lag). | by Sajid Lhessani | Towards Data Science

Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.
In this article, you will learn how to get live stock market data, using Python packages without calling an expensive API such as Bloomberg. I have tested it for you. Does that work? Let’s see.
If you are keen to hack it yourselves now, you can get the full Python code at the end of this article
You will be able to get stock market data such as price, volume and fundamental data using Python packages. (With a lag lower than 0.01 seconds)
This article is a complement of the article below, where I am demonstrating how to get market data in less than 3 lines of code:
towardsdatascience.com
Ok, let’s start.
First of all, if you want to track my progress and before to start, you will need to have installed on your machine a Python 3 version and the following packages:
Pandas
NumPy
Yfinance
Plotly (Not mandatory, but useful for plotting)
If any of these packages are not already installed, you can use the pip command, as shown below.
pip install yfinancepip install plotly
Once you have ensured that the following packages are installed, we can start.
If you already have experience with Python, you can jump to the second step. The first step will consist of importing data.
The first step will consist of importing the necessary packages.
You will start by importing your packages previously installed by using the following lines of code:
Once we are set up, let’s pursue the next step.
Now that libraries are imported, we can now import markets data.
Now that the different packages needed have been uploaded. We are going to use UBER as an example to set up our import through Yahoo Finance API.
Yahoo Finance API will need 3 mandatory arguments in this order:
Tickers (1)
Start date + End date or Period (2)
Interval (3)
For our case, the ticker(argument 1) will be UBER. Furthermore, we are going to choose for this example the 5 last days period(argument 2) instead of defining a Start and End date. And we will set up an interval(argument 3) of 5 minutes. As a quick reminder, UBER’s ticker is UBER.
To call your data, you will have to use the following structure:
Before to go further, I am going to bring some details on the third argument (interval).
I want to give you a quick shot on the different interval you can set up using yahoo finance API.
Detailed below the full list of interval possibilities which can be required:
The interval will not interfere with the option to get live data.
Let’s continue.
At the time when I am executing the line below, we are the 23rd October 2020, and it is actually 5:13 PM in London which means that the market is open.
For your information: 5:13 PM UK time correspond to 1:13 PM New York time.
We have defined our 3 arguments, let’s execute the code below:
For a recap, the following line of code is calling Yahoo finance API and requesting to get data for the last 5 days, with an interval of 5 minutes.
And here is the output:
The price has been updated instantly; I compared the data with the current values, we matching.
Boom!
As you can see, you have an update released each second. For instance, if you look at the output below, the latest data provided are given for 1:13 PM and 50 seconds. If you re-run your kernel, the data will be updated again.
Mission accomplished.
I pushed the live test further, and you can watch the data being updated each second, it is impressive. Furthermore, you can get the full code explained in the explanation video below:
And here the final live graph which is spread using Python and Plotly:
Sajid Lhessani
If you want to go further on this topic, here is the second part of this article with a real algorithmic trading example:
medium.com
(1) Learn algorithmic trading in one day: