gpt4 book ai didi

python - 将 API 数据存储到 DataFrame 中

转载 作者:行者123 更新时间:2023-12-05 06:15:37 25 4
gpt4 key购买 nike

我正在运行一个 Python 脚本来从 Interactive Brokers API 收集金融市场数据。连接到 API 后,终端打印出请求的历史数据。如何将数据保存到数据框中而不是在终端中流式传输?

from ibapi.wrapper import EWrapper
from ibapi.client import EClient
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 historicalData(self, reqId, bar):
print("historicalData. ", reqId, "Data:", bar.date, "Open:", bar.open, "High:", bar.high, "low:", bar.low, "close:", bar.close, "Volume:", bar.volume, "WAP:", bar.average)


def main():
app = TestApp()
app.connect("127.0.0.1", 7497, 0)

contract = Contract()
contract.symbol = "EUR"
contract.secType = "CASH"
contract.exchange = "IDEALPRO"
contract.currency = "USD"

app.reqHistoricalData(1, contract, "", "1 D", "1 min", "MIDPOINT", 0, 1, False, [])

app.run()


if __name__ == "__main__":
main()

代码的输出打印历史数据提要,例如:

historicalData.  1 Data: 20200616  11:53:00 Open: 1.125985 High: 1.12601 low: 1.12592 close: 1.12592 Volume: -1 WAP: -1.0
historicalData. 1 Data: 20200616 11:54:00 Open: 1.12592 High: 1.125925 low: 1.12583 close: 1.125885 Volume: -1 WAP: -1.0
historicalData. 1 Data: 20200616 11:55:00 Open: 1.125885 High: 1.126045 low: 1.125865 close: 1.126045 Volume: -1 WAP: -1.0

我如何将这些信息存储到数据框中而不是仅仅打印在终端中?

最佳答案

您可以在 TestApp 对象中创建一个数据框,然后在每次调用 historicalData() 时向其中添加一行。

def __init__(self):
...
self.df = pd.DataFrame(columns=['date', 'open', 'high', 'low', 'close', 'volume'])


def historicalData(self, reqId, bar):
self.df.loc[len(self.df)] = [bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume]

关于python - 将 API 数据存储到 DataFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62416071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com