Interactive Brokers
The integration with Interactive Brokers supports both:
-
Live Data feeding
-
Live Trading
Test any strategy thoroughly with a Paper Trading account or the TWS Demo before going in production.
IBStore - the store
The store is the keystone of the live data feed/trade support, providing a layer of adaptation between the IbPy module and the needs of a data feed and a broker proxy. Store是建立在IbPy上的一个自适应层,以便于和Backtrader的交互,提供DataFeed
和 Broker Proxy.
A store 的功能主要有:
- 是IB实体的交易中心: IbStore是IB实体交易的中心
- 通过IBStore.getbroker(*args, **kwargs)获取broker实例
- 通过IBStore.getdata(*args, kwargs)提供data feed 实例
在IbStore中kwargs 和普通的data feed 基本相同,主要是: dataname, fromdate, todate, sessionstart, sessionend, timeframe, compression
IBStore提供了以下功能:
-
Connectivity target (host and port parameters) 和交易工作站或IB网管相连
-
Identification (clientId parameter) 连接认证
-
Re-connectivity control (reconnect and timeout parameters) 重连接控制
-
Time offset check (timeoffset parameters, see below) 时间补偿的检验
-
Notification and debugging 信息提醒和bug信息的转发
notifyall (default: False): in this case each and every message received from TWS will be print out to standard output
_debug (default: False):in this case any error message (many are simply informative) sent by IB will be relayed to Cerebro/Strategy
IBData feeds
Data Options
-
Historical download requests 历史数据的下载请求可能被拆分
-
RealTime Data in 3 flavors 支持三种事件获取实时数据
- tickPrice events (via IB reqMktData)
- tickString events (aka RTVolume (via IB reqMktData)
- RealTimeBars events (via IB reqRealTimeBars)
-
Backfilling 除非用户请求下载历史数据,data feed 会自动的回填数据:
- At the start: with the maximum possible duration. Example: for a Days/1 (timeframe/compression) combination the maximum default duration at IB is 1 year and this is the amount of time that will be backfilled
- After a data disconnection: in this case the amount of data downloaded for the backfilling operation will be reduced to the minimum by looking at the latest data received before the disconnection.
Data Contract Check
During the start phase, the data feed will try to download the details of the specified contract (see the reference for how to specify it). If no such contract is found or multiple matches are found, the data will refuse to carry on and will notify it to the system. Some examples.
data = ibstore.getdata(dataname='TWTR') # Twitter
data = ibstore.getdata(dataname='AAPL') # Error -> multiple contracts
data = ibstore.getdata(dataname='AAPL-STK-SMART-USD') # 1 contract found
Data Notifications
The data feed will report the current status via one or more of the following (check the Cerebro and Strategy reference)
-
Cerebro.notify_data (if overriden)n
-
A callback addded with Cerebro.adddatacb
-
Strategy.notify_data (if overriden)
Example:
class IBStrategy(bt.Strategy):
def notify_data(self, data, status, *args, **kwargs):
if status == data.LIVE: # the data has switched to live data
# do something
pass
Notifications includes:
- CONNECTED
- DISCONNECTED
- CONNBROKEN
- NOTSUBSCRIBED
- DELAYED
- LIVE
Developers of strategies should consider which actions to undertake in cases like when a disconnection takes place or when receiving delayed data.
网友评论