Ws examples

Co-Authored-By: itsdeka <dario.moceri@bitfinex.com>
This commit is contained in:
Davide Casale
2023-02-17 20:23:59 +01:00
parent 32a179fc00
commit f4c6a21ef4
3 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
# python -c "import examples.websocket.derivatives_status"
from bfxapi import Client, PUB_WSS_HOST
from bfxapi.websocket.enums import Error, Channel
from bfxapi.websocket.types import DerivativesStatus
from bfxapi.websocket import subscriptions
bfx = Client(WSS_HOST=PUB_WSS_HOST)
@bfx.wss.on("derivatives_status_update")
def on_derivatives_status_update(subscription: subscriptions.Status, data: DerivativesStatus):
print(f"{subscription}:", data)
@bfx.wss.on("wss-error")
def on_wss_error(code: Error, msg: str):
print(code, msg)
@bfx.wss.once("open")
async def open():
await bfx.wss.subscribe(Channel.STATUS, key="deriv:tBTCF0:USTF0")
bfx.wss.run()

View File

@@ -0,0 +1,29 @@
# python -c "import examples.websocket.trades"
from bfxapi import Client, PUB_WSS_HOST
from bfxapi.websocket.enums import Error, Channel
from bfxapi.websocket.types import Candle, TradingPairTrade
from bfxapi.websocket import subscriptions
bfx = Client(WSS_HOST=PUB_WSS_HOST)
@bfx.wss.on("candles_update")
def on_candles_update(subscription: subscriptions.Candles, candle: Candle):
print(f"New candle: {candle}")
@bfx.wss.on("t_trade_executed")
def on_t_trade_executed(subscription: subscriptions.Trades, trade: TradingPairTrade):
print(f"New trade: {trade}")
@bfx.wss.on("wss-error")
def on_wss_error(code: Error, msg: str):
print(code, msg)
@bfx.wss.once("open")
async def open():
await bfx.wss.subscribe(Channel.CANDLES, key="trade:1m:tBTCUSD")
await bfx.wss.subscribe(Channel.TRADES, symbol="tBTCUSD")
bfx.wss.run()

View File

@@ -0,0 +1,30 @@
# python -c "import examples.websocket.wallet_balance"
import os
from typing import List
from bfxapi import Client, WSS_HOST
from bfxapi.websocket.enums import Error
from bfxapi.websocket.types import Wallet
bfx = Client(
WSS_HOST=WSS_HOST,
API_KEY=os.getenv("BFX_API_KEY"),
API_SECRET=os.getenv("BFX_API_SECRET")
)
@bfx.wss.on("wallet_snapshot")
def log_snapshot(wallets: List[Wallet]):
for wallet in wallets:
print(f"Balance: {wallet}")
@bfx.wss.on("wallet_update")
def log_update(wallet: Wallet):
print(f"Balance update: {wallet}")
@bfx.wss.on("wss-error")
def on_wss_error(code: Error, msg: str):
print(code, msg)
bfx.wss.run()