Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 57 additions & 49 deletions examples/13.orderbook_updates.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,28 @@
"""
When ever the state of orderbook changes, an event is emitted by exchange.
In this code example we open a socket connection and listen to orderbook update event
"""
####
## When ever the state of orderbook changes, an event is emitted by exchange.
## In this code example we open a socket connection and listen to orderbook update event
####
import sys, os

sys.path.append(os.getcwd() + "/src/")
import time
from config import TEST_ACCT_KEY, TEST_NETWORK
import asyncio
from bluefin_v2_client import (
BluefinClient,
Networks,
MARKET_SYMBOLS,
SOCKET_EVENTS,
ORDER_SIDE,
ORDER_TYPE,
ORDER_STATUS,
OrderSignatureRequest,
)

import asyncio

event_received = False


async def place_limit_order(client: BluefinClient):
# default leverage of account is set to 3 on Bluefin
user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH)

# creates a LIMIT order to be signed
signature_request = OrderSignatureRequest(
symbol=MARKET_SYMBOLS.ETH, # market symbol
price=1300, # price at which you want to place order
quantity=0.01, # quantity
side=ORDER_SIDE.SELL,
orderType=ORDER_TYPE.LIMIT,
leverage=user_leverage,
)
# create signed order
signed_order = client.create_signed_order(signature_request)

print("Placing a limit order")
# place signed order on orderbook
resp = await client.post_signed_order(signed_order)

# returned order with PENDING state
print(resp)
return


async def main():

client = BluefinClient(True, Networks[TEST_NETWORK], TEST_ACCT_KEY)
await client.init(True)

Expand All @@ -56,36 +31,69 @@ def callback(event):
print("Event data:", event)
event_received = True

# must open socket before subscribing
print("Making socket connection to Bluefin exchange")
async def connection_callback():
# This callback will be invoked as soon as the socket connection is established
# subscribe to global event updates for ETH market
status = await client.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.ETH)
print("Subscribed to global ETH events: {}".format(status))

# triggered when orderbook updates are received
print("Listening to orderbook updates")
await client.socket.listen(SOCKET_EVENTS.ORDERBOOK_UPDATE.value, callback)

async def disconnection_callback():
print("Sockets disconnected, performing actions...")
resp = await client.cancel_all_orders(MARKET_SYMBOLS.ETH, [ORDER_STATUS.OPEN, ORDER_STATUS.PARTIAL_FILLED])
print(resp)


# must specify connection_callback before opening the sockets below
await client.socket.listen("connect", connection_callback)
await client.socket.listen("disconnect", disconnection_callback)

print("Making socket connection to bluefin exchange")
await client.socket.open()

# subscribe to global event updates for ETH market
await client.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.ETH)
print("Subscribed to ETH Market events")
######## Placing an Order ########

# default leverage of account is set to 3 on bluefin
user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH)

print("Listening to ETH Orderbook update event")
await client.socket.listen(SOCKET_EVENTS.ORDERBOOK_UPDATE.value, callback)
# creates a MARKET order to be signed
signature_request = OrderSignatureRequest(
symbol=MARKET_SYMBOLS.ETH,
leverage=user_leverage,
price=1300,
quantity=0.5,
side=ORDER_SIDE.BUY,
orderType=ORDER_TYPE.LIMIT
)

await place_limit_order(client)
# create signed order
signed_order = client.create_signed_order(signature_request)

timeout = 30
print("Placing a market order")
# place signed order on orderbook
resp = await client.post_signed_order(signed_order)
print(resp)
###### Closing socket connections after 30 seconds #####
timeout = 10
end_time = time.time() + timeout
while not event_received and time.time() < end_time:
time.sleep(1)
status = await client.socket.unsubscribe_global_updates_by_symbol(
MARKET_SYMBOLS.ETH
)
print("Unsubscribed from orderbook update events for ETH Market: {}".format(status))

# close socket connection
# # close socket connection
print("Closing sockets!")
await client.socket.close()

await client.close_connections()


if __name__ == "__main__":
### make sure keep the loop initialization same
# as below to ensure closing the script after receiving
# completion of each callback on socket events ###
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
loop.create_task(main())
pending = asyncio.all_tasks(loop=loop)
group = asyncio.gather(*pending)
loop.run_until_complete(group)
loop.close()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "bluefin_v2_client"
version = "2.6.0"
version = "2.7.0"
description = "Library to interact with Bluefin exchange protocol including its off-chain api-gateway and on-chain contracts"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
Loading