如何将Live Websocket数据组织到数据帧中



我与Kotak Sec Broker有Websocket连接,用于实时数据,正在构建ORB Algo交易机器人

try:
def callback_method(msg):
print(msg)

#print("Your logic/computation will come here.")
# subscribe to the streamingAPI
client.subscribe(input_tokens="96798,39958", callback=callback_method, broadcast_host="https://wstreamer.kotaksecurities.com/feed")
except Exception as e:
print("Exception when calling StreamingApi->subscribe: %sn" % e)

我尝试了各种方法来将这些数据组织在行列中,或者说Dataframe来处理它进一步的请告诉我怎么做。

WebSocket connection accepted with {'sid': 'OmxNu5RY3xIc04OgCh5z', 'upgrades': [], 'pingInterval': 25000, 'pingTimeout': 5000}
Engine.IO connection established
Sending packet MESSAGE data 0
Received packet MESSAGE data 0
Namespace / is connected
Emitting event "pageload" [/]
Received packet MESSAGE data 2["message","Welcome to StreamerIO (Build: 20200709)."]
Sending packet MESSAGE data 2["pageload",{"inputtoken":"96798,39958"}]
Received event "message" [/]
Received packet MESSAGE data 2["message","Welcome to StreamerIO (Build: 20200709)."]
Received event "message" [/]
Output from spyder call 'get_namespace_view':
Received packet MESSAGE data 2["getdata",["0157101","96798","7051.0000","900","7052.0000","800","7051.0000","7063.0000","7020.0000","7038.9000","7058.0000","7040.0000","-0.0992","20500","37400","159700","6690","112411290000.00","2","05/10/2022 17:26:56","-7.0000","7340.0000","6776.0000"]]
Received event "getdata" [/]
Output from spyder call 'get_namespace_view':
['0157101', '96798', '7051.0000', '900', '7052.0000', '800', '7051.0000', '7063.0000', '7020.0000', '7038.9000', '7058.0000', '7040.0000', '-0.0992', '20500', '37400', '159700', '6690', '112411290000.00', '2', '05/10/2022 17:26:56', '-7.0000', '7340.0000', '6776.0000']
Received packet MESSAGE data 2["getdata",["0157101","96798","7051.0000","500","7052.0000","800","7051.0000","7063.0000","7020.0000","7038.9000","7058.0000","7040.0000","-0.0992","20500","34800","159700","6690","112411290000.00","2","05/10/2022 17:26:56","-7.0000","7340.0000","6776.0000"]]
Received event "getdata" [/]
['0157101', '96798', '7051.0000', '500', '7052.0000', '800', '7051.0000', '7063.0000', '7020.0000', '7038.9000', '7058.0000', '7040.0000', '-0.0992', '20500', '34800', '159700', '6690', '112411290000.00', '2', '05/10/2022 17:26:56', '-7.0000', '7340.0000', '6776.0000']
Received packet MESSAGE data 2["getdata"

我猜您的websocket响应是JSON。您可以使用pd.normalize_json()从JSON创建Dataframe。

import pandas as pd
pd.normalize_json(json)

如果要将数据帧转换为html表,可以使用df.to_html()

最新更新