Rewrite, edit and organize examples/websocket demos.

This commit is contained in:
Davide Casale
2023-02-20 18:22:57 +01:00
parent cec9d6ba68
commit 6f61b983a5
10 changed files with 41 additions and 41 deletions

View File

@@ -0,0 +1,44 @@
# python -c "import examples.websocket.authenticated.submit_order"
import os
from bfxapi import Client, WSS_HOST
from bfxapi.enums import Error, OrderType
from bfxapi.websocket.types import Notification, Order
bfx = Client(
WSS_HOST=WSS_HOST,
API_KEY=os.getenv("BFX_API_KEY"),
API_SECRET=os.getenv("BFX_API_SECRET")
)
@bfx.wss.on("wss-error")
def on_wss_error(code: Error, msg: str):
print(code, msg)
@bfx.wss.on("authenticated")
async def on_authenticated(event):
print(f"Authentication: {event}")
await bfx.wss.inputs.submit_order(
type=OrderType.EXCHANGE_LIMIT,
symbol="tBTCUSD",
amount="0.1",
price="10000.0"
)
print("The order has been sent.")
@bfx.wss.on("on-req-notification")
async def on_notification(notification: Notification[Order]):
print(f"Notification: {notification}.")
@bfx.wss.on("order_new")
async def on_order_new(order_new: Order):
print(f"Order new: {order_new}")
@bfx.wss.on("subscribed")
def on_subscribed(subscription):
print(f"Subscription successful for <{subscription}>.")
bfx.wss.run()

View File

@@ -0,0 +1,30 @@
# python -c "import examples.websocket.authenticated.wallets"
import os
from bfxapi import Client
from bfxapi.enums import Error
from bfxapi.websocket.types import List, Wallet
bfx = Client(
API_KEY=os.getenv("BFX_API_KEY"),
API_SECRET=os.getenv("BFX_API_SECRET"),
filter=["wallet"]
)
@bfx.wss.on("wss-error")
def on_wss_error(code: Error, msg: str):
print(code, msg)
@bfx.wss.on("wallet_snapshot")
def on_wallet_snapshot(wallets: List[Wallet]):
for wallet in wallets:
print(f"Wallet: {wallet.wallet_type} | {wallet.currency}")
print(f"Available balance: {wallet.available_balance}")
print(f"Wallet trade details: {wallet.trade_details}")
@bfx.wss.on("wallet_update")
def on_wallet_update(wallet: Wallet):
print(f"Wallet update: {wallet}")
bfx.wss.run()