我有一个python脚本,它从TWS API(Interactive Brokers(读取数据,并希望将数据转储到csv文件中。
现在它只是覆盖数据并打印最后一行以及我不想要的其他值。
它通过print(df(打印出精细的值。
代码:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from threading import Timer
from ibapi.ticktype import *
import pandas as pd
import numpy as np
class TestApp(EWrapper, EClient):
def __init__(self):
EWrapper.__init__(self)
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def nextValidId(self, orderId):
self.start()
def contractDetails(self, reqId, contractDetails):
self.data = [contractDetails]
df = pd.DataFrame(self.data)
df.to_csv('options_test.csv')
print(df)
def contractDetailsEnd(self, reqId):
print("ncontractDetails Endn")
def start(self):
#self.reqSecDefOptParams(1, "AAPL", "", "STK", 265598)
contract = Contract()
contract.symbol = 'AAPL'
contract.secType = 'OPT'
contract.exchange = 'SMART'
contract.currency = 'USD'
#contract.primaryExchange = 'NASDAQ'
contract.lastTradeDateOrContractMonth = '202010'
#contract.strike = 175
#contract.right = "C"
#contract.multiplier = "100"
global underlying
underlying = contract.symbol
self.reqMktData(1, contract, '106', False, False, [])
self.reqContractDetails(1, contract)
def stop(self):
self.done = True
self.disconnect()
def main():
app = TestApp()
app.nextOrderId = 0
app.connect('127.0.0.1', 7497, 123)
app.data = []
Timer(4, app.stop).start()
app.run()
if __name__ == "__main__":
main()
我尝试使用append((,但它显示了一个错误。
def contractDetails(self, reqId, contractDetails):
self.data = [contractDetails]
df = pd.append(self.data)
df.to_csv('options_test.csv')
print(df)
..raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'append'
我只想将"contractDetails"中收到的数据保存到csv中。
contractDetails是一个contractDetails细节对象,我不确定panda将如何生成数据帧。最常用的字段是具有conId的合同对象,可以用于请求数据或下订单。它还有一个符号,让你知道你在交易什么。contractDetails还有其他信息,如交易时间和交易所。检查Contract的源代码。为了提高效率,我修改了数据帧的制作方式。我还对代码进行了一些修改,并删除了不需要的Timer。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from threading import Timer
from ibapi.ticktype import *
import collections
import pandas as pd
class TestApp(EWrapper, EClient):
def __init__(self):
EWrapper.__init__(self)
EClient.__init__(self, self)
self.data=collections.defaultdict(list)
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def nextValidId(self, orderId):
self.nextOrderId=orderId
self.start()
def contractDetails(self, reqId, contractDetails):
self.data["conid"].append(contractDetails.contract.conId)
self.data["symbol"].append(contractDetails.contract.localSymbol)
def contractDetailsEnd(self, reqId):
print("ncontractDetails Endn")
self.df=pd.DataFrame.from_dict(app.data)
self.stop()
def start(self):
#self.reqSecDefOptParams(1, "AAPL", "", "STK", 265598)
contract = Contract()
contract.symbol = 'AAPL'
contract.secType = 'OPT'
contract.exchange = 'SMART'
contract.currency = 'USD'
#contract.primaryExchange = 'NASDAQ'
contract.lastTradeDateOrContractMonth = '202010'
#contract.strike = 175
#contract.right = "C"
#contract.multiplier = "100"
self.reqContractDetails(1, contract)
def stop(self):
self.disconnect()
print(self.df)
#self.df.to_csv('options_test.csv')
def main():
app = TestApp()
app.connect('127.0.0.1', 7496, 123)
app.run()