Welcome!

Cognitive Computing Authors: Pat Romanski, Liz McMillan, David Linthicum, Elizabeth White, Yeshim Deniz

Blog Feed Post

Quantitative Finance Applications in R

by Daniel Hanson, QA Data Scientist, Revolution Analytics Extracting Financial Data from Internet Source Using R (first in a series) Earlier this month, a colleague and I attended a presentation on Computational Finance in R, given by Guy Yollin of the University of Washington Applied Mathematics faculty, at a meeting of the Seattle useR Group. The first among several topics was extracting financial time series from internet sources using two R packages: quantmod, and Quandl. This will be the subject of today’s blog post. For those of us who do not have access to a Bloomberg terminal but wish to have data to experiment with financial models, these two packages are a real boon.  While quantmod also contains plotting and charting functionality, we will look at its capability of downloading stock and index prices from Yahoo Finance and Google Finance.  The Quandl package is a bit different in that it is tied in with the Quandl website, a source of financial data itself, as well as a portal to economic and social science data.  We will focus on accessing financial futures data on Quandl. quantmod Many readers are likely familiar with the finance sites on Yahoo and Google as sources for tracking stock, mutual fund, and exchange traded fund (ETF) prices and returns.  Using quantmod, one can easily load this data into R by specifying the the same ticker symbol that is used in these two web sources.   As with any other R package, one must install the quantmod package in the usual way.  As an aside, included with the installation is also the xts package for time series data, which we will discuss briefly later on. After installing the package, using it is quite simple and straightforward.  For example, to retrieve all available data for the PowerShares QQQ Nasdaq 100 Index ETF, one enters the following R command: getSymbols("QQQ") The data set is returned to the R session in the form of an xts object with the name QQQ.  To check the contents, the head(.) and tail(.) R functions are useful: head(QQQ) gives us           QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume QQQ.Adjusted 2007-01-03    43.46    44.06   42.52     43.24  167689500        41.272007-01-04    43.30    44.21   43.15     44.06  136853500        42.052007-01-05    43.95    43.95   43.48     43.85  138958800        41.852007-01-08    43.89    44.12   43.64     43.88  106401600        41.882007-01-09    44.01    44.29   43.63     44.10  121577500        42.092007-01-10    43.96    44.66   43.82     44.62  121070100        42.58 and tail(QQQ)gives us           QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume QQQ.Adjusted 2013-12-12    85.21    85.42   84.90     84.96   33630800        84.962013-12-13    85.36    85.40   84.71     84.85   34072000        84.852013-12-16    85.27    85.81   85.21     85.32   24902500        85.322013-12-17    85.32    85.53   84.99     85.15   24411800        85.152013-12-18    85.25    86.22   84.05     86.14   68270800        86.142013-12-19    86.00    86.01   85.52     85.90   44964100        85.90 Now, a couple of remarks about what we have done, and other options that are available.  First, note that we are given daily prices.  In order to retrieve, say, monthly values, one may use an xts command on the return object; xts also provides functionality to extract a subset of the data based on a range of dates.  This is a discussion worth having, given that xts is widely used in financial modeling in R, but we will defer it to a later blog post.  Secondly, note that we did not specify the data source; this is because the default is Yahoo. So, as another example, and employing a little bit of irony, let’s download Yahoo’s stock price history, using Google Finance as the data source: # use single quotes and specify data source:getSymbols("YHOO", src = "google")  # src = "yahoo" is the default We can then extract the closing prices to an R vector: # coerce from an xts object to a standard numerical R vector:as.vector(YHOO[, "YHOO.Close"])   Finally, we can calculate the daily log returns in one fell swoop: log.yahoo <- log(yhoo.close[-1]> head(log.yahoo)[1]  0.047282921  0.032609594  0.006467863 -0.012252406  0.039806250  0.017271587> tail(log.yahoo)[1]  0.004840157  0.009610595  0.000000000 -0.005552765  0.013325150  0.003988041 Quandl One of the limitations of data available from Yahoo and Google, as may be noticed above, is that it only dates back to January of 2007, while fund tracking and analysis will often require a time series of 20 years or more of historical returns.  Furthermore, some of the index-tracking ETF’s didn’t come into existence until the early 2000’s, and new tracking funds for emerging markets and other indices have even shorter histories.  To get return data going back farther in time, it is common to look to futures markets, and Quandl provides a rich set of historical futures prices. As with the quantmod package, it is necessary to install the Quandl package in the same way.  The Quandl R package help  page gives step by step instructions, followed by a first example of loading futures price data into R, which we also show here: # Oil futures price data from the National Stock Exchange of India:mydata = Quandl("NSE/OIL") The default for the return object from Quandl is a standard R dataframe; an xts option is also available, but for this demonstration we will stick with a dataframe.  Checking the data, we get: > head(mydata)  Date       Open   High   Low    Last   Close   Quantity Turnover(Lacs)1 2013-12-12 462.00 470.65 458.50 461.00 460.05     622936      2903.942 2013-12-11 469.10 474.50 462.20 463.60 463.25     398041      1849.263 2013-12-10 472.00 479.85 472.00 474.30 473.90     476841      2279.755 2013-12-06 461.90 468.00 460.25 462.00 462.05     207715       960.856 2013-12-05 473.30 475.00 461.00 461.70 461.90     313943      1462.70 > tail(mydata)  Date       Open   High   Low    Last    Close  Quantity Turnover(Lacs)  2009-10-08 1145.0 1179.8 1142.0 1178.10 1170.20   788173      9138.99  2009-10-07 1153.8 1160.7 1140.0 1141.45 1141.60   698216      8032.98  2009-10-06 1149.8 1157.2 1132.1 1143.30 1144.90   627957      7185.90  2009-10-05 1152.0 1165.9 1136.6 1143.00 1140.55   919832     10581.13  2009-10-01 1102.0 1173.7 1102.0 1167.00 1166.35  3074254     35463.78  2009-09-30 1096.0 1156.7 1090.0 1135.00 1141.20 19748012    223877.07 This gives us a first example, but note that the data only goes back to September of 2009.  As a more realistic example, let’s look at futures prices that have a much longer record, and let’s specify that we want monthly rather than daily (the default setting) data.  A good example is the price of gold futures, but how would we find the lookup symbol?  There is an R search function in the Quandl package, but at least at the time of this writing, it exhibits, shall we say, some need for improvement.   We have found a more reliable approach at this stage is to start with the Futures section of the Quandl website, and then select Metals from the list that appears at left.  At the top of the Metals list that is now displayed, click on Gold (Symbol GC/Exchange COMEX).  A new Gold Futures page will load, with two contracts (GC1 and GC2) shown under Latest Quotes.  Choose Historical Data for GC2, and a page showing a plot of returns and price data will be displayed; however, we want to get the symbol to use in our R command, and to do this, scroll all the way down to the bottom of the page where a box entitled DATASET INFORMATION is displayed, as shown here: We can now get the lookup symbol from the end of the Permalink line, namely OFDP/FUTURE_GC2 and pop this into an R command to load the gold futures price data: # set collapse="monthly" to retrieve monthly pricesgold <- quandl("ofdp> tail(gold)      Date       Open  High  Low   Settle Volume Open Interest463 1975-05-31 169.1 169.5 168.2  169.0     NA          2539464 1975-04-30 169.6 169.8 168.9  168.9     NA           813465 1975-03-31 178.2 179.7 178.0  179.5     NA          2058466 1975-02-28 185.8 185.9 184.0  185.9     NA           504467 1975-01-31 178.0 178.4 177.0  178.0     NA          2016468 1974-12-31 195.4 195.4 185.7  185.7     NA           366 Although this example may seem a bit cumbersome, running through it a few times with different types of futures contracts and trying it out, the process of obtaining the lookup symbol eventually becomes somewhat routine. Finally, suppose we don’t want to go back to 1974, but instead we want a 20 year period of monthly prices from June 1992 through May 2012.  In this case, we simply put in start_date and end_date parameters as follows: gold20 <- quandl("ofdp> head(log.gold)[1]  0.063351108  0.003235085  0.024869116  0.015285152 -0.103421591  0.109093821 > tail(log.gold)[1]  0.00388234  0.01772572  0.02286972 -0.01180379  0.04002237 -0.03741933 In future posts, we will expand into topics of interest in quantitative finance and risk management.  In the meantime, for more examples of using Quandl data in R, take a look at Joseph Rickert’s mini-tutorial on Quandl.  

Read the original blog entry...

More Stories By David Smith

David Smith is Vice President of Marketing and Community at Revolution Analytics. He has a long history with the R and statistics communities. After graduating with a degree in Statistics from the University of Adelaide, South Australia, he spent four years researching statistical methodology at Lancaster University in the United Kingdom, where he also developed a number of packages for the S-PLUS statistical modeling environment. He continued his association with S-PLUS at Insightful (now TIBCO Spotfire) overseeing the product management of S-PLUS and other statistical and data mining products.<

David smith is the co-author (with Bill Venables) of the popular tutorial manual, An Introduction to R, and one of the originating developers of the ESS: Emacs Speaks Statistics project. Today, he leads marketing for REvolution R, supports R communities worldwide, and is responsible for the Revolutions blog. Prior to joining Revolution Analytics, he served as vice president of product management at Zynchros, Inc. Follow him on twitter at @RevoDavid

IoT & Smart Cities Stories
Intel is an American multinational corporation and technology company headquartered in Santa Clara, California, in the Silicon Valley. It is the world's second largest and second highest valued semiconductor chip maker based on revenue after being overtaken by Samsung, and is the inventor of the x86 series of microprocessors, the processors found in most personal computers (PCs). Intel supplies processors for computer system manufacturers such as Apple, Lenovo, HP, and Dell. Intel also manufactu...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Apptio fuels digital business transformation. Technology leaders use Apptio's machine learning to analyze and plan their technology spend so they can invest in products that increase the speed of business and deliver innovation. With Apptio, they translate raw costs, utilization, and billing data into business-centric views that help their organization optimize spending, plan strategically, and drive digital strategy that funds growth of the business. Technology leaders can gather instant recomm...
OpsRamp is an enterprise IT operation platform provided by US-based OpsRamp, Inc. It provides SaaS services through support for increasingly complex cloud and hybrid computing environments from system operation to service management. The OpsRamp platform is a SaaS-based, multi-tenant solution that enables enterprise IT organizations and cloud service providers like JBS the flexibility and control they need to manage and monitor today's hybrid, multi-cloud infrastructure, applications, and wor...
The Master of Science in Artificial Intelligence (MSAI) provides a comprehensive framework of theory and practice in the emerging field of AI. The program delivers the foundational knowledge needed to explore both key contextual areas and complex technical applications of AI systems. Curriculum incorporates elements of data science, robotics, and machine learning-enabling you to pursue a holistic and interdisciplinary course of study while preparing for a position in AI research, operations, ...
After years of investments and acquisitions, CloudBlue was created with the goal of building the world's only hyperscale digital platform with an increasingly infinite ecosystem and proven go-to-market services. The result? An unmatched platform that helps customers streamline cloud operations, save time and money, and revolutionize their businesses overnight. Today, the platform operates in more than 45 countries and powers more than 200 of the world's largest cloud marketplaces, managing mo...
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and Bi...
The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get tailored market studies; and more.