Introduction
With the release of TWS API 10.40, Interactive Brokers has introduced the Synchronous API Wrapper class, combining the functionality of EClient and EWrapper. This class provides a synchronous structure so that values are returned directly through the function call rather than handled by a separate streaming class.
The interface is exclusively available through the Python programming language.
The content shown here is an example of what the Sync Wrapper structure looks like and how it can be used to build a contract, request market data, then place an order.
Initialize our connection
This will display "Connected to TWS" in our console.
For those familiar with the standard Trader Workstation API, this function call for connect_and_start consolidate the requests for connect() and run() while also splitting the run() functionality to it’s own thread.
Establish Our Contract Object
Now that the session is connect, we are able to start working with the API.
In this scenario, we will define and qualify a Contract object to trade. Here we are trading AAPL, though the procedure can be interchanged with any supported product at Interactive Brokers.
Contract details requests will return all contracts the match the details of our contract object in a list.
Because a list is returned, we are taking the first (or 0 index) contract returned.
This returns a single qualified contract which can be used in market data requests or order submissions.
We can save time in future sessions by storing information like the ConID and Exchange to a database for future reference.
Request Market Data
With a qualified contract, we begin acting on the behavior. In this case, we need to find the current price of the contract prior to submitting a trade.
This is done with get_market_data_snapshot() and retrieving the tick type from the returned list.
This will return a dictionary of tick types.
Create and Place an Order
With the price of the instrument in hand we can make an informed decision to price our trade.
We would start by defining the attributes of our order object, including those required parameters such as action, orderType, totalQuantity, tif, and because of our orderType, we’ll also assign lmtPrice. The lmtPrice is precisely why the market data was needed in this scenario.
The Order class can then be submitted to the market using the place_order_sync function call.
The returned order status object includes details like our current 'status' and orderId which can be used to track the order, or cancel it if need be.

