mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 06:44:22 +01:00
Add wss exmaple to create order, refactoring
This commit is contained in:
43
examples/websocket/create_order.py
Normal file
43
examples/websocket/create_order.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# python -c "from examples.websocket.create_order import *"
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from bfxapi.client import Client, Constants
|
||||||
|
from bfxapi.utils.cid import generate_unique_cid
|
||||||
|
from bfxapi.websocket.enums import Error, OrderType
|
||||||
|
from bfxapi.websocket.typings import Inputs
|
||||||
|
|
||||||
|
bfx = Client(
|
||||||
|
WSS_HOST=Constants.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_open(event):
|
||||||
|
print(f"Auth event {event}")
|
||||||
|
|
||||||
|
order: Inputs.Order.New = {
|
||||||
|
"gid": generate_unique_cid(),
|
||||||
|
"type": OrderType.EXCHANGE_LIMIT,
|
||||||
|
"symbol": "tBTCUST",
|
||||||
|
"amount": "0.1",
|
||||||
|
"price": "10000.0"
|
||||||
|
}
|
||||||
|
await bfx.wss.inputs.order_new(order)
|
||||||
|
|
||||||
|
print(f"Order sent")
|
||||||
|
@bfx.wss.on("notification")
|
||||||
|
async def on_notification(notification):
|
||||||
|
print(f"Notification {notification}")
|
||||||
|
@bfx.wss.on("order_new")
|
||||||
|
async def on_order_new(order_new: Inputs.Order.New):
|
||||||
|
print(f"Order new {order_new}")
|
||||||
|
@bfx.wss.on("subscribed")
|
||||||
|
def on_subscribed(subscription):
|
||||||
|
print(f"Subscription successful <{subscription}>")
|
||||||
|
|
||||||
|
bfx.wss.run()
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
|
# python -c "from examples.websocket.order_book import *"
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from bfxapi import Client, Constants
|
from bfxapi import Client, Constants
|
||||||
|
|
||||||
from bfxapi.websocket import BfxWebsocketClient
|
|
||||||
from bfxapi.websocket.enums import Channels, Error
|
from bfxapi.websocket.enums import Channels, Error
|
||||||
from bfxapi.websocket.typings import Subscriptions, TradingPairBook
|
from bfxapi.websocket.typings import Subscriptions, TradingPairBook
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
|
# python -c "from examples.websocket.raw_order_book import *"
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from bfxapi import Client, Constants
|
from bfxapi import Client, Constants
|
||||||
|
|
||||||
from bfxapi.websocket import BfxWebsocketClient
|
|
||||||
from bfxapi.websocket.enums import Channels, Error
|
from bfxapi.websocket.enums import Channels, Error
|
||||||
from bfxapi.websocket.typings import Subscriptions, TradingPairRawBook
|
from bfxapi.websocket.typings import Subscriptions, TradingPairRawBook
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
# python -c "from examples.websocket.ticker import *"
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from bfxapi import Client, Constants
|
from bfxapi import Client, Constants
|
||||||
@@ -16,4 +18,4 @@ def on_t_ticker_update(subscription: Subscriptions.TradingPairTicker, data: Trad
|
|||||||
async def open():
|
async def open():
|
||||||
await bfx.wss.subscribe(Channels.TICKER, symbol="tBTCUSD")
|
await bfx.wss.subscribe(Channels.TICKER, symbol="tBTCUSD")
|
||||||
|
|
||||||
asyncio.run(bfx.wss.start())
|
bfx.wss.run()
|
||||||
Reference in New Issue
Block a user