Tuesday, March 28, 2017

Get a handle on financial portfolio analysis using R

I read a very illuminating article on R-Bloggers which was originally published on R-Curtiss Miller's Personal Website.

Ever since Microsoft SQL Server began supporting R in its 2016 version, I got interested in R language. It is extremely rich with wide applicability from Astronomy to Zoology and everything in between.

Financial information from Yahoo as the source, the package 'quantmod' brings with it most of the useful financial information related functions about stocks. quantmod gets data from Yahoo Finance and Google Finance plus data  from other sources.

In order to work with financial data you should download the package which can done as shown:
------------
> # Get quantmod
> if (!require("quantmod")) {
+     install.packages("quantmod")
+     library(quantmod)
+ }

Loading required package: quantmod
Installing package into ‘C:/Users/Jayaram/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
also installing the dependencies ‘xts’, ‘zoo’, ‘TTR’

[ I chose the CRAN site in California ]
trying URL 'https://cran.cnr.berkeley.edu/bin/windows/contrib/3.2/xts_0.9-7.zip'
Content type 'application/zip' length 662188 bytes (646 KB)
downloaded 646 KB

trying URL 'https://cran.cnr.berkeley.edu/bin/windows/contrib/3.2/zoo_1.7-14.zip'
Content type 'application/zip' length 905140 bytes (883 KB)
downloaded 883 KB

trying URL 'https://cran.cnr.berkeley.edu/bin/windows/contrib/3.2/TTR_0.23-1.zip'
Content type 'application/zip' length 432456 bytes (422 KB)
downloaded 422 KB

trying URL 'https://cran.cnr.berkeley.edu/bin/windows/contrib/3.2/quantmod_0.4-7.zip'
Content type 'application/zip' length 473601 bytes (462 KB)
downloaded 462 KB

package ‘xts’ successfully unpacked and MD5 sums checked
package ‘zoo’ successfully unpacked and MD5 sums checked
package ‘TTR’ successfully unpacked and MD5 sums checked
package ‘quantmod’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\Jayaram\AppData\Local\Temp\RtmpOIHqF6\downloaded_packages
Loading required package: xts
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘quantmod’
2: package ‘quantmod’ was built under R version 3.2.5
3: package ‘xts’ was built under R version 3.2.5
4: package ‘zoo’ was built under R version 3.2.5
5: package ‘TTR’ was built under R version 3.2.5

---------------
Once it is installed. You can set up start and end dates variables for viewing your stocks like in:
>start<- as.Date("2016-1-1")
>end <- as.Date("2016-10-1")


With these defined, you can get the Apple's stock price using its ticker symbol AAPL using the functions in the package as shown obtaining data from the Yahoo source.
> getSymbols("AAPL", src="yahoo", from=start, to=end)
To view the data just run the statement
>head(AAPL)
The result comes out quick as shown.


No comments:

DMCA.com Protection Status