Creates a specified number of persistent async socket connections to Interactive Brokers. Returns them in a list, initialized and ready to send and receive data.

create_new_connections(
  number_of_new_socks = 1,
  include_master = FALSE,
  include_tws = FALSE,
  host = active_itr_option("host"),
  port = active_itr_option("port")
)

Arguments

number_of_new_socks

Integer, default = 1. Total number of new connections to open, including Master and TWS (if requested). Interactive Brokers allows a maximum of 32 connections open at a time, per username.

include_master

Boolean, default = FALSE. If TRUE, then a connection using the master_client_idMaster Client ID will be opened and assigned the name "master".

include_tws

Boolean, default = FALSE. If TRUE, then a connection using client_id_0Client ID 0 will be included in the result and assigned the name "tws".

host

Character vector of length 1 giving the hostname for the session of IB Gateway or TWS to which the connection(s) should be made. Usually is "localhost" because most users connect to an instance of TWS/IBG that is running on the same computer as InteractiveTradeR.

port

Character vector length 1 or numeric giving the port number for the session of IB Gateway or TWS to which the connection(s) should be made.

Value

NULL. This function is called for its side effects: opening sockets and storing pointers that refer to them within InteractiveTradeR.

Details

Client IDs In InteractiveTradeR, Client IDs are not assigned by the user; instead, they are tracked internally. The user may assign subscriptions and requests to the Master Client, the TWS Client, (Client ID = 0) or a generic async socket.

See also

Examples

# Creating and Disconnecting IB Sockets # The following example walks you through creating and disconnecting the three # kinds of async socks: Master, TWS, and general. After each step, socks() is # called to view the current connected sockets so that you can observe what's # been created / disconnected. # As you go through this example yourself, remember that it's helpful to use the # "connections" window in the Interactive Brokers apps. In IB Gateway, you can # easily see connections from the default window; in TWS, it can be accessed by # clicking on the "DATA" text in the upper right-hand corner of the user # interface window of the app. # Disconnect all sockets that might exist so that we start with a clean slate. disconnect()
#> Sock drawer cleaned out! #> Disconnected 1 sockets.
# Create five (5) new socket connections. Include a Master and a TWS Socket. create_new_connections(5, include_master = TRUE, include_tws = TRUE) # Print the socket connections you created socks()
#> master: A socket connection on '->localhost:7497'. #> Client ID: 22998 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_1: A socket connection on '->localhost:7497'. #> Client ID: 1 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_2: A socket connection on '->localhost:7497'. #> Client ID: 2 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_3: A socket connection on '->localhost:7497'. #> Client ID: 3 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #> tws: A socket connection on '->localhost:7497'. #> Client ID: 0 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #>
# Remove two general-purpose (i.e., not TWS or Master) sockets: disconnect(2)
#> Disconnected 2 sockets.
# See that two general-purpose connections are now gone socks()
#> master: A socket connection on '->localhost:7497'. #> Client ID: 22998 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_1: A socket connection on '->localhost:7497'. #> Client ID: 1 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> tws: A socket connection on '->localhost:7497'. #> Client ID: 0 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #>
# Create two more, view with socks() create_new_connections(2) socks()
#> master: A socket connection on '->localhost:7497'. #> Client ID: 22998 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_1: A socket connection on '->localhost:7497'. #> Client ID: 1 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_2: A socket connection on '->localhost:7497'. #> Client ID: 2 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #> sock_3: A socket connection on '->localhost:7497'. #> Client ID: 3 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #> tws: A socket connection on '->localhost:7497'. #> Client ID: 0 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #>
# Disconnect only the TWS and the Master, view with socks() disconnect( number_to_disconnect = 2, disconnect_master = TRUE, disconnect_tws = TRUE )
#> Master socket disconnected. #> TWS socket (Client ID = 0) disconnected.
#> sock_1: A socket connection on '->localhost:7497'. #> Client ID: 1 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_2: A socket connection on '->localhost:7497'. #> Client ID: 2 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #> sock_3: A socket connection on '->localhost:7497'. #> Client ID: 3 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #>
# Re-connect TWS and Master, view with socks() create_new_connections( number_of_new_socks = 2, include_master = TRUE, include_tws = TRUE ) socks()
#> master: A socket connection on '->localhost:7497'. #> Client ID: 22998 Server Version: 155 Connection time: 2020-03-17 13:11:58 #> Subscriptions: #> None #> sock_1: A socket connection on '->localhost:7497'. #> Client ID: 1 Server Version: 155 Connection time: 2020-03-17 13:11:56 #> Subscriptions: #> None #> sock_2: A socket connection on '->localhost:7497'. #> Client ID: 2 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #> sock_3: A socket connection on '->localhost:7497'. #> Client ID: 3 Server Version: 155 Connection time: 2020-03-17 13:11:57 #> Subscriptions: #> None #> tws: A socket connection on '->localhost:7497'. #> Client ID: 0 Server Version: 155 Connection time: 2020-03-17 13:11:58 #> Subscriptions: #> None #>
# Disconnect ALL socks, view with socks() disconnect()
#> Sock drawer cleaned out! #> Disconnected 1 sockets.