Real-Time Streaming¶
WebSocket-based live price streaming from Binance.
Price Stream¶
import asyncio
from financelib.streaming import PriceStream
async def on_tick(symbol, price, data):
print(f"{symbol}: ${price}")
stream = PriceStream(
symbols=["btcusdt", "ethusdt"],
on_price=on_tick,
source="binance",
)
# Run (blocks until stopped)
asyncio.run(stream.start())
Price Aggregator (OHLCV Candles)¶
from financelib.streaming import PriceStream, PriceAggregator
async def on_candle(candle):
print(f"{candle['symbol']}: O={candle['open']:.2f} H={candle['high']:.2f} "
f"L={candle['low']:.2f} C={candle['close']:.2f}")
agg = PriceAggregator(interval_seconds=60, on_candle=on_candle)
stream = PriceStream(["btcusdt"], on_price=agg.on_tick)
asyncio.run(stream.start())
Async Data Fetchers¶
import asyncio
from financelib.stock.async_client import AsyncStock
async def main():
# Single stock
async with AsyncStock("THYAO.IS") as stock:
price = await stock.get_price()
quote = await stock.get_quote()
print(f"THYAO: {price}")
# Multiple stocks concurrently
prices = await AsyncStock.get_prices(["THYAO.IS", "GARAN.IS", "ASELS.IS"])
print(prices)
# Search
results = await AsyncStock.search("Turkish Airlines")
print(results)
asyncio.run(main())