交互式代理Python API连接缓冲区
我正在尝试开发一个股票筛选器,用于在特定时间范围内筛选多只股票的价格行为。因此,我需要在具有足够内存缓冲区的多个仪器上自动从IB提取数据 如果我只运行一次脚本,服务器可以顺利连接交互式代理Python API连接缓冲区,python,api,interactive-brokers,ibpy,Python,Api,Interactive Brokers,Ibpy,我正在尝试开发一个股票筛选器,用于在特定时间范围内筛选多只股票的价格行为。因此,我需要在具有足够内存缓冲区的多个仪器上自动从IB提取数据 如果我只运行一次脚本,服务器可以顺利连接 TWS Time at connection:20171206 12:00:11 CST 成功连接后,我使用ibpy提取历史数据。数据已成功下载,没有问题。 但是,当我尝试再次从相同设置的相同仪器中提取数据时 TWS Time at connection:20171206 12:00:11 CST Server Er
TWS Time at connection:20171206 12:00:11 CST
成功连接后,我使用ibpy提取历史数据。数据已成功下载,没有问题。
但是,当我尝试再次从相同设置的相同仪器中提取数据时
TWS Time at connection:20171206 12:00:11 CST
Server Error: <error id=None, errorCode=None, errorMsg=unpack requires a buffer of 1 bytes>
我每天都在使用IBpy库,我从来没有遇到过缓冲区大小的问题。 也许您会告诉我们更多关于您的环境(操作系统、CPU、RAM)的信息? 其他IBpy的例子有用吗
与IB的通信并不容易,但通过放置一些
打印(“第xx行工作”)
您将知道错误发生的位置,并且通过消息向IB发送更多具体信息,他们将帮助您友好地进行操作。在提取历史数据时也会遇到同样的问题。虽然当我试图提取大量历史数据时,会出现错误消息。试图识别特定实例…我可以提取2m的1min条数据,但尝试提取3m的1min条数据会导致此错误:服务器错误:解包需要1字节的缓冲区
def connect_to_tws(self):
self.tws_conn = Connection.create(port=7497, clientId=5)
self.tws_conn.connect()
self.register_callback_functions()
def contract_creation(self):
self.listbox1.delete(0,END) # clears contents of the listbox
self.tws_conn.cancelHistoricalData(5) #cancels historical data
mySymbol = self.input.symbol # get the symbol from the combobox
contract = self.create_contract(mySymbol,
'STK', # security STK = stock
'SEHK', # exchange
'',# primary exchange
'HKD') # currency
duration = self.input.duration # get the duration ie. 1 D, 1 M, 1 Y
bar_size = self.input.barsize # get the bar size ie. 5 mins, 2 mins, 1 day
self.tws_conn.reqHistoricalData(tickerId = 5, # contract number can be any number
contract=contract, # contract detail from about
endDateTime=self.input.now, # end date and time
durationStr=duration,
barSizeSetting=bar_size,
whatToShow='TRADES', # what to show ie. MIDPOINT, BID, ASK,
useRTH=1, # Regular trading hours 1 = RTH, 0 = all data
formatDate=1) # 1 = 20161021 09:30:00 2 = Unix time (Epoch)
def register_callback_functions(self):
# Assign server messages handling function.
self.tws_conn.registerAll(self.server_handler)
# Assign error handling function.
self.tws_conn.register(self.error_handler, 'Error')
def error_handler(self, msg):
if msg.typeName == 'error'and msg.id != -1:
print ('Server Error:', msg)
def server_handler(self, msg):
if msg.typeName == 'historicalData':
hd_date = msg.date
hd_open = msg.open
hd_high = msg.high
hd_low = msg.low
hd_close = msg.close
hd_volume = msg.volume
str_date = str(hd_date)
str_open = str(hd_open)
str_high = str(hd_high)
str_low = str(hd_low)
str_close = str(hd_close)
str_volume = str(hd_volume)
# creates a string containing date, open, high, low, close, volume
priceData2 = hd_date+","+str_open+","+str_high+","+str_low+","+str_close+","+str_volume
if 'finished' in hd_date:
pass
else:
str_data = hd_date, hd_open, hd_high, hd_low, hd_close, hd_volume
print (str_data) # prints info to the Python shell
self.listbox1.insert(END, priceData2) # adds info to the listbox
elif msg.typeName == "error" and msg.id != -1:
return
def create_contract(self, symbol, sec_type, exch, prim_exch, curr):
contract = Contract()
contract.m_symbol = symbol
contract.m_secType = sec_type
contract.m_exchange = exch
contract.m_primaryExch = prim_exch
contract.m_currency = curr
return contract