Skip to content

Commit 1ba25c3

Browse files
authored
[EXC-1686] add support for auto reconnection/disconnection callbacks to (#19)
1 parent afe5331 commit 1ba25c3

File tree

3 files changed

+130
-154
lines changed

3 files changed

+130
-154
lines changed

examples/13.orderbook_updates.py

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,28 @@
1-
"""
2-
When ever the state of orderbook changes, an event is emitted by exchange.
3-
In this code example we open a socket connection and listen to orderbook update event
4-
"""
1+
####
2+
## When ever the state of orderbook changes, an event is emitted by exchange.
3+
## In this code example we open a socket connection and listen to orderbook update event
4+
####
55
import sys, os
6-
76
sys.path.append(os.getcwd() + "/src/")
87
import time
98
from config import TEST_ACCT_KEY, TEST_NETWORK
10-
import asyncio
119
from bluefin_v2_client import (
1210
BluefinClient,
1311
Networks,
1412
MARKET_SYMBOLS,
1513
SOCKET_EVENTS,
1614
ORDER_SIDE,
1715
ORDER_TYPE,
16+
ORDER_STATUS,
1817
OrderSignatureRequest,
1918
)
20-
19+
import asyncio
2120

2221
event_received = False
2322

2423

25-
async def place_limit_order(client: BluefinClient):
26-
# default leverage of account is set to 3 on Bluefin
27-
user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH)
28-
29-
# creates a LIMIT order to be signed
30-
signature_request = OrderSignatureRequest(
31-
symbol=MARKET_SYMBOLS.ETH, # market symbol
32-
price=1300, # price at which you want to place order
33-
quantity=0.01, # quantity
34-
side=ORDER_SIDE.SELL,
35-
orderType=ORDER_TYPE.LIMIT,
36-
leverage=user_leverage,
37-
)
38-
# create signed order
39-
signed_order = client.create_signed_order(signature_request)
40-
41-
print("Placing a limit order")
42-
# place signed order on orderbook
43-
resp = await client.post_signed_order(signed_order)
44-
45-
# returned order with PENDING state
46-
print(resp)
47-
return
48-
49-
5024
async def main():
25+
5126
client = BluefinClient(True, Networks[TEST_NETWORK], TEST_ACCT_KEY)
5227
await client.init(True)
5328

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

59-
# must open socket before subscribing
60-
print("Making socket connection to Bluefin exchange")
34+
async def connection_callback():
35+
# This callback will be invoked as soon as the socket connection is established
36+
# subscribe to global event updates for ETH market
37+
status = await client.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.ETH)
38+
print("Subscribed to global ETH events: {}".format(status))
39+
40+
# triggered when orderbook updates are received
41+
print("Listening to orderbook updates")
42+
await client.socket.listen(SOCKET_EVENTS.ORDERBOOK_UPDATE.value, callback)
43+
44+
async def disconnection_callback():
45+
print("Sockets disconnected, performing actions...")
46+
resp = await client.cancel_all_orders(MARKET_SYMBOLS.ETH, [ORDER_STATUS.OPEN, ORDER_STATUS.PARTIAL_FILLED])
47+
print(resp)
48+
49+
50+
# must specify connection_callback before opening the sockets below
51+
await client.socket.listen("connect", connection_callback)
52+
await client.socket.listen("disconnect", disconnection_callback)
53+
54+
print("Making socket connection to bluefin exchange")
6155
await client.socket.open()
6256

63-
# subscribe to global event updates for ETH market
64-
await client.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.ETH)
65-
print("Subscribed to ETH Market events")
57+
######## Placing an Order ########
58+
59+
# default leverage of account is set to 3 on bluefin
60+
user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH)
6661

67-
print("Listening to ETH Orderbook update event")
68-
await client.socket.listen(SOCKET_EVENTS.ORDERBOOK_UPDATE.value, callback)
62+
# creates a MARKET order to be signed
63+
signature_request = OrderSignatureRequest(
64+
symbol=MARKET_SYMBOLS.ETH,
65+
leverage=user_leverage,
66+
price=1300,
67+
quantity=0.5,
68+
side=ORDER_SIDE.BUY,
69+
orderType=ORDER_TYPE.LIMIT
70+
)
6971

70-
await place_limit_order(client)
72+
# create signed order
73+
signed_order = client.create_signed_order(signature_request)
7174

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

81-
# close socket connection
85+
# # close socket connection
8286
print("Closing sockets!")
8387
await client.socket.close()
8488

85-
await client.close_connections()
86-
8789

8890
if __name__ == "__main__":
91+
### make sure keep the loop initialization same
92+
# as below to ensure closing the script after receiving
93+
# completion of each callback on socket events ###
8994
loop = asyncio.new_event_loop()
90-
loop.run_until_complete(main())
95+
loop.create_task(main())
96+
pending = asyncio.all_tasks(loop=loop)
97+
group = asyncio.gather(*pending)
98+
loop.run_until_complete(group)
9199
loop.close()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "bluefin_v2_client"
3-
version = "2.6.0"
3+
version = "2.7.0"
44
description = "Library to interact with Bluefin exchange protocol including its off-chain api-gateway and on-chain contracts"
55
readme = "README.md"
66
requires-python = ">=3.8"

0 commit comments

Comments
 (0)