Professional Documents
Culture Documents
Stock Market Analysis With Python, Plotly, Dash, and PowerBI - by Edward Low - Feb, 2022 - DataDrivenInvestor
Stock Market Analysis With Python, Plotly, Dash, and PowerBI - by Edward Low - Feb, 2022 - DataDrivenInvestor
Published in DataDrivenInvestor
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.
1. Click Get Data -> More -> Other -> Python Script
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
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
Remember to check your Python installation Path and installed the necessary libraries.
Necessary Modules:
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 Explanation:
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)
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
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.
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.
Code Snippets:
import dash
import plotly.graph_objects as go
app = dash.Dash()
def stock_prices():
])
xaxis_title = 'Dates',
yaxis_title = 'Prices'
return fig
'marginTop':40,'marginBottom':40}),
]
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.
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
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
Drag and drop the column from right-hand-side Dataset Fields. Finally, we have done
our first visualization. Then end our course.
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
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
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
Next, it will pop out another window to show the top 1000 rows of data.
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
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
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
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
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.
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.
plt.figure(figsize=(12, 6))
fig.show()
Output:
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
PowerBI
For PowerBI, we will be using the copy and paste (Ctrl + C and Ctrl + v) method to do
it in a quick way.
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:
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
It shows that10 Companies’ options, filtered one by one for each visual respectively.
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
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
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.
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
Conclusion
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.
https://medium.datadriveninvestor.com/stock-market-analysis-with-python-plotly-dash-and-powerbi-48975c48fe73 25/25