req_open_orders: Request and Bind Open Orders Placed from Within TWS Itself

View source: R/req_open_orders.R

req_open_ordersR Documentation

Request and Bind Open Orders Placed from Within TWS Itself

Description

This function as implemented in InteractiveTradeR differs slightly from the default *reqOpenOrders*() function implemented in the standard API in that 'req_open_orders'() takes no arguments as it may only be invoked for Client ID 0, the TWS GUI client ID.

For requesting open orders on other sockets, use req_all_open_orders to fetch all orders, no matter what socket was used to open them, and then use subsetting to filter the output tibble by client ID to select the orders you're interested in.

The only reason to use 'req_open_orders'() in InteractiveTradeR is due to an additional special property of 'req_open_orders'(): **when called, 'req_open_orders'() causes any and all open orders that were created within the TWS itself to become *bound***.

**Definition of "Bound"**:

In IB jargon, an order is "*bound*" if it is assigned an order ID. By default, orders placed within the TWS do not have order IDs (in other words, they're *unbound*). Because the API requires order IDs to modify or cancel an order, a call to 'req_open_orders'() is necessary to enable the API to cancel or modify orders placed within TWS.

Relevant IB documentation may be found on the Open Orders page.

Usage

req_open_orders()

Value

This function is called for its side effect of updating the treasury, which takes place every time the function executes. Additionally, the function's return value depends upon the value passed in as return_data as follows:

  • If return_data == FALSE: A Boolean success flag, returned invisibly, indicating that the function executed correctly and updated the treasury with any new data retrieved.

  • If return_data == TRUE: Any new data retrieved will be returned in a tibble in addition to being added to the treasury. If no new data is available, returns NULL.

return_data defaults to TRUE unless channel is specified.

Use Case

The default behavior of the TWS is to *not* assign order IDs to orders opened within the app itself. That being the case, consider the situation in which you or a colleague has opened orders from within the TWI app. Because the open orders have no IDs, you have no way to identify them in the API, and are therefore unable to cancel or modify them using the API.

By calling 'req_open_orders'(), all open orders that were opened within the TWS app are returned in a tibble as the function's output. Additionally, **these orders are given an order ID so that they can now be modified/canceled within the API**.

Use 'req_auto_open_orders'() to Assign IDs to Future TWS Orders

By calling req_auto_open_orders, the behavior of the TWS app will be reconfigured to bind all orders created within the app, but will not affect open orders that were opened within the app before 'req_auto_open_orders'() was called.

Best Practice

Make a call to 'req_open_orders'() and 'req_auto_open_orders'() during the initialization section of any Shiny app (or script) created with InteractiveTradeR. This will ensure that:

1) Open orders that were placed within the TWS app before the InteractiveTradeR app was started are assigned an order ID by 'req_open_orders'() so that they are accessible within the API

2) Future orders placed within the TWS app while the InteractiveTradeR app is running are also accessible to the InteractiveTradeR app because the call to 'req_auto_open_orders'() reconfigured the TWS to assign them order IDs by default.

With this practice, any InteractiveTradeR app only ever needs to call 'req_open_orders'() once during initialization.

OPEN_ORDERS and ORDER_STATUSES Treasury Objects

This function updates the following treasury objects:

OPEN_ORDERS A tibble in which each row represents an order that, at the time it was last updated, had not fully filled. The column names are described in the Contract, Order, and OrderState sections of IB's documentation.

ORDER_STATUSES A tibble in which each row represents an order that, at the time it was last updated, had not fully filled. The column names are described in the Order Status section of IB's documentation.

See Also

req_auto_open_orders

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


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