-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIBClient.R
More file actions
585 lines (394 loc) · 20.8 KB
/
IBClient.R
File metadata and controls
585 lines (394 loc) · 20.8 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
IBClient <- R6Class("IBClient",
class= FALSE,
cloneable= FALSE,
lock_class= TRUE,
private= list(
socket= NULL, # Socket connection
serverVersion= NULL, # Returned on connection
serverTimestamp= NULL, # Returned on connection
id= NULL, # Client ID
#
# Finalizer
#
finalize= function() {
self$disconnect()
},
encodeInit= function(msg) {
stopifnot(length(msg) == 1L,
is.character(msg),
!is.na(msg))
raw_msg <- charToRaw(msg) # Doesn't terminate with '\0'
len <- length(raw_msg)
stopifnot(raw_msg < as.raw(0x80L), # Only ASCII chars are allowed
len <= MAX_MSG_LEN)
header <- writeBin(len, raw(), size=HEADER_LEN, endian="big")
# Write to socket
writeBin(c(API_SIGN, header, raw_msg), private$socket)
},
#
# Encode and send a message
#
# msgid: message id as integer
# msg: protobuf message
#
encodeMsg= function(msgid, msg) {
stopifnot(is.integer(msgid),
missing(msg) || inherits(msg, "Message"))
# Apply offset
msgid <- msgid + PROTOBUF_MSG_ID
raw_msg <- c(writeBin(msgid, raw(), size=RAWID_LEN, endian="big"),
if(!missing(msg)) RProtoBuf::serialize(msg, NULL))
len <- length(raw_msg)
stopifnot(len <= MAX_MSG_LEN)
header <- writeBin(len, raw(), size=HEADER_LEN, endian="big")
# Write to socket
writeBin(c(header, raw_msg), private$socket)
},
readInit= function() {
# Read header and decode message length
len <- readBin(private$socket, integer(), size=HEADER_LEN, endian="big")
# Invalid socket
if(length(len) == 0L)
stop("lost connection")
# Header consistency check
stopifnot(len > 0L,
len <= MAX_MSG_LEN)
raw_msg <- readBin(private$socket, raw(), n=len)
# Count the fields
n <- sum(raw_msg == as.raw(0L))
# Consistency checks
stopifnot(length(raw_msg) == len,
raw_msg[len] == as.raw(0L),
n == 2L)
readBin(raw_msg, character(), n=n)
},
#
# Read one message. BLOCKING
#
# Return a list(msgid(int), msg(message iterator or raw message))
#
readOneMsg= function() {
# Read header and decode message length
len <- readBin(private$socket, integer(), size=HEADER_LEN, endian="big")
# Invalid socket
if(length(len) == 0L)
stop("lost connection")
# Header consistency check
stopifnot(len > 0L,
len <= MAX_MSG_LEN)
msgid <- readBin(private$socket, integer(), size=RAWID_LEN, endian="big")
raw_msg <- readBin(private$socket, raw(), n=len - RAWID_LEN)
list(msgid=msgid, msg=raw_msg)
},
# Convenience wrappers for simple payload requests
req_int= function(msgid, reqId)
private$encodeMsg(msgid,
RProtoBuf::new(IBProto.SingleInt32, value=reqId)),
req_bool= function(msgid, value)
private$encodeMsg(msgid,
if(value) RProtoBuf::new(IBProto.SingleBool, value=value)
else RProtoBuf::new(IBProto.SingleBool)),
req_intstring= function(msgid, reqId, data)
private$encodeMsg(msgid,
RProtoBuf::new(IBProto.StringData, reqId=reqId, data=data)),
encodeargs= function(msgid, descriptor) {
args <- sapply(formalArgs(sys.function(-1L)), get, envir=parent.frame(), simplify=FALSE)
stopifnot(names(args) %in% names(descriptor))
msg <- maptopb(args, descriptor)
private$encodeMsg(msgid, msg)
}
),
active= list(
serVersion= function() private$serverVersion,
serTimestamp= function() private$serverTimestamp,
clientId= function() private$id
),
public= list(
connect= function(host="localhost", port=4002L, clientId=1L, connectOptions="", optionalCapabilities="") {
stopifnot(is.null(private$socket))
# Open connection to server
private$socket <- socketConnection(host=host, port=port, open="r+b", blocking=TRUE)
# Prefix " " to connectOptions, if not empty
if(nzchar(connectOptions, keepNA=TRUE))
connectOptions <- paste0(" ", connectOptions)
# Start handshake
private$encodeInit(paste0("v", MIN_CLIENT_VER, "..", MAX_CLIENT_VER, connectOptions))
# Server response
res <- private$readInit()
private$serverVersion <- as.integer(res[1])
private$serverTimestamp <- res[2]
message("server version: ", private$serverVersion, " timestamp: ", private$serverTimestamp)
# Check server version
if(private$serverVersion < MIN_CLIENT_VER ||
private$serverVersion > MAX_CLIENT_VER) {
self$disconnect()
stop("unsupported version")
}
# startAPI
self$startApi(clientId, optionalCapabilities)
private$id <- clientId
# TODO
# Verify that connection was successful
},
disconnect= function() {
if(!is.null(private$socket)) {
close(private$socket)
private$socket <-
private$serverVersion <-
private$serverTimestamp <-
private$id <- NULL
}
},
#
# Process server responses
#
# Block up to timeout
# If wrap is missing, messages are discarded
# otherwise callbacks are dispatched
#
checkMsg= function(wrap, timeout=0.2) {
count <- 0L
while(socketSelect(list(private$socket), write=FALSE, timeout=timeout)) {
count <- count + 1L
msg <- private$readOneMsg()
if(missing(wrap))
next
# Decode message
res <- decode(msg, private$serverVersion)
# Dispatch callback
if(!is.null(res))
do.call(wrap[[res$fname]], res$fargs)
}
count
},
# ########################################################################
#
# Send requests to the server
#
# ########################################################################
reqMktData= function(reqId, contract, genericTicks, snapshot, regulatorySnapshot=FALSE, mktDataOptions=character())
private$encodeargs(1L, IBProto.MarketDataRequest), ### REQ_MKT_DATA
cancelMktData= function(reqId) private$req_int(2L, reqId), ### CANCEL_MKT_DATA
placeOrder= function(id, contract, order) {
msgid <- 3L ### PLACE_ORDER
c <- maptopb(contract, IBProto.Contract, "lastTradeDate")
n <- length(order$orderComboLegs)
stopifnot(n %in% c(0L, length(contract$comboLegs)))
for(i in seq_len(n))
c$comboLegs[[i]]$perLegPrice <- order$orderComboLegs[i]
threestatebool <- c("routeMarketableToBbo",
"usePriceMgmtAlgo",
"seekPriceImprovement")
attachedorders <- c("slOrderId",
"slOrderType",
"ptOrderId",
"ptOrderType")
o <- maptopb(order, IBProto.Order, c("orderId",
"rule80A",
"auctionStrategy",
"basisPoints",
"basisPointsType",
"orderComboLegs",
"mifid2DecisionMaker",
"mifid2DecisionAlgo",
"mifid2ExecutionTrader",
"mifid2ExecutionAlgo",
"autoCancelDate",
"filledQuantity",
"refFuturesConId",
"shareholder",
"parentPermId",
# Require separate handling
"totalQuantity", # Decimal
threestatebool,
attachedorders))
o$totalQuantity <- as.character(order$totalQuantity)
for(n in threestatebool)
if(!is.na(order[[n]]))
o[[n]] <- order[[n]]
if(self$serVersion < MIN_SERVER_VER_ADDITIONAL_ORDER_PARAMS_1)
for(n in c("deactivate", "postOnly", "allowPreOpen", "ignoreOpenAuction"))
if(o$has(n)) stop("Order parameter not supported: ", n)
if(self$serVersion < MIN_SERVER_VER_ADDITIONAL_ORDER_PARAMS_2)
for(n in c("routeMarketableToBbo", "seekPriceImprovement", "whatIfType"))
if(o$has(n)) stop("Order parameter not supported: ", n)
if(self$serVersion < MIN_SERVER_VER_HEDGE_MAX_SIZE && o$has("hedgeMaxSize"))
stop("Order parameter not supported: hedgeMaxSize")
ao <- RProtoBuf::new(IBProto.AttachedOrders)
for(n in attachedorders) {
val <- order[[n]]
if(is.na(val) || !nzchar(val))
next
if(self$serVersion < MIN_SERVER_VER_ATTACHED_ORDERS)
stop("Attached order parameter not supported: ", n)
else
ao[[n]] <- val
}
msg <- RProtoBuf::new(IBProto.PlaceOrderRequest,
orderId=id,
contract=c,
order=o,
attachedOrders=ao)
private$encodeMsg(msgid, msg)
},
cancelOrder= function(id, orderCancel)
private$encodeMsg(4L, ### CANCEL_ORDER
RProtoBuf::new(IBProto.CancelOrderRequest,
orderId=id,
orderCancel=maptopb(orderCancel, IBProto.OrderCancel))),
reqOpenOrders= function() private$encodeMsg(5L), ### REQ_OPEN_ORDERS
reqAccountUpdates= function(subscribe, acctCode)
private$encodeargs(6L, IBProto.AccountDataRequest), ### REQ_ACCT_DATA
reqExecutions= function(reqId, filter)
private$encodeargs(7L, IBProto.ExecutionRequest), ### REQ_EXECUTIONS
reqIds= function()
# numIds is omitted as it's deprecated and unused
private$encodeMsg(8L), ### REQ_IDS
reqContractDetails= function(reqId, contract)
private$encodeargs(9L, IBProto.ContractDataRequest), ### REQ_CONTRACT_DATA
reqMktDepth= function(reqId, contract, numRows, isSmartDepth, mktDepthOptions=character())
private$encodeargs(10L, IBProto.MarketDepthRequest), ### REQ_MKT_DEPTH
cancelMktDepth= function(reqId, isSmartDepth)
private$encodeargs(11L, IBProto.CancelMarketDepth), ### CANCEL_MKT_DEPTH
reqNewsBulletins= function(allMsgs) private$req_bool(12L, allMsgs), ### REQ_NEWS_BULLETINS
cancelNewsBulletins= function() private$encodeMsg(13L), ### CANCEL_NEWS_BULLETINS
setServerLogLevel= function(logLevel) private$req_int(14L, logLevel), ### SET_SERVER_LOGLEVEL
reqAutoOpenOrders= function(bAutoBind) private$req_bool(15L, bAutoBind), ### REQ_AUTO_OPEN_ORDERS
reqAllOpenOrders= function() private$encodeMsg(16L), ### REQ_ALL_OPEN_ORDERS
reqManagedAccts= function() private$encodeMsg(17L), ### REQ_MANAGED_ACCTS
requestFA= function(faDataType) private$req_int(18L, map_enum2int("FaDataType", faDataType)), ### REQ_FA
replaceFA= function(reqId, faDataType, xml)
private$encodeMsg(19L, ### REPLACE_FA
RProtoBuf::new(IBProto.FAReplace,
reqId=reqId,
faDataType=map_enum2int("FaDataType", faDataType),
xml=xml)),
reqHistoricalData= function(reqId, contract, endDateTime, duration, barSizeSetting,
whatToShow, useRTH, formatDate, keepUpToDate,
chartOptions=character())
private$encodeargs(20L, IBProto.HistoricalDataRequest), ### REQ_HISTORICAL_DATA
exerciseOptions= function(reqId, contract, exerciseAction, exerciseQuantity,
account, override, manualOrderTime, customerAccount,
professionalCustomer)
private$encodeargs(21L, IBProto.ExerciseOptionsRequest), ### EXERCISE_OPTIONS
reqScannerSubscription= function(reqId, subscription, scannerSubscriptionOptions=character(), scannerSubscriptionFilterOptions=character()) {
msgid <- 22L ### REQ_SCANNER_SUBSCRIPTION
sub <- maptopb(c(subscription,
list(scannerSubscriptionOptions=scannerSubscriptionOptions,
scannerSubscriptionFilterOptions=scannerSubscriptionFilterOptions)),
IBProto.ScannerSubscription)
msg <- RProtoBuf::new(IBProto.ScannerSubscriptionRequest,
reqId=reqId,
subscription=sub)
private$encodeMsg(msgid, msg)
},
cancelScannerSubscription= function(reqId) private$req_int(23L, reqId), ### CANCEL_SCANNER_SUBSCRIPTION
reqScannerParameters= function() private$encodeMsg(24L), ### REQ_SCANNER_PARAMETERS
cancelHistoricalData= function(reqId) private$req_int(25L, reqId), ### CANCEL_HISTORICAL_DATA
reqCurrentTime= function() private$encodeMsg(49L), ### REQ_CURRENT_TIME
reqRealTimeBars= function(reqId, contract, barSize, whatToShow, useRTH, realTimeBarsOptions=character())
private$encodeargs(50L, IBProto.RealTimeBarsRequest), ### REQ_REAL_TIME_BARS
cancelRealTimeBars= function(reqId) private$req_int(51L, reqId), ### CANCEL_REAL_TIME_BARS
reqFundamentalData= function(reqId, contract, reportType, fundamentalDataOptions=character())
private$encodeargs(52L, ### REQ_FUNDAMENTAL_DATA
IBProto.FundamentalDataRequest),
cancelFundamentalData= function(reqId) private$req_int(53L, reqId), ### CANCEL_FUNDAMENTAL_DATA
calculateImpliedVolatility= function(reqId, contract, optionPrice, underPrice, miscOptions=character())
private$encodeargs(54L, ### REQ_CALC_IMPLIED_VOLAT
IBProto.CalculateImpliedVolatilityRequest),
calculateOptionPrice= function(reqId, contract, volatility, underPrice, miscOptions=character())
private$encodeargs(55L, ### REQ_CALC_OPTION_PRICE
IBProto.CalculateOptionPriceRequest),
cancelCalculateImpliedVolatility= function(reqId) private$req_int(56L, reqId), ### CANCEL_CALC_IMPLIED_VOLAT
cancelCalculateOptionPrice= function(reqId) private$req_int(57L, reqId), ### CANCEL_CALC_OPTION_PRICE
reqGlobalCancel= function(orderCancel)
private$encodeMsg(58L, ### REQ_GLOBAL_CANCEL
RProtoBuf::new(IBProto.GlobalCancelRequest,
orderCancel=maptopb(orderCancel, IBProto.OrderCancel))),
reqMarketDataType= function(marketDataType) private$req_int(59L, marketDataType), ### REQ_MARKET_DATA_TYPE
reqPositions= function() private$encodeMsg(61L), ### REQ_POSITIONS
reqAccountSummary= function(reqId, group, tags)
private$encodeargs(62L, IBProto.AccountSummaryRequest), ### REQ_ACCOUNT_SUMMARY
cancelAccountSummary= function(reqId) private$req_int(63L, reqId), ### CANCEL_ACCOUNT_SUMMARY
cancelPositions= function() private$encodeMsg(64L), ### CANCEL_POSITIONS
verifyRequest= function(apiName, apiVersion)
private$encodeargs(65L, IBProto.VerifyRequest), ### VERIFY_REQUEST
verifyMessage= function(apiData)
private$encodeMsg(66L, ### VERIFY_MESSAGE
RProtoBuf::new(IBProto.SingleString, value=apiData)),
queryDisplayGroups= function(reqId) private$req_int(67L, reqId), ### QUERY_DISPLAY_GROUPS
subscribeToGroupEvents= function(reqId, groupId)
private$encodeargs(68L, IBProto.SubscribeToGroupEventsRequest), ### SUBSCRIBE_TO_GROUP_EVENTS
updateDisplayGroup= function(reqId, contractInfo)
private$req_intstring(69L, reqId, contractInfo), ### UPDATE_DISPLAY_GROUP
unsubscribeFromGroupEvents= function(reqId) private$req_int(70L, reqId), ### UNSUBSCRIBE_FROM_GROUP_EVENTS
startApi= function(clientId, optionalCapabilities)
private$req_intstring(71L, clientId, optionalCapabilities), ### START_API
# verifyAndAuthRequest= function(apiName, apiVersion, opaqueIsvKey) {
#
# # WARN: Assume extraAuth = TRUE
#
# msgid <- 72L ### VERIFY_AND_AUTH_REQUEST
#
# msg <- c("1", apiName, apiVersion, opaqueIsvKey)
#
# private$encodeMsg(msgid, msg)
# },
#
# verifyAndAuthMessage= function(apiData, xyzResponse) private$req_simple(73L, "1", apiData, xyzResponse), ### VERIFY_AND_AUTH_MESSAGE
reqPositionsMulti= function(reqId, account, modelCode)
private$encodeargs(74L, IBProto.PositionsMultiRequest), ### REQ_POSITIONS_MULTI
cancelPositionsMulti= function(reqId) private$req_int(75L, reqId), ### CANCEL_POSITIONS_MULTI
reqAccountUpdatesMulti= function(reqId, account, modelCode, ledgerAndNLV)
private$encodeargs(76L, IBProto.AccountUpdatesMultiRequest), ### REQ_ACCOUNT_UPDATES_MULTI
cancelAccountUpdatesMulti= function(reqId) private$req_int(77L, reqId), ### CANCEL_ACCOUNT_UPDATES_MULTI
reqSecDefOptParams= function(reqId, underlyingSymbol, futFopExchange, underlyingSecType, underlyingConId)
private$encodeargs(78L, IBProto.SecDefOptParamsRequest), ### REQ_SEC_DEF_OPT_PARAMS
reqSoftDollarTiers= function(reqId) private$req_int(79L, reqId), ### REQ_SOFT_DOLLAR_TIERS
reqFamilyCodes= function() private$encodeMsg(80L), ### REQ_FAMILY_CODES
reqMatchingSymbols= function(reqId, pattern)
private$req_intstring(81L, reqId, pattern), ### REQ_MATCHING_SYMBOLS
reqMktDepthExchanges= function() private$encodeMsg(82L), ### REQ_MKT_DEPTH_EXCHANGES
reqSmartComponents= function(reqId, bboExchange)
private$req_intstring(83L, reqId, bboExchange), ### REQ_SMART_COMPONENTS
reqNewsArticle= function(reqId, providerCode, articleId, newsArticleOptions=character())
private$encodeargs(84L, IBProto.NewsArticleRequest), ### REQ_NEWS_ARTICLE
reqNewsProviders= function() private$encodeMsg(85L), ### REQ_NEWS_PROVIDERS
reqHistoricalNews= function(reqId, conId, providerCodes, startDateTime, endDateTime, totalResults, historicalNewsOptions=character())
private$encodeargs(86L, IBProto.HistoricalNewsRequest), ### REQ_HISTORICAL_NEWS
reqHeadTimestamp= function(reqId, contract, whatToShow, useRTH, formatDate)
private$encodeargs(87L, IBProto.HeadTimestampRequest), ### REQ_HEAD_TIMESTAMP
reqHistogramData= function(reqId, contract, useRTH, timePeriod)
private$encodeargs(88L, IBProto.HistogramDataRequest), ### REQ_HISTOGRAM_DATA
cancelHistogramData= function(reqId) private$req_int(89L, reqId), ### CANCEL_HISTOGRAM_DATA
cancelHeadTimestamp= function(reqId) private$req_int(90L, reqId), ### CANCEL_HEAD_TIMESTAMP
reqMarketRule= function(marketRuleId) private$req_int(91L, marketRuleId), ### REQ_MARKET_RULE
reqPnL= function(reqId, account, modelCode)
private$encodeargs(92L, IBProto.PnLRequest), ### REQ_PNL
cancelPnL= function(reqId) private$req_int(93L, reqId), ### CANCEL_PNL
reqPnLSingle= function(reqId, account, modelCode, conId)
private$encodeargs(94L, IBProto.PnLSingleRequest), ### REQ_PNL_SINGLE
cancelPnLSingle= function(reqId) private$req_int(95L, reqId), ### CANCEL_PNL_SINGLE
reqHistoricalTicks= function(reqId, contract, startDateTime, endDateTime, numberOfTicks, whatToShow, useRTH, ignoreSize, miscOptions=character())
private$encodeargs(96L, IBProto.HistoricalTicksRequest), ### REQ_HISTORICAL_TICKS
reqTickByTickData= function(reqId, contract, tickType, numberOfTicks, ignoreSize)
private$encodeargs(97L, IBProto.TickByTickRequest), ### REQ_TICK_BY_TICK_DATA
cancelTickByTickData= function(reqId) private$req_int(98L, reqId), ### CANCEL_TICK_BY_TICK_DATA
reqCompletedOrders= function(apiOnly) private$req_bool(99L, apiOnly), ### REQ_COMPLETED_ORDERS
reqWshMetaData= function(reqId) private$req_int(100L, reqId), ### REQ_WSH_META_DATA
cancelWshMetaData= function(reqId) private$req_int(101L, reqId), ### CANCEL_WSH_META_DATA
reqWshEventData= function(reqId, wshEventData) {
msgid <- 102L ### REQ_WSH_EVENT_DATA
msg <- maptopb(wshEventData, IBProto.WshEventDataRequest)
msg$reqId <- reqId
private$encodeMsg(msgid, msg)
},
cancelWshEventData= function(reqId) private$req_int(103L, reqId), ### CANCEL_WSH_EVENT_DATA
reqUserInfo= function(reqId) private$req_int(104L, reqId), ### REQ_USER_INFO
reqCurrentTimeInMillis= function() private$encodeMsg(105L), ### REQ_CURRENT_TIME_IN_MILLIS
cancelContractData= function(reqId) private$req_int(106L, reqId), ### CANCEL_CONTRACT_DATA
cancelHistoricalTick= function(reqId) private$req_int(107L, reqId), ### CANCEL_HISTORICAL_TICKS
reqConfig= function(reqId) private$req_int(108L, reqId), ### REQ_CONFIG
updateConfig= function(updateConfigRequest) private$encodeMsg(109L, updateConfigRequest) ### UPDATE_CONFIG
)
)