Building and Testing Stock Portfolios in R (2024)

Using data science to make smarter investment decisions

Building and Testing Stock Portfolios in R (1)

Published in

Towards Data Science

·

5 min read

·

Jun 23, 2020

--

Building and Testing Stock Portfolios in R (3)

In this article, we’ll examine how to get stock data, analyze it to make investment decisions, and visualize the results.

With the recent surge in retail investors entering the market, it’s more important than ever that new traders are armed with the tools they need to compare stocks by analyzing their performance over time. In this post, we’ll use the stock data of three familiar companies — Starbucks, Carnival, and Apple — to construct a portfolio, examine its historical performance, and compare it to the S&P 500.

Three very useful packages for financial analysis in R are quantmod , to pull stock data from Yahoo Finance; PerformanceAnalytics , to construct and test portfolios; and dygraphs , to produce interactive and informative visualizations of our data. If you don’t have these packages installed, you can install and load them into your R environment using the code below.

Once we have the packages installed and loaded, we can write a function to get monthly return data for our individual stocks. Our function takes two arguments: ticker , the stock’s symbol, and base_year , the year that we want to start analyzing the data.

This function can appear a bit complicated if you are unfamiliar with R and its packages. Here’s an explanation of each line:

  • Line 4: Passes our ticker symbol to the getSymbols() function to get our stock data from Yahoo Finance
  • Lines 5–8: Removes any missing values from the data and isolates the “Adjusted Price” column (the sixth column), which accounts for stock splits, dividends, and other corporate actions (more on that here)
  • Lines 10–12: Uses R’s built-in paste0() and Sys.Date()functions to create a string we can pass between brackets so that only observations between our base year and today’s date are selected
  • Line 15: Calculates monthly arithmetic returns for our adjusted closing stock price data
  • Line 18: Assigns our monthly return data to R’s global environment to ensure that we can access it by its ticker symbol later

After writing our function, it’s now available for us to calculate monthly returns for our three stocks. We need to call our function four times — once for each stock we want to analyze and once for the S&P 500 — so that we have a benchmark against which to judge each stock’s monthly performance. We can then merge all our monthly returns into one time series object and look at the last several years’ performance for each using the dygraphs package (check out the interactive version here). The last line prints the last five months of return data and its output is reproduced below.

Building and Testing Stock Portfolios in R (4)
Building and Testing Stock Portfolios in R (5)

From our returns data set, we can get a sense of how well each stock has performed relative to the S&P 500 over the last several years. For example, when news of the spread of COVID-19 was roiling financial markets in March 2020, the S&P 500 recorded a loss of about 12.5% while Carnival lost more than 60% of its value for that month. Apple, by contrast, experienced a loss of only about 7%. Using corrplot::corrplot(cor(returns), method = number) generates a correlation matrix to indicate how these stocks’ returns are related to each other.

Building and Testing Stock Portfolios in R (6)

A fundamental principle of portfolio management is that you should select stocks with low correlations to each other. You wouldn’t want all the stocks in your portfolio to always rise and fall together — that could expose you to excess volatility that you may want to avoid, especially if this is a retirement account where preservation of principle is your main concern. A portfolio made up of highly correlated stocks is subject to unsystematic risk stemming from the firm-specific risks inherent to each stock.

From our correlation matrix, we observe that all of our stocks are positively correlated, albeit in varying degrees. Apple is only weakly correlated with Starbucks (0.27), but Carnival and the S&P 500 have a high correlation (0.71) with each other. Importantly, all of our stocks have a fairly high positive correlation with the market, meaning they tend to move with the market most months.

We can use the PerformanceAnalytics package to assign weights to our stocks and build a hypothetical portfolio from them. In the following code, we assume that we are investing one-third of our money in Starbucks, one-third in Carnival, and one-third in Apple, excluding the S&P 500 for the moment. The Return.portfolio() function allows us to pass in our individual stock data from the returns object along with their weights. We can set the wealth.index argument equal to TRUE to show how $1 invested in our portfolio in 2015 would have grown over time. Then, we can follow the same process for the S&P 500, excluding the weights argument. After merging our data into one xts object, we can contrast our portfolio with the S&P 500 using another graph (interactive version here):

Building and Testing Stock Portfolios in R (7)

If we had invested $1 into our Starbucks-Carnival-Apple portfolio at the beginning of 2015 and didn’t touch it (i.e., no re-balancing), our portfolio’s value would have almost doubled (92.31%). This beat the performance of the S&P 500, which still yielded an impressive return of about 73% over the same time horizon. Of course, we could make some minor tweaks to our code to change portfolio weights, add additional stocks (or other assets like government bonds and precious metals), and experiment with re-balancing. With R and its libraries, our ability to construct and test portfolios is virtually unlimited.

I hope I’ve made the process of getting and analyzing stock data a little less intimidating. With the code above, we were able to import a large amount of financial information, construct a portfolio, examine its composition, and analyze historical performance relative to a benchmark. In future posts, I’ll explore some other ways we can use R and its libraries to analyze financial data.

Thanks for reading!

As a seasoned data science enthusiast with a focus on financial analysis, I bring forth a wealth of experience in utilizing data science techniques to make informed investment decisions. My expertise extends to the practical application of data science tools, particularly in the realm of stock analysis and portfolio management.

In the article titled "Using data science to make smarter investment decisions" by Christian Kincaid, the author delves into the process of obtaining stock data, analyzing it for investment decisions, and visualizing the results. The key concepts covered in the article include:

  1. Data Collection and Analysis Packages:

    • The author highlights the use of three essential packages in R for financial analysis: quantmod for pulling stock data from Yahoo Finance, PerformanceAnalytics for constructing and testing portfolios, and dygraphs for producing interactive visualizations.
  2. Monthly Return Data Function:

    • A custom function is introduced to obtain monthly return data for individual stocks. This function takes the stock's symbol (ticker) and the starting year for analysis (base_year) as arguments.
  3. Portfolio Performance Comparison:

    • The article emphasizes the importance of comparing stock performance against a benchmark, in this case, the S&P 500. The author demonstrates how to merge monthly returns for selected stocks and the S&P 500 into a time series object.
  4. Correlation Analysis:

    • The correlation matrix is employed to assess how the returns of different stocks are related to each other. The article underscores the significance of selecting stocks with low correlations to manage portfolio risk.
  5. Portfolio Construction and Testing:

    • The article uses the PerformanceAnalytics package to assign weights to individual stocks and build a hypothetical portfolio. The performance of this portfolio is compared to the S&P 500 over a specified time horizon.
  6. Portfolio Visualization:

    • Interactive graphs generated using the dygraphs package provide visual insights into the historical performance of the constructed portfolio and its comparison with the S&P 500.
  7. Portfolio Growth and Flexibility:

    • The author showcases the flexibility of the approach by adjusting weights, adding stocks, or experimenting with re-balancing. The presented R code allows for virtually unlimited possibilities in constructing and testing portfolios.

In essence, the article guides readers through the process of leveraging data science tools in R to make informed investment decisions, showcasing the practicality and versatility of the presented approach. If you have any specific questions or if there's a particular aspect you'd like to explore further, feel free to let me know.

Building and Testing Stock Portfolios in R (2024)

FAQs

Should I be 100 percent in stocks? ›

New paper suggests a portfolio of 100% stocks is better, even in retirement. The paper suggests the volatility fears of relying on stocks in retirement is overrated and outweighed by their consistently higher returns over bonds. Bonds also tend to get smashed at the same time as stocks, but take way longer to recover.

How do I know if my portfolio is doing well? ›

Relative performance — Comparing your return to the overall market is a better measure. If your total portfolio is up 20% for the year and the overall market is only up 15%, you have done very well. Or if your portfolio is down 10% and the overall market is down 15%, you have done well.

How do you build a good stock portfolio? ›

How to Build a Stock Portfolio in the Stock Market
  1. Your goals. Determining your goals is the first step to creating a stock portfolio. ...
  2. Asset allocation. Once you've determined what your goals are, the next step is to allocate assets accordingly. ...
  3. Diversification.

How do I start an investment portfolio? ›

Starting an investment portfolio
  1. Identify your investing goals.
  2. Weigh your comfort with investment risk.
  3. Understand your investment time horizon.
  4. Agree on an optimal portfolio mix.
  5. Ensure proper portfolio diversification.

Is owning 30 stocks too much? ›

So if you're looking to build a collection of 45 stocks, you'll have to do research 45 times over. For some context, the Motley Fool recommends owning at least 25 different stocks and says the average diversified portfolio contains between 20 and 30 stocks.

Is 20 stocks too much? ›

It's a lot easier to track 15 to 20 high-quality stocks than a large basket of 50 to 100 stocks. It's true that you shouldn't put all your eggs in one basket. But that doesn't mean you should own all the eggs out there. Diversification is good, but too much of it can be bad.

What does a well balanced stock portfolio look like? ›

Typically, balanced portfolios are divided between stocks and bonds, either equally or with a slight tilt, such as 60% in stocks and 40% in bonds. Balanced portfolios may also maintain a small cash or money market component for liquidity purposes.

Should I compare my portfolio to S&P 500? ›

Don't simply look at the S&P 500 Index

And for a diversified portfolio that might include international investments and other asset classes such as bonds, commodities and cash, it provides little guidance. An appropriate benchmark should reflect your portfolio's risk level and allocation.

What should my stock portfolio look like at 30? ›

The old rule about the best portfolio balance by age is that you should hold the percentage of stocks in your portfolio that is equal to 100 minus your age. So a 30-year-old investor should hold 70% of their portfolio in stocks. This should change as the investor gets older.

How many stocks should a beginner portfolio have? ›

Assuming you do go down the road of picking individual stocks, you'll also want to make sure you hold enough of them so as not to concentrate too much of your wealth in any one company or industry. Usually this means holding somewhere between 20 and 30 stocks unless your portfolio is very small.

How many stocks is a good portfolio? ›

What's the right number of companies to invest in, even if portfolio size doesn't matter? “Studies show there's statistical significance to the rule of thumb for 20 to 30 stocks to achieve meaningful diversification,” says Aleksandr Spencer, CFA® and chief investment officer at Bogart Wealth.

How much money do you need to build a portfolio? ›

It is possible to start a thriving portfolio with an initial investment of just $1,000, followed by monthly contributions of as little as $100. There are many ways to obtain an initial sum you plan to put toward investments.

What are the 7 steps of portfolio process? ›

Steps of Portfolio Management
  • Step 1: Identifying the objective. An investor needs to identify the objective. ...
  • Step 2: Estimating capital markets. ...
  • Step 3: Asset Allocation. ...
  • Step 4: Formulation of a Portfolio Strategy. ...
  • Step 5: Implementing portfolio. ...
  • Step 6: Evaluating portfolio.
Oct 12, 2023

What is the best stock to buy for beginners? ›

Best Stocks To Invest In 2024 For Beginners
  • UnitedHealth Group Incorporated (NYSE:UNH) Number of Hedge Fund Holders: 104. Quarterly Revenue Growth: 14.10% ...
  • JPMorgan Chase & Co. (NYSE:JPM) Number of Hedge Fund Holders: 109. ...
  • Advanced Micro Devices, Inc. (NASDAQ:AMD) ...
  • Adobe Inc. (NASDAQ:ADBE) ...
  • Salesforce, Inc. (NYSE:CRM)
Feb 7, 2024

How do you build assets with little money? ›

7 easy ways to start investing with little money
  1. Workplace retirement account. If your investing goal is retirement, you can take part in an employer-sponsored retirement plan. ...
  2. IRA retirement account. ...
  3. Purchase fractional shares of stock. ...
  4. Index funds and ETFs. ...
  5. Savings bonds. ...
  6. Certificate of Deposit (CD)
Jan 22, 2024

What is a good percentage for stocks? ›

The common rule of asset allocation by age is that you should hold a percentage of stocks that is equal to 100 minus your age. So if you're 40, you should hold 60% of your portfolio in stocks. Since life expectancy is growing, changing that rule to 110 minus your age or 120 minus your age may be more appropriate.

What percentage should be in stocks? ›

Calculating How Much to Invest

A common rule of thumb is the 50-30-20 rule, which suggests allocating 50% of your after-tax income to essentials, 30% to discretionary spending and 20% to savings and investments. Within that 20% allocation, the portion designated for stocks depends on your risk tolerance.

What percentage of stocks should I have? ›

If you wish moderate growth, keep 60% of your portfolio in stocks and 40% in cash and bonds. Finally, adopt a conservative approach, and if you want to preserve your capital rather than earn higher returns, then invest no more than 50% in stocks.

Why not 100 percent stocks? ›

For example, they found that a strategy of investing 50% in domestic stocks and 50% in international stocks throughout one's lifetime generated more wealth at retirement, provided higher initial retirement consumption, was less likely to exhaust savings, and was more likely to leave a large inheritance.

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5335

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.