Skip to content

Commit 71d7e1b

Browse files
committed
Move wrap out of IBClient.
Refactor IBContract() and IBOrder().
1 parent 2303291 commit 71d7e1b

File tree

6 files changed

+25
-58
lines changed

6 files changed

+25
-58
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
importFrom(R6, R6Class)
2+
13
# Main classes
24
export(IBClient)
35
export(IBWrap)

R/Decoder.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Decoder <- R6::R6Class("Decoder",
1+
Decoder <- R6Class("Decoder",
22

33
class= FALSE,
44
cloneable= FALSE,

R/IBClient.R

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
IBClient <- R6::R6Class("IBClient",
1+
IBClient <- R6Class("IBClient",
22

33
class= FALSE,
44
cloneable= FALSE,
@@ -13,8 +13,6 @@ IBClient <- R6::R6Class("IBClient",
1313

1414
Id= NULL, # Client ID
1515

16-
wrap= NULL, # Callbacks wrapper
17-
1816
decoder= NULL, # Decoder
1917

2018
#
@@ -121,11 +119,6 @@ IBClient <- R6::R6Class("IBClient",
121119

122120
public= list(
123121

124-
initialize= function(wrap) {
125-
126-
self$replaceWrap(wrap)
127-
},
128-
129122
connect= function(host="localhost", port=4002L, clientId=1L, connectOptions="", optionalCapabilities="") {
130123

131124
stopifnot(is.null(private$socket))
@@ -183,25 +176,14 @@ IBClient <- R6::R6Class("IBClient",
183176
}
184177
},
185178

186-
#
187-
# Replace wrapper
188-
#
189-
replaceWrap= function(wrap) {
190-
191-
# Check if "wrap" is an IBWrap class
192-
# TODO Improve this
193-
stopifnot(exists("verifyAndAuthCompleted", wrap, mode="function", inherits=FALSE))
194-
195-
private$wrap <- wrap
196-
},
197-
198179
#
199180
# Process server responses
200181
#
201182
# Block up to timeout
202-
# Discard messages if flush=TRUE
183+
# If wrap is missing, messages are discarded
184+
# otherwise callbacks are dispatched
203185
#
204-
checkMsg= function(timeout=0.2, flush=FALSE) {
186+
checkMsg= function(wrap, timeout=0.2) {
205187

206188
stopifnot(self$isOpen)
207189

@@ -213,14 +195,14 @@ IBClient <- R6::R6Class("IBClient",
213195

214196
msg <- private$readOneMsg()
215197

216-
if(!flush) {
198+
if(!missing(wrap)) {
217199

218200
# Decode message
219201
res <- private$decoder$decode(msg)
220202

221-
# Dispatch
203+
# Dispatch callback
222204
if(!is.null(res))
223-
do.call(private$wrap[[res$fname]], res$fargs)
205+
do.call(wrap[[res$fname]], res$fargs)
224206
}
225207
}
226208

@@ -602,16 +584,6 @@ IBClient <- R6::R6Class("IBClient",
602584
pack_tagvalue(scannerSubscriptionFilterOptions, mode="string"),
603585
pack_tagvalue(scannerSubscriptionOptions, mode="string"))
604586

605-
# TODO: remove this?
606-
# Check that NA's are only in allowed fields
607-
idx <- which(is.na(payload))
608-
stopifnot(names(payload)[idx] %in% c("numberOfRows", "abovePrice", "belowPrice",
609-
"aboveVolume", "marketCapAbove", "marketCapBelow",
610-
"couponRateAbove", "couponRateBelow",
611-
"excludeConvertible", "averageOptionVolumeAbove"))
612-
# Convert NA -> ""
613-
payload[idx] <- ""
614-
615587
msg <- c(msg, tickerId, private$sanitize(payload))
616588

617589
private$encodeMsg(msg)

R/IBWrap.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
IBWrap <- R6::R6Class("IBWrap",
1+
IBWrap <- R6Class("IBWrap",
22

33
class= FALSE,
44
cloneable= FALSE,

R/IBWrapSimple.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
IBWrapSimple <- R6::R6Class("IBWrapSimple",
1+
IBWrapSimple <- R6Class("IBWrapSimple",
22

33
class= FALSE,
44
cloneable= FALSE,
@@ -73,7 +73,7 @@ IBWrapSimple <- R6::R6Class("IBWrapSimple",
7373
execDetails= function(reqId, contract, execution) {
7474
self$context$ex_contract <- contract
7575
self$context$execution <- execution
76-
cat("execDetails:", reqId, contract$symbol, execution$side, execution$shares, execution$price, "\n")
76+
cat("execDetails:", reqId, contract$symbol, execution$time, execution$side, execution$shares, execution$price, "\n")
7777
},
7878

7979
execDetailsEnd= function(reqId)

R/factory.R

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
22
# Helper functions to generate IB structs
33
#
44

5-
IBContract <- function(symbol="", secType="STK", exchange="SMART",
6-
currency="USD", conId=0L, localSymbol="")
5+
setfields <- function(s, ...)
76
{
8-
res <- Contract
7+
args <- list(...)
98

10-
res$conId <- conId
11-
res$symbol <- symbol
12-
res$secType <- secType
13-
res$exchange <- exchange
14-
res$currency <- currency
15-
res$localSymbol <- localSymbol
9+
k <- names(args)
1610

17-
res
18-
}
11+
stopifnot(!is.null(k))
1912

20-
IBOrder <- function(action="BUY", totalQuantity, orderType="LMT", lmtPrice)
21-
{
22-
res <- Order
13+
idx <- match(k, names(s))
2314

24-
res$action <- action
25-
res$totalQuantity <- totalQuantity
26-
res$orderType <- orderType
27-
res$lmtPrice <- lmtPrice
15+
stopifnot(!is.na(idx))
2816

29-
res
17+
s[idx] <- args
18+
19+
s
3020
}
3121

22+
IBContract <- function(...) setfields(Contract, ...)
23+
24+
IBOrder <- function(...) setfields(Order, ...)
3225

3326
#
3427
# Generate Condition structs

0 commit comments

Comments
 (0)