Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.11.0 - 2024-12-19
### Added
- A new optional parameter `time_unit` can be used to select the time unit.

## 3.10.0 - 2024-11-29
### Added
- Margin
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ from binance.spot import Spot as Client
client= Client(timeout=1)
```

### Time Unit

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:
- `microsecond`
- `millisecond`
- `MICROSECOND`
- `MILLISECOND`

By default, `time_unit` is set to `None` and will return a timestamp values in milliseconds.

```python
from binance.spot import Spot as Client

client = Client(time_unit="microsecond")
```

### Proxy

Proxy is supported.
Expand Down Expand Up @@ -270,6 +286,39 @@ logging.info("closing ws connection")
my_client.stop()
```

### Time Unit

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:
- `microsecond`
- `millisecond`
- `MICROSECOND`
- `MILLISECOND`

By default, `time_unit` is set to `None` and will return a timestamp values in milliseconds.

```python
# WebSocket API Client
import logging
from binance.websocket.spot.websocket_api import SpotWebsocketAPIClient

def message_handler(_, message):
logging.info(message)


my_client = SpotWebsocketAPIClient(on_message=message_handler, time_unit='microsecond')
```

```python
# WebSocket Stream Client
import logging
from binance.websocket.spot.websocket_stream import SpotWebsocketStreamClient

def message_handler(_, message):
logging.info(message)

my_client = SpotWebsocketStreamClient(on_message=message_handler, time_unit="microsecond")
```

#### Proxy

Proxy is supported for both WebSocket API and WebSocket Stream.
Expand Down
2 changes: 1 addition & 1 deletion binance/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.10.0"
__version__ = "3.11.0"
10 changes: 10 additions & 0 deletions binance/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class API(object):
proxies (obj, optional): Dictionary mapping protocol to the URL of the proxy. e.g. {'https': 'http://1.2.3.4:8080'}
show_limit_usage (bool, optional): whether return limit usage(requests and/or orders). By default, it's False
show_header (bool, optional): whether return the whole response header. By default, it's False
time_unit (str, optional): select a time unit. By default, it's None.
private_key (str, optional): RSA private key for RSA authentication
private_key_pass(str, optional): Password for PSA private key
"""
Expand All @@ -33,6 +34,7 @@ def __init__(
proxies=None,
show_limit_usage=False,
show_header=False,
time_unit=None,
private_key=None,
private_key_pass=None,
):
Expand All @@ -54,6 +56,14 @@ def __init__(
}
)

if (
time_unit == "microsecond"
or time_unit == "millisecond"
or time_unit == "MILLISECOND"
or time_unit == "MICROSECOND"
):
self.session.headers.update({"X-MBX-TIME-UNIT": time_unit})

if show_limit_usage is True:
self.show_limit_usage = True

Expand Down
10 changes: 9 additions & 1 deletion binance/websocket/binance_socket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ def __init__(
on_pong=None,
logger=None,
timeout=None,
time_unit=None,
proxies: Optional[dict] = None,
):
threading.Thread.__init__(self)
if not logger:
logger = logging.getLogger(__name__)
self.logger = logger
self.stream_url = stream_url
self.stream_url = (
stream_url + f"?timeUnit={time_unit}"
if time_unit == "microsecond"
or time_unit == "millisecond"
or time_unit == "MILLISECOND"
or time_unit == "MICROSECOND"
else stream_url
)
self.on_message = on_message
self.on_open = on_open
self.on_close = on_close
Expand Down
2 changes: 2 additions & 0 deletions binance/websocket/spot/websocket_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(
on_ping=None,
on_pong=None,
timeout=None,
time_unit=None,
logger=None,
proxies: Optional[dict] = None,
):
Expand All @@ -32,6 +33,7 @@ def __init__(
on_pong=on_pong,
logger=logger,
timeout=timeout,
time_unit=time_unit,
proxies=proxies,
)

Expand Down
2 changes: 2 additions & 0 deletions binance/websocket/spot/websocket_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(
on_pong=None,
is_combined=False,
timeout=None,
time_unit=None,
logger=None,
proxies: Optional[dict] = None,
):
Expand All @@ -31,6 +32,7 @@ def __init__(
on_ping=on_ping,
on_pong=on_pong,
timeout=timeout,
time_unit=time_unit,
logger=logger,
proxies=proxies,
)
Expand Down
4 changes: 4 additions & 0 deletions binance/websocket/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
on_pong=None,
logger=None,
timeout=None,
time_unit=None,
proxies: Optional[dict] = None,
):
if not logger:
Expand All @@ -36,6 +37,7 @@ def __init__(
on_pong,
logger,
timeout,
time_unit,
proxies,
)

Expand All @@ -54,6 +56,7 @@ def _initialize_socket(
on_pong,
logger,
timeout,
time_unit,
proxies,
):
return BinanceSocketManager(
Expand All @@ -66,6 +69,7 @@ def _initialize_socket(
on_pong=on_pong,
logger=logger,
timeout=timeout,
time_unit=time_unit,
proxies=proxies,
)

Expand Down
8 changes: 8 additions & 0 deletions docs/source/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Changelog
=========

3.11.0 - 2024-12-19
-------------------

Added
^^^^^

* A new optional parameter `time_unit` can be used to select the time unit.

3.10.0 - 2024-11-29
-------------------

Expand Down
17 changes: 17 additions & 0 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ Proxy is supported.

client= Client(proxies=proxies)

Time Unit
---------

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:
* `microsecond`
* `millisecond`
* `MICROSECOND`
* `MILLISECOND`

By default, `time_unit` is set to `None` and will return a timestamp values in milliseconds.

.. code-block:: python

from binance.spot import Spot as Client

client = Client(time_unit="microsecond")

Timeout
-------

Expand Down
2 changes: 1 addition & 1 deletion examples/spot/market/trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

config_logging(logging, logging.DEBUG)

spot_client = Client(base_url="https://testnet.binance.vision")
spot_client = Client(base_url="https://testnet.binance.vision", time_unit="microsecond")

logging.info(spot_client.trades("BTCUSDT"))
logging.info(spot_client.trades("BTCUSDT", limit=10))
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def message_handler(_, message):
logging.info(message)


my_client = SpotWebsocketAPIClient(on_message=message_handler, on_close=on_close)
my_client = SpotWebsocketAPIClient(
on_message=message_handler, on_close=on_close, time_unit="microsecond"
)


my_client.historical_trades(symbol="BNBBUSD", apiKey="", limit=1)
Expand Down
4 changes: 3 additions & 1 deletion examples/websocket/spot/websocket_stream/agg_trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def message_handler(_, message):
logging.info(message)


my_client = SpotWebsocketStreamClient(on_message=message_handler, is_combined=True)
my_client = SpotWebsocketStreamClient(
on_message=message_handler, is_combined=True, time_unit="microsecond"
)

# Subscribe to a single symbol stream
my_client.agg_trade(symbol="bnbusdt")
Expand Down
Loading