Plotting financial data
Trading strategies developed using quantstrat
contain several characteristics, including indicators developed from market data, signals triggered by certain combinations of indicators, and rules acted on by certain signals. The first step in developing any trading system is to obtain market data, and maybe even examine how it looks.
As you saw in the video, the quantmod package has a function to obtain data from various sources. This is the getSymbols() command, which returns an object with the same name as the symbol.
In this exercise, you will obtain data for SPY, an exchange traded fund (ETF) that tracks the top 500 companies in the United States by market cap. This data is from Yahoo! Finance, which is a sufficient source of data for strategies that do not require instantaneous "see the close, buy the close" execution. You will then plot it and add a trendline to it.
For example, to obtain adjusted data for SPY in 2013 from Yahoo! Finance and then plot the maximum values traded each day, you would run the following code. Notice how only the first reference to the SPY data is enclosed in quotation marks.
getSymbols("SPY",
from = "2013-01-01",
to = "2013-12-31",
src = "yahoo",
adjust = TRUE)
plot(Hi(SPY))
The quantmod
package has been loaded for you.
This exercise is part of the course
Financial Trading in R
Exercise instructions
- Use
getSymbols()
to obtain adjusted data for SPY from January 1, 2000 through June 30, 2016 from Yahoo! Finance. - Use
Cl()
to plot the closing price of SPY.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Get SPY from yahoo
getSymbols(___,
from = ___,
to = ___,
src = ___,
adjust = ___)
# Plot the closing price of SPY
___(___(___))