Skip to content

Commit cf2bfbc

Browse files
authored
Merge pull request #352 from binance/rc-v3.11.0
Release v3.11.0
2 parents 5d76597 + d2880a5 commit cf2bfbc

File tree

13 files changed

+113
-5
lines changed

13 files changed

+113
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.11.0 - 2024-12-19
4+
### Added
5+
- A new optional parameter `time_unit` can be used to select the time unit.
6+
37
## 3.10.0 - 2024-11-29
48
### Added
59
- Margin

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ from binance.spot import Spot as Client
165165
client= Client(timeout=1)
166166
```
167167

168+
### Time Unit
169+
170+
The `time_unit` parameter is optional and allows you to retrieve data with timestamps in `microsecond` or `millisecond`. Users can set it with the following values:
171+
- `microsecond`
172+
- `millisecond`
173+
- `MICROSECOND`
174+
- `MILLISECOND`
175+
176+
By default, `time_unit` is set to `None` and will return a timestamp values in milliseconds.
177+
178+
```python
179+
from binance.spot import Spot as Client
180+
181+
client = Client(time_unit="microsecond")
182+
```
183+
168184
### Proxy
169185

170186
Proxy is supported.
@@ -270,6 +286,39 @@ logging.info("closing ws connection")
270286
my_client.stop()
271287
```
272288

289+
### Time Unit
290+
291+
The `time_unit` parameter is optional and allows you to retrieve data with timestamps in `microsecond` or `millisecond`. Users can set it with the following values:
292+
- `microsecond`
293+
- `millisecond`
294+
- `MICROSECOND`
295+
- `MILLISECOND`
296+
297+
By default, `time_unit` is set to `None` and will return a timestamp values in milliseconds.
298+
299+
```python
300+
# WebSocket API Client
301+
import logging
302+
from binance.websocket.spot.websocket_api import SpotWebsocketAPIClient
303+
304+
def message_handler(_, message):
305+
logging.info(message)
306+
307+
308+
my_client = SpotWebsocketAPIClient(on_message=message_handler, time_unit='microsecond')
309+
```
310+
311+
```python
312+
# WebSocket Stream Client
313+
import logging
314+
from binance.websocket.spot.websocket_stream import SpotWebsocketStreamClient
315+
316+
def message_handler(_, message):
317+
logging.info(message)
318+
319+
my_client = SpotWebsocketStreamClient(on_message=message_handler, time_unit="microsecond")
320+
```
321+
273322
#### Proxy
274323

275324
Proxy is supported for both WebSocket API and WebSocket Stream.

binance/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.10.0"
1+
__version__ = "3.11.0"

binance/api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class API(object):
2020
proxies (obj, optional): Dictionary mapping protocol to the URL of the proxy. e.g. {'https': 'http://1.2.3.4:8080'}
2121
show_limit_usage (bool, optional): whether return limit usage(requests and/or orders). By default, it's False
2222
show_header (bool, optional): whether return the whole response header. By default, it's False
23+
time_unit (str, optional): select a time unit. By default, it's None.
2324
private_key (str, optional): RSA private key for RSA authentication
2425
private_key_pass(str, optional): Password for PSA private key
2526
"""
@@ -33,6 +34,7 @@ def __init__(
3334
proxies=None,
3435
show_limit_usage=False,
3536
show_header=False,
37+
time_unit=None,
3638
private_key=None,
3739
private_key_pass=None,
3840
):
@@ -54,6 +56,14 @@ def __init__(
5456
}
5557
)
5658

59+
if (
60+
time_unit == "microsecond"
61+
or time_unit == "millisecond"
62+
or time_unit == "MILLISECOND"
63+
or time_unit == "MICROSECOND"
64+
):
65+
self.session.headers.update({"X-MBX-TIME-UNIT": time_unit})
66+
5767
if show_limit_usage is True:
5868
self.show_limit_usage = True
5969

binance/websocket/binance_socket_manager.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ def __init__(
2424
on_pong=None,
2525
logger=None,
2626
timeout=None,
27+
time_unit=None,
2728
proxies: Optional[dict] = None,
2829
):
2930
threading.Thread.__init__(self)
3031
if not logger:
3132
logger = logging.getLogger(__name__)
3233
self.logger = logger
33-
self.stream_url = stream_url
34+
self.stream_url = (
35+
stream_url + f"?timeUnit={time_unit}"
36+
if time_unit == "microsecond"
37+
or time_unit == "millisecond"
38+
or time_unit == "MILLISECOND"
39+
or time_unit == "MICROSECOND"
40+
else stream_url
41+
)
3442
self.on_message = on_message
3543
self.on_open = on_open
3644
self.on_close = on_close

binance/websocket/spot/websocket_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(
1616
on_ping=None,
1717
on_pong=None,
1818
timeout=None,
19+
time_unit=None,
1920
logger=None,
2021
proxies: Optional[dict] = None,
2122
):
@@ -32,6 +33,7 @@ def __init__(
3233
on_pong=on_pong,
3334
logger=logger,
3435
timeout=timeout,
36+
time_unit=time_unit,
3537
proxies=proxies,
3638
)
3739

binance/websocket/spot/websocket_stream.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(
1515
on_pong=None,
1616
is_combined=False,
1717
timeout=None,
18+
time_unit=None,
1819
logger=None,
1920
proxies: Optional[dict] = None,
2021
):
@@ -31,6 +32,7 @@ def __init__(
3132
on_ping=on_ping,
3233
on_pong=on_pong,
3334
timeout=timeout,
35+
time_unit=time_unit,
3436
logger=logger,
3537
proxies=proxies,
3638
)

binance/websocket/websocket_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(
2121
on_pong=None,
2222
logger=None,
2323
timeout=None,
24+
time_unit=None,
2425
proxies: Optional[dict] = None,
2526
):
2627
if not logger:
@@ -36,6 +37,7 @@ def __init__(
3637
on_pong,
3738
logger,
3839
timeout,
40+
time_unit,
3941
proxies,
4042
)
4143

@@ -54,6 +56,7 @@ def _initialize_socket(
5456
on_pong,
5557
logger,
5658
timeout,
59+
time_unit,
5760
proxies,
5861
):
5962
return BinanceSocketManager(
@@ -66,6 +69,7 @@ def _initialize_socket(
6669
on_pong=on_pong,
6770
logger=logger,
6871
timeout=timeout,
72+
time_unit=time_unit,
6973
proxies=proxies,
7074
)
7175

docs/source/CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Changelog
33
=========
44

5+
3.11.0 - 2024-12-19
6+
-------------------
7+
8+
Added
9+
^^^^^
10+
11+
* A new optional parameter `time_unit` can be used to select the time unit.
12+
513
3.10.0 - 2024-11-29
614
-------------------
715

docs/source/getting_started.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,23 @@ Proxy is supported.
258258
259259
client= Client(proxies=proxies)
260260
261+
Time Unit
262+
---------
263+
264+
The `time_unit` parameter is optional and allows you to retrieve data with timestamps in `microsecond` or `millisecond`. Users can set it with the following values:
265+
* `microsecond`
266+
* `millisecond`
267+
* `MICROSECOND`
268+
* `MILLISECOND`
269+
270+
By default, `time_unit` is set to `None` and will return a timestamp values in milliseconds.
271+
272+
.. code-block:: python
273+
274+
from binance.spot import Spot as Client
275+
276+
client = Client(time_unit="microsecond")
277+
261278
Timeout
262279
-------
263280

0 commit comments

Comments
 (0)