Skip to content

pairs trading with IBroker

2 messages · Soumendra, Jeff Ryan

#
Hi all.

if I use reqMktData(), it gives me a continuous feed of market data of
a SINGLE stock/future and then I can write callback functions to
handle the incoming data.

For the task at hand, I need a feed of at least two (preferably 40)
different assets/derivatives. At each iteration, I would like to pick
up the market data for about 40 stocks/futures. Currently, there
doesn't seem to be anything in IBrokers that could help me with this
(please correct me if I am wrong). twsBag seemed it could fit the
bill, but I can't figure out given the extreme lack of documentation.

It would be possible to have two different feeds getting the data
using to different connections (client ids) and use a third routine to
look at them and do some kind of pseudo-synchronisation and take the
trading decisions, but that would be an ugly solution.

Any ideas?

Thanks in advance,

Soumendra
#
Hi Soumendra,
On Tue, Nov 30, 2010 at 12:09 AM, Soumendra <soumendra at gmail.com> wrote:
You can specify more than one contract by simply creating a list of
twsContracts.  This is mentioned in a few places (slides, videos), but
the ?reqMktData doesn't really give a good example of how.  See below
for an example, but I'll update the docs to be more clear.  This
approach works for RealTimeBars as well.

Note that the API restricts the number of symbols you can display (via
API and TWS) to 100 typically, though that number can increase
depending on your trading/account/fees.
No, twsBag is for combo orders.  Not the same.
Best,
Jeff

tws <- twsConnect()
cons <- lapply(c("AAPL","MSFT","GOOG"), twsSTK)  # create a list of
twsEquity contracts
reqMktData(tws, cons)
<20101130 22:35:06.049830> id=1 symbol=AAPL Volume: 175973
<20101130 22:35:06.060365> id=1 symbol=AAPL highPrice: 314.36
<20101130 22:35:06.060701> id=1 symbol=AAPL lowPrice: 310.87
<20101130 22:35:06.235706> id=3 symbol=GOOG lastTimestamp: 1291165056
<20101130 22:35:06.239321> id=3 symbol=GOOG lastTimestamp: 1291165125
<20101130 22:35:06.239760> id=3 symbol=GOOG lastPrice: 554.0
<20101130 22:35:06.240131> id=3 symbol=GOOG lastSize: 0
<20101130 22:35:06.240497> id=3 symbol=GOOG Volume: 69711
<20101130 22:35:06.240863> id=3 symbol=GOOG highPrice: 574.32
<20101130 22:35:06.241232> id=3 symbol=GOOG lowPrice: 553.31
<20101130 22:35:06.241588> id=3 symbol=GOOG closePrice: 555.71
<20101130 22:35:06.241930> id=3 symbol=GOOG openPrice: 574.32
<20101130 22:35:06.242317> id=2 symbol=MSFT lastTimestamp: 1291164560
<20101130 22:35:06.242686> id=2 symbol=MSFT lastTimestamp: 1291164601
<20101130 22:35:06.243007> id=2 symbol=MSFT lastPrice: 25.22
<20101130 22:35:06.243322> id=2 symbol=MSFT lastSize: 10
<20101130 22:35:06.243639> id=2 symbol=MSFT Volume: 748140
<20101130 22:35:06.243961> id=2 symbol=MSFT highPrice: 25.47
<20101130 22:35:06.244245> id=2 symbol=MSFT lowPrice: 25.0
<20101130 22:35:06.244538> id=2 symbol=MSFT closePrice: 25.26
<20101130 22:35:06.244831> id=2 symbol=MSFT openPrice: 25.05
<20101130 22:35:06.245177> id=1 symbol=AAPL lastTimestamp: 1291165192
<20101130 22:35:06.245505> id=1 symbol=AAPL lastTimestamp: 1291165193
<20101130 22:35:06.245792> id=1 symbol=AAPL lastPrice: 310.61
<20101130 22:35:06.246074> id=1 symbol=AAPL lastSize: 3
<20101130 22:35:06.246354> id=1 symbol=AAPL highPrice: 314.36
<20101130 22:35:06.246640> id=1 symbol=AAPL lowPrice: 310.87
<20101130 22:35:06.246953> id=1 symbol=AAPL closePrice: 311.15
<20101130 22:35:06.247247> id=1 symbol=AAPL openPrice: 313.54
<20101130 22:35:06.247533> id=3 symbol=GOOG bidPrice: -1.0  bidSize: 0
<20101130 22:35:06.247817> id=3 symbol=GOOG askPrice: -1.0  askSize: 0
<20101130 22:35:06.248106> id=3 symbol=GOOG bidSize: 0
<20101130 22:35:06.248407> id=3 symbol=GOOG askSize: 0
<20101130 22:35:06.248668> id=2 symbol=MSFT bidPrice: -1.0  bidSize: 0
<20101130 22:35:06.248950> id=2 symbol=MSFT askPrice: -1.0  askSize: 0
<20101130 22:35:06.249240> id=2 symbol=MSFT bidSize: 0
<20101130 22:35:06.249495> id=2 symbol=MSFT askSize: 0
<20101130 22:35:06.249760> id=1 symbol=AAPL bidPrice: -1.0  bidSize: 0
<20101130 22:35:06.250384> id=1 symbol=AAPL askPrice: -1.0  askSize: 0
<20101130 22:35:06.250686> id=1 symbol=AAPL bidSize: 0
<20101130 22:35:06.250944> id=1 symbol=AAPL askSize: 0
<20101130 22:35:06.738089> id=3 symbol=GOOG optionCallVolume: 90983
<20101130 22:35:06.739385> id=3 symbol=GOOG optionPutVolume: 57624
<20101130 22:35:06.739859> id=3 symbol=GOOG optionCallOpenInterest: 221292
<20101130 22:35:06.740257> id=3 symbol=GOOG optionPutOpenInterest: 233827
<20101130 22:35:06.740883> id=3 symbol=GOOG optionHistoricalVol:
0.178315171961558 NA
<20101130 22:35:06.741311> id=3 symbol=GOOG optionImpliedVol:
0.3274484519180543
<20101130 22:35:06.759913> id=3 symbol=GOOG averageVolume: 30729
<20101130 22:35:06.762184> id=3 symbol=GOOG 13-week High: 630.84997559
<20101130 22:35:06.762629> id=3 symbol=GOOG 13-week Low: 448.0
<20101130 22:35:06.765432> id=3 symbol=GOOG 26-week High: 630.84997559
<20101130 22:35:06.765952> id=3 symbol=GOOG 26-week Low: 433.63000488
<20101130 22:35:06.766370> id=3 symbol=GOOG 52-week High: 630.84997559
<20101130 22:35:06.766793> id=3 symbol=GOOG 52-week Low: 433.63000488
<20101130 22:35:06.767260> id=3 symbol=GOOG shortable: 3.0
<20101130 22:35:06.767604> id=1 symbol=AAPL optionCallVolume: 144457
<20101130 22:35:06.767959> id=1 symbol=AAPL optionPutVolume: 83082
<20101130 22:35:06.768316> id=1 symbol=AAPL optionCallOpenInterest: 890598
<20101130 22:35:06.768651> id=1 symbol=AAPL optionPutOpenInterest: 756497
<20101130 22:35:06.769025> id=1 symbol=AAPL optionHistoricalVol:
0.2603498662626889 NA
<20101130 22:35:06.769351> id=1 symbol=AAPL optionImpliedVol:
0.3097714963644166
<20101130 22:35:06.769626> id=1 symbol=AAPL averageVolume: 190711
<20101130 22:35:06.769918> id=1 symbol=AAPL 13-week High: 321.29998779
<20101130 22:35:06.770234> id=1 symbol=AAPL 13-week Low: 240.3500061
<20101130 22:35:06.770544> id=1 symbol=AAPL 26-week High: 321.29998779
<20101130 22:35:06.770867> id=1 symbol=AAPL 26-week Low: 235.55999756
<20101130 22:35:06.771188> id=1 symbol=AAPL 52-week High: 321.29998779
<20101130 22:35:06.771533> id=1 symbol=AAPL 52-week Low: 188.67999268
<20101130 22:35:06.771925> id=1 symbol=AAPL shortable: 3.0
<20101130 22:35:06.772224> id=2 symbol=MSFT optionCallVolume: 42292
<20101130 22:35:06.772529> id=2 symbol=MSFT optionPutVolume: 27265
<20101130 22:35:06.772831> id=2 symbol=MSFT optionCallOpenInterest: 2053102
<20101130 22:35:06.773118> id=2 symbol=MSFT optionPutOpenInterest: 1508825
<20101130 22:35:06.773455> id=2 symbol=MSFT optionHistoricalVol:
0.220308007620513 NA
<20101130 22:35:06.773761> id=2 symbol=MSFT optionImpliedVol:
0.23314777896940903
<20101130 22:35:06.774028> id=2 symbol=MSFT averageVolume: 619543
<20101130 22:35:06.774307> id=2 symbol=MSFT 13-week High: 27.32999992
<20101130 22:35:06.774609> id=2 symbol=MSFT 13-week Low: 23.15999985
<20101130 22:35:06.774904> id=2 symbol=MSFT 26-week High: 27.32999992
<20101130 22:35:06.775214> id=2 symbol=MSFT 26-week Low: 22.44000053
<20101130 22:35:06.775515> id=2 symbol=MSFT 52-week High: 31.14999962
<20101130 22:35:06.775829> id=2 symbol=MSFT 52-week Low: 22.44000053
<20101130 22:35:06.776213> id=2 symbol=MSFT shortable: 3.0