-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrib.wrap.R
320 lines (219 loc) · 13.5 KB
/
rib.wrap.R
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
.wrap <-
R6::R6Class("IBWrap",
class = FALSE,
cloneable = FALSE,
lock_class = TRUE,
inherit= rib::IBWrap,
public= list(
Data= NULL,
Settings = NULL,
initialize= function(...) {
self$Settings <- new.env()
self$Settings$storeMessages <- FALSE
self$Settings$showMessages <- TRUE
ARGS <- list(...)
for (setting in names(ARGS)) {
self$Settings[[setting]] <- ARGS[[setting]]
}
self$Data <- new.env()
self$Data$orders <- list()
self$Data$orderStatus <- list()
self$Data$executions <- list()
self$Data$contracts <- list()
self$Data$commissionReport <- list()
self$Data$accountSummary <- list()
self$Data$headTimestamp <- list()
self$Data$historicalData <- list()
self$Data$recentMessages <- list()
self$Data$Close <- list()
},
tickPrice= function(tickerId, field, price, size, attrib) warning("default 'tickPrice' implementation"),
tickSize= function(tickerId, field, size) warning("default 'tickSize' implementation"),
tickOptionComputation= function(tickerId, tickType,
tickAttrib, impliedVol,
delta, optPrice, pvDividend,
gamma, vega, theta, undPrice)
warning("default 'tickOptionComputation' implementation"),
tickGeneric= function(tickerId, tickType, value) warning("default 'tickGeneric' implementation"),
tickString= function(tickerId, tickType, value) warning("default 'tickString' implementation"),
tickEFP= function(tickerId, tickType, basisPoints,
formattedBasisPoints, totalDividends,
holdDays, futureLastTradeDate, dividendImpact,
dividendsToLastTradeDate)
warning("default 'tickEFP' implementation"),
orderStatus = function(orderId, status, filled, remaining,
avgFillPrice, permId, parentId, lastFillPrice,
clientId, whyHeld, mktCapPrice) {
self$Data$orderStatus[[as.character(permId)]] <-
as.list(match.call())[-1L]
},
openOrder= function(orderId, contract, order, orderstate) {
self$Data$orders[[as.character(order$permId)]] <-
list(orderId = orderId,
contract = contract,
order = order,
orderstate = orderstate)
if (self$Settings$showMessages)
message("order ", orderId, ": added to/updated in Data$orders")
},
openOrderEnd= function() {
invisible(NULL)
},
# connectionClosed= function() warning("default implementation"),
updateAccountValue= function(key, val, currency, accountName) {
v <- list(key = key, value = val, currency = currency, accountName = accountName)
self$Data$accountData <-
c(self$Data$accountData, v)
},
updatePortfolio= function(contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName) {
v <- list(contract, position, marketPrice, marketValue,
averageCost, unrealizedPNL, realizedPNL, accountName)
self$Data$portfolioData <- c(self$Data$portfolioData, list(v))
},
updateAccountTime= function(timeStamp) {
invisible(NULL)
},
accountDownloadEnd= function(accountName) {
invisible(NULL)
},
nextValidId= function(orderId) {
self$Data$nextValidId <- orderId
},
contractDetails= function(reqId, contractDetails) {
self$Data$contracts[[as.character(reqId)]] <- contractDetails
},
bondContractDetails= function(reqId, contractDetails) warning("default 'bondContractDetails' implementation"),
contractDetailsEnd= function(reqId) {
invisible(NULL)
},
execDetails= function(reqId, contract, execution) {
self$Data$executions_contracts[[execution$execId]] <- contract
self$Data$executions[[execution$execId]] <- execution
## cat("ExecDetails:", reqId, contract$symbol, execution$side, execution$shares, execution$price, "\n")
},
execDetailsEnd = function(reqId) {
## cat("ExecDetailsEnd:", reqId, "\n")
},
error= function(id, errorTime, errorCode, errorString, advancedOrderRejectJson) {
if (self$Settings$storeMessages) {
n <- as.character(round(unclass(Sys.time()), 6))
self$Data$recentMessages[[n]] <- list(id,
errorTime,
errorCode,
errorString,
advancedOrderRejectJson)
}
if (self$Settings$showMessages &&
errorCode %in% c(2104, 2158, 2106)) {
cat(format(errorCode, width = 6),
": ", errorString, "\n")
} else if (self$Settings$showMessages)
cat(format(id, width = 6),
"|", format(errorCode, width = 6), ": ", errorString, "\n")
},
updateMktDepth= function(id, position, operation, side, price, size) warning("default 'updateMktDepth' implementation"),
updateMktDepthL2= function(id, position, marketMaker, operation, side, price, size, isSmartDepth) warning("default 'updateMktDepthL2' implementation"),
updateNewsBulletin= function(msgId, msgType, newsMessage, originExch) warning("default 'updateNewsBulletin' implementation"),
managedAccounts= function(accountsList) {
if (self$Settings$showMessages) {
cat("Accounts: \n")
acc <- paste(paste(" ",
sort(unlist(strsplit(accountsList, ",")))),
collapse = "\n")
cat(acc, "\n\n")
} else {
self$Data$managedAccounts <- sort(unlist(strsplit(accountsList, ",")))
}
invisible(NULL)
},
receiveFA= function(faDataType, xml) warning("default 'receiveFA' implementation"),
historicalData= function(reqId, bar) {
self$Data$historicalData[[as.character(reqId)]] <- bar
if (self$Settings$showMessages)
message("request id: ", reqId, "; received rows: ",
nrow(self$Data$historicalData[[as.character(reqId)]]))
},
scannerParameters= function(xml) warning("default 'scannerParameters' implementation"),
scannerData= function(reqId, rank, contractDetails, distance, benchmark, projection, legsStr) warning("default 'scannerData' implementation"),
realtimeBar= function(reqId, time, open, high, low, close,
volume, wap, count) {
message(reqId, " | ", .POSIXct(time), " | ", open, " | ", close)
self$Data$Close[reqId] <- close
},
currentTime= function(time) warning("default 'currentTime' implementation"),
fundamentalData= function(reqId, data) warning("default 'fundamentalData' implementation"),
deltaNeutralValidation= function(reqId, deltaNeutralContract) warning("default 'deltaNeutralValidation' implementation"),
tickSnapshotEnd= function(reqId) warning("default 'tickSnapshotEnd' implementation"),
marketDataType= function(reqId, marketDataType) warning("default 'marketDataType' implementation"),
commissionReport= function(commissionReport) {
self$Data$commissionReport[[commissionReport$execId]] <-
commissionReport
},
position= function(account, contract, position, avgCost) {
L <- list(account = account, contract = contract,
position = position, avgCost = avgCost)
self$Data$positions <- c(self$Data$positions, list(L))
},
positionEnd= function() {
invisible(NULL)
},
accountSummary= function(reqId, account, tag, value, currency) {
L <- list(account = account, tag = tag,
value = value, currency = currency)
self$Data$accountSummary <- c(self$Data$accountSummary, list(L))
},
accountSummaryEnd= function(reqId) {
invisible(NULL)
},
verifyMessageAPI= function(apiData) warning("default 'verifyMessageAPI' implementation"),
verifyCompleted= function(isSuccessful, errorText) warning("default 'verifyCompleted' implementation"),
displayGroupList= function(reqId, groups) warning("default 'displayGroupList' implementation"),
displayGroupUpdated= function(reqId, contractInfo) warning("default 'displayGroupUpdated' implementation"),
verifyAndAuthMessageAPI= function(apiData, xyzChallange) warning("default 'verifyAndAuthMessageAPI' implementation"),
verifyAndAuthCompleted= function(isSuccessful, errorText) warning("default 'verifyAndAuthCompleted' implementation"),
# connectAck= function() warning("default implementation"),
positionMulti= function(reqId, account, modelCode, contract, position, avgCost) warning("default 'positionMulti' implementation"),
positionMultiEnd= function(reqId) warning("default 'positionMultiEnd' implementation"),
accountUpdateMulti= function(reqId, account, modelCode, key, value, currency) warning("default 'accountUpdateMulti' implementation"),
accountUpdateMultiEnd= function(reqId) warning("default 'accountUpdateMultiEnd' implementation"),
securityDefinitionOptionalParameter= function(reqId, exchange, underlyingConId, tradingClass, multiplier, expirations, strikes) warning("default 'securityDefinitionOptionalParameter' implementation"),
securityDefinitionOptionalParameterEnd= function(reqId) warning("default 'securityDefinitionOptionalParameterEnd' implementation"),
softDollarTiers= function(reqId, tiers) warning("default 'softDollarTiers' implementation"),
familyCodes= function(familyCodes) warning("default 'familyCodes' implementation"),
symbolSamples= function(reqId, contractDescriptions) warning("default 'symbolSamples' implementation"),
mktDepthExchanges= function(depthMktDataDescriptions) warning("default 'mktDepthExchanges' implementation"),
tickNews= function(tickerId, timeStamp, providerCode, articleId, headline, extraData) warning("default 'tickNews' implementation"),
smartComponents= function(reqId, theMap) warning("default 'smartComponents' implementation"),
tickReqParams= function(tickerId, minTick, bboExchange, snapshotPermissions) warning("default 'tickReqParams' implementation"),
newsProviders= function(newsProviders) warning("default 'newsProviders' implementation"),
newsArticle= function(requestId, articleType, articleText) warning("default 'newsArticle' implementation"),
historicalNews= function(requestId, time, providerCode, articleId, headline) warning("default 'historicalNews' implementation"),
historicalNewsEnd= function(requestId, hasMore) warning("default 'historicalNewsEnd' implementation"),
headTimestamp= function(reqId, headTimestamp) {
self$Data$headTimestamp[[as.character(reqId)]] <- headTimestamp
## message("headTimestamp ", headTimestamp)
## warning("default 'headTimestamp' implementation")
},
histogramData= function(reqId, data) warning("default 'histogramData' implementation"),
historicalDataUpdate= function(reqId, bar) warning("default 'historicalDataUpdate' implementation"),
rerouteMktDataReq= function(reqId, conid, exchange) warning("default 'rerouteMktDataReq' implementation"),
rerouteMktDepthReq= function(reqId, conid, exchange) warning("default 'rerouteMktDepthReq' implementation"),
marketRule= function(marketRuleId, priceIncrements) warning("default 'marketRule' implementation"),
pnl= function(reqId, dailyPnL, unrealizedPnL, realizedPnL) warning("default 'pnl' implementation"),
pnlSingle= function(reqId, pos, dailyPnL, unrealizedPnL, realizedPnL, value) warning("default 'pnlSingle' implementation"),
historicalTicks= function(reqId, ticks, done) warning("default 'historicalTicks' implementation"),
historicalTicksBidAsk= function(reqId, ticks, done) warning("default 'historicalTicksBidAsk' implementation"),
historicalTicksLast= function(reqId, ticks, done) warning("default 'historicalTicksLast' implementation"),
tickByTickAllLast= function(reqId, tickType, time, price, size, attribs, exchange, specialConditions) warning("default 'tickByTickAllLast' implementation"),
tickByTickBidAsk= function(reqId, time, bidPrice, askPrice, bidSize, askSize, attribs) warning("default 'tickByTickBidAsk' implementation"),
tickByTickMidPoint= function(reqId, time, midPoint) warning("default 'tickByTickMidPoint' implementation"),
orderBound= function(permId, clientId, orderId) warning("default 'orderBound' implementation"),
completedOrder= function(contract, order, orderState) warning("default 'completedOrder' implementation"),
completedOrdersEnd= function() warning("default 'completedOrdersEnd' implementation"),
replaceFAEnd= function(reqId, text) warning("default 'replaceFAEnd' implementation"),
wshMetaData= function(reqId, dataJson) warning("default 'wshMetaData' implementation"),
wshEventData= function(reqId, dataJson) warning("default 'wshEventData' implementation"),
historicalSchedule= function(reqId, startDateTime, endDateTime, timeZone, sessions) warning("default 'historicalSchedule' implementation"),
userInfo= function(reqId, whiteBrandingId) warning("default 'userInfo' implementation")
)
)