如何获得基本比率通用报价 (python 3.x) (Interactive Broker)
How to get Fundamental Ratios generic ticks (python 3.x) (Interactive Broker)
基于以下网站,我想通过 reqMktData "233,236,258"
下载基本比率通用报价
https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html
但是我试了很多次,都无法改正错误。
'Error reading request:Message id 1004. Unable to parse data. java.lang.NumberFormatException: For input string: "Snapshot"']
AttributeError: 'IBWrapper' 对象没有属性 'reqMktData'
备注:"IBWrapper"不是IB官网提供的,由以下link提供:
https://github.com/anthonyng2/ib
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 20 19:37:23 2018
@author: Owner
"""
import time
from IBWrapper import IBWrapper, contract
from ib.ext.EClientSocket import EClientSocket
callback = IBWrapper() # Instantiate IBWrapper. callback
tws = EClientSocket(callback) # Instantiate EClientSocket and return data to
callback
host = "127.0.0.1"
port = 7496
clientId = 25
tws.eConnect(host, port, clientId) # connect to TWS
create = contract() # Instantiate contract class
callback.initiate_variables()
contract_Details = create.create_contract(700, 'STK', 'SEHK', 'HKD')
tickerId = 1004
tws.reqMktData(tickerId,
contract_Details,
"233,236,258",
"ReportSnapshot" #ReportsFinSummary ReportsOwnership ReportsFinStatements
#RESC CalendarReport ReportSnapshot
)
time.sleep(5)
print(callback.reqMktData)
tws.eDisconnect()
认真研究了一下,IBWrapper不支持通用的tick数据,只能导入ibpy
from ib.ext.Contract import Contract
from ib.opt import ibConnection
from time import sleep
import csv
class Downloader(object):
tickType47value = ''
#field4price = ''
def __init__(self):
self.tws = ibConnection('localhost', 7496, 20)
self.tws.register(self.tickPriceHandler, 'TickString')
self.tws.connect()
self._reqId = 1003 # current request id
def tickPriceHandler(self,msg):
if msg.tickType == 47: # tickType=47
self.tickType47value = msg.value
#print('[debug]', msg)
def requestData(self,contract):
self.tws.reqMktData(self._reqId, contract, "233, 236, 258", False) #"233, 236, 258",
self._reqId+=1
def cancelData(self):
#self.tws.cancelMktData(1003)
self.tws.disconnect()
以下 link 都是可用的基本比率 - "headers" 中存在的标记值:
https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html
if __name__=='__main__':
headers = ['TickNo',
'TTMNPMGN',
'NLOW',
'TTMPRCFPS',
'TTMGROSMGN',
'TTMCFSHR',
'QCURRATIO',
'TTMREV',
'TTMINVTURN',
'TTMOPMGN',
'TTMPR2REV',
'AEPSNORM',
'TTMNIPEREM',
'EPSCHNGYR',
'TTMPRFCFPS',
'TTMRECTURN',
'TTMPTMGN',
'QCSHPS',
'TTMFCF',
'LATESTADATE',
'APTMGNPCT',
'AEBTNORM',
'TTMNIAC',
'NetDebt_I',
'PRYTDPCTR',
'TTMEBITD',
'AFEEPSNTM',
'PR2TANBK',
'EPSTRENDGR',
'QTOTD2EQ',
'TTMFCFSHR',
'QBVPS',
'NPRICE',
'YLD5YAVG',
'REVTRENDGR',
'TTMEPSXCLX',
'QTANBVPS',
'PRICE2BK',
'MKTCAP',
'TTMPAYRAT',
'TTMINTCOV',
'TTMREVCHG',
'TTMROAPCT',
'TTMROEPCT',
'TTMREVPERE',
'APENORM',
'TTMROIPCT',
'REVCHNGYR',
'CURRENCY',
'DIVGRPCT',
'TTMEPSCHG',
'PEEXCLXOR',
'QQUICKRATI',
'TTMREVPS',
'BETA',
'TTMEBT',
'ADIV5YAVG',
'ANIACNORM',
'QLTD2EQ',
'NHIG']
stocks=['700']
with open('Your path', 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
csvwriter.writerow(headers)
sleep(0.5)
for x in stocks:
for _ in range(5): #10 #If markets are open, there can be no more than 5 requests pending for the same contract.
dl = Downloader()
c = Contract()
c.m_symbol = x
c.m_secType = 'STK'
c.m_exchange = 'SEHK'
c.m_currency = 'HKD'
sleep(1)
dl.requestData(c)
sleep(1)
m0 = str(x)
m = dl.tickType47value
#data = m.split(';')
#pairs = { tuple(datum.split('=')) for datum in data}
#print(m)
sleep(1)
if dl.tickType47value:
with open(r'c:\Users\Owner\Desktop\extracedCSV\ALLHKSTOCK@Finratio_2.csv', 'a', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
row = []
row.append(m0)
row.append(m)
csvwriter.writerow(row)
dl.cancelData()
sleep(0.5)
break
print("Data is empty")
dl.cancelData()
sleep(0.5)
基于以下网站,我想通过 reqMktData "233,236,258"
下载基本比率通用报价https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html
但是我试了很多次,都无法改正错误。
'Error reading request:Message id 1004. Unable to parse data. java.lang.NumberFormatException: For input string: "Snapshot"']
AttributeError: 'IBWrapper' 对象没有属性 'reqMktData'
备注:"IBWrapper"不是IB官网提供的,由以下link提供: https://github.com/anthonyng2/ib
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 20 19:37:23 2018
@author: Owner
"""
import time
from IBWrapper import IBWrapper, contract
from ib.ext.EClientSocket import EClientSocket
callback = IBWrapper() # Instantiate IBWrapper. callback
tws = EClientSocket(callback) # Instantiate EClientSocket and return data to
callback
host = "127.0.0.1"
port = 7496
clientId = 25
tws.eConnect(host, port, clientId) # connect to TWS
create = contract() # Instantiate contract class
callback.initiate_variables()
contract_Details = create.create_contract(700, 'STK', 'SEHK', 'HKD')
tickerId = 1004
tws.reqMktData(tickerId,
contract_Details,
"233,236,258",
"ReportSnapshot" #ReportsFinSummary ReportsOwnership ReportsFinStatements
#RESC CalendarReport ReportSnapshot
)
time.sleep(5)
print(callback.reqMktData)
tws.eDisconnect()
认真研究了一下,IBWrapper不支持通用的tick数据,只能导入ibpy
from ib.ext.Contract import Contract
from ib.opt import ibConnection
from time import sleep
import csv
class Downloader(object):
tickType47value = ''
#field4price = ''
def __init__(self):
self.tws = ibConnection('localhost', 7496, 20)
self.tws.register(self.tickPriceHandler, 'TickString')
self.tws.connect()
self._reqId = 1003 # current request id
def tickPriceHandler(self,msg):
if msg.tickType == 47: # tickType=47
self.tickType47value = msg.value
#print('[debug]', msg)
def requestData(self,contract):
self.tws.reqMktData(self._reqId, contract, "233, 236, 258", False) #"233, 236, 258",
self._reqId+=1
def cancelData(self):
#self.tws.cancelMktData(1003)
self.tws.disconnect()
以下 link 都是可用的基本比率 - "headers" 中存在的标记值: https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html
if __name__=='__main__':
headers = ['TickNo',
'TTMNPMGN',
'NLOW',
'TTMPRCFPS',
'TTMGROSMGN',
'TTMCFSHR',
'QCURRATIO',
'TTMREV',
'TTMINVTURN',
'TTMOPMGN',
'TTMPR2REV',
'AEPSNORM',
'TTMNIPEREM',
'EPSCHNGYR',
'TTMPRFCFPS',
'TTMRECTURN',
'TTMPTMGN',
'QCSHPS',
'TTMFCF',
'LATESTADATE',
'APTMGNPCT',
'AEBTNORM',
'TTMNIAC',
'NetDebt_I',
'PRYTDPCTR',
'TTMEBITD',
'AFEEPSNTM',
'PR2TANBK',
'EPSTRENDGR',
'QTOTD2EQ',
'TTMFCFSHR',
'QBVPS',
'NPRICE',
'YLD5YAVG',
'REVTRENDGR',
'TTMEPSXCLX',
'QTANBVPS',
'PRICE2BK',
'MKTCAP',
'TTMPAYRAT',
'TTMINTCOV',
'TTMREVCHG',
'TTMROAPCT',
'TTMROEPCT',
'TTMREVPERE',
'APENORM',
'TTMROIPCT',
'REVCHNGYR',
'CURRENCY',
'DIVGRPCT',
'TTMEPSCHG',
'PEEXCLXOR',
'QQUICKRATI',
'TTMREVPS',
'BETA',
'TTMEBT',
'ADIV5YAVG',
'ANIACNORM',
'QLTD2EQ',
'NHIG']
stocks=['700']
with open('Your path', 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
csvwriter.writerow(headers)
sleep(0.5)
for x in stocks:
for _ in range(5): #10 #If markets are open, there can be no more than 5 requests pending for the same contract.
dl = Downloader()
c = Contract()
c.m_symbol = x
c.m_secType = 'STK'
c.m_exchange = 'SEHK'
c.m_currency = 'HKD'
sleep(1)
dl.requestData(c)
sleep(1)
m0 = str(x)
m = dl.tickType47value
#data = m.split(';')
#pairs = { tuple(datum.split('=')) for datum in data}
#print(m)
sleep(1)
if dl.tickType47value:
with open(r'c:\Users\Owner\Desktop\extracedCSV\ALLHKSTOCK@Finratio_2.csv', 'a', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
row = []
row.append(m0)
row.append(m)
csvwriter.writerow(row)
dl.cancelData()
sleep(0.5)
break
print("Data is empty")
dl.cancelData()
sleep(0.5)