在我的开发过程中,我试图每10秒收集一种以上货币的市场数据,只过滤出价格、时间和货币名称,并将其放入数据帧中。这是我到目前为止的代码:
import websocket, json, pandas as pd
def on_open(ws):
print("The Socket is Opened")
subscribe_message = { 'type': 'subscribe',
'channels':[
{'name':'ticker',
'product_ids':['BTC-USD','ETH-USD','ATOM-USD']}
]
}
ws.send(json.dumps(subscribe_message))
def on_message(ws, message):
data = json.loads(message)
df = createframe(data)
time.sleep(10)
print(df)
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:,['product_id','price','time']]
df.columns = ['Crypto','Price','Time']
return df
socket = "wss://ws-feed.pro.coinbase.com"
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)
ws.run_forever()
我的成绩很好,但不完全是我想要的。它每10秒给我一个新的数据帧,但其中只有一个加密,我希望所有3个都有。我还有另一个程序,它将所有三种密码编译到一个整洁的数据帧上,但每次执行只做一次,我也会发布它,以防有人可以循环它或其他什么。
import cbpro
import pandas as pd
c = cbpro.PublicClient()
BTCTicker = c.get_product_ticker('BTC-USD')
ETHTicker = c.get_product_ticker('ETH-USD')
ATOMTicker = c.get_product_ticker('ATOM-USD')
BTCPrice = (BTCTicker['price'])
ETHPrice = (ETHTicker['price'])
ATOMPrice = (ATOMTicker['price'])
BTCTime = (BTCTicker['time'])
ETHTime = (ETHTicker['time'])
ATOMTime = (ATOMTicker['time'])
Data = {
'Price': [BTCPrice, ETHPrice, ATOMPrice],
'Time': [BTCTime, ETHTime, ATOMTime]
}
Row_Labels= ['Bitcoin', 'Ethereum', 'Cosmos']
df = pd.DataFrame(data=Data, index=Row_Labels)
print(df)
谢谢你的帮助!
我没有什么可以提供的,除了你可能会喜欢看异步库,它很有趣,但一旦你进入就不难了
除此之外,你可能想研究制作"获取股票代码"阻止进入之类的东西
markets = ['BTC-USD','ETH-USD','ATOM-USD']
for cur_market in markets:
ticker = c.get_product_ticker(cur_market)
price = ticker['price']
time = ticker['time']
....