I try to understand if and how steramlit can be used to read data from a feed and display say the latest 10 min data in a plot. For data source I like to use cryptofeed, which is pretty cool but I did not find any reasonable starting point, neither in the doc nor here. My simple minded approach to add new data in a handler fails as there is no current event loop in thread ‘ScriptRunner.scriptThread’
import pandas as pd
import streamlit as st
import numpy as np
from decimal import Decimal
from collections import deque
from cryptofeed import FeedHandler
from cryptofeed.callback import BookCallback, FundingCallback, TickerCallback, TradeCallback
from cryptofeed.defines import BID, ASK, BLOCKCHAIN, COINBASE, FUNDING, GEMINI, L2_BOOK, OPEN_INTEREST, TICKER, TRADES, VOLUME
from cryptofeed.exchanges import Coinbase
prices = deque(maxlen=10)
chart = st.line_chart(np.array([[0.0]]))
async def trade(feed, pair, order_id, timestamp, side, amount, price, receipt_timestamp):
prices.append(price)
print(f"Timestamp: {timestamp} rts {receipt_timestamp} Feed: {feed} Pair: {pair} ID: {order_id} Side: {side} Amount: {amount} Price: {price}")
chart.add_rows(np.array([[price]]))
def handler():
f = FeedHandler()
f.add_feed(Coinbase(pairs=['BTC-USD'], channels=[TRADES], callbacks={TRADES: TradeCallback(trade)}))
f.run()
handler()
Any ideas or suggestions?
Dan