deephaven_ib

class BarDataType(value)[source]

Bases: Enum

Bar data type.

ADJUSTED_LAST = 6

Bid/Ask prices.

AGGTRADES = 15

Aggregate trade prices.

ASK = 4

Ask prices.

BID = 3

Bid prices.

BID_ASK = 5

Bid/Ask prices.

FEE_RATE = 10

Fee rate.

HISTORICAL_VOLATILITY = 7

Historical volatility.

MIDPOINT = 2

Midpoint prices.

OPTION_IMPLIED_VOLATILITY = 8

Option implied volatility.

REBATE_RATE = 9

Rebate rate.

TRADES = 1

Trade prices.

YIELD_ASK = 12

Ask yield.

YIELD_BID = 11

Bid yield.

YIELD_BID_ASK = 13

Bid/Ask yield.

YIELD_LAST = 14

Last yield.

class BarSize(value)[source]

Bases: Enum

Bar data sizes.

DAY_1 = '1 day'

1 day bar.

HOUR_1 = '1 hour'

1 hour bar.

HOUR_2 = '2 hour'

2 hour bar.

HOUR_3 = '3 hour'

3 hour bar.

HOUR_4 = '4 hour'

4 hour bar.

HOUR_8 = '8 hour'

8 hour bar.

MIN_1 = '1 min'

1 minute bar.

MIN_10 = '10 mins'

10 minute bar.

MIN_15 = '15 mins'

15 minute bar.

MIN_2 = '2 mins'

2 minute bar.

MIN_20 = '20 mins'

20 minute bar.

MIN_3 = '3 mins'

3 minute bar.

MIN_30 = '30 mins'

30 minute bar.

MIN_5 = '5 mins'

5 minute bar.

MONTH_1 = '1M'

1 month bar.

SEC_1 = '1 sec'

1 second bar.

SEC_10 = '10 secs'

10 second bar.

SEC_15 = '15 secs'

15 second bar.

SEC_30 = '30 secs'

30 second bar.

SEC_5 = '5 secs'

5 second bar.

WEEK_1 = '1W'

1 week bar.

class Duration(value)[source]

Bases: object

Time period to request data for.

static days(value)[source]

Create a duration of a specified number of days.

Parameters:

value (int) – number of days

Return type:

Duration

Returns:

A duration.

static months(value)[source]

Create a duration of a specified number of months.

Parameters:

value (int) – number of months

Return type:

Duration

Returns:

A duration.

static seconds(value)[source]

Create a duration of a specified number of seconds.

Parameters:

value (int) – number of seconds

Return type:

Duration

Returns:

A duration.

static weeks(value)[source]

Create a duration of a specified number of weeks.

Parameters:

value (int) – number of weeks

Return type:

Duration

Returns:

A duration.

static years(value)[source]

Create a duration of a specified number of years.

Parameters:

value (int) – number of years

Return type:

Duration

Returns:

A duration.

class IbSessionTws(host='', port=7497, client_id=0, download_short_rates=True, order_id_strategy=OrderIdStrategy.INCREMENT, read_only=True, is_fa=False)[source]

Bases: object

IB TWS session.

NOTE: Some tables are data specific to the current client_id (e.g. orders_submitted). A client_id of 0 includes data manually entered into the TWS session. For example, orders entered by hand.

Parameters:
  • host (str) – The host name or IP address of the machine where TWS is running. Leave blank to connect to the local host. When run inside docker, you probably want host.docker.internal.

  • port (int) – TWS port, specified in TWS on the Configure->API->Socket Port field. By default production trading uses port 7496 and paper trading uses port 7497.

  • client_id (int) –

    A number used to identify this client connection. All orders placed/modified from this client will be associated with this client identifier.

    NOTE: Each client MUST connect with a unique clientId.

  • download_short_rates (bool) – True to download a short rates table.

  • order_id_strategy (OrderIdStrategy) – strategy for obtaining new order ids.

  • read_only (bool) – True to create a read only client that can not trade; false to create a read-write client that can trade. Default is true.

  • is_fa (bool) – True for financial advisor accounts; false otherwise. Default is false.

Tables:
  • errors: an error log.

  • requests: requests to IB.

  • contract_details: details describing contracts of interest. Automatically populated.

  • contracts_matching: contracts matching query strings provided to request_contracts_matching.

  • market_rules: market rules indicating the price increment a contract can trade in. Automatically populated.

  • short_rates: interest rates for shorting securities. Automatically populated if download_short_rates=True.

  • accounts_managed: accounts managed by the TWS session login. Automatically populated.

  • accounts_family_codes: account family. Automatically populated.

  • accounts_groups: account groups. Automatically populated.

  • accounts_allocation_profiles: allocation profiles for accounts. Automatically populated.

  • accounts_value: account values. Automatically populated.

  • accounts_overview: overview of account details. Automatically populated.

  • accounts_summary: account summary. Automatically populated.

  • accounts_positions: account positions. Automatically populated.

  • accounts_pnl: account PNL. Automatically populated.

  • accounts_pnl_single: single PNL. populated by calling request_single_pnl() on a specific contract.

  • news_providers: currently subscribed news sources. Automatically populated.

  • news_bulletins: news bulletins. Automatically populated.

  • news_articles: the content of news articles requested via request_news_article.

  • news_historical: historical news headlines requested via request_news_historical.

  • ticks_price: real-time tick market data of price values requested via request_market_data.

  • ticks_size: real-time tick market data of size values requested via request_market_data.

  • ticks_string: real-time tick market data of string values requested via request_market_data.

  • ticks_efp: real-time tick market data of exchange for physical (EFP) values requested via request_market_data.

  • ticks_generic: real-time tick market data of generic floating point values requested via request_market_data.

  • ticks_option_computation: real-time tick market data of option computations requested via request_market_data.

  • ticks_trade: real-time tick market data of trade prices requested via request_tick_data_historical or request_tick_data_realtime.

  • ticks_bid_ask: real-time tick market data of bid and ask prices requested via request_tick_data_historical or request_tick_data_realtime.

  • ticks_mid_point: real-time tick market data of mid-point prices requested via request_tick_data_historical or request_tick_data_realtime.

  • bars_historical: historical price bars requested via request_bars_historical. Real-time bars change as new data arrives.

  • bars_realtime: real-time price bars requested via request_bars_realtime.

  • orders_submitted: submitted orders FOR THE THE CLIENT ID. A client ID of 0 contains manually entered orders. Automatically populated.

  • orders_status: order statuses. Automatically populated.

  • orders_completed: completed orders. Automatically populated.

  • orders_exec_details: order execution details. Automatically populated.

  • orders_exec_commission_report: order execution commission report. Automatically populated.

property client_id

Client ID.

Returns:

Client ID.

connect()[source]

Connect to an IB TWS session. Raises an exception if already connected.

Return type:

None

Returns:

None

Raises:

Exception – problem executing action.

disconnect()[source]

Disconnect from an IB TWS session.

Return type:

None

Returns:

None

get_registered_contract(contract)[source]

Gets a contract that has been registered in the framework. The registered contract is confirmed to exist in the IB system and contains a complete description of the contract.

Parameters:

contract (Contract) – contract to search for

Return type:

RegisteredContract

Returns:

A contract that has been registered with deephaven-ib.

Raises:

Exception – problem executing action.

property host

Client host.

Returns:

Client host.

is_connected()[source]

Is there a connection with TWS?

Return type:

bool

Returns:

an indication if the client is connected to TWS.

order_cancel(order_id)[source]

Cancels an order.

Parameters:

order_id (int) – order ID

Return type:

None

Returns:

None

Raises:

Exception – problem executing action.

order_cancel_all()[source]

Cancel all open orders.

Return type:

None

Returns:

None

Raises:

Exception – problem executing action.

order_place(contract, order)[source]

Places an order.

Parameters:
Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

property port

Client port.

Returns:

Client port.

property read_only

Is the client read only?

Returns:

a boolean indicating if the client is read only.

request_account_overview(account, model_code='')[source]

Request portfolio overview updates. Results are returned in the accounts_overview table.

Parameters:
  • account (str) – Account to request an overview for. “All” requests for all accounts.

  • model_code (str) – Model portfolio code to request an overview for.

Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

request_account_pnl(account='All', model_code='')[source]

Request PNL updates. Results are returned in the accounts_pnl table.

Parameters:
  • account (str) – Account to request PNL for. “All” requests for all accounts.

  • model_code (str) – Model portfolio code to request PNL for.

Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

request_account_positions(account, model_code='')[source]

Request portfolio position updates. Results are returned in the accounts_positions table.

Parameters:
  • account (str) – Account to request positions for. “All” requests for all accounts.

  • model_code (str) – Model portfolio code to request positions for.

Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

request_bars_historical(contract, duration, bar_size, bar_type, end=None, market_data_type=MarketDataType.FROZEN, keep_up_to_date=True)[source]

Requests historical bars for a contract. Results are returned in the bars_historical table.

Registered contracts that are associated with multiple contract details produce multiple requests.

Parameters:
Return type:

List[Request]

Returns:

All of the requests created by this action.

Raises:

Exception – problem executing action.

request_bars_realtime(contract, bar_type, bar_size=5, market_data_type=MarketDataType.FROZEN)[source]

Requests real time bars for a contract. Results are returned in the bars_realtime table.

Registered contracts that are associated with multiple contract details produce multiple requests.

Parameters:
  • contract (RegisteredContract) – contract data is requested for

  • bar_type (BarDataType) – Type of bars that will be returned.

  • bar_size (int) – Bar size in seconds.

  • market_data_type (MarketDataType) – Type of market data to return after the close.

Return type:

List[Request]

Returns:

All of the requests created by this action.

Raises:

Exception – problem executing action.

request_contracts_matching(pattern)[source]

Request contracts matching a pattern. Results are returned in the contracts_matching table.

Parameters:

pattern (str) – pattern to search for. Can include part of a ticker or part of the company name.

Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

request_market_data(contract, generic_tick_types=[], snapshot=False, regulatory_snapshot=False)[source]

Request market data for a contract. Results are returned in the ticks_price, ticks_size, ticks_string, ticks_efp, ticks_generic, and ticks_option_computation tables.

Registered contracts that are associated with multiple contract details produce multiple requests.

Parameters:
  • contract (RegisteredContract) – contract data is requested for

  • generic_tick_types (List[GenericTickType]) – generic tick types being requested

  • snapshot (bool) – True to return a single snapshot of Market data and have the market data subscription cancel. Do not enter any genericTicklist values if you use snapshots.

  • regulatory_snapshot (bool) – True to get a regulatory snapshot. Requires the US Value Snapshot Bundle for stocks.

Return type:

List[Request]

Returns:

A Request.

Raises:

Exception – problem executing action.

request_news_article(provider_code, article_id)[source]

Request the text of a news article. Results are returned in the news_articles table.

Parameters:
  • provider_code (str) – short code indicating news provider, e.g. FLY

  • article_id (str) – id of the specific article

Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

request_news_historical(contract, start, end, provider_codes=None, total_results=100)[source]

Request historical news for a contract. Results are returned in the news_historical table.

Registered contracts that are associated with multiple contract details produce multiple requests.

Parameters:
Return type:

List[Request]

Returns:

All of the requests created by the action.

Raises:

Exception – problem executing action.

request_single_pnl(contract, account, model_code='')[source]

Request PNL updates for a single position. Results are returned in the accounts_pnl_single table.

Parameters:
  • contract (RegisteredContract) – contract data is requested for.

  • account (str) – Account to request PNL for.

  • model_code (str) – Model portfolio code to request PNL for.

Return type:

Request

Returns:

A Request.

Raises:

Exception – problem executing action.

request_tick_data_historical(contract, tick_type, number_of_ticks, start=None, end=None, market_data_type=MarketDataType.FROZEN, ignore_size=False)[source]

Requests historical tick-by-tick data. Results are returned in the ticks_trade, ticks_bid_ask, and ticks_mid_point tables.

Registered contracts that are associated with multiple contract details produce multiple requests.

Parameters:
Return type:

List[Request]

Returns:

All of the requests created by this action.

Raises:

Exception – problem executing action.

request_tick_data_realtime(contract, tick_type, number_of_ticks=0, ignore_size=False)[source]

Requests real-time tick-by-tick data. Results are returned in the ticks_trade, ticks_bid_ask, and ticks_mid_point tables.

Registered contracts that are associated with multiple contract details produce multiple requests.

Parameters:
  • contract (RegisteredContract) – contract data is requested for

  • tick_type (TickDataType) – Type of market data to return.

  • number_of_ticks (int) – Number of historical ticks to request.

  • ignore_size (bool) – should size values be ignored.

Return type:

List[Request]

Returns:

All of the requests created by this action.

Raises:

Exception – problem executing action.

set_market_data_type(market_data_type)[source]

Sets the default type of market data.

Parameters:

market_data_type (MarketDataType) – market data type

Return type:

None

Returns:

None

Raises:

Exception – problem executing action.

property tables

Gets a dictionary of all data tables.

Returns:

Dictionary of all data tables.

property tables_raw

Gets a dictionary of all raw data tables. Raw tables are just as the data comes from IB.

Returns:

Dictionary of all raw data tables.

class MarketDataType(value)[source]

Bases: Enum

Type of market data to use.

DELAYED = 3

Delayed market data.

FROZEN = 2

Real-time market data during regular trading hours, and frozen prices after the close.

REAL_TIME = 1

Real-time market data.

class OrderIdStrategy(value)[source]

Bases: Enum

Strategy used to obtain order IDs.

BASIC = (False, True)

Request a new order ID from TWS every time one is needed.

INCREMENT = (False, False)

Use the initial order ID and increment the value upon every call. This is fast, but it may fail for multiple, concurrent sessions.

RETRY = (True, True)

Request a new order ID from TWS every time one is needed. Retry if TWS does not respond quickly. TWS seems to have a bug where it does not always respond.

class RegisteredContract(query_contract, contract_details)[source]

Bases: object

Details describing a financial instrument that has been registered in the framework. This can be a stock, bond, option, etc.

When some contracts are registered, details on multiple contracts are returned.

is_multi()[source]

Does the contract have multiple contract details?

Return type:

bool

Returns:

An indication if the requested contract is associated with multiple contract details.

class Request(request_id, cancel_func=None)[source]

Bases: object

IB session request.

cancel()[source]

Cancel the request.

Return type:

None

Returns:

None

Raises:

Exception – request is not cancellable.

is_cancellable()[source]

Is the request cancellable?

Return type:

bool

Returns:

An indication if the request is cancellable.

class TickDataType(value)[source]

Bases: Enum

Tick data type.

BID_ASK = 'BidAsk'

“Most recent bid and ask.

LAST = 'Last'

Most recent trade.

MIDPOINT = 'MidPoint'

Most recent midpoint.