cefi.handler.ibkr.IbHandler#

class cefi.handler.ibkr.IbHandler(**kwargs)[source]#

Bases: CexClient

CEX client for IBKR using ibind library

Requires ibind library: pip install ibind Requires IBeam gateway running: Voyz/ibeam

Configuration in settings.toml: [cex.ibkr] enabled = true host = “http://127.0.0.1:5000” # IBeam URL account = “YOUR_IBKR_ACCOUNT_UXXXXXXX” mapping = [

{

id = “EURUSD”, alt = “EUR.USD”, secType = “CASH”, exchange = “IDEALPRO”, currency = “USD”,

}, {

id = “SPY”, alt = “SPY”, secType = “STK”, exchange = “SMART”, currency = “USD”,

}, # Add other mappings here…

]

Initializes the IBKR handler using ibind. Loads configuration from kwargs.

Methods

calculate_pnl([from_date, to_date])

Calculate the PnL for a given start date.

execute_order(order_params)

Execute order using IBKR REST API

get_account_balance()

Get account balance summary

get_account_pnl([period, from_date, to_date])

Return account pnl.

get_account_position()

Get current positions

get_bid(instrument)

Asynchronously retrieves the bid for the specified instrument.

get_info()

Retrieves account information (Summary)

get_instrument_decimals(instrument)

Get the number of decimal places for the token.

get_offer(symbol)

Asynchronously fetches a ask/offer quote for the specified instrument.

get_order_amount(quantity, instrument[, ...])

Calculate the order amount based on the risk percentage or money amount.

get_quote(instrument)

Get market data snapshot for instrument

get_trade_confirmation(trade, instrument, action)

Asynchronously retrieves the trade confirmation for a given trade, instrument, and action.

get_trading_asset_balance()

Alias for get_account_balance

modify_position(order_params)

Modify opened position

pre_order_checks(order_params)

Implement custom pre-trade checks if needed

replace_instrument(instrument)

Replace instrument by an alternative instrument, if the instrument is not in the mapping, it will be ignored.

search_contract(instrument)

Find contract CONID using symbol and mapping configuration.

shutdown()

async get_info()[source]#

Retrieves account information (Summary)

async get_quote(instrument)[source]#

Get market data snapshot for instrument

async get_account_balance()[source]#

Get account balance summary

async get_account_position()[source]#

Get current positions

async search_contract(instrument)[source]#

Find contract CONID using symbol and mapping configuration. Uses secType, exchange, currency from the mapping.

async execute_order(order_params)[source]#

Execute order using IBKR REST API

async calculate_pnl(from_date=None, to_date=None)#

Calculate the PnL for a given start date.

Parameters:
  • from_date – The start date for which to calculate PnL.

  • to_date – The end date for which to calculate PnL.

Returns:

The calculated PnL value.

Return type:

pnl

async get_account_pnl(period=None, from_date=None, to_date=None)#

Return account pnl.

Parameters:

None

Returns:

pnl

async get_bid(instrument)#

Asynchronously retrieves the bid for the specified instrument.

Parameters:
  • instrument – The instrument for which

  • retrieved. (the bid is to be)

Returns:

The bid for the specified instrument.

async get_instrument_decimals(instrument)#

Get the number of decimal places for the token.

Returns:

The number of decimal places for the token.

Return type:

int

async get_offer(symbol)#

Asynchronously fetches a ask/offer quote for the specified instrument.

Parameters:

instrument – The instrument for which the quote is to be fetched.

Returns:

The fetched quote.

async get_order_amount(quantity, instrument, is_percentage=True)#

Calculate the order amount based on the risk percentage or money amount.

Parameters:
  • quantity – The quantity of the order.

  • instrument – The instrument of the asset.

  • is_percentage – True if quantity is a risk percentage,

  • amount. (False if it is a money)

Returns:

The calculated order amount.

async get_trade_confirmation(trade, instrument, action)#

Asynchronously retrieves the trade confirmation for a given trade, instrument, and action.

Parameters:
  • self – The object instance

  • trade – The trade object

  • instrument – The instrument for the trade

  • action – The action for the trade

Returns:

A string containing the trade confirmation, or None if an error occurs.

async get_trading_asset_balance()[source]#

Alias for get_account_balance

async modify_position(order_params)#

Modify opened position

Parameters:

order_params (dict)

Returns:

trade_confirmation(dict)

async replace_instrument(instrument)#

Replace instrument by an alternative instrument, if the instrument is not in the mapping, it will be ignored.

Parameters:

order (dict)

Returns:

dict

async shutdown()#
async pre_order_checks(order_params)[source]#

Implement custom pre-trade checks if needed