req_ids: Request IDs

View source: R/req_ids.R

req_idsR Documentation

Request IDs

Description

Fetch the next valid order ID available for the socket specified by channel. In InteractiveTradeR, the need for req_ids() is largely abstracted away and it is unlikely that the user will need this function. However, it is used by other functions in the package and is included here for completeness.

Usage

req_ids(channel = NULL)

Arguments

channel

One of the following:

  • Not Specified (Default): Opens a new connection to IB, uses it to issue the request and retrieve the response, and closes connection behind itself upon completion.

  • The Name of a Sock: Character vector, length 1. The name of an open, connected socket in the sock_drawer; e.g., "master", "tws", or "sock_123"

  • Numeric Client ID: Numeric, length 1. The client ID for which open orders are to be retrieved; e.g., 0, 874, 123. If a client ID is passed, and no socket in the sock_drawer is connected on that ID, then a new socket will be opened on that ID, and closed upon function exit.

  • A sockconn Connection: An open connection object of class "sockconn", connected to the IB API; e.g., sock_drawer$tws

Details

More information can be found in IB's documentation here: Next Valid Identifier

Unlike orders placed using the TWS graphical user interface – which all use orderId = 0 – every order placed by an API on a particular socket must have a unique integer order identifier. This identifier is the "ID" meant by the function's name "Request IDs".

Keeping track of order IDs is necessary because in practice, any number of different users may need to submit orders – each requiring a unique order ID – to a given account or account structure. Although each user may keep track of order IDs locally, there is no way to be sure that some other user hasn't already used the ID without querying the IB servers.

req_ids() should, perhaps, really be named "req_id", without the "s", because it only returns the emphnext valid id – a single integer. The plural form of the function's name is due to historical reasons arising from older versions of the IB API system.

The function req_ids() fulfills this role by querying IB and returning the next valid ID: the lowest-valued integer that hasn't yet been used as an order ID by any user on a particular client, and therefore may be utilized as the ID for a new order to be placed on that socket.

Value

Integer. The next valid order ID available on the specified socket.

See Also

Other orders: cancel_order(), place_order(), req_all_open_orders(), req_auto_open_orders(), req_completed_orders(), req_open_orders()

Examples

# Create some socket connections
create_new_connections(5, include_master = TRUE, include_tws = TRUE)

# Fetch the next valid ID on the master socket:
req_ids("master")

# Fetch the next valid ID on the TWS socket:
req_ids("tws")

# Note that opening orders within TWS itself does not affect the next valid TWS
# id -- only orders submitted via the API will cause it to increment.

# Repeat for the other sockets:
req_ids("sock_1")
req_ids("sock_2")
req_ids("sock_3")

# Calling req_ids() in sync mode will work, but isn't of much use because
# because, by definition, the socket isn't specified:
req_ids()

JakeVestal/InteractiveTradeR documentation built on June 5, 2024, 2:21 p.m.