【发布时间】:2021-06-17 19:05:03
【问题描述】:
正如标题所示,我试图从 TWS API 获取给定证券的价格,并将其用作我在其他地方的程序中的变量。下面的代码(直接来自 Interactive Broker 的教程之一)将运行并在屏幕上打印价格,但我无法以一种可以创建包含价格的变量/对象的方式对其进行更改。该代码每十次尝试也只能运行一次,如果我在那里做错了什么,请告诉我。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print('Error: ', reqId, ' ', errorCode, ' ', errorString)
def tickPrice(self, reqId, tickType, price, attrib):
print('Tick Price. Ticker Id:', reqId, 'tickType:', TickTypeEnum.to_str(tickType),
'Price:', price, end=' ')
def main():
app = TestApp()
app.connect('127.0.0.1', 7496, 0)
contract = Contract()
contract.symbol = 'AAPL'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'SMART'
app.reqMarketDataType(1)
app.reqMktData(1, contract, '', False, False, [])
app.run()
if __name__ == '__main__':
main()
【问题讨论】: