Open In App

Stock Price Analysis With Python

Last Updated : 23 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for making data-based analyses and visualizations. It also has a wide range of open-source libraries that can be used off the shelf for some great functionalities. Python Dash is a library that allows you to build web dashboards and data visualizations without the hassle of complex front-end HTML, CSS, or JavaScript. In this article, we will be learning to build a Stock data dashboard using Python Dash, Pandas, and Yahoo’s Finance API. 

Create Stock Visualisation Dashboard using Dash in Python

We will create the dashboard for stock listed on the New York Stock Exchange(NYSE). For making a dashboard we will need some Python libraries which do not come preinstalled with Python. we will use the pip command to install all these libraries one by one. 

Install Required Libraries For Visualization 

Install  Pandas DataReader

Pandas DataReader is used to download and read data from the internet 

pip install pandas_datareader

Install the latest version of Dash

Dash is used to create interactive dashboards for the data. We will see its implementation in our code 

pip install dash

Install Yahoo Finance 

Yahoo Finance provides a stock dataset for the required company. We can use the Yahoo library to directly import the data into DataFrame.

pip install yfinance

Python Code for Stock Data Visualization 

Import all the required libraries 

Python3




# importing required libraries
import datetime
import yfinance as yf
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output


Create a user interface using the dash library

We are going to make a simple yet functional user interface, one will be a simple Heading title and an input textbox for the user to type in the stock names. 

Python3




app = dash.Dash()
app.title = "Stock Visualisation"
 
app.layout = html.Div(children=[
    html.H1("Stock Visualisation Dashboard"),
    html.H4("Please enter the stock name"),
    dcc.Input(id='input', value='AAPL', type='text'),
    html.Div(id='output-graph')
])


The input text box is now just a static text box. To get the input data, which in this case is the stock name of a company, from the user interface, we should add app callbacks. The read stock name(input_data) is passed as a parameter to the method update_value. The function then gets all the stock data from the Yahoo Finance API from 1st January 2010 till now, the current day, and is stored in a Pandas data frame. A graph is plotted, with the X-axis being the index of the data frame, which is time in years, the Y-axis with the closing stock price of each day, and the name of the graph being the stock name(input_data). This graph is returned to the callback wrapper which then displays it on the user interface.

Code For Reading and Creating Dashboard 

Python3




# callback Decorator
@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='input', component_property='value')]
)
def update_graph(input_data):
    start = datetime.datetime(2010, 1, 1)
    end = datetime.datetime.now()
 
    try:
        df = web.DataReader(input_data, 'yahoo', start, end)
 
        graph = dcc.Graph(id ="example", figure ={
            'data':[{'x':df.index, 'y':df.Close, 'type':'line', 'name':input_data}],
            'layout':{
                'title':input_data
            }
        })
 
    except:
        graph = html.Div("Error retrieving stock data.")
 
    return graph


Finally, run the server. 

Python3




if __name__ == '__main__':
    app.run_server()


Execution

The web application will now run on the local host at 8050 by default.

127.0.0.1:8050

Example Graph 

Let’s consider an example. The stock name of Google is GOOGL. Let’s enter this data into the input text box. Below is the result.

Screenshot of Google’s Stock Data



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads