The key here is to select a portfolio that minimizes the correlations between stocks, thus further minimizing risk. These are the top 30 stocks according to high growth and low risk. Here’s a sneak peek at the correlation analysis, which is useful in determining which pairs of stocks to select to minimize risk. Make sure you zoom in on sections of interest and hover over the dots to gain insights. The tool can be used to visualize stocks with good characteristics: High growth (large mean log returns), low variability (low standard deviation), and high number of samples (lots of days traded). Here’s a sneak peek at the interactive S&P500 stock screening visualization that compares stocks based on growth (reward), variability (risk), and number of samples (risk). And, the entire script runs in less than five minutes so you can begin screening stocks quickly. Whether you are a veteran trader or a newbie stock enthusiast, you’ll learn a useful workflow for modeling and managing massive data sets using the tidyverse packages.
Finally, the code that generates the plotly and corrplot visualizations is made available on GitHub for future stock screening. As a bonus we’ll investigate correlations to add diversification to your portfolio. At the end, you will have a visualization that compares the entire set of S&P500 stocks, and we’ll screen them to find those with the best future prospects on a quantitative basis.
I’ll show you how to use quantmod to collect daily stock prices and calculate log returns, rvest to web scrape the S&P500 list of stocks from Wikipedia, purrr to map functions and perform calculations on nested tibbles ( tidyverse data frames), and plotly to visualize risk versus reward and extract actionable information for use in your trading strategies. In this post, I analyze every stock in the S&P500 to screen in terms of risk versus reward. Yend = 0.Quantitative trading strategies are easy to develop in R if you can manage the data workflow.
Y = Return), data = min_var, color = 'red') + Title = "Portfolio Optimization & Efficient Frontier") + Scale_x_continuous(labels = scales::percent) + Scale_y_continuous(labels = scales::percent) + Ggplot(aes(x = Risk, y = Return, color = SharpeRatio)) + Three best performing stocks in the last decade.įinally lets plot all the random portfolios and visualize the efficient frontier. This portfolio has most of the assets invested in Amazon, Netflix and Apple. Not surprisingly, the portfolio with the highest sharpe ratio has very little invested in Exxon Mobil and AT&T. Scale_y_continuous(labels = scales::percent) Labs(x = 'Assets', y = 'Weights', title = "Minimum Variance Portfolio Weights") + Ggplot(aes(x = fct_reorder(Asset,Weights), y = Weights, fill = Asset)) + The tangency portfolio (the portfolio with highest sharpe ratio).Next lets look at the portfolios that matter the most. We have the weights in each asset with the risk and returns along with the Sharpe ratio of each portfolio. Next lets calculate the mean daily returns for each asset. # date_var, : Non-numeric columns being dropped: date # Using column `date` for date_var. Tk_xts() # Warning in tk_xts_.ame(data = data, select = select, date_var =
And we will also convert it into a time series object using xts() function. We will use the spread() function to convert it to a wide format. head(log_ret_tidy) # A tibble: 6 x 3Īs you can see that this data is in tidy format. Next lets select a few stocks to build our portfolios. Library(timetk) # To manipulate the data series Library(plotly) # To create interactive charts
First lets load our packages library(tidyquant) # To download the data