Skip to content

Commit ce4565f

Browse files
ready -> test
1 parent 11b7c28 commit ce4565f

File tree

4 files changed

+114
-29
lines changed

4 files changed

+114
-29
lines changed

binance_websocket_api_futures.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,66 @@ async def handle_socket_message(stream_id=None):
1616
data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id)
1717
print(f"received data:\r\n{data}\r\n")
1818

19-
print(f"Starting the stream:")
19+
print(f"Starting the stream ...\r\n")
2020
api_stream = ubwa.create_stream(api=True,
2121
api_key=os.getenv('BINANCE_API_KEY'),
2222
api_secret=os.getenv('BINANCE_API_SECRET'),
2323
stream_label="Bobs Future Websocket API",
2424
process_asyncio_queue=handle_socket_message)
2525

26-
print(f"Executing API requests on Binance Futures:")
26+
print(f"Executing API requests on Binance Futures:\r\n")
27+
2728
account_status = ubwa.api.futures.get_account_status(stream_id=api_stream, return_response=True)
2829
print(f"Status of account_status request: {account_status['status']}\r\n")
30+
2931
ubwa.api.futures.get_listen_key(stream_id=api_stream)
32+
3033
ubwa.api.futures.get_server_time(stream_id=api_stream)
34+
3135
print(f"Account balance: "
32-
f"{str(ubwa.api.futures.get_account_balance(stream_id=api_stream, return_response=True))[:100]} ...")
36+
f"{str(ubwa.api.futures.get_account_balance(stream_id=api_stream, return_response=True))[:100]} ...\r\n")
37+
3338
print(f"Account position: "
34-
f"{str(ubwa.api.futures.get_account_position(stream_id=api_stream, return_response=True))[:100]} ...")
39+
f"{str(ubwa.api.futures.get_account_position(stream_id=api_stream, return_response=True))[:100]} ...\r\n")
40+
3541
print(f"Account status: "
36-
f"{str(ubwa.api.futures.get_account_status(stream_id=api_stream, return_response=True))[:100]} ...")
42+
f"{str(ubwa.api.futures.get_account_status(stream_id=api_stream, return_response=True))[:100]} ...\r\n")
43+
3744
print(f"Ticker order book: "
38-
f"{str(ubwa.api.futures.get_ticker_order_book(stream_id=api_stream, return_response=True))[:100]} ...")
45+
f"{str(ubwa.api.futures.get_ticker_order_book(stream_id=api_stream,
46+
symbol=market,
47+
return_response=True))[:100]} ...\r\n")
48+
3949
print(f"Ticker price: "
40-
f"{str(ubwa.api.futures.get_ticker_price(stream_id=api_stream, return_response=True))[:100]} ...")
50+
f"{str(ubwa.api.futures.get_ticker_price(stream_id=api_stream,
51+
symbol=market,
52+
return_response=True))[:100]} ...\r\n")
53+
4154
orig_client_order_id = ubwa.api.futures.create_order(stream_id=api_stream, price=1.0, order_type="LIMIT",
4255
quantity=15.0, side="SELL", symbol=market, time_in_force="GTC")
56+
4357
order_book = ubwa.api.futures.get_order_book(stream_id=api_stream, symbol=market, limit=10, return_response=True)
44-
print(f"Orderbook, lastUpdateId={order_book['result']['lastUpdateId']}: \r\nASKS {order_book['result']['asks']}\r\n"
45-
f"BIDS {order_book['result']['bids']}\r\n")
46-
#ubwa.api.futures.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)
47-
#new_orig_client_order_id = ubwa.api.futures.modify_order(stream_id=api_stream, symbol=market,
48-
# orig_client_order_id=orig_client_order_id)
49-
#ubwa.api.futures.cancel_order(stream_id=api_stream, symbol=market, orig_client_order_id=new_orig_client_order_id)
50-
#ubwa.api.futures.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=new_orig_client_order_id)
51-
print(f"PING: {ubwa.api.futures.ping(stream_id=api_stream, return_response=True)}")
52-
await asyncio.sleep(2)
53-
print(f"Stopping!")
58+
print(f"Orderbook, lastUpdateId={order_book['result']['lastUpdateId']}: \r\n{order_book['result']}\r\n")
59+
60+
ubwa.api.futures.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)
61+
62+
new_orig_client_order_id = ubwa.api.futures.modify_order(stream_id=api_stream, symbol=market,
63+
quantity=15.0, side="SELL",
64+
orig_client_order_id=orig_client_order_id)
65+
66+
ubwa.api.futures.cancel_order(stream_id=api_stream, symbol=market, orig_client_order_id=new_orig_client_order_id)
67+
68+
ubwa.api.futures.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=new_orig_client_order_id)
69+
70+
print(f"PING: {ubwa.api.futures.ping(stream_id=api_stream, return_response=True)}\r\n")
71+
72+
print(f"Stopping!\r\n")
5473

5574
def process_stream_signal(signal_type=None, stream_id=None, data_record=None, error_msg=None):
5675
# More info about `stream_signals`:
5776
# https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/%60stream_signals%60
5877
print(f"Received stream_signal for stream '{ubwa_manager.get_stream_label(stream_id=stream_id)}': "
59-
f"{signal_type} - {stream_id} - {data_record} - {error_msg}")
78+
f"{signal_type} - {stream_id} - {data_record} - {error_msg}\r\n")
6079

6180
if __name__ == "__main__":
6281
logging.basicConfig(level=logging.DEBUG,

binance_websocket_api_spot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async def handle_socket_message(stream_id=None):
2424
process_asyncio_queue=handle_socket_message)
2525

2626
print(f"Executing API requests on Binance Spot:")
27+
2728
current_average_price = ubwa.api.spot.get_current_average_price(stream_id=api_stream, symbol=market, return_response=True)
2829
print(f"current_average_price: {current_average_price}\r\n")
2930

@@ -81,13 +82,13 @@ async def handle_socket_message(stream_id=None):
8182

8283
recent_trades = ubwa.api.spot.get_recent_trades(stream_id=api_stream, symbol=market, return_response=True)
8384
print(f"recent_trades: {recent_trades['result'][:5]}\r\n")
84-
print(f"Stopping!")
85+
print(f"Stopping!\r\n")
8586

8687
def process_stream_signal(signal_type=None, stream_id=None, data_record=None, error_msg=None):
8788
# More info about `stream_signals`:
8889
# https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/%60stream_signals%60
8990
print(f"Received stream_signal for stream '{ubwa_manager.get_stream_label(stream_id=stream_id)}': "
90-
f"{signal_type} - {stream_id} - {data_record} - {error_msg}")
91+
f"{signal_type} - {stream_id} - {data_record} - {error_msg}\r\n")
9192

9293
if __name__ == "__main__":
9394
logging.basicConfig(level=logging.DEBUG,

unicorn_binance_websocket_api/api/futures.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ def cancel_order(self, order_id: int = None, orig_client_order_id: str = None,
165165
]
166166
}
167167
"""
168+
if symbol is None or (order_id is None and orig_client_order_id is None):
169+
raise ValueError(f"Missing mandatory parameter: symbol, order_id/orig_client_order_id")
170+
168171
if stream_id is None:
169172
if stream_label is not None:
170173
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -180,9 +183,9 @@ def cancel_order(self, order_id: int = None, orig_client_order_id: str = None,
180183
"timestamp": self._manager.get_timestamp()}
181184

182185
if order_id is not None:
183-
params['orderId'] = order_id
186+
params['orderId'] = int(order_id)
184187
if orig_client_order_id is not None:
185-
params['origClientOrderId'] = orig_client_order_id
188+
params['origClientOrderId'] = str(orig_client_order_id)
186189
if recv_window is not None:
187190
params['recvWindow'] = str(recv_window)
188191

@@ -444,6 +447,9 @@ def create_order(self,
444447
]
445448
}
446449
"""
450+
if side is None or symbol is None or order_type is None:
451+
raise ValueError(f"Missing mandatory parameter: order_type, side, symbol")
452+
447453
if stream_id is None:
448454
if stream_label is not None:
449455
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -1353,6 +1359,9 @@ def get_order(self, order_id: int = None, orig_client_order_id: str = None, proc
13531359
]
13541360
}
13551361
"""
1362+
if symbol is None or (order_id is None and orig_client_order_id is None):
1363+
raise ValueError(f"Missing mandatory parameter: symbol, order_id/orig_client_order_id")
1364+
13561365
if stream_id is None:
13571366
if stream_label is not None:
13581367
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -1497,6 +1506,9 @@ def get_order_book(self, process_response=None, limit: int = None, recv_window:
14971506
]
14981507
}
14991508
"""
1509+
if symbol is None:
1510+
raise ValueError(f"Missing mandatory parameter: symbol")
1511+
15001512
if stream_id is None:
15011513
if stream_label is not None:
15021514
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -1738,6 +1750,9 @@ def get_ticker_order_book(self, process_response=None, request_id: str = None, r
17381750
]
17391751
}
17401752
"""
1753+
if symbol is None:
1754+
raise ValueError(f"Missing mandatory parameter: symbol")
1755+
17411756
if stream_id is None:
17421757
if stream_label is not None:
17431758
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -1874,6 +1889,9 @@ def get_ticker_price(self, process_response=None, request_id: str = None, return
18741889
]
18751890
}
18761891
"""
1892+
if symbol is None:
1893+
raise ValueError(f"Missing mandatory parameter: symbol")
1894+
18771895
if stream_id is None:
18781896
if stream_label is not None:
18791897
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -2064,6 +2082,14 @@ def modify_order(self,
20642082
]
20652083
}
20662084
"""
2085+
if (price is None or
2086+
quantity is None or
2087+
side is None or
2088+
symbol is None or
2089+
(order_id is None and orig_client_order_id is None)):
2090+
raise ValueError(f"Missing mandatory parameter: price, quantity, side, symbol, "
2091+
f"order_id/orig_client_order_id")
2092+
20672093
if stream_id is None:
20682094
if stream_label is not None:
20692095
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -2082,9 +2108,9 @@ def modify_order(self,
20822108
"timestamp": self._manager.get_timestamp()}
20832109

20842110
if order_id is not None:
2085-
params['orderId'] = order_id
2111+
params['orderId'] = int(order_id)
20862112
if orig_client_order_id is not None:
2087-
params['origClientOrderId'] = orig_client_order_id
2113+
params['origClientOrderId'] = str(orig_client_order_id)
20882114
if price_match is not None:
20892115
params['priceMatch'] = price_match
20902116
if recv_window is not None:

unicorn_binance_websocket_api/api/spot.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ def cancel_and_replace_order(self,
343343
]
344344
}
345345
"""
346+
if (cancel_replace_mode is None or
347+
quantity is None or
348+
side is None or
349+
symbol is None or
350+
order_type is None or
351+
(cancel_order_id is None and cancel_orig_client_order_id is None)):
352+
raise ValueError(f"Missing mandatory parameter: cancel_replace_mode, quantity, side, symbol, order_type,"
353+
f"cancel_order_id/cancel_orig_client_order_id")
354+
346355
if stream_id is None:
347356
if stream_label is not None:
348357
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -362,11 +371,11 @@ def cancel_and_replace_order(self,
362371
"type": order_type}
363372

364373
if cancel_order_id is not None:
365-
params['cancelOrderId'] = cancel_order_id
374+
params['cancelOrderId'] = int(cancel_order_id)
366375
if cancel_orig_client_order_id is not None:
367-
params['cancelOrigClientOrderId'] = cancel_orig_client_order_id
376+
params['cancelOrigClientOrderId'] = str(cancel_orig_client_order_id)
368377
if cancel_new_client_order_id is not None:
369-
params['cancelNewClientOrderId'] = cancel_new_client_order_id
378+
params['cancelNewClientOrderId'] = str(cancel_new_client_order_id)
370379
if cancel_restrictions is not None:
371380
params['cancelRestrictions'] = cancel_restrictions
372381
if iceberg_qty is not None:
@@ -588,6 +597,9 @@ def cancel_open_orders(self, process_response=None, return_response: bool = Fals
588597
]
589598
}
590599
"""
600+
if symbol is None:
601+
raise ValueError(f"Missing mandatory parameter: symbol")
602+
591603
if stream_id is None:
592604
if stream_label is not None:
593605
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -755,6 +767,9 @@ def cancel_order(self, cancel_restrictions: Optional[Literal['ONLY_NEW', 'ONLY_P
755767
]
756768
}
757769
"""
770+
if symbol is None or (order_id is None and orig_client_order_id is None):
771+
raise ValueError(f"Missing mandatory parameter: symbol, order_id/orig_client_order_id")
772+
758773
if stream_id is None:
759774
if stream_label is not None:
760775
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -774,9 +789,9 @@ def cancel_order(self, cancel_restrictions: Optional[Literal['ONLY_NEW', 'ONLY_P
774789
if new_client_order_id is not None:
775790
params['newClientOrderId'] = new_client_order_id
776791
if order_id is not None:
777-
params['orderId'] = order_id
792+
params['orderId'] = int(order_id)
778793
if orig_client_order_id is not None:
779-
params['origClientOrderId'] = orig_client_order_id
794+
params['origClientOrderId'] = str(orig_client_order_id)
780795
if recv_window is not None:
781796
params['recvWindow'] = str(recv_window)
782797

@@ -1001,6 +1016,9 @@ def create_order(self,
10011016
]
10021017
}
10031018
"""
1019+
if side is None or symbol is None or order_type is None:
1020+
raise ValueError(f"Missing mandatory parameter: order_type, side, symbol")
1021+
10041022
if stream_id is None:
10051023
if stream_label is not None:
10061024
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -1528,6 +1546,9 @@ def get_aggregate_trades(self, process_response=None, end_time: int = None, from
15281546
]
15291547
}
15301548
"""
1549+
if symbol is None:
1550+
raise ValueError(f"Missing mandatory parameter: symbol")
1551+
15311552
if stream_id is None:
15321553
if stream_label is not None:
15331554
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -1965,6 +1986,9 @@ def get_historical_trades(self, process_response=None, from_id: int = None, limi
19651986
]
19661987
}
19671988
"""
1989+
if symbol is None:
1990+
raise ValueError(f"Missing mandatory parameter: symbol")
1991+
19681992
if stream_id is None:
19691993
if stream_label is not None:
19701994
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -2125,6 +2149,9 @@ def get_klines(self, process_response=None,
21252149
]
21262150
}
21272151
"""
2152+
if symbol is None:
2153+
raise ValueError(f"Missing mandatory parameter: symbol")
2154+
21282155
if stream_id is None:
21292156
if stream_label is not None:
21302157
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -2533,6 +2560,9 @@ def get_order(self, order_id: int = None, orig_client_order_id: str = None, proc
25332560
]
25342561
}
25352562
"""
2563+
if symbol is None or (order_id is None and orig_client_order_id is None):
2564+
raise ValueError(f"Missing mandatory parameter: symbol, order_id/orig_client_order_id")
2565+
25362566
if stream_id is None:
25372567
if stream_label is not None:
25382568
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -2708,6 +2738,9 @@ def get_order_book(self, process_response=None, limit: int = None, recv_window:
27082738
]
27092739
}
27102740
"""
2741+
if symbol is None:
2742+
raise ValueError(f"Missing mandatory parameter: symbol")
2743+
27112744
if stream_id is None:
27122745
if stream_label is not None:
27132746
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -2832,6 +2865,9 @@ def get_recent_trades(self, process_response=None, limit: int = None,
28322865
]
28332866
}
28342867
"""
2868+
if symbol is None:
2869+
raise ValueError(f"Missing mandatory parameter: symbol")
2870+
28352871
if stream_id is None:
28362872
if stream_label is not None:
28372873
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)
@@ -3167,6 +3203,9 @@ def get_ui_klines(self,
31673203
]
31683204
}
31693205
"""
3206+
if symbol is None:
3207+
raise ValueError(f"Missing mandatory parameter: symbol")
3208+
31703209
if stream_id is None:
31713210
if stream_label is not None:
31723211
stream_id = self._manager.get_stream_id_by_label(stream_label=stream_label)

0 commit comments

Comments
 (0)