Original link:tecdat.cn/?p=16453
Original source:Tuo End number according to the tribe public number
One of the most important tasks in financial markets is to analyze the historical returns of various investments. To perform this analysis, we need historical data for the asset. There are many data providers, some free, most paid. In this article, we will use data from the Yahoo Finance website.
In this article, we will:
- Download closing price
- Calculated rate of return
- Calculate the mean and standard deviation of returns
Let’s load the library first.
library(tidyquant)
library(timetk)
Copy the code
We’re going to get the closing price of Netflix.
netflix <- tq_get("NFLX",
from = '2009-01-01',
to = "2018-03-01",
get = "stock.prices")
Copy the code
Next, we plot the adjusted closing price of Netflix.
netflix %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
ggtitle("Netflix since 2009") +
labs(x = "Date", "Price") +
scale_x_date(date_breaks = "years", date_labels = "%Y") +
labs(x = "Date", y = "Adjusted Price") +
theme_bw()
Copy the code
Calculate the daily and monthly returns of individual stocks
Once we download the closing price from Yahoo Finance, the next step is to calculate the revenue. We will use the TidyQuant package again for the calculations. We’ve downloaded Netflix pricing data above, see the section above if you haven’t already.
Netflix_daily_returns < -netflix %>% tq_transmute(select = adjusted, mutate_fun = periodReturn, # this specifies how to process the column period = "daily", Col_rename = "nflx_RETURNS ") # Rename column # Calculate monthly returns netflix_monthly_RETURNS < -Netflix %>% tq_transmute(select = Adjusted, mutate_fun = periodReturn, period = "monthly", # Col_rename = "nflx_returns")Copy the code
Chart Netflix’s daily and monthly earnings
Ggplot (AES (x = date, Y = nflx_RETURNS)) + geom_line() + theme_classic() +Copy the code
Looking at Netflix’s daily earnings chart, we can conclude that earnings are very volatile and the stock can move +/ -5% on any given day. To understand the distribution of returns, we can plot a histogram.
Netflix_daily_returns %>% ggplot(AES (x = nflX_RETURNS)) + GEOM_histogram (binWidth = 0.015) + theme_classic() +Copy the code
Next, we can plot Netflix’s monthly returns since 2009. We use bar charts to plot data.
# Chart Netflix's monthly earnings. Use the bar graph GGplot (AES (x = date, y = nflx_RETURNS)) + geom_bar(stat = "identity") + theme_classic() +Copy the code
Calculate the cumulative return on Netflix stock
Plotting daily and monthly returns is useful for understanding the daily and monthly fluctuations of investments. To calculate the growth of an investment, in other words, the total return on an investment, we need to calculate the cumulative return on that investment. To calculate the cumulative returns, we will use the cumprod () function.
Mutate (cr = cumprod(1 + nflx_returns)) %>% #Copy the code
ggplot(aes(x = date, y = cumulative_returns)) +
geom_line() +
theme_classic() +
Copy the code
The chart shows Netflix’s cumulative earnings since 2009. With the power of post-mortem, it has made $85 on a $1 investment since 2009. But as far as we know, that’s easier said than done. Over a decade or so, investments lost 50 per cent of their value during the Qwickster fiasco. During this period, few investors can stick with their money.
ggplot(aes(x = date, y = cumulative_returns)) +
geom_line() +
theme_classic() +
Copy the code
We can see visually that the monthly income statement is much smoother than the daily chart.
Multiple stocks
Download stock market data for multiple stocks.
Tickers < -c ("FB", "AMZN", "AAPL", "NFLX", "GOOG")Copy the code
Plot the price of multiple stocks
Next, we’ll chart the price of a number of stocks
multpl_stocks %>%
ggplot(aes(x = date, y = adjusted,
Copy the code
This is not what we expected. Because these stocks have large price differentials (FB below 165, AMZN above 1950), they differ in size. We can overcome this problem by plotting stocks in their respective y proportions.
Facet_wrap (~symbol, scales = "free_y") + # facet_wrap Used to make different pages theme_classic() +Copy the code
Calculate the returns of multiple stocks
It is as easy to calculate the returns of multiple stocks as it is of individual stocks. There is only one additional parameter to pass. We need to use the parameter group_BY (symbol) to calculate the return of an individual stock.
Tq_transmute (select = adjusted, period = periodReturn, period = 'daily', Tq_transmute (select = adjusted, mutate_fun = periodReturn, period = 'monthly', col_rename = 'returns')Copy the code
Chart the returns of multiple stocks
Once you have a payoff calculation, you can plot the payoff on a graph.
multpl_stock_daily_returns %>%
ggplot(aes(x = date, y = returns)) +
geom_line() +
geom_hline(yintercept = 0) +
Copy the code
Multpl_stock_monthly_returns %>% ggplot(AES (x = date, y = return scale_fill_brewer(Palette = "Set1", # We will give them a different color instead of blackCopy the code
Among FAANG stocks, Apple was the least volatile, while Facebook and Netflix were the most volatile. This is obvious for the business they are in. Apple is a stable company with stable cash flow. Its products are loved and used by millions of people who have great loyalty to Apple. Netflix and Facebook are also incredible businesses, but they’re in a high-growth phase, and any issues (a drop in revenue or subscriber growth) could have a big impact on the stock.
Calculate the cumulative returns of multiple stocks
In general, we want to see which investments have worked best in the past. To do this, we can calculate the cumulative results. Below we compare the results of all FAANG stocks since 2013. What has been the best investment since 2013?
multpl_stock_monthly_returns %>%
mutate(returns e_returns = cr - 1) %>%
ggplot(aes(x = date, y = cumulative_returns, color = symbol)) +
geom_line() +
labs(x = "Date"
Copy the code
Not surprisingly, Netflix had its highest earnings since 2013. Amazon and Facebook were second and third.
statistics
Calculate the mean and standard deviation of individual stocks
We already have the daily and monthly revenue figures for Netflix. Now we will calculate the daily and monthly average and standard deviation of returns. For this, we will use the mean () and sd () functions.
[[1]] %>% mean(na.rm = TRUE) nflx_Monthly_mean_ret < -netFL Turns) %>%.[[1]] %>% mean(na.rm = TRUE) # nflx_daily_sd_ret <- netflirns) %>% .[[1]] %>% sd() nflx_monthly_sd_ret <- netflix_rns) %>% .[[1]] %>% sd() nflx_statCopy the code
## # A tibble: 2 x 3 ## period mean SD ## < CHR > < DBL > < DBL > ## 1 Daily 0.00240 0.0337 ## 2 Monthly 0.0535 0.176Copy the code
We can see Netflix’s average daily earnings of 0.2% and standard deviation of 3.3%. Its monthly average return is 5.2% and its standard deviation is 17%. The data cover the entire period since 2009. What if we wanted to calculate the mean and standard deviation from year to year. We can do this by grouping Netflix revenue data by year and performing calculations.
netflix %>%
summarise(Monthly_Mean_Returns = mean(nflx_returns),
MOnthly_Standard_Deviation = sd(nflx_returns)
Copy the code
## # A tibble: 10 x 3 ## year Monthly_Mean_Returns MOnthly_Standard_Deviation ## < DBL > < DBL > < DBL > ## 1 2009 0.0566 0.0987 ## 2 2010 0.110 0.142 ## 3 2011 -0.0492 0.209 ## 4 2012 0.0562 0.289 ## 5 2013 0.137 0.216 ## 6 2014 0.00248 0.140 ## 7 2015 0.0827 0.148 ## 8 2016 0.0138 0.126 ## 9 2017 0.0401 0.0815 ## 10 2018 0.243 0.233Copy the code
And we can plot the results to make sense.
netflix_monthly_returns %>%
mutate(year = rns, Standard_Deviation, keyistic)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(b ) +
theme_bw() +
Copy the code
As we can see, monthly returns and standard deviations have fluctuated widely since 2009. In 2011, the average monthly yield was minus 5%.
Calculate the mean and standard deviation of multiple stocks
Next, we can calculate the mean and standard deviation of multiple stocks.
group_by(symbol) %>%
summarise(mean = mean(returns),
sd = sd(returns))
Copy the code
## # A tibble: 5 x 3 ## symbol mean sd ## < CHR > < DBL > < DBL > ## 1 AAPL 0.00100 0.0153 ## 2 AMZN 0.00153 0.0183 ## 3 FB 0.00162 0.0202 ## 4 NFLX 0.00382 0.0200 ## 5 NFLX 0.00382 0.0300Copy the code
group_by(symbol) %>%
summarise(mean = mean(returns),
sd = sd(returns))
Copy the code
## # A tibble: 5 x 3 ## symbol mean sd ## < CHR > < DBL > < DBL > ## 1 AAPL 0.0213 0.0725 ## 2 AMZN 0.0320 0.0800 ## 3 FB 0.0339 0.0900 ## 4 NFLX 0.0614 0.157Copy the code
Calculate the annual mean and standard deviation of returns.
%>%
group_by(symbol, year) %>%
summarise(mean = mean(returns),
sd = sd(returns))
Copy the code
## # A tibble: 30 x 4 ## # Groups: symbol [?] ## symbol year mean SD ## < CHR > < DBL > < DBL > < DBL > ## 1 AAPL 2013 0.0210 0.0954 ## 2 AAPL 2014 0.0373 0.0723 ## 3 AAPL 2015-303030 0.0629 ## 4 AAPL 2016 0.0125 0.0752 ## 5 AAPL 2017 0.0352 0.0616 ## 6 AAPL 2018 0.0288 0.0557 ## 7 AMZN 2013 AMZN 2014 -0.0734 0.0734 ## 9 AMZN 2015 0.0734 0.0734 ## 9 AMZN 2016 0.0734 0.0734 ##... with 20 more rowsCopy the code
We can also plot this statistic.
Multpl_stock_monthly_returns %>% mutate(year = year(date)) %>% group_by(symbol, yea s = seq(-0.1,0.4,0.02), Labels = scales::percent) + scale_x_continuous(breaks = seq(2009,2018,1)) + LABS (x = "Year", y = Stocks") + ggtitleCopy the code
multpl_stock_monthly_returns %>% mutate(year = year(date)) %>% ggplot(aes(x = year, y = sd, fill = symbol)) + geom_bar(stat = "identity", position = "dodge", Width = 0.7) + scale_y_continuous(breaks = seq(-0.1,0.4,0.02), labels = scales::p scale_fill_brewer(Palette = "Set1", labels = scales:: P scale_fill_brewer)Copy the code
Calculate covariance and correlation of multiple stocks
Another important statistical calculation is the correlation and covariance of stocks. To calculate these statistics, we need to modify the data. We convert it to an XTS object.
Covariance table
Tk_xts (silent = TRUE) %>% cov()Copy the code
## AAPL AMZN FB GOOG NFLX ## AAPL AMZN FB GOOG NFLX ## AAPL AMZN FB GOOG NFLX ## AAPL AMZN FB GOOG NFLX # 0.006399439 0.001418561 0.0028531565 4.754894e-03 ## FB 6.998180e-04 0.001418561 0.008091594 0.0013566480 3.458228e-03 ## nflx-1.528193e-05 0.004754894 0.00345833 ## nflx-1.528193e-05 0.004754894 0.00345833 ## nflx-1.528193e-05 0.004754894 0.00345833 0.0035292451 2.464202 e-02Copy the code
Related tables
% tk_xts(silent = TRUE) %>% cor()Copy the code
## AAPL AMZN FB GOOG NFLX ## AAPL 1.000000000 0.2566795 0.1073230 0.1801471-0.001342964 ## AMZN 0.256679539 1.0000000 0.1971334 0.6276759 0.378644485 ## FB 0.107322952 0.1971334 1.0000000 0.2654184 0.244905437 ## GOOG 0.180147089 0.6276759 0.2654184 1.0000000 0.395662114 ## nflX-0.001342964 0.3786445 0.2449054 0.3956621 1.000000000Copy the code
We can use the corrplot() package to plot the correlation matrix.
# # corrplot 0.84 the loadedCopy the code
cor() %>%
corrplot()
Copy the code
Most welcome insight
1. Machine learning to identify changing stock market conditions — the application of hidden Markov model (HMM)
2. Garch-dcc model and DCC (MVT) modeling estimation in R language
3.R language implementation Copula algorithm modeling dependency case analysis report
4.R language COPULAS and VaR analysis of financial time series data
5.R language multivariate COPULA GARCH model time series prediction
6. Use R language to realize neural network to predict stock cases
7. Realization of R language volatility prediction: ARCH model and HAR-RV model
8.R language how to do Markov switching model
9. Matlab uses Copula simulation to optimize market risks