avatarMr. Q

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5330

Abstract

2">We will first calculate the daily log-returns and join two DataFrames together. <b>Please check Day 5 for detail explanation</b>.</p><figure id="c8ab"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7KNEqnaUsCI6K2iBjvXMqw.png"><figcaption></figcaption></figure><p id="2e48">With the daily log-returns, we can do simple math to compute the cumulative returns.</p><figure id="d4e3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rCS4CFwSCVwevDQ48FxdkA.png"><figcaption></figcaption></figure><p id="3ddf">Finance is all about compound returns, so we used the function “cumprod”. It starts from the second row and for each row, it does the multiplications with all previous rows. In Excel, we usually need two columns with iterative multiplications. Now we can plot the chart with the DataFrame directly.</p><figure id="a1db"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*dfHJMPlSW54LUH6lUbYv3A.png"><figcaption></figcaption></figure><p id="3ce5">Notice the difference is that, in the first example, we selected the column first, then plotted the column. Here we applied the function “<b>plot</b>” with the whole DataFrame.</p><h2 id="d6ce">Scatter</h2><p id="96dd">Scatter is one of the most common charts people use in Finance. Let’s check this out with another example. We could use it to visualize the correlation between two stocks, MSFT and IBM.</p><figure id="b8b5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*y25fQFasdZe16nq-5cDYQg.png"><figcaption></figcaption></figure><p id="ed1c">As a default, the function “<b>plot</b>” does the line chart. If we need a different type of charts, we can use the parameter “<b>kind</b>”. For the scatter chart, we need to supply column names of <b>x</b> and <b>y</b>, which should match the column names with the DataFrame.</p><h2 id="3b83">Bar</h2><p id="0c72">Another common chart people do and let’s do another simple example. Say we want to compare the one-year return between two stocks with a bar plot.</p><figure id="e4eb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VdbPts5CMDfsL9ROas8ixw.png"><figcaption></figcaption></figure><figure id="d628"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*neuwE5vTASWOyNe23MI_FQ.png"><figcaption></figcaption></figure><p id="52a0">First, we take the last row from the DataFrame “<b>cum_return_df</b>”. Then to give it a better name, we replaced the value of the index with “<b>1Y</b>”. Finally, we plot the chart by telling we need “<b>kind</b>” to be “<b>bar</b>”. To better visualize the bar, we show the gridlines too by setting the parameter “<b>grid</b>” to be <b>true</b>.</p><h2 id="e41d">Two Axes Two Scales</h2><p id="34a1">One of the common needs in financial analysis is to have <b>two axes </b>with <b>two scales</b>. For example, if we want to plot price with volume, we cannot place them into one scale, because the volume has much larger numbers than price. So, let’s do this example.</p><p id="c91f">First, we plot a regular line chart of price, just as what we did previously.</p><figure id="1f10"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7g9kUKwLx1qb49wYiGeLDQ.png"><figcaption></figcaption></figure><p id="504e">Second, we add another “area” plot (basically a line with the area below filled with colour).</p><figure id="0d10"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*BUaW54CODSOtD0KCi0T_FA.png"><figcaption></figcaption></figure><p id="219b">Notice this time, we used two more parameters:</p><blockquote id="e92b"><p><b>secondary_y</b>: have the plot using secondary y axis (right handside)</p></blockquote><blockquote id="867a"><p><b>alpha</b>: to make the area transparent, value between 0 and 1, where 0 means fully transparent</p></blockquote><p id="4698">There are several things we can improve. First, there’s overlapping between the price line and the volume area. We can push the volume a bit down by increase the maximum value of y-axis on the right. Second, we can make the colour of the right y-axis and ticks orange to match the colour of the area plot. Let’s improve our code.</p><figure id="5684"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bEO74QbVIFKONxVPTRrNwg.png"><figcaption></figcaption></figure><p id="6e36">One of the major differences is that we now assign the plot as a variable “<b>ax</b>”. This variable can then be used to modify the components of the chart. To understand the chart components, we can check the official tutorial of Matplotlib below.</p><p id="26ad"><a href="https://matplotlib.org/3.1.1/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py">https://matplotlib.org/3.1.1/tutorials/introductory/usage.html</a></p><h2 id="172d">Fill Between Lines</h2><p id="9c5a">Many technical indicators require highlighting upper and lower bounds. For example, Bollinger Band is a typical one. We can see how to do so with a simple example of a rolling standard deviation band.</p><p id="4d34">We start the example by calculating the rolling standard deviation and upper/lower bands.</p><figure id="b443"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kZX0C1xxlPQLqYIx5vXtlQ.png"><figcaption></figcaption></figure><p id="85c5">Again, please check Day 5 for d

Options

etails of the function “rolling”.</p><p id="bcdc">Then we can plot the simple line chart as before and adding the highlight of the bands with the function “<b>fill_between</b>”.</p><figure id="0f6f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*qmqFtm8iS-BhjHuCV7Xh3w.png"><figcaption></figcaption></figure><p id="a5ff">Remember we imported “plt” from Matplotlib at the beginning. It provides the function “<b>fill_between</b>”, where we need to give the following inputs:</p><blockquote id="6845"><p><b>x</b>: the x-axis values</p></blockquote><blockquote id="04c3"><p><b>y1</b>: one of the band series</p></blockquote><blockquote id="93ba"><p><b>y2</b>: another series of the band</p></blockquote><blockquote id="0c65"><p><b>color</b>: the colour of the band</p></blockquote><blockquote id="a9c4"><p><b>alpha</b>: transparency level of the band</p></blockquote><h2 id="9075">Why not Candlestick?</h2><p id="c7a7">The number one chart people do in Finance. But it won’t be optimal to plot in Jupyter. Candlestick is used for us to add annotations interactively for analyzing the market trend. We will need a good technical analysis platform and Jupyter just not good for that analysis. You may find many technical analysis platforms online, such as TradingView.</p><div id="7646" class="link-block"> <a href="https://uk.tradingview.com/"> <div> <div> <h2>Free Stock Charts, Stock Quotes and Trade Ideas</h2> <div><h3>Live quotes, stock charts and expert trading ideas. TradingView is a social network for traders and investors on Stock…</h3></div> <div><p>uk.tradingview.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*H_zNuet8mdx4rzPr)"></div> </div> </div> </a> </div><p id="dd5e">The key use of Jupyter and Python is to do the systematic and quantitative analysis so that we could implement precisely. Plotting Candlestick here may not be helpful. However, if we do want to plot such a chart, it is still straight forward. Check out the following link for Plotly.</p><div id="5185" class="link-block"> <a href="https://plotly.com/python/candlestick-charts/"> <div> <div> <h2>Candlestick Charts</h2> <div><h3>Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly…</h3></div> <div><p>plotly.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*x3Z8ZzNHGq0EIpjZ)"></div> </div> </div> </a> </div><p id="5b70">Congratulations! We have done <b>Python for Financial Analysis Series — Python Tools</b>. If you are new to Python or would like to refresh some basic Python knowledge, please check out the first series, <b>Python Core</b>.</p><div id="300f" class="link-block"> <a href="https://readmedium.com/python-for-financial-analysis-series-python-core-day-1-b46f6aa78204"> <div> <div> <h2>Python for Financial Analysis Series — Python Core Day 1</h2> <div><h3>Following the last introduction session, we will start with the first part, Python Core. It will be a 5 days sub-series…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*JNxA_VHJLnTPWoOX0iUe9A.png)"></div> </div> </div> </a> </div><h2 id="8346">NEXT STEP</h2><p id="aea4">This is my first time blogging the tutorials on Medium. For the last three months, I have written two series, 12 articles. It was such a milestone for me, as a non-native English speaker. I hope it helps financial professionals advancing the daily works with new Python skills.</p><p id="7dff">From here I plan to start writing about practical case studies. One thing I notice is that many people on Medium writing about very futuristic topics such as AI and ML. However, the majority of people in the financial market still need to apply basic Python solving ordinary day to day tasks. I hope by writing more practical case studies, it may help more people to improve the daily workflows.</p><p id="ec49" type="7">Bye for now and I will talk to you very soon!</p><div id="1a75" class="link-block"> <a href="https://readmedium.com/backtesting-of-trading-strategy-with-technical-indicator-1-f782b252d873"> <div> <div> <h2>Backtesting of Trading Strategy with Technical Indicator #1</h2> <div><h3>Want to trade? Better to backtest the idea first. With this series, I want to extend Python Financial Series into…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*DIZ7Wa-RLgQqt5ZwVzTnfw.png)"></div> </div> </div> </a> </div></article></body>

Python for Financial Analysis Series — Python Tools Day 6

Let’s Matplotlib! This is the last session of the series. We will do the essential data visualization with Matplotlib. We’ll plot lines, scatter, and bars.

Tip before start

Visualization is cool, but for analysis, it’s a tool

The VALUE comes from the STATS and METRICS. Imagine we do the backtest of a trading strategy. It’s nice if we could plot the PnL (Profit Loss), but it is the metrics, like return, trading frequency, and maximum drawdown, that tells the true story. We are more likely to evaluate the strategies by comparing the metrics rather than visually picking up the charts.

The goal of this session is to provide examples of the essential charts in an easy way, which is supplementary to the core data anlaysis.

IMPORTANT UPDATE

Since Microsoft Azure Notebooks has moved to GitHub driven, which may be challenging for a beginner to start with. I have put all the material under Google Colab. It is a very similar Jupyter Notebook environment and you may check it out by click here.

All my code and files are shared and you may click here to open.

Get the Real Data

If you haven’t read the last session and haven’t got the API key to retrieve the stock price from IEX Cloud, please check out the last one below.

We will work with two stocks’ data, MSFT and IBM, so let’s download a one-year historical price for both with IEX Cloud.

The same as the last session, we first install the IEX Cloud SDK with the command “!pip install”, then we import all the packages, including Matplotlib. Once all packages imported, we will initialize the connection to IEX Cloud with our sandbox testing token. Finally, we can download the historical price with the function “chartDF”.

Simple Line Chart

Now we can start plotting. Good news is that Pandas is so integrated with Matplotlib that we can plot directly with DataFrame, using the function “plot”. Here’s the simple example of a line chart for Microsoft daily price.

It just cannot be simpler than that. Get the column you want and apply the function “plot”, then we get a chart! Here we used three parameters.

figsize: it’s a Python tuple, defining the width and height of the chart

title: the header on the top middle of the chart

legend: show/hide legend

What if we need to plot IBM as well? Just add one more similar line for IBM.

As we can see, the default plotting will be in the same chart (more precisely the same figure) and it will automatically use the same x-axis and y-axis scales.

Another way to plot the lines in the same chart is to plot DataFrame with multiple columns. Let’s do one example. Imagine we want to compare the cumulative log-returns of both stocks.

We will first calculate the daily log-returns and join two DataFrames together. Please check Day 5 for detail explanation.

With the daily log-returns, we can do simple math to compute the cumulative returns.

Finance is all about compound returns, so we used the function “cumprod”. It starts from the second row and for each row, it does the multiplications with all previous rows. In Excel, we usually need two columns with iterative multiplications. Now we can plot the chart with the DataFrame directly.

Notice the difference is that, in the first example, we selected the column first, then plotted the column. Here we applied the function “plot” with the whole DataFrame.

Scatter

Scatter is one of the most common charts people use in Finance. Let’s check this out with another example. We could use it to visualize the correlation between two stocks, MSFT and IBM.

As a default, the function “plot” does the line chart. If we need a different type of charts, we can use the parameter “kind”. For the scatter chart, we need to supply column names of x and y, which should match the column names with the DataFrame.

Bar

Another common chart people do and let’s do another simple example. Say we want to compare the one-year return between two stocks with a bar plot.

First, we take the last row from the DataFrame “cum_return_df”. Then to give it a better name, we replaced the value of the index with “1Y”. Finally, we plot the chart by telling we need “kind” to be “bar”. To better visualize the bar, we show the gridlines too by setting the parameter “grid” to be true.

Two Axes Two Scales

One of the common needs in financial analysis is to have two axes with two scales. For example, if we want to plot price with volume, we cannot place them into one scale, because the volume has much larger numbers than price. So, let’s do this example.

First, we plot a regular line chart of price, just as what we did previously.

Second, we add another “area” plot (basically a line with the area below filled with colour).

Notice this time, we used two more parameters:

secondary_y: have the plot using secondary y axis (right handside)

alpha: to make the area transparent, value between 0 and 1, where 0 means fully transparent

There are several things we can improve. First, there’s overlapping between the price line and the volume area. We can push the volume a bit down by increase the maximum value of y-axis on the right. Second, we can make the colour of the right y-axis and ticks orange to match the colour of the area plot. Let’s improve our code.

One of the major differences is that we now assign the plot as a variable “ax”. This variable can then be used to modify the components of the chart. To understand the chart components, we can check the official tutorial of Matplotlib below.

https://matplotlib.org/3.1.1/tutorials/introductory/usage.html

Fill Between Lines

Many technical indicators require highlighting upper and lower bounds. For example, Bollinger Band is a typical one. We can see how to do so with a simple example of a rolling standard deviation band.

We start the example by calculating the rolling standard deviation and upper/lower bands.

Again, please check Day 5 for details of the function “rolling”.

Then we can plot the simple line chart as before and adding the highlight of the bands with the function “fill_between”.

Remember we imported “plt” from Matplotlib at the beginning. It provides the function “fill_between”, where we need to give the following inputs:

x: the x-axis values

y1: one of the band series

y2: another series of the band

color: the colour of the band

alpha: transparency level of the band

Why not Candlestick?

The number one chart people do in Finance. But it won’t be optimal to plot in Jupyter. Candlestick is used for us to add annotations interactively for analyzing the market trend. We will need a good technical analysis platform and Jupyter just not good for that analysis. You may find many technical analysis platforms online, such as TradingView.

The key use of Jupyter and Python is to do the systematic and quantitative analysis so that we could implement precisely. Plotting Candlestick here may not be helpful. However, if we do want to plot such a chart, it is still straight forward. Check out the following link for Plotly.

Congratulations! We have done Python for Financial Analysis Series — Python Tools. If you are new to Python or would like to refresh some basic Python knowledge, please check out the first series, Python Core.

NEXT STEP

This is my first time blogging the tutorials on Medium. For the last three months, I have written two series, 12 articles. It was such a milestone for me, as a non-native English speaker. I hope it helps financial professionals advancing the daily works with new Python skills.

From here I plan to start writing about practical case studies. One thing I notice is that many people on Medium writing about very futuristic topics such as AI and ML. However, the majority of people in the financial market still need to apply basic Python solving ordinary day to day tasks. I hope by writing more practical case studies, it may help more people to improve the daily workflows.

Bye for now and I will talk to you very soon!

Data Science
Programming
Machine Learning
Artificial Intelligence
Finance
Recommended from ReadMedium