开始使用免费开始使用

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.

本练习是课程的一部分

Financial Trading in R

查看课程

练习说明

  • 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.

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Get SPY from yahoo
getSymbols(___, 
           from = ___, 
           to = ___, 
           src =  ___, 
           adjust =  ___)

# Plot the closing price of SPY
___(___(___))
编辑并运行代码