- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 pandas 模块应用于我的代码,以便重新组织从 IB TWS 服务器收到的消息。
代码是
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
class MyWrapper(EWrapper):
def nextValidId(self, orderId:int):
print("Setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
self.start()
def historicalData(self, reqId, bar):
print("HistoricalData. ", reqId, "Date:", bar.date, "Open:", bar.open, "High:", bar.high, "Low:", bar.low, "Close:", bar.close, "Volume:", bar.volume, "Average:", bar.average, "Count:", bar.barCount)
def historicalDataUpdate(self, reqId, bar):
print("HistoricalDataUpdate. ", reqId, "Date:", bar.date, "Open:", bar.open, "High:", bar.high, "Low:", bar.low, "Close:", bar.close, "Volume:", bar.volume, "Average:", bar.average, "Count:", bar.barCount)
def error(self, reqId, errorCode, errorString):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
def start(self):
queryTime = ""
contract = Contract()
contract.secType = "STK"
contract.symbol = "NIO"
contract.currency = "USD"
contract.exchange = "SMART"
app.reqHistoricalData(1, contract, queryTime, "1 D", "5 secs", "TRADES", 0, 1, True, [])
app = EClient(MyWrapper())
app.connect("127.0.0.1", 7496, clientId=123)
app.run()
此代码检索给定股票的历史数据,然后返回最新的更新。
HistoricalDataUpdate. 1 Date: 20200708 08:31:00 Open: 14.17 High: 14.17 Low: 14.17 Close: 14.17 Volume: -1 Average: 14.15 Count: -1
当我尝试以重新组织的方式检索数据时,例如
HistoricalDataUpdate. 1 Date: Open: High: Low: Close: Volume: Average: Count:
20200708 08:31:00 14.17 14.17 14.17 14.17 -1 14.15 -1
帮助将不胜感激。
最佳答案
回调为您提供 ibapi.common.BarData 您可以读取它的 vars 以获得像 {date:..., open:123...}
这样的字典等等。
Pandas 可以从一个字典列表中创建一个数据框,所以将它们存储在一个列表中
也许你想要日期作为索引,pandas 也可以这样做,令人惊讶的是它可以读取格式。
完成后,您可以将数据保存在 csv 文件中。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import pandas as pd
class MyWrapper(EWrapper):
def __init__(self):
self.data = []
self.df=None
def nextValidId(self, orderId:int):
print("Setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
self.start()
def historicalData(self, reqId, bar):
self.data.append(vars(bar));
def historicalDataUpdate(self, reqId, bar):
line = vars(bar)
# pop date and make it the index, add rest to df
# will overwrite last bar at that same time
self.df.loc[pd.to_datetime(line.pop('date'))] = line
def historicalDataEnd(self, reqId: int, start: str, end: str):
print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
self.df = pd.DataFrame(self.data)
self.df['date'] = pd.to_datetime(self.df['date'])
self.df.set_index('date', inplace=True)
def error(self, reqId, errorCode, errorString):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
def start(self):
queryTime = ""
# so everyone can get data use fx
fx = Contract()
fx.secType = "CASH"
fx.symbol = "USD"
fx.currency = "JPY"
fx.exchange = "IDEALPRO"
# setting update to 1 minute still sends an update every tick? but timestamps are 1 min
# I don't think keepUpToDate sends a realtimeBar every 5 secs, just updates the last bar.
app.reqHistoricalData(1, fx, queryTime, "1 D", "1 min", "MIDPOINT", 0, 1, True, [])
wrap = MyWrapper()
app = EClient(wrap)
app.connect("127.0.0.1", 7497, clientId=123)
#I just use this in jupyter so I can interact with df
import threading
threading.Thread(target = app.run).start()
#this isn't needed in jupyter, just run another cell
import time
time.sleep(300) # in 5 minutes check the df and close
print(wrap.df)
wrap.df.to_csv("myfile.csv")#save in file
app.disconnect()
#in jupyter to show plot
%matplotlib inline
wrap.df.close.plot()
我使用 jupyter notebook,所以我添加了线程,这样我仍然可以交互。
HistoricalDataEnd. ReqId: 1 from 20200707 14:23:19 to 20200708 14:23:19
然后在 300 秒后我打印数据帧。检查 ohlc 是否合乎逻辑,并每分钟注意一个新的柱线。 14:28 栏只是我假设的前 19 秒,因为我的五分钟(300 秒)从 14:23:19 开始。这正是您希望和期望保持图表最新的行为。
2020-07-08 14:24:00 107.231 107.236 107.231 107.233 -1 -1
2020-07-08 14:25:00 107.233 107.234 107.23 107.232 -1 -1
2020-07-08 14:26:00 107.232 107.232 107.225 107.232 -1 -1
2020-07-08 14:27:00 107.232 107.239 107.231 107.239 -1 -1
2020-07-08 14:28:00 107.239 107.239 107.236 107.236 -1 -1
您可以看到它获取所有条形(仅在图形中关闭)并使其保持最新状态。
关于Pandas 数据帧和系列 - IB TWS 历史数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62794972/
我正在尝试实现此处建议的 redis 解决方案:http://www.slideshare.net/cacois/cois-palkostrata2013 : 这是目前为止我发现的最好的。 我有以下数
只是想知道您是否对关注有意见。 想象一下,我有一个简单的应用程序可以为客户存储发票。 简化发票表是: ID int, NUMBER varchar(20) CustomerID INT 客户数据是:
我正在尝试将 pandas 模块应用于我的代码,以便重新组织从 IB TWS 服务器收到的消息。 代码是 from ibapi.client import EClient from ibapi.wra
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
这个查询对我有用 select * from yahoo.finance.xchange where pair in ("EURUSD","GBPUSD") 可以在以下链接中看到: https://d
我正在尝试学习如何使用 Python、requests 和 BeautifulSoup 从 Coinmarketcap.com 网络抓取 BTC 历史数据。 我想解析以下内容: 1)日期 2)关闭 3
我有一个包含两列(Column_A 和 Column_B)的模型 (Model_A) 我的模型中有以下方法,两个基于 column_A 生成 column_B 的值 Class Model_A < A
让我们以关系数据库为例,例如MySQL。为了简单起见,我将专注于重要的事情:有一个包含订单的表,其中包含 order_id(主键)order_date 和外键 fk_supplier 等字段,引用表
我正在做一个快速的概念验证,以了解从 Google Analytics(分析)中提取历史数据以进一步用于离线数据拼接以生成数据及其分析的整体 View 的过程。我没有找到任何详细的在线文档来了解优缺点
我是一名优秀的程序员,十分优秀!