Python Investing

Scott Andersen
4 min readDec 10, 2020

--

As an investor, it’s a good idea to diversify your portfolio with short term and long term investments. When you begin to analyze companies and determine which bucket a company falls in, you probably want to know the expected return on an annual basis.

In this article, I’d like to walk you through one example of how you can use python to quickly analyze the average daily return in different date ranges. In this case we will be analyzing semiconductor stocks.

  • 1 year
  • 3 year
  • 5 year

Import ‘Closing’ prices and Read in Data for Semiconductor companies: AMD, MU, ON

import pandas as pd
from pathlib import Path
%matplotlib inline
# Set the path
amd_path = Path('../Resources/amd_stock_data.csv')
mu_path = Path('../Resources/mu_stock_data.csv')
on_path = Path('../Resources/on_stock_data.csv')
# Read in the CSV as a DataFrame and set Index to Date column.amd_csv = pd.read_csv(amd_path,,index_col="Date", infer_datetime_format=True, parse_dates=True)mu_csv = pd.read_csv(mu_path,,index_col="Date", infer_datetime_format=True, parse_dates=True)on_csv = pd.read_csv(on_path,,index_col="Date", infer_datetime_format=True, parse_dates=True)

Note:

I like to rename the columns after I’ve imported them with Pandas by using the code below. This helps me to distinguish between each Close price.

amd_csv.rename(columns={"Close": "AMD"}, inplace=True)
mu_csv.rename(columns={"Close": "MU"}, inplace=True)
on_csv.rename(columns={"Close": "ON"}, inplace=True)

At this point I would recommend you print out the ‘ .head()’ for these companies to see what it looks like and make sure the code worked.

(If you are using a csv downloaded from Google Sheets the DataFrame will include 16:00:00 at the end of the date.)

Calculate Daily Returns:

mu_daily_returns = mu_csv.pct_change()
amd_daily_returns = amd_csv.pct_change()
on_daily_returns = on_csv.pct_change()

Next, we are going to select our ranges: 1-Year, 3-Year and 5-Years. You might notice ‘NaN’ values you can either ‘dropna’ or in this case just leave them.

Select Date Range:

# 1-Year Daily Returnsmu_daily_1year = mu_daily_returns.loc['2019-12-09':'2020-12-09']
amd_daily_1year = amd_daily_returns.loc['2019-12-09':'2020-12-09']
on_daily_1year = on_daily_returns.loc['2019-12-09':'2020-12-09']
# 3-Year Daily Returnsmu_daily_3year = mu_daily_returns.loc['2017-12-09':'2020-12-09']
amd_daily_3year = amd_daily_returns.loc['2017-12-09':'2020-12-09']
on_daily_3year = on_daily_returns.loc['2017-12-09':'2020-12-09']
# 5-Year Daily Returnsmu_daily_5year = mu_daily_returns.loc['2015-12-09':'2020-12-09']
amd_daily_5year = amd_daily_returns.loc['2015-12-09':'2020-12-09']
on_daily_5year = on_daily_returns.loc['2015-12-09':'2020-12-09']

Now we need to combine each range into one DataFrame so that we can visually compare the Daily Returns in one graph.

Year_1_Returns = pd.concat([mu_daily_1year,amd_daily_1year,on_daily_1year], axis="columns", join="inner")Year_3_Returns = pd.concat([mu_daily_3year,amd_daily_3year,on_daily_3year], axis="columns", join="inner")Year_5_Returns = pd.concat([mu_daily_5year,amd_daily_5year,on_daily_5year], axis="columns", join="inner")

Plot results for these ranges…

In our last step, we want to find the actual return written out by a percentage to make it easier to decide which company we want to invest in for the upcoming year. Then we will decide if one of these companies fit our desired return. To do this we will want to go back and look at the mean return for each timeframe.

Example:

mu_daily_1year.describe()
amd_daily_1year.describe()
on_daily_1year.describe()

For speed purposes, here are the totals…

# ON Semiconductor Corp.
1-Year Avg. Return = 0.002584 or 2.58%
3-Year Avg. Return = 0.001263 or 1.26%
5-Year Avg. Return = 0.001337 or 1.34%

# Advanced Micro Devices
1-Year Avg. Return = 0.004118 or 4.12%
3-Year Avg. Return = 0.003665 or 3.67%
5-Year Avg. Return = 0.003731 or 3.73%

# Micron Technology, Inc.
1-Year Avg. Return = 0.002340 or 2.34%
3-Year Avg. Return = 0.001211 or 1.21%
5-Year Avg. Return = 0.001741 or 1.74%

Based on the varying distributions of daily returns over 1-Year, 3-Year and 5-Year periods, it would appear at that trading AMD in the short-term would be the most profitable option.

Thanks for reading!

If you found this article useful, feel welcome to download my personal codes on GitHub. You can also email me directly at scottandersen23@gmail.com and find me on LinkedIn. Interested in learning more about data analytics, data science and machine learning applications? Follow me on Medium.

--

--