Brokerage

class quanttrader.brokerage.backtest_brokerage.BacktestBrokerage(events_engine, data_board)

Market order is immediately filled. Limit or stop order is saved to _active_orders for next tick

__init__(events_engine, data_board)

Initialize Backtest Brokerage.

Parameters
  • events_engine – send fill_event to event engine

  • data_board – retrieve latest price from data_board

cancel_order(order_id)

Handle cancel order request from client.

Parameters

order_id – order id of the order to be canceled

Returns

no return; cancel feedback is pushed into message queue

next_order_id()

Return next available order id for client to use.

Returns

next available new order id

on_tick(tick_event)

Cross standing orders against new tick_event

Market order can be potentially saved and then filled here against tomorrow’s open price

Parameters

tick_event – new tick just came in

Returns

no return; if orders are filled, they are pushed into message queue

place_order(order_event)

Place and fill client order; return fill event.

Market order is immediately filled, no latency or slippage the alternative is to save the orders and fill in on_tick function

Parameters

order_event – client order received

Returns

no return; fill_event is pushed into message queue

reset()

Reset Backtest Brokerage.

class quanttrader.brokerage.ib_brokerage.InteractiveBrokers(msg_event_engine, tick_event_engine, account: str)
__init__(msg_event_engine, tick_event_engine, account: str)

Initialize InteractiveBrokers brokerage.

Currently, the client is strongly coupled to broker without an incoming queue, e.g. client calls broker.place_order to place order directly.

Parameters
  • msg_event_engine – used to broadcast messages the broker generates back to client

  • tick_event_engine – used to broadcast market data back to client

  • account – the IB account

cancel_all_orders()

Cancel all standing orders, for example, before one wants to shut down completely for some reasons.

cancel_historical_data(reqid)

Cancel historical data request. Usually not necessary.

Parameters

reqid – the historical data request id

cancel_order(order_id)

Cancel client order.

Parameters

order_id – order id of the order to be canceled

Returns

no return. If order is successfully canceled, IB will return an orderstatus message.

connect(host='127.0.0.1', port=7497, clientId=0)

Connect to IB. Request open orders under clientid upon successful connection.

Parameters
  • host – host address

  • port – socket port

  • clientId – client id

static contract_to_symbol(ib_contract)

Convert IB contract to full symbol

Parameters

ib_contract – IB contract

Returns

full symbol

disconnect()

Disconnect from IB

heartbeat()

Request server time as heartbeat

static ib_order_to_order(ib_order)

Convert IB order to order event

Parameters

ib_order – IB representation of order

Returns

internal representation of order

log(msg)

Broadcast server log message through message queue

Parameters

msg – message to be broadcast

Returns

no return; log meesage is placed into message queue

next_order_id()

Return next available order id

Returns

next order id available for next orders

static order_to_ib_order(order_event)

Convert order event to IB order

Parameters

order_event – internal representation of order

Returns

IB representation of order

place_order(order_event)

Place order to IB

Parameters

order_event – client order to be placed

Returns

no return. An order event is pushed to message queue with order status Acknowledged

reqCurrentTime()

Request server time on broker side

request_historical_data(symbol, end=None)

Request 1800 S (30 mins) historical bar data from Interactive Brokers.

Parameters
  • symbol – the contract whose historical data is requested

  • end – the end time of the historical data

Returns

no returns; data is broadcasted through message queue

request_historical_ticks(symbol, start_time, reqtype='TICKS')

Request historical time and sales data from Interactive Brokers. See here https://interactivebrokers.github.io/tws-api/historical_time_and_sales.html

Parameters
  • symbol – the contract whose historical data is requested

  • start_time – i.e. “20170701 12:01:00”. Uses TWS timezone specified at login

  • reqtype – TRADES, BID_ASK, or MIDPOINT

Returns

no returns; data is broadcasted through message queue

setServerLogLevel(level=1)

Set server side log level or the log messages received from server.

Parameters

level – log level

subscribe_account_summary()

Request account summary from broker

subscribe_market_data(sym)

Subscribe market L1 data. Market data for this symbol will then be streamed to client.

Parameters

sym – the symbol to be subscribed.

subscribe_market_datas()

Subscribe market L1 data for all symbols used in strategies. Market data for this symbol will then be streamed to client.

subscribe_market_depth(sym)

Subscribe market L2 data. Market data for this symbol will then be streamed to client.

Parameters

sym – the symbol to be subscribed.

subscribe_positions()

Request existing positions from broker

static symbol_to_contract(symbol)

Convert fulll symbol string to IB contract

TODO CL.HO BAG 174230608 1 NYMEX 257430162 1 NYMEX NYMEX # Inter-comdty ES.NQ BAG 371749798 1 GLOBEX 371749745 1 GLOBEX GLOBEX # Inter-comdty CL.HO BAG 257430162 1 NYMEX 174230608 1 NYMEX NYMEX

Parameters

symbol – full symbol, e.g. AMZN STK SMART

Returns

IB contract

unsubscribe_account_summary()

Stop receiving account summary from broker

unsubscribe_market_data(sym)

Unsubscribe market L1 data. Market data for this symbol will stop streaming to client.

Parameters

sym – the symbol to be subscribed.

unsubscribe_market_depth(sym)

Unsubscribe market L2 data. Market data for this symbol will stop streaming to client.

Parameters

sym – the symbol to be subscribed.

unsubscribe_positions()

Stop receiving existing position message from broker.