Build a Program to Retrieve and Graph Live Stock Market Data

Source: Python: Build a Program to Retrieve and Graph Live Stock Market Data | by Jordan Steinberg | Medium

Jordan Steinberg
Nov 25, 2021

In this article I am going to utilize the new yahoo finance API to write a program that will retrieve live stock market data and display it using Python and Plotly — All for free!

In this article I will show you how to get live stock market data, for any stock of your choice, without calling expensive APIs or needing to pay for access. To do this, I will be using popular python packages that can all be installed using PIP.

Source(s): How to Get Live Market Data (Less Than 0.1-Second Lag):

Before We Get Started:

To get the results seen in this article, you will need to have installed on your device the following packages:

Yfinance
Pandas
NumPy
Plotly (Can use alternative, but useful for plotting results)
If any of these packages are not already installed you can use PIP commands to install them in your command window as follows:

pip install yfinance 
pip install pandas

If you are new to python, you might not be familiar with pip installer. Here, I have linked a guide to installing pip on windows.

Once these packages are installed, we can begin creating our program.

I. Import Packages

The first step is to import the aforementioned packages.

You can import these packages using the following lines of code:

# Raw Package 
import numpy as np 
import pandas as pd
from pandas_datareader import data as pdr 

# Market Data 
import yfinance as yf

# Graphing / Visualization 
import plotly.graph_objs as go 

Full Code:

# Raw Package
import numpy as np
import pandas as pd
from pandas_datareader import data as pdr

# Market Data 
import yfinance as yf

#Graphing/Visualization
import datetime as dt 
import plotly.graph_objs as go 

# Override Yahoo Finance 
yf.pdr_override()

# Create input field for our desired stock 
stock=input("Enter a stock ticker symbol: ")

# Retrieve stock data frame (df) from yfinance API at an interval of 1m 
df = yf.download(tickers=stock,period='1d',interval='1m')

print(df)

# Declare plotly figure (go)
fig=go.Figure()

fig.add_trace(go.Candlestick(x=df.index,
                open=df['Open'],
                high=df['High'],
                low=df['Low'],
                close=df['Close'], name = 'market data'))

fig.update_layout(
    title= str(stock)+' Live Share Price:',
    yaxis_title='Stock Price (USD per Shares)')               

fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=15, label="15m", step="minute", stepmode="backward"),
            dict(count=45, label="45m", step="minute", stepmode="backward"),
            dict(count=1, label="HTD", step="hour", stepmode="todate"),
            dict(count=3, label="3h", step="hour", stepmode="backward"),
            dict(step="all")
        ])
    )
)

fig.show()

Python: Build a Program to Retrieve and Graph Live Stock Market Data

In this article I am going to utilize the new yahoo finance API to write a program that will retrieve live stock market data and display it using Python and Plotly — All for free!

Photo by Nicholas Cappello on Unsplash

Python: How to Get Live Market Data (Less Than 0.1-Second Lag).

This article is going to be a bit special. I am going to test the latest release from Yahoo Finance API for Python…

towardsdatascience.com

  • Yfinance
  • Pandas
  • NumPy
  • Plotly (Can use alternative, but useful for plotting results)
pip install yfinance 
pip install pandas
Imports the necessary packages we will need to retrieve and plot live market data
  • First, we will need to override yahoo finance api
yf.pdr_override() 
  • Next, we will need to create an input field for us to input our desired stock symbol. By setting variable ‘stock’ equal to the input, we can utilize the stock variable to be the specified symbol. This will allow us to query yahoo finance api for our desired data.
stock=input("Enter a stock symbol: ")
print(stock)
  • Running the above lines of code will ensure that python recognizes the stock variable and the output should be whatever ticker symbol you put in.
The first line will prompt you to enter a stock symbol. The print statement verifies that the variable ‘stock’ has been registered.
  1. Ticker
  • To call your data you will need to use the following syntax
yf.download(tickers=argument1, period=argument2, Interval=argument3)
  • The output is as follows with the NVDA example:
Since this was ran on 11/24/2021 The Yahoo Finance API Generates the following data in accordance with our commands.
  • If you are saving your file, you can run it in a command window using the following:
python YOUR_FILE_NAME.py
  • When running our program and entering symbol ‘AMZN’ the following returns:
  • There are numerous ways to visualize this data and I highly suggest you play around with what’s available to get your optimal result.
  • The output:
Plotly provides a slider and several view points which allows us to view the data on a termed basis.

Python: How to Get Live Market Data (Less Than 0.1-Second Lag).

This article is going to be a bit special. I am going to test the latest release from Yahoo Finance API for Python…

towardsdatascience.com

Python
Finance
Plotly
Stock Market
Data Science

Leave a Reply

The maximum upload file size: 500 MB. You can upload: image, audio, video, document, spreadsheet, interactive, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here