# NOT RUN {
# Construct instruments for several different asset classes
# Define a currency and some stocks
require("FinancialInstrument")
currency(c("USD", "EUR")) # define some currencies
stock(c("SPY", "LQD", "IBM", "GS"), currency="USD") # define some stocks
exchange_rate("EURUSD") # define an exchange rate
ls_stocks() #get the names of all the stocks
ls_instruments() # all instruments
getInstrument("IBM")
update_instruments.yahoo(ls_stocks())
update_instruments.TTR(ls_stocks()) # doesn't update ETFs
update_instruments.masterDATA(ls_stocks()) # only updates ETFs
getInstrument("SPY")
## Compare instruments with all.equal.instrument method
all.equal(getInstrument("USD"), getInstrument("USD"))
all.equal(getInstrument("USD"), getInstrument("EUR"))
all.equal(getInstrument("SPY"), getInstrument("LQD"))
## Search for the tickers of instruments that contain words
find.instrument("computer") #IBM
find.instrument("bond") #LQD
## Find only the ETFs; update_instruments.masterDATA added a "Fund.Type" field
## to the ETFs, but not to the stocks
ls_instruments_by("Fund.Type") # all instruments that have a "Fund.Type" field
# build data.frames with instrument attributes
buildHierarchy(ls_stocks(), "Name", "type", "avg.volume")
## before defining a derivative, must define the root (can define the underlying
## in the same step)
future("ES", "USD", multiplier=50, tick_size=0.25,
underlying_id=synthetic("SPX", "USD", src=list(src='yahoo', name='^GSPC')))
# above, in addition to defining the future root "ES", we defined an instrument
# named "SPX". Using the "src" argument causes setSymbolLookup to be called.
# Using the "src" arg as above is the same as
# setSymbolLookup(SPX=list(src='yahoo', name='^GSPC'))
getSymbols("SPX") # this now works even though the Symbol used by
# getSymbols.yahoo is "^GSPC", not "SPX"
## Back to the futures; we can define a future_series
future_series("ES_U2", identifiers=list(other="ESU2"))
# identifiers are not necessary, but they allow for the instrument to be found
# by more than one name
getInstrument("ESU2") #this will find the instrument even though the primary_id
#is "ES_U2"
# can also add indentifiers later
add.identifier("ES_U2", inhouse="ES_U12")
# can add an arbitrary field with instrument_attr
instrument_attr("ES_U2", "description", "S&P 500 e-mini")
getInstrument("ES_U2")
option_series.yahoo("GS") # define a bunch of options on "GS"
# option root was automatically created
getInstrument(".GS")
# could also find ".GS" by looking for "GS", but specifiying type
getInstrument("GS", type='option')
# if you do not know what type of instrument you need to define, try
instrument.auto("ESM3")
getInstrument("ESM3")
instrument.auto("USDJPY")
getInstrument("USDJPY")
instrument.auto("QQQ") #doesn't work as well on ambigous tickers
getInstrument("QQQ")
# Some functions that make it easier to work with futures
M2C() # Month To Code
M2C()[5]
M2C("may")
C2M() # Code To Month
C2M("J")
C2M()[7]
MC2N("G") # Month Code to Numeric
MC2N("H,K,M")
parse_id("ES_U3")
parse_id("EURUSD")
next.future_id("ES_U2")
next.future_id("ZC_H2", "H,K,N,U,Z")
prev.future_id("CL_H2", 1:12)
sort_ids(ls_instruments()) # sort by expiration date, then alphabetically for
# things that don't expire.
format_id("ES_U2", "CYY")
format_id("ES_U2", "CYY", sep="")
format_id("ES_U2", "MMMYY")
## Saving the instrument environment to disk
tmpdir <- tempdir()
saveInstruments("MyInstruments.RData", dir=tmpdir)
rm_instruments(keep.currencies=FALSE)
ls_instruments() #NULL
loadInstruments("MyInstruments.RData", dir=tmpdir)
ls_instruments()
unlink(tmpdir, recursive=TRUE)
#build a spread:
fn_SpreadBuilder(getSymbols(c("IBM", "SPY"), src='yahoo'))
head(IBM.SPY)
getInstrument("IBM.SPY")
# alternatively, define a spread, then build it
spread(members=c("IBM", "GS", "SPY"), memberratio=c(1, -2, 1))
buildSpread("IBM.GS.SPY") #Since we hadn't yet downloaded "GS", buildSpread
#downloaded it temporarily
chartSeries(IBM.GS.SPY)
## fn_SpreadBuilder will return as many columns as it can
## (Bid, Ask, Mid, or Op, Cl, Ad), but only works on 2 instrument spreads
## buildSpread works with any number of legs, but returns a single price column
getFX("EUR/USD", from=Sys.Date()-499) # download exchange rate from Oanda
IBM.EUR <- redenominate("IBM", "EUR") #price IBM in EUR instead of dollars
chartSeries(IBM, subset='last 500 days', TA=NULL)
addTA(Ad(IBM.EUR), on=1, col='red')
# }
Run the code above in your browser using DataLab