Playing Financial Data Series(1)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
These days I became interested in financial data, such as stock price, exchange rate and so on. Obviously there are a lot of available models to fit, analyze and predict these types of data. For instance, basic time series model arima(p,d,q), Garch model, and multivariate time series model such as VARX model, state space models. Although it is a little hard to propose a new and effective model in a short time, I believe that it is also meaningful to apply the existing models and methods to play the financial data. Probably some valuable conclusions will be found.
1 Obtaining data from Internet
It is extremely convenient to obtain the newest financial data from Internet via R. The package quantmod provides some useful functions which can get relevant data from google, yahoo and some other website. For example, view the following code
library(quantmod) getSymbols("GOOG",src="yahoo",from="2012-01-24",to="2014-01-24")
Then the stock data of google will be loaded into work space. These data including the open price, highest price, lowest price, close price, volume, adjusted price of the stock and the corresponding date.
> getSymbols("AAPL",src="yahoo",from="2014-01-20",to="2014-01-28"); [1] "AAPL" > AAPL AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted 2014-01-21 540.99 550.07 540.42 549.07 11733100 549.07 2014-01-22 550.91 557.29 547.81 551.51 13570900 551.51 2014-01-23 549.94 556.50 544.81 556.18 14401400 556.18 2014-01-24 554.00 555.62 544.75 546.07 15334100 546.07 2014-01-27 550.07 554.80 545.75 550.50 19817100 550.50
Making charts is as easy as getting data including basic charts and technical charts. Please run the following code
Sys.setlocale("LC_TIME","english"); dev.new(); barChart(GOOG,theme="white"); addBBands();
And a professional chart will be in R. In addition, the theme of the charts is also allowed to be modified via function chartTheme().
Besides getting data and making charts, another significant function of quantmod is basic data manipulation like calculating returns and percent change. The next step is to do data pre-processing, and it will be introduced in the next article.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.