twsInstrument-package: twsInstrument package: Integrates IBrokers and...

Description Details Author(s) References Examples

Description

Easily create twsContract and instrument objects Includes functions to make requesting historical data (esp. TBBO data) from IB easier defines stocks from vector of ticker symbols, a csv file, or a data.frame (package includes dataset)

Details

Mostly just wrappers for IBrokers and FinancialInstrument See ?buildIBcontract for the twsInstrument function help page

Package: twsInstrument
Type: Package
Version: 1.0-13
Depends: FinancialInstrument, IBrokers, quantmod
Date: 2011-05-27
License: GPL-3
LazyLoad: yes

An instrument object that has a slot called IB containing a twsContract will be classed as twsInstrument.

The twsInstrument function makes it easy to create twsContracts (for the IBrokers package) or instruments (for the FinancialInstrument package) or both. You can give it a twsContract, a twsInstrument, an instrument, the name of an instrument, or an Interactive Brokers conId, and get back an instrument, a twsContract, or both, updated with information from the other. Most functions have an addIBslot to control whether to add a slot with the twsContract (and add twsInstrument class) to the instrument you're dealing with.

update_instruments.yahoo can be called with a vector of instrument names or with ‘all’ in which case all stocks will be updated. (Currently, yahoo is only implemented for stocks and synthetics (e.g. stock indexes). update_instruments.TTR uses the data frame given by TTR:::stockSymbols to define/update stocks. update_instruments.IB can accept a vector of instrument names, or sQuoteall or sQuotestocks, but it also accepts one of, sQuotefutures, sQuoteoptions, or sQuotecurrencies

define_stocks, define_options, define_futures and define_FX make it easy to define several instruments of the same type. front_future attempts to define the near term liquid futures contract of a given future_series.

get_quote will return a recent quote snapshot using IBrokers by default (although if called with src='yahoo', it will call quantmod::getQuote.

getBAT will download and merge the closing price of Bid, Ask, Trade, and Midpoint

getContract will get a twsContract. It will search the .instrument environment for an instrument that holds the contract. If not found, it will download it with a call to reqContractDetails

conId is a generic that will get the conId. If it cannot find it easily, it will call getContract and extract the conId from that.

The following will eventually be moved to the FinancialInstrument package saveInstruments and loadInstruments allow you to save your .instrument environment. loadInstruments will add to your .instrument environment all the instruments found in a file that was created with saveInstruments.

ls_instruments and the several related functions (ls_stocks, ls_synthetics, etc) help you see the names of your defined instruments. ls_instruments_by help you subset the names of your instruments based on values of attributes they have.

rm_instruments and related functions remove instruments from your .instrument environment.

Before using, enable ActiveX and socket clients and add your computer to trusted IP addresses in your IB TWS global configuration. Read the IBrokers documentation before using.

Use a paper trading account.

See ?IBrokers and vignette("IBrokers")

See examples.

Many functions in this package need data from IB. The ones that do will automatically connect and disconnect using the twsConnect and twsDisconnect. This package will only use clientIds 100 through 150. clientId 150 is used as a last resort, and any time it is used, you will be notified with a warning message. Other clientIds used are as follows: buildIBcontract may use 100:104, getContract may use 110:114, getBAT may use 120:124, getIBEquities may use 125:129, get_quote.IB may use 130:132, and twsClock may use 140:144).

Author(s)

Garrett See

Maintainer: Garrett See <gsee000@gmail.com>

References

Yahoo! Finance finance.yahoo.com gummy-stuff.org www.gummy-stuff.org/Yahoo-data.htm InteractiveBrokers www.interactivebrokers.com IB API http://interactivebrokers.com/php/apiUsersGuide/apiguide.htm tdameritrade www.tdameritrade.com

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#Before using, enable ActiveX and socket clients and add your computer
#to trusted IP addresses in your IB TWS global configuration.
#See ?IBrokers and vignette("IBrokers")

## Not run: 

twsInstrument(twsSTK("SPY","USD"))
twsInstrument(twsSTK("DIA","USD"))

option('.SPY','USD',100,right='P',strike=135,
     expiry=201112,underlying_id="SPY")

contract <- Contr_From_Instr('.SPY') #doesn't change instrument

Instr_From_Contr(contract)
Instr_From_Contr(twsFUT('ES',exch='GLOBEX',expiry='201109'))

option('.DIA','USD',100,right='P',strike=125,
     expiry=201112,underlying_id="DIA")

define_stocks(c('G','GA','GAR'), use.yahoo=FALSE)
define_stocks(c('S','SE','SEE'),currency="USD", use.IB=FALSE)

ls_stocks()
ls_derivatives()
ls_options()
ls_twsInstruments()

instrument.table(ls_yahoo(),attrs.of='S')
instrument.table(ls_options(), attrs.of='.SPY')

getInstrument('SEE')
update_instruments.IB('SEE')
getContract('SEE')

getInstrument('GAR')
update_instruments.yahoo()
getInstrument('GAR')

#create twsInstruments for SP500 stocks, updated with
#info from attached SP500desc data, IB, and yahoo,
define_stocks()
ls_stocks()


## Using the IB stuff without Using FinancialInstrument
rm_instruments(keep.currencies=FALSE)

getBAT('GS')   #Not recommended
#Assumes 'GS' is the name of an equity

#alternatively, pass a twsContract object
contract <- twsSTK('GR')
getBAT(contract) #recommended

#for an instrument:
instr <- stock('GT','USD')
getBAT(instr) #recommended

#getBAT and reqTBBO are the exact same function
reqTBBO(twsFUT(symbol='ES',exch='GLOBEX',expiry='201106',multiplier=5))

#get all fields for TRADES, for one or more stocks
#by default they are assigned to .GlobalEnv similarly to getSymbols
getIBEquities(c('S','SE', 'SEE'))

ls_IB()
ls_IB(ls_stocks())
ls_IB(ls_derivatives()

plotRelPerf(c('GT','SEE'))

#### Using FinancialInstrument but not IB ####

rm_instruments(keep.currencies=FALSE) #remove all instruments and currencies

define_stocks(c('GA','GAR'),"EUR",use.IB=FALSE, use.yahoo=FALSE)
define_stocks(c('RRE','TT'),"CAD",use.IB=FALSE, use.yahoo=FALSE)
define_stocks(c('EE','GS','SEE'),"USD",use.IB=FALSE)
ls_instruments()
ls_currencies()
ls_EUR() #GA GAR
ls_CAD() #RRE TT
ls_USD() #EE GS SEE

#data.frame view of stocks in .instrument environment.
#columns will be defined by 'GAR' instrument
instrument.table(ls_stocks(), attrs.of='GAR')

#create instrument for stocks (S&P500 stocks by default)
define_stocks( ,"USD",use.IB=FALSE)
ls_yahoo()

ls_instruments("SE") #only the one stock
rm_stocks('SE')
ls_instruments(c('S','SE','SEE')) #only the 2 that exist are returned
ls_instruments("S", match=FALSE) #anything with "S" in name

stock('SPY','USD',1)
#derivatives
option('.SPY','USD',multiplier=100,expiry='201106', strike=130, right='P', underlying_id='SPY')
future('ES', 'USD', multiplier=50, expiry='201106', underlying_id='ES')
option('.ES','USD',multiplier=1, expiry='201106',strike=1350, right='C', underlying_id='ES')

ls_currencies()
ls_options()
ls_futures()
ls_derivatives()
ls_non_derivatives()

rm_options('.SPY')
rm_futures()
ls_instruments()
#rm_instruments('EUR') # <-- INCORRECT USAGE
rm_instruments('EUR', keep.currencies=FALSE)
ls_currencies()
rm_currencies('CAD')
ls_instruments()
rm_instruments() #remove all but currency
rm_instruments(keep.currencies=FALSE) #remove everything from .instrument


SP500.symbols <- as.character(SP500desc[,1])
addIBslot(SP500.symbols) #Add IB slot to S&P 500 instrument metadata

getBAT('XOM') #uses twsContract as defined by addIBslot
#head(XOM)

syms <- SP500.symbols[1:3]
getIBEquities(syms) #get all fields for TRADES

plotRelPerf(syms)


####### Misc. ############

ls_twsInstruments()
ls_non_twsInstruments()

## make twsContracts using information stored in instruments
#just make a twsContract without changing anything about the instrument
buildIBcontract('XOM',addIBslot=FALSE)

#or add a slot in the instrument to hold the twsContract
buildIBcontract('XOM')

#or update everything with the new information retrieved from IB.
buildIBcontract('XOM',updateInstrument=TRUE)

#reqTBBO('XOM') #will use twsContract object in IB slot of XOM instrument.

## End(Not run)

twsInstrument documentation built on May 2, 2019, 5:24 p.m.