2017-12-06 2 views
0

여러 주식의 특정 시간 프레임에서 가격 조치 심사를위한 주식 스크리너를 개발하려고합니다. 따라서 충분한 메모리 버퍼가있는 여러 계측기에서 IB로부터 데이터를 가져 오는 작업을 자동화해야합니다.대화 형 중개인 Python API 연결 버퍼

스크립트를 한 번만 실행하면 서버를 원활하게 연결할 수 있습니다.

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 핵심 코드를 편집 할 필요가 없습니다.)

API 지원을 위해 IB 고객 서비스에 연락하려고했지만 오류 코드가 없어서 헛된 일이었습니다.

다음은 IB 연결과 관련된 코드입니다.

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   

답변

1

저는 IBpy 라이브러리를 매일 사용하고 있으며 버퍼 크기에 문제가 없었습니다. 환경 (OS, CPU, RAM)에 대해 더 자세히 알려주시겠습니까? IBpy에서 다른 예제를 사용 했습니까?

IB와의 통신은 쉽지 않지만 일부는 오류가 발생한 위치를 알게되며 메시지로 IB에 자세한 정보를 보내면 사용자에게 도움이 될 것입니다.

관련 문제