<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Finance - Not A Programmer</title>
	<atom:link href="https://notaprogrammer.com/category/finance/feed/" rel="self" type="application/rss+xml" />
	<link>https://notaprogrammer.com</link>
	<description>Marco Cerrato Personal Blog</description>
	<lastBuildDate>Thu, 20 Jul 2023 15:10:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://notaprogrammer.com/wp-content/uploads/2020/08/favicon-s.png</url>
	<title>Finance - Not A Programmer</title>
	<link>https://notaprogrammer.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to automate a liquidity ratio analysis using python</title>
		<link>https://notaprogrammer.com/automate-liquidity-ratio-analysis-using-python/</link>
					<comments>https://notaprogrammer.com/automate-liquidity-ratio-analysis-using-python/#respond</comments>
		
		<dc:creator><![CDATA[Marco Cerrato]]></dc:creator>
		<pubDate>Fri, 14 Aug 2020 02:42:00 +0000</pubDate>
				<category><![CDATA[Finance]]></category>
		<guid isPermaLink="false">http://notaprogrammer.com/?p=194</guid>

					<description><![CDATA[Write a script that automates the process of calculating liquidity ratios for Apple Inc.]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Overview</h2>



<p>In the following exercise, we will practice writing an automation script to calculate liquidity ratios. This script will also create an MS Excel file containing the results and three graphs so that we can visualize the liquidity ratios.</p>



<p>The objective of this exercise is to practice working with real-world data and to see how automation can help you be more productive. </p>



<p>You can also read the article "<a class="rank-math-link" href="https://notaprogrammer.com/python-module-calculate-20-financial-ratios/">How to use a python custom module to simplify your work" to learn </a>more about the python module used in this exercise.</p>



<h2 class="wp-block-heading">Task Requirement</h2>



<p>We want to <strong>automate</strong> the creation of a worksheet that contains a table with three liquidity ratios for Apple Inc. for the last 10 years. We also want to visualize the liquidity ratios while comparing them with industry ratios when possible.</p>



<p>The liquidity ratios that we want to calculate:</p>



<ul class="wp-block-list"><li>Current Ratio</li><li>Quick Ratio or Acid Test</li><li>Cash Ratios</li></ul>



<p>According to <a aria-label="Guru Focus (opens in a new tab)" href="https://www.gurufocus.com/industry_overview.php" rel="noreferrer noopener" class="rank-math-link" target="_blank">Guru Focus</a>, as of 8-10-2020, the industry liquidity ratios are <strong>1.96</strong> for the Current Ratio and <strong>1.58</strong> for the Quick Ratio. There is no industry ratio for the Cash Ratio, so we will need to compare it with the results from previous years.</p>



<h2 class="wp-block-heading">Resources</h2>



<p>The following resources are made available to us for this exercise:</p>



<ul class="wp-block-list"><li>A file named <code>apple.csv</code>, which contains the last ten years of Apple Inc.'s annual accounting data for the following balance sheet accounts:<ul><li>Cash &amp; Cash Equivalent</li><li>Inventories</li><li>Total Current Assets</li><li>Total Current Liabilities</li></ul></li><li>A file named <code>financialratios.py</code>. This file is the custom python module that stores the functions needed to calculate each ratio.</li></ul>



<p>Both files and a copy of the final python script are available from the <a href="https://github.com/cerratom/Financial-Ratios-Module.git" target="_blank" aria-label="project repository (opens in a new tab)" rel="noreferrer noopener" class="rank-math-link">project repository</a>.  You can also get the complete financial information of Apple Inc. <a aria-label="here (opens in a new tab)" target="_blank" href="https://investor.apple.com/sec-filings/default.aspx" rel="noreferrer noopener" class="rank-math-link">here</a>.</p>



<p>Additionally, you will also need to have installed <a aria-label="Python 3.8 (opens in a new tab)" rel="noreferrer noopener" href="https://www.python.org/downloads/release/python-382/" target="_blank" class="rank-math-link">Python 3.8</a>  on your computer with the following libraries:</p>



<ul class="wp-block-list"><li><a aria-label="Pandas library installed (opens in a new tab)" rel="noreferrer noopener" class="rank-math-link" href="https://pandas.pydata.org/" target="_blank">Pandas library</a></li><li><a aria-label="Matplotlib (opens in a new tab)" rel="noreferrer noopener" class="rank-math-link" href="https://matplotlib.org/" target="_blank">Matplotlib</a></li></ul>



<p>Finally, you will need a code editor. I will be using Microsoft Visual Code as my code editor.</p>



<h2 class="wp-block-heading">Instructions</h2>



<p>The instructions for this exercise are as follows:</p>



<ol class="wp-block-list"><li>Read and learn about liquidity ratios. </li><li>Download the <code>apple.csv</code> and <code>financialratios.py</code> files from the <a aria-label="project repository (opens in a new tab)" href="https://github.com/cerratom/Case-Study-Liquidity-Ratios.git" rel="noreferrer noopener" class="rank-math-link" target="_blank">project repository</a>.</li><li>Save both files to the project's folder.</li><li>On your project's folder, create a python script that does the following:<ol><li>Imports the necessary libraries and module</li><li>Imports the dataset into a pandas DataFrame and name it <code>df</code>.</li><li>Creates an empty DataFrame called <code>df_ratios</code> to store Apple Inc.'s liquidity ratios</li><li>Use the required functions to calculate each ratio and save the results into the <code>df_ratios</code> DataFrame.</li><li>Export the results to an MS Excel file.</li><li>Create a graph for each of the liquidity ratios</li></ol></li></ol>



<h2 class="wp-block-heading">What are Liquidity Ratios?</h2>



<p>Liquidity ratios are metrics used to measure a company's ability to meet its short-term obligation or liabilities. The two most common liquidity ratios used in financial analysis are the <strong>current ratio</strong> and the <strong>quick ratio</strong>. You can learn more about financial ratios by reading the article "<a href="https://corporatefinanceinstitute.com/resources/knowledge/finance/financial-ratios/" target="_blank" rel="noopener">What are Financial Ratios?</a>" by the <a href="https://corporatefinanceinstitute.com/" target="_blank" rel="noopener">Corporate Finance Institute</a> or any Financial Management textbook.</p>



<h2 class="wp-block-heading">Solution</h2>



<p>The following steps can be followed to writing the script about liquidity ratios:</p>



<h3 class="wp-block-heading">Step 1: Setting the project's folder</h3>



<p>Before we begin to write the Python script, <a aria-label="download (opens in a new tab)" rel="noreferrer noopener" href="https://github.com/cerratom/Financial-Ratios-Module.git" target="_blank" class="rank-math-link">download</a> the exercise files <code>apple.csv</code> and the python module <code>financialratios.py</code>. Save both files to the project's folder.</p>



<p>Next, open your code editor of choice and create the file for the script in the project's folder where you have stored the <code>apple.csv</code> and the <code>financialratios.py</code> module.</p>



<p>With everything setup, we can now start writing the script.</p>



<h3 class="wp-block-heading">Step 2: Importing the required libraries and module</h3>



<p>The first step is to import the necessary python module and libraries. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from matplotlib.pyplot import colorbar
import financialratios as fr
import pandas as pd 
import matplotlib.pyplot as plt</pre>



<h3 class="wp-block-heading">Step 3: Importing the <code>apple.csv</code> file</h3>



<p>Our third step is to import the data stores in the file apple.csv. We will import the data into a panda's DataFrame object called df.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">df = pd.read_csv('apple.csv', index_col='Date', parse_dates=True)</pre>



<p>Because the <code>apple.csv</code> the file also contains dates; we can also set the column <code>"Date"</code> as our index. We can parse the content in the "Date" column as a <code>datetime</code> datatype.</p>



<h3 class="wp-block-heading">Step 4: Creating an empty DataFrame</h3>



<p>Now that we have imported our data and saved it to the DataFrame df, we can create the empty DataFrame where we want to store the results. We can call this DataFrame <code>df_ratios</code> by writing the following code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Create the column names
column_names = ["Current Ratio", "Quick Ratio", "Cash Ratio"]
# Create the empty dataframe
df_ratios = pd.DataFrame(columns=column_names)</pre>



<h3 class="wp-block-heading">Step 5: Calculating the Current Ratios</h3>



<p>Our fifth step is to use the custom module <code>financialratios</code> to call the <code>function </code>get_current_ratio and calculate the <strong>current ratio</strong>. We will store the results into the new DataFrame df_ratios under the column named "Current Ratio". Here is the line of code we need to write:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">df_ratios["Current Ratio"] = fr.get_current_ratio(df['current assets'], df['current liabilities'])</pre>



<p>Let us dig down to see what we just wrote:</p>



<ol class="wp-block-list"><li>We specify that we want to save the results of our calculation in the column <code>Current Ratio</code> of the DataFrame <code>df_ratios</code> .</li><li>We call the function <code>get_current_ratio</code> from the custom module that we imported and named <code>fr</code>.</li><li>The function accepts two parameters. The first one is for the values in the "Current Assets" column in the DataFrame <code>df</code>.</li><li>The second parameter that we pass to the functions is the values in the "Current Liabilities" column of the DataFrame <code>df</code>.</li></ol>



<p>We can check that the calculation has been done correctly by calling the <code>.head</code> method on <code>df_ratios</code> by printing <code>df_ratios.head()</code></p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="580" height="279" src="https://notaprogrammer.com/wp-content/uploads/2020/08/02-Checking-the-Current-Ratio-min.jpg" alt="Liquidity Ratio Incomplete" class="wp-image-389" srcset="https://notaprogrammer.com/wp-content/uploads/2020/08/02-Checking-the-Current-Ratio-min.jpg 580w, https://notaprogrammer.com/wp-content/uploads/2020/08/02-Checking-the-Current-Ratio-min-300x144.jpg 300w" sizes="(max-width: 580px) 100vw, 580px" /></figure>



<h3 class="wp-block-heading">Step 6: Calculating the Quick and Cash Ratio</h3>



<p>Now that we know our script is working, we can continue calculating the other two ratios in the same way we did with the current ratio.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">df_ratios["Quick Ratio"] = fr.get_quick_ratio(df['current assets'], df['inventories'], df['current liabilities'])
df_ratios["Cash Ratio"] =  fr.get_cash_ratio(df['cash &amp; cash equivalent'], df['current liabilities'])</pre>



<p>We can check that the calculations have been done correctly by calling the <code>.head</code> method on <code>df_ratios</code>.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://notaprogrammer.com/wp-content/uploads/2020/08/04-Checking-the-Quick-and-Cash-Ratio-min.jpg" alt="Liquidity Ratio Table" class="wp-image-388" width="580" height="372" srcset="https://notaprogrammer.com/wp-content/uploads/2020/08/04-Checking-the-Quick-and-Cash-Ratio-min.jpg 580w, https://notaprogrammer.com/wp-content/uploads/2020/08/04-Checking-the-Quick-and-Cash-Ratio-min-300x192.jpg 300w" sizes="(max-width: 580px) 100vw, 580px" /></figure>



<h3 class="wp-block-heading">Step 7: Exporting the DataFrame to an MS Excel File</h3>



<p>Now that we have calculated all the liquidity ratios for the last ten years, we can export the table to a Microsoft Excel file. We can export the results by writing the following line of code.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">df_ratios.to_excel('liquidity-ratios.xlsx')</pre>



<p>As we can see in our folder, the <code>liquidity-ratios.xlsx</code> file has been created and can now be open using Microsoft Excel.</p>



<h3 class="wp-block-heading">Step 8: Visualizing the Current Ratio</h3>



<p>In this step, we will use the library matplotlib to plot the <strong>current ratios</strong> for the past 10 years. Remember that we will set the industry average at <strong>1.96</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.plot(df_ratios['Current Ratio'])
plt.axhline(y=1.96, color='r')
plt.xticks(rotation=45)
plt.title('Current Ratio')
plt.xlabel('Years')
plt.ylabel('Ratio')
plt.show()</pre>



<p>If we run the script we get the following graph:</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://notaprogrammer.com/wp-content/uploads/2020/08/06-Current-Ratio-min.jpg" alt="Liquidity - Current Ratio Graph" class="wp-image-386" width="580" height="426" srcset="https://notaprogrammer.com/wp-content/uploads/2020/08/06-Current-Ratio-min.jpg 580w, https://notaprogrammer.com/wp-content/uploads/2020/08/06-Current-Ratio-min-300x220.jpg 300w" sizes="(max-width: 580px) 100vw, 580px" /></figure>



<h3 class="wp-block-heading">Step 9: Visualizing the Quick Ratio</h3>



<p>Next, we will visualize the <strong>quick ratio</strong>, setting the industry ratio to <strong>1.58</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.plot(df_ratios['Quick Ratio'])
plt.axhline(y=1.58, color='g')
plt.axhline(y=1, color='r')
plt.xticks(rotation=45)
plt.title('Quick Ratio')
plt.xlabel('Years')
plt.ylabel('Ratio')
plt.show()</pre>



<p>If we run the script we get the following graph:</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://notaprogrammer.com/wp-content/uploads/2020/08/07-Quick-Ratio-min.jpg" alt="Liquidity - Quick Ratio Graph" class="wp-image-385" width="580" height="426" srcset="https://notaprogrammer.com/wp-content/uploads/2020/08/07-Quick-Ratio-min.jpg 580w, https://notaprogrammer.com/wp-content/uploads/2020/08/07-Quick-Ratio-min-300x220.jpg 300w" sizes="auto, (max-width: 580px) 100vw, 580px" /></figure>



<h3 class="wp-block-heading">Step 10: Visualizing the Cash Ratio</h3>



<p>The last step to be included in the script is the lines of code that will allow us to visualize Apple's cash ratio for the last 10 years.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.plot(df_ratios['Cash Ratio'])
plt.xticks(rotation=45)
plt.title('Cash Ratio')
plt.xlabel('Years')
plt.ylabel('Ratio')
plt.show()</pre>



<p>If we run the script we get the following graph:</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://notaprogrammer.com/wp-content/uploads/2020/08/08-Cash-Ratio-min.jpg" alt="Liquidity - Cash Ratio Graph" class="wp-image-384" width="580" height="426" srcset="https://notaprogrammer.com/wp-content/uploads/2020/08/08-Cash-Ratio-min.jpg 580w, https://notaprogrammer.com/wp-content/uploads/2020/08/08-Cash-Ratio-min-300x220.jpg 300w" sizes="auto, (max-width: 580px) 100vw, 580px" /></figure>



<h2 class="wp-block-heading">Final Thoughts</h2>



<p>I hope you found this project useful and that you now have a better understanding of how python can be of great tool to use for automating repetitive tasks like calculating financial ratios. <span style="font-size: calc(var(--rem) * 1px * 1.0625); letter-spacing: 0px;">Moreover</span>, I hope you can apply what you have learned in your job. Remember that you can extend the script you just wrote to calculate other ratios. You can also modify the python module to included different ratios too.</p>



<p>Please feel free to share this article with your coworkers, friends, and family through social media. You can also subscribe to my mailing list to receive information when I publishing new content.</p>



<p>Again thank you for taking the time to read this article. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://notaprogrammer.com/automate-liquidity-ratio-analysis-using-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to  use a custom python module to simplify your work</title>
		<link>https://notaprogrammer.com/use-custom-python-module-to-simplify-your-work/</link>
					<comments>https://notaprogrammer.com/use-custom-python-module-to-simplify-your-work/#respond</comments>
		
		<dc:creator><![CDATA[Marco Cerrato]]></dc:creator>
		<pubDate>Wed, 12 Aug 2020 15:10:19 +0000</pubDate>
				<category><![CDATA[Finance]]></category>
		<guid isPermaLink="false">http://notaprogrammer.com/?p=74</guid>

					<description><![CDATA[Save time by reusing code through python modules. Create a module to store 20 functions and call them when writing a scripts for financial analysis.]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Overview</h2>



<p>In this article, we will create a python module by writing and storing twenty functions into a file ending with the .py extension. The functions included in this file can calculate several types of financial ratios.</p>



<p>The objective of this article is to show the usefulness of python modules and how they can save time when reusing code. In this exercise, we will create a python module that contains several functions. Other programs and scripts can later call these functions to do specific tasks or calculations.</p>



<p>The python module created in this exercise can be useful when writing programs or scripts to automate or conduct financial analysis. You can see an example of how python modules can be helpful by doing the exercise "<a href="https://notaprogrammer.com/liquidity-ratios-automate-calculations-10-steps/" class="rank-math-link">How to automate a liquidity ratio analysis using python</a>".</p>



<h2 class="wp-block-heading">What is a Python Module?</h2>



<p>Before we begin creating the custom python module, we need to understand what a python module is. We can define a python module as a file with the extension .py that file stores code that can be reused to write other programs and scripts.  </p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h2 class="wp-block-heading">What is a Python Function?</h2>



<p>A function is a block of related statements that run when called upon and performs a task. In this exercise, our functions will help us break our module into smaller, more manageable pieces of code. Here is an example of a python function:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_price_earning_ratio(share_price, earning_per_share):
    """Calculates the Price Earning Ratio. You need to pass two values. The first one is the value for share price and the second value is for the earning per share."""
    price_earning_ratio = share_price / earning_per_share
    return(price_earning_ratio)</pre>



<p>A python function typically contains the following components:</p>



<ul class="wp-block-list">
<li>A header that starts with the keyword <code>def</code>.</li>



<li>The keyword <code>def</code> is followed by the name we want to give the function. In our case this name is <code>get_price_earning_ratio</code>.</li>



<li>After the name of the function comes the arguments or parameters wrapped around parenthesis. <code>(share_price, earning_per_share</code>).</li>



<li>A colon (<span class="has-inline-color has-vivid-red-color">:</span>) marks the end of the function header.</li>



<li>Next, it is considered good practice to use a docstring to describe what the function does. </li>



<li>Following the docstring, we add the block of code for our function. This block is nested within the function. One or more statements make this block.</li>



<li>You usually will end the function with a return statement. This statement might return a value or perform another function.   </li>
</ul>



<p>You can learn more about python functions and python modules by reading the <a aria-label="python&#039;s documentation (opens in a new tab)" href="https://docs.python.org/3/" target="_blank" rel="noreferrer noopener" class="rank-math-link">python's documentation</a>.  </p>
</div></div>



<h2 class="wp-block-heading">Task Requirement</h2>



<p>We want to save time writing code when creating scripts that automate financial ratio analysis tasks. We want to be able to reuse code that helps us calculate a list of financial ratios. The list of financial ratios are:</p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<h4 class="wp-block-heading">Liquidity Ratios</h4>



<ul class="wp-block-list">
<li>Current Ratio</li>



<li>Quick Ratio</li>



<li>Cash Ratio</li>



<li>Operating Cashflow Ratio</li>
</ul>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<h4 class="wp-block-heading">Leverage Ratio</h4>



<ul class="wp-block-list">
<li>Debt-To-Equity</li>



<li>Debt-To-Total Assets</li>



<li>Coverage Ratio</li>



<li>Debt-To-Service Coverage Ratio</li>
</ul>
</div>
</div>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<h4 class="wp-block-heading">Efficiency Ratios</h4>



<ul class="wp-block-list">
<li>Asset Turnover Ratio</li>



<li>Inventory Turnover Ratio</li>



<li>Receivables Turnover Ratio</li>



<li>Days In Sales Inventory Ratio</li>
</ul>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<h4 class="wp-block-heading">Profitability</h4>



<ul class="wp-block-list">
<li>Gross Margin Ratio</li>



<li>Operating Margin Ratio</li>



<li>Return On Assets Ratio</li>



<li>Return On Equity Ratio</li>
</ul>
</div>
</div>



<h4 class="wp-block-heading">Market Value</h4>



<ul class="wp-block-list">
<li>Book Value Per Share Ratio</li>



<li>Dividend Yield Ratio</li>



<li>Earning Per Share Ratio</li>



<li>Price-Earning-Ratio</li>
</ul>



<p>At the end of this exercise, we want to be able to call a function to calculate any one of these ratios when writing other programs or scripts. </p>



<h2 class="wp-block-heading">Resources</h2>



<p>The following resources are made available to help you create the financial ratios module:</p>



<ul class="wp-block-list">
<li>The equations used in this exercise are from the article "<a aria-label="What are Financial Ratios? (opens in a new tab)" rel="noreferrer noopener" target="_blank" href="https://corporatefinanceinstitute.com/resources/knowledge/finance/financial-ratios/" class="rank-math-link">What are Financial Ratios?</a>" by the <a aria-label="Corporate Finance Institute (opens in a new tab)" rel="noreferrer noopener nofollow" target="_blank" href="https://corporatefinanceinstitute.com/" class="rank-math-link">Corporate Finance Institute</a>.</li>



<li>A finished copy of the module file is in the <a aria-label="project repo (opens in a new tab)" rel="noreferrer noopener" target="_blank" href="https://github.com/cerratom/Financial-Ratios-Module.git" class="rank-math-link">project repository</a>.</li>



<li>You can practice applying the module <a aria-label="here (opens in a new tab)" rel="noreferrer noopener" target="_blank" href="https://notaprogrammer.com/liquidity-ratios-automate-calculations-10-steps/" class="rank-math-link">here</a>.</li>
</ul>



<h2 class="wp-block-heading">Instructions</h2>



<p>The instructions for this exercise are straightforward. You need to write a function for each financial ratio and then save the file with the file extension <code>.py</code>. Below the function for each financial ratio.</p>



<h3 class="wp-block-heading">Functions for Liquidity Ratios</h3>



<p>In this section, we will write the functions for the current ratio, quick ratio, cash ratio, and the operating cash flow ratio.</p>



<h5 class="wp-block-heading">A function for the Current Ratio</h5>



<p>The function for the current ratio can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_current_ratio(current_assets, current_liabilities):
    "Returns the current ratio. You will need to pass two values. The first value is for the parameter current assets and a second value for the parameter current liabilities"
    current_ratio = current_assets/current_liabilities
    return(current_ratio)</pre>



<h5 class="wp-block-heading">A function for the Quick Ratio or Acid Test</h5>



<p>The function for the quick ratio or acid test can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_quick_ratio(current_assets, inventories, current_liabilities):
    "Returns the quick or acid test ratio. Your need to pass three values. The first value is for the parameter current assets, the second value for the parameter inventories, and a third value for the parameter current liabilities"
    quick_ratio = (current_assets - inventories)/current_liabilities
    return(quick_ratio)</pre>



<h5 class="wp-block-heading">A function for the Cash Ratio</h5>



<p>The function for the cash ratio can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_cash_ratio(cash, current_liabilities, cash_equivalent=0):
    "Returns the cash ratio. Your need to pass atleast two values. The first value for the parameter cash account, the second value for parameter current liabilities, and an optional third value for the parameter cash equivalent assets."
    cash_ratio = (cash + cash_equivalent)/current_liabilities
    return(cash_ratio)</pre>



<h5 class="wp-block-heading">A function for the Operating Cash Flow Ratio</h5>



<p>The function for the operating cash flow ratio  can be written as follows:</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_operating_cashflow_ratio(operating_cashflow, current_liabilities):
    "Returns the operating cashflow ratio. Your need to pass at least two values. The first value for the parameter operating cashflow and the second value for parameter current liabilities."
    operating_cashflow_ratio = operating_cashflow / current_liabilities
    return(operating_cashflow_ratio)</pre>



<h3 class="wp-block-heading">Functions for Leverage Ratios</h3>



<p>In this section, we will write the functions for the Debt-To-Equity ratio, Debt-To-Total-Assets ratio, Interest Coverage Ratio, and the Debt Service Coverage Ratio.</p>



<h5 class="wp-block-heading">A function for the Debt-to-Equity Ratio</h5>



<p>The function for the debt-to-equity ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_debt_to_equity_ratio(total_liabilities, shareholders_equity):
    "Calculates the debt to equity ratio. You need to pass atleast two values. The first one is the value for total liabilities and the second value is shareholders equity."
    debt_to_equity_ratio = total_liabilities / shareholders_equity
    return(debt_to_equity_ratio)</pre>
</div></div>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h5 class="wp-block-heading">A function for the Debt-to-Assets Ratio</h5>
</div></div>



<p>The function for the debt-to-assets ratio can be written as follows:</p>
</div></div>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_debt_to_total_assets_ratio(total_liabilities, total_assets):
    "Calculates the debt to total assets ratio. You need to pass two values. The first one is the value for total liabilities and the second value is for total assets."
    debt_to_total_assets_ratio = total_liabilities / total_assets
    return(debt_to_total_assets_ratio)</pre>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h5 class="wp-block-heading">A function for the Interest Coverage Ratio</h5>



<p>The function for the interest coverage ratio can be written as follows:</p>
</div></div>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_interest_coverage_ratio(operating_income, interest_expenses):
    "Calculates the interest coverage ratio. You need to pass two values. The first one is the value for operating income and the second value is for interest expenses."
    interest_coverage_ratio = operating_income / interest_expenses
    return(interest_coverage_ratio)</pre>



<h5 class="wp-block-heading">A function for the Debt Service Coverage Ratio</h5>



<p>The function for the debt service coverage ratio can be written as follows:</p>
</div></div>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_debt_service_coverage_ratio(operating_income, total_debt_service):
    "Calculates the service coverage ratio. You need to pass two values. The first value is for operating income and the second value is for total_debt_service."
    debt_service_coverage_ratio = operating_income / total_debt_service
    return(debt_service_coverage_ratio)</pre>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h3 class="wp-block-heading">Functions for Efficiency Ratios</h3>



<p>In this section, we will write the functions for the asset turnover ratio, inventory turnover ratio, receivable turnover ratio, and days in sales inventory ratio.</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h5 class="wp-block-heading">A function for the Asset Turnover Ratio</h5>



<p>The function for the asset turnover ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_asset_turnonver_ratio(net_sales, total_assets):
    "Calculates the asset turnover ratio. You need to pass two values. The first one is the value for net sales and the second value is for total assets."
    asset_turnover_ratio = net_sales / total_assets
    return(asset_turnover_ratio)</pre>
</div></div>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h5 class="wp-block-heading">A function for the Inventory Turnover Ratio</h5>



<p>The function for the inventory turnover ratio  can be written as follows:</p>
</div></div>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_inventory_turnonver_ratio(cost_of_good_sold, average_inventory):
    "Calculates the inventory turnover ratio. You need to pass two values. The first one is the value for cost of goods sold and the average of inventory."
    inventory_turnover_ratio = cost_of_good_sold / average_inventory
    return(inventory_turnover_ratio)</pre>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h5 class="wp-block-heading">A function for the Receivable Turnover Ratio</h5>



<p>The function for the receivable turnover ratio  can be written as follows:</p>
</div></div>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_receivable_turnonver_ratio(net_credit_sales, average_account_receivables):
    "Calculates the receivable turnover ratio. You need to pass two values. The first one is the value for Net Credit Sales and the second value is for average account receivables."
    receivable_turnover_ratio = net_credit_sales / average_account_receivables
    return(receivable_turnover_ratio)</pre>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h5 class="wp-block-heading">A function for the Days In Sales Inventory Ratio</h5>



<p>The function for the days in sales inventory ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_day_sale_inventory_ratio(cost_of_good_sold, average_inventory):
    "Calculates the day sale inventory ratio. You need to pass two values. The first one is the value for cost of good sold and the second value is for average inventory."
    day_sale_inventory_ratio = 365 / \
        get_inventory_turnonver_ratio(cost_of_good_sold, average_inventory)
    return(day_sale_inventory_ratio)</pre>
</div></div>
</div></div>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h3 class="wp-block-heading">Functions for Profitability Ratios</h3>



<p>In this section, we will write the functions for the gross margin ratio, operating margin ratio, return on assets ratio, and the return on equity ratio.</p>



<h5 class="wp-block-heading">A function for the Gross Margin Ratio</h5>



<p>The function for the gross margin ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_gross_margin_ratio(gross_profit, net_sales):
    "Calculates the gross margin ratio. You need to pass two values. The first one is the value for gross profit and the second value is for net sales."
    gross_margin_ratio = gross_profit / net_sales
    return(gross_margin_ratio)</pre>



<h5 class="wp-block-heading">A function for the Operating Margin Ratio</h5>



<p>The function for the operating margin ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_operating_margin_ratio(operating_income, net_sales):
    "Calculates the operating margin ratio. You need to pass two values. The first one is the value for operating income and the second value is for net sales."
    operating_margin_ratio = operating_income / net_sales
    return(operating_margin_ratio)</pre>



<h5 class="wp-block-heading">A function for the Return On Assets Ratio</h5>



<p>The function for the return on assets ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_return_on_asset_ratio(net_income, total_assets):
    "Calculates the return on asset ratio. You need to pass two values. The first one is the value for net income and the second value is for total assets."
    return_on_assets_ratio = net_income / total_assets
    return(return_on_assets_ratio)</pre>



<h5 class="wp-block-heading">A function for the Return On Equity Ratio</h5>



<p>The function for the return on equity ratio  can be written as follows:</p>
</div></div>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_return_on_equity_ratio(net_income, shareholders_equity):
    "Calculates the return on equity ratio. You need to pass two values. The first one is the value for net income and the second value is for shareholders equity."
    return_on_equity_ratio = net_income / shareholders_equity
    return(return_on_equity_ratio)
</pre>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h3 class="wp-block-heading">Functions for Market Value Ratios</h3>



<p>In this section, we will write the functions for the book value per share ratio, dividend yield ratio, earning per share ratio, and price-earning ratio.</p>



<h5 class="wp-block-heading">A function for the Book Value Per Share Ratio</h5>



<p>The function for the book value per share ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_bookvalue__per_share_ratio(shareholders_equity, total_share_outstanding):
    "Calculates the Book Value per Share Ratio. You need to pass two values. The first one is the value for shareholders equity and the second value is for total shares outstanding."
    bookvalue_per_share_ratio = shareholders_equity / total_share_outstanding
    return(bookvalue_per_share_ratio)</pre>



<h5 class="wp-block-heading">A function for the Dividend Yield Ratio</h5>



<p>The function for the dividend yield ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_dividend_yield_ratio(dividend_per_share, share_price):
    "Calculates the Dividend Yield Ratio. You need to pass two values. The first one is the value for dividend per share and the second value is for the share price."
    dividend_yield_ratio = dividend_per_share / share_price
    return(dividend_yield_ratio)</pre>



<h5 class="wp-block-heading">A function for the Earning Per Share Ratio</h5>



<p>The function for the earning per share ratio  can be written as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_earning_per_share_ratio(net_earning, total_share_outstanding):
    "Calculates the Earning per Share Ratio. You need to pass two values. The first one is the value for net earning and the second value is for the total sahres outstanding."
    earning_per_share_ratio = net_earning / total_share_outstanding
    return(earning_per_share_ratio)</pre>



<h5 class="wp-block-heading">A function for the Price-Earning Ratio</h5>



<p>The function for the price-earning ratio  can be written as follows:</p>
</div></div>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_price_earning_ratio(share_price, earning_per_share):
    "Calculates the Price Earning Ratio. You need to pass two values. The first one is the value for share price and the second value is for the earning per share."
    price_earning_ratio = share_price / earning_per_share
    return(price_earning_ratio)</pre>



<h2 class="wp-block-heading">Final Thoughts</h2>



<p>I hope you found this exercise useful.  Please remember that there are other types of financial ratios that you can use, so feel free to expand this python module to your liking. </p>



<p>You can also practice using this python module by doing the exercise included in the article "<a href="https://notaprogrammer.com/liquidity-ratios-automate-calculations-10-steps/" class="rank-math-link">Automate the Calculation of Liquidity Ratios in 10 Simple Steps Using Python"</a>.</p>



<p>You can also create other python modules for other financial calculations, different ratios, or indicators. Here are some ideas:</p>



<ul class="wp-block-list">
<li>Create a Python module of key performance indicators</li>



<li>Create a Python module of financial formulas like present value, internal rate of return, etc.</li>



<li>Create a Python module of marketing indicators</li>



<li>Other repetitive calculations and tasks that can solve using Python modules and function</li>
</ul>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://notaprogrammer.com/use-custom-python-module-to-simplify-your-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
