At this point in my development I am trying to gather market data for more than one currency every 10 seconds and filter out only price, time, and currency name and put it in a dataframe. Here is my code so far:
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()
My result is good but not entirely what I want. It gives me a new dataframe every 10 seconds but only has one crypto in it, I would like to have all 3. I also have another program that compiles all three cryptos onto one neat dataframe but it only does it once per execution, I will post that as well in case someone can just loop it or something.
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)
Thanks for any help!