Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022

ow | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Published in DataDrivenInvestor

Edward Low Follow

Feb 26 · 9 min read

Photo by Nick Chong on Unsplash

Stock Market Analysis with Python, Plotly,


Dash, and PowerBI
Data Collecting: Python, Data Visualization: Plotly, Dash and PowerBI

I would like to contribute more valuable articles to this channel, your support is crucial
to this ecosystem. Please follow me and clap if you like my writing. Thank you.

Prologue

Stock Market Analysis has always been a popular topic all the time. Stock markets are
always volatile stock prices are frustrated all the time To Data professionals keeping a
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 1/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
always volatile, stock prices are frustrated all the time. To Data professionals, keeping a
track of such changes and trends can be tedious. In this post,Open
I willinperform
app a stock
Get started

market analysis and use data visualization to show the market trends with 3
visualization methods such as Plotly, Dash, and PowerBI. I will explain more about
PowerBI since not all data scientists are able to use Python for doing their data
analysis.

Introudction
The stock market datasets are changing every day, therefore we need to have the latest
or real-time to support data analysis. Of course, in the market, there are a lot of APIs
provided to extract the dataset. Some of the APIs may incur some costs due to
providing the real-time data on time. In this post, I will use Python third-party module
to extract the data from Yahoo-Finance. Pandas will be used for data processing, while
PowerBI use for data visualization.

In this article, I will guide how to use Python In Power Bi to build Stock Market
Analysis and data visualization in a faster way. Moreover, I will show the differences
between using Python Plotly&Dash and PowerBI. I will separate it into 3 Parts, Data
preparation, Data Processing, and Data visualization.

Part 1: Data Preparation


First of all, we need to import data into PBI. There are 2 ways to open up the python
script.

1. Click Get Data -> More -> Other -> Python Script

Figure 1.0 Power BI Get Data (source: Desktop app)

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 2/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Figure 1.1 Power BI Python Script (source: Desktop app)

2. From Visualization Tab and choose Py Icon

Figure 1.2 Power BI Python Script (source: Desktop app)

It will pop up a box, so we just write our python script here to extract the data from API
and import it to Power BI.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 3/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Figure 1.2 Power BI Python Script Box (source: Desktop app)

Remember to check your Python installation Path and installed the necessary libraries.

Necessary Modules:

pip install pandas_datareader pandas

Background:
For our analysis, we will use pandas_datareader to import stock market data from
Yahoo Finance. We will import the top 10 popular companies such as Apple, Microsoft,
Alphabet (Google), Amazon, Tesla, TSMC, Meta (Facebook), NVIDIA, Tencent, and
Samsung.

Normally analyzing the stock market, we need 5 years of historical data to analyze the
stock patterns. First, we will search all stock tickers for the above companies on Yahoo.
A stock ticker is a unique stock symbol with a series of letters assigned to a particular
stock for trading purposes.

Code Snippets: (For PowerBI)

1 import pandas_datareader as data


2 import pandas as pd
3 from datetime import datetime
4 import requests
5 from bs4 import BeautifulSoup
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 4/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
5 from bs4 import BeautifulSoup
6 from dateutil.relativedelta import relativedelta Get started
Open in app
7
8 html = requests.get('https://companiesmarketcap.com/tech/largest-tech-companies-by-market-cap/'
9 soup = BeautifulSoup(html, 'html.parser')
10 companyName = [x.get_text(strip=True) for x in soup.find_all('div', {'class': 'company-name'})]
11 companyCode = [x.get_text(strip=True) for x in soup.find_all('div', {'class': 'company-code'})]
12
13 now = datetime.now()
14 data_source = 'yahoo'
15 start_date = now - relativedelta(years=5)
16 end_date = now
17 dataArray = []
18 for x, y in zip(companyName,companyCode):
19 d = data.DataReader(y, data_source, start_date, end_date)
20 d['stockName'] = x
21 d['stockCode'] = y
22 dataArray.append(d)
23 df = pd.concat(dataArray)
24 df.reset_index(inplace=True)
25 print(df)

Code Explanation:

1. Importing the necessary libraries such as pandas_datareader, pandas, datetime,


requests, BeautifulSoup, and relativedelta. (line 1–6)

2. Extract the top 10 tech companies from companiesmarketcap.com by using


requests library (line 8)

3. Parse HTML text to the beautifulsoup element (line 9)

4. Inspect the web element and paste the DOM class attribute value. (line 10–11)

5. Extract the company name and company code and store them into an array list,
slice to a maximum of 10 companies. (line 10–11)

6. Defining the start_time (relativedelta to get 5 years ago datatime) and end_date.
(line 13–16)

7. Passing tickers, data_source, start_date, and end_date to extract the data from
yahoo finance. (line 19)

8. Concatenate 10 dataframe into one dataframe (line 23)

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 5/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Part 2: Data Visualization Open in app Get started

Next, we will visualize stock movements in the last 5 years among 10 companies by
using graphic charts to understand the stock movements. In this part, I will show 2
methods, 1 is using Python Plotly and Dash, 2 is using PowerBI to do visualization. Let
us see what is the differences.

Let’s start with Python Plotly first.

Apple Stock Market Trend (Close Price)

Code Snippets:

df_clone = df.copy()

df_clone.set_index('Date')

fig =px.line(df_clone[df_clone['stockCode']=='AAPL']['Close'])

fig.update_yaxes(title_text='')

fig.update_traces(line_color='#FF0000')

fig.show()

Note: According to the chart, we can notice that Apple Stock has been incredibly
increasing starting from 2020
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 6/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
increasing starting from 2020.
Open in app Get started
Dash
Dash is a python framework created by Plotly for creating interactive web
applications.

Apple Stock Market (Dash)

Code Snippets:

import dash

import dash_html_components as html

import dash_core_components as dcc

import plotly.graph_objects as go

app = dash.Dash()

def stock_prices():

fig = go.Figure([go.Scatter(x = df[df['stockCode']=='AAPL']


['Date'], y = df[df['stockCode']=='AAPL']['Close'],\

line = dict(color = 'firebrick', width = 4),


name = 'Apple')

])

fig.update_layout(title = 'Closed Prices over time',

xaxis_title = 'Dates',

yaxis_title = 'Prices'

return fig

app.layout = html.Div(id = 'parent', children = [

html.H1(id = 'H1', children = 'Apple Stock Market Analysis',


style = {'textAlign':'center',\

'marginTop':40,'marginBottom':40}),

d G h(id 'li l t' fi t k


https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73
i ()) 7/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
dcc.Graph(id = 'line_plot', figure = stock_prices())

]
Open in app Get started
)

app.run_server()

PowerBI
By using PowerBI, we can easily create the relevant dashboard we need without
writing any code. Since not all data scientists are fully capable of Python skills.

First and foremost, we understand the data type of our datasets on PowerBI Tools.

Data Type (Power BI)

Adj Close, Close, High, Low, Open, Volume := Number

Date, stockCode, StockName := Text

We can change our data type when importing the data or using DAX formula to change
column type. I will show you later.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 8/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Visualization Tab (PowerBI)

Do you notice there are a lot of visualization components? Choose the proper one for
our visualization. In this article, I will choose line charts for our demonstration.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 9/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Axis and Values Configuration

Drag and drop the column from right-hand-side Dataset Fields. Finally, we have done
our first visualization. Then end our course.

….. Hahahaha. It is not ended. Let us look at the output.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 10/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Weird Chart

I guess you get the points, the data look very weird, all the data look like remain in the
same line, actually, there have some differences between each value. We can move to
hover the lines, we find out that data are different from each other.

This data is not useful for our analysis. How can we make it better to view stock market
trends? Therefore, we need to find out the issues from datasets.

Let’s look at the x-axis, it shows that there are all 2017 data. We know what is the
issue. Did you remember our dataset Date data type, it is a text value (string). So the
easy to solve this problem is to change the data type. There are 2 ways to do it.

1. Change from the data source, when we import the data into PowerBI.

Edit Queries

or hover to df dataset, right-click to choose Edit Query.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 11/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Edit Query on Dataset

Next, it will pop out another window to show the top 1000 rows of data.

PowerBI Edit Query Preview

Move to Date Column and right-click, it will pop out a menu, choose change Type, and
then choose either Date or Date/Time.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 12/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

2. Create another new column to convert the data type

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 13/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Create new Column

DAX: Date_converted=DATEVALUE(df[Date])

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 14/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Both methods show the same results, but it is still weird.

We still need to make some changes, remember our data structures, there are 10
companies in the dataset. So we need to separate it out to different visuals or add a
filter function.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 15/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Filtering

Filtering is always necessary when building a dashboard for data analysis. Therefore,
end-users are able to view the relevant data.

We can change the filtering display options. Datetime and Number will have different
options respectively. For more guidance, I will create a new article regarding PowerBI
Basic Functions.

The trend is the same as Python Plotly, just some slightly different due to the line gap
from the configuration it doesn’t matter
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 16/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
from the configuration, it doesn t matter.
Open in app Get started
Let us continue to show those top 10 tech companies’ trends on one screen.

As usual, I will show the Plotly method first.

Plotly
In order to make it easy to do data processing, we can enhance our old code written
above to extract the data and visualization. Let’s change it a little bit. The only change
start from line 20, please compare it from the previous code.

Next, we need to implement the visualization to show the 10 companies' market


trends
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 17/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
trends.
Open in app Get started

import matplotlib.pyplot as plt

df.columns.names = ['Stock Ticker', 'Stock Info']

close = df.xs(key='Close', axis=1, level='Stock Info')

plt.figure(figsize=(12, 6))

fig = px.area(close, facet_col='Stock Ticker', facet_col_wrap=2)

fig.show()

Output:

Pycharm Jupyter Notebook

It shows that the Samsung data have an issue from Yahoo Finance (need to investigate
later), higher than other companies. For the current situation, we just remove Samsung
for further analysis.

df.drop('Samsung',axis=1, inplace=True)

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 18/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Pycharm Jupyter Notebook Visualization

It looks much better than the previous one.

PowerBI
For PowerBI, we will be using the copy and paste (Ctrl + C and Ctrl + v) method to do
it in a quick way.

Copy and paste within 1 minutes (PowerBI)

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 19/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

In consequence, we will filter each visual with different companies. From here,
Open in app
we
Get started
can’t use filter visual to filter one by one, it is not efficient. Let me show how to filter
the company with each visual.

Click the visual, you will notice the filter tab will show the picture below:

Drag either stock name or stock code column.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 20/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

It shows that10 Companies’ options, filtered one by one for each visual respectively.

Apple Stock Market (PowerBI)

The results as previously, in order to make it look better, let us change some
configurations.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 21/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Visual Settings

We can change the configurations here to make the visuals look better. Changing title,
background color, labeling, line style, and ext.

Next, let’s complete the whole dashboard development. The result is as below:

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 22/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Once experienced using Power BI, completing one interactive dashboard is just 10
minutes’ task.

Date Range

We can choose the date range we wanna show on the dashboard, for example, 2020–
01–01 to 2022–02–26.

Therefore, it will only show 2020 until now.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 23/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor

Open in app Get started

Conclusion

The differences between Power BI and Plotly


1. Power BI supports PC, iOS, Android, and Windows devices, whereas Plotly is only
workable for Windows and Mac devices.

2. Power BI is easy for using, support drag and drop features, shortens the
development time, whereas Plotly required lots of technical skills to do data
analysis.

3. Power BI support for importing third-party built-in visuals available for more
analysis tools, whereas Plotly needs to more spend time coding the same features.

4. Plotly is more flexible compared to Power Bi, some of the functions Power BI is not
able to have, but Plotly can code the function.

Schedule a DDIChat Session in Coding, Software, and Mobile Development:

Experts - Coding, Software, and Mobile Development - DDIChat


DDIChat allows individuals and businesses to speak directly with
subject matter experts. It makes consultation fast…
app.ddichat.com
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 24/25
04/03/22, 10:07 Stock Market Analysis with Python, Plotly, Dash, and PowerBI | by Edward Low | Feb, 2022 | DataDrivenInvestor
app.ddichat.com

Open in app Get started

Apply to be a DDIChat Expert here.

Work with DDI: https://datadriveninvestor.com/collaborate

Subscribe to DDIntel here.

https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 25/25

You might also like