quantmod: examples :: intro (2024)

{ examples :: intro }

Introducing quantmod:

  1. Getting data
  2. Charting with quantmod
  3. Using the data to generate signals

It is possible with one quantmod function to load data froma variety of sources, including...

How you ask?

Getting data

> getSymbols("YHOO",src="google") # from google finance
[1] "YHOO"
> getSymbols("GOOG",src="yahoo") # from yahoo finance
[1] "GOOG"
> getSymbols("DEXJPUS",src="FRED") # FX rates from FRED
[1] "DEXJPUS"
> getSymbols("XPT/USD",src="Oanda") # Platinum from Oanda
[1] "XPTUSD"
Each call results in the data being loaded directly into your workspace,with the name of the object returned from the call.Sort of handy, but it gets better... > # Specify lookup parameters, and save for future sessions.
>
> setSymbolLookup(YHOO='google',GOOG='yahoo')
> setSymbolLookup(DEXJPUS='FRED')
> setSymbolLookup(XPTUSD=list(name="XPT/USD",src="oanda"))
> saveSymbolLookup(file="mysymbols.rda")
> # new sessions call loadSymbolLookup(file="mysymbols.rda")
>
> getSymbols(c("YHOO","GOOG","DEXJPUS","XPTUSD"))
[1] "YHOO" "GOOG" "DEXJPUS" "XPTUSD"
Now it's easy to load data from different sources into yourworkspace (or any other environment) without explicitly requiring assignment,or constantly remembering/specifying connection parameters.Think of it as a

load

command that can fetch data fromalmost anywhere. Try it yourself gettingdata.R

Charting with quantmod

Now that we have some data we may want to look at it. Enter thenew function chartSeries. At present this is a nice tool tovisualize financial time series in a way that many practicioners arefamiliar with - line charts, as well as OHLC bar and candle charts.There are convenience wrappers to these different styles (lineChart,barChart, and candleChart), thoughchartSeries does quite a bit to automatically handle data in themost appropriate way.

A quick look at how to create some charts, including some featuresand a look at what's coming infuture releases.> # Specify lookup parameters, and save for future sessions.
>
> getSymbols("AAPL",src="yahoo")
[1] "AAPL"
> barChart(AAPL)

Click to see the full chart:

> # Add multi-coloring and change background to white
> candleChart(AAPL,multi.col=TRUE,theme="white")

Click to see the full chart:

Non-OHLC and Volume series are handled automatically
> getSymbols("XPT/USD",src="oanda")
[1] "XPTUSD"
> chartSeries(XPTUSD,name="Platinum (.oz) in $USD")

Click to see the full chart:

Platinum, now weekly with custom color candlesusing the quantmod function to.weekly
> chartSeries(to.weekly(XPTUSD),up.col='white',dn.col='blue')

Click to see the full chart:

Try it yourself here: chartingwithquantmod.R

Technical analysis charting tools

As of version 0.3-0 one can now add technical analysisstudies from package TTRto the above chart. A detailed examples page will follow shortly, but here is a bit ofthe goodness:

Very nice technical functionality from the library by Josh Ulrich - onCRAN
> require(TTR)
> getSymbols("AAPL")
[1] "AAPL"
> chartSeries(AAPL)
> addMACD()
> addBBands()

Click to see the full chart:

Using the data to generate signals

Building models will mostly be left for a later example series, butfor those eager to continue wasting a Friday afternoon at work (when mostof my visitors seem to appear), I will continue.

Modelling in R is what R is about. Data feeds into this discussionmost prevelently due to the fact that much financial data is not contained in single data objects. Much,if not all, has to collected and aggregated by you, the modeller.

This is where pre-specifying data sources and connection parameterscomes in so handy. setSymbolLookupallows the modeller the opportunity to instruct quantmod to sourcedata - given a specific symbol - in a particular manner. When buildingmodels in R, often a formula is passed to the fitting functionalong with the appropriate data object to search.

To handle many different sources it is necessary to either createa data object with all the columns prespecified, OR to use objects visiblewithin the user's environment. Both have obvious drawbacks - not theleast of which is a reliance on the modeller to have manuallyloaded and aligned the series in question.

At the very best this istime consuming and certainly not very enlightening. At its worst it can be dangerousas data handling is inherently error-prone. Data errors in research can becostly, data errors in trading can quickly lead to a new career. That said, I will reemphasizethe terms of the LICENSE stating the COMPLETE LACK OF WARRANTY with regard to this software and all ofR for that matter. User beware!

To facilitate this relatively unique data issue, quantmoddynamically creates data objects for use within the modellingprocess, creating a model frame internally after going througha series of steps to identify the sources of data required - loading if necessary. specifyModel is the workhorse functionto handle all the data issues, and it's help file should be read tofully understand what is happening internally. For our purposes here,it is enough to know that one can specify ANY data within thecall to specifyModel, and quantmod will handle to lookup and dataaggregation for you. Of course the data has to be locatable and unique,but that was probably suspected.

Lets's take a look at an example of specifyModel: > # Create a quantmod object for use in
> # in later model fitting. Note there is
> # no need to load the data before hand.
>
> setSymbolLookup(SPY='yahoo',
+ VXN=list(name='^VIX',src='yahoo'))
>
> mm <- specifyModel(Next(OpCl(SPY)) ~ OpCl(SPY) + Cl(VIX))
>
> modelData(mm)

mm is now a quantmod object holding the model formulaand data structure implying the next (Next) period's open to closeof the S&P 500 ETF (OpCl(SPY)) is modelled as afucntion of the current period open to close and the current closeof the VIX (Cl(VIX)).

The call to modelData extracts the relevant data set, with transforms magicallyapplied. You can take the data and do with it as you'd like. A more directfunction to accomplish the same end is buildData.

What's next?

How about some examples of quantmod's data handling

This software is written and maintained by Jeffrey A. Ryan. Seelicense for details on copying and use. Copyright 2008.

quantmod: examples :: intro (2024)
Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5681

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.