Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:51bitquant Ai hedge fund crypto Binance Helpers

From Leeroopedia
Knowledge Sources
Domains Crypto_Markets, Utilities
Last Updated 2026-10-28 14:00 GMT

Overview

Concrete tool for normalizing human-readable trading parameters (date strings, interval strings, quantities) into Binance-API-compatible values, provided by the `helpers` module functions.

Description

The helpers module supplies six pure utility functions used across the sync and async Binance clients and the websocket layer: date_to_milliseconds parses a human date string (`dateparser`) to UTC epoch ms; interval_to_milliseconds maps a Binance interval string (e.g. `"1h"`) to milliseconds via a unit-seconds table; round_step_size rounds a quantity down to the exchange lot `step_size` using `Decimal`; convert_ts_str normalizes a timestamp that may be `None`, `int`, or a date string to ms; convert_list_to_json_array serializes a list to a compact JSON string for query params; and get_loop returns the current asyncio event loop, creating one if missing (used by the websocket managers).

Usage

  • Order placement: call `round_step_size(quantity, step_size)` to satisfy exchange `LOT_SIZE` filters before sending orders.
  • Kline requests: call `interval_to_milliseconds(interval)` or `date_to_milliseconds(date_str)` to build `startTime`/`endTime`/interval parameters.
  • Asyncio bootstrap: call `get_loop()` from websocket managers to obtain a usable event loop in any thread.
  • Param serialization: call `convert_list_to_json_array(symbols)` for endpoints accepting JSON array query values.

Code Reference

Source Location

Signature

def date_to_milliseconds(date_str: str) -> int:
    """Convert UTC date string to epoch milliseconds via dateparser."""

def interval_to_milliseconds(interval: str) -> Optional[int]:
    """Convert a Binance interval string (e.g. '1h') to milliseconds."""

def round_step_size(
    quantity: Union[float, Decimal], step_size: Union[float, Decimal]
) -> float:
    """Round a quantity down to a specific step size."""

def convert_ts_str(ts):
    """Convert a timestamp (str|int|None) to milliseconds if it is a string."""

def convert_list_to_json_array(l):
    """Serialize a list to a compact JSON string (or None)."""

def get_loop():
    """Return the current asyncio event loop, creating one if missing."""

Import

from src.gateway.binance.helpers import (
    date_to_milliseconds,
    interval_to_milliseconds,
    round_step_size,
    convert_ts_str,
    convert_list_to_json_array,
    get_loop,
)

I/O Contract

Inputs

Name Type Required Description
date_str str Yes (date_to_milliseconds) Readable date, e.g. `"1 Jan 2024"`, `"now UTC"`
interval str Yes (interval_to_milliseconds) Binance interval, e.g. `"1m"`, `"1h"`, `"1d"`, `"1w"`
quantity float/Decimal Yes (round_step_size) Raw quantity to round
step_size float/Decimal Yes (round_step_size) Exchange lot step size
ts str/int/None Yes (convert_ts_str) Timestamp to normalize
l list/None Yes (convert_list_to_json_array) List to serialize

Outputs

Name Type Description
date_to_milliseconds int Epoch milliseconds (UTC)
interval_to_milliseconds int/None Interval in ms, or `None` for an unrecognized interval
round_step_size float Quantity rounded down to nearest `step_size`
convert_ts_str int/None ms if input was a string, else the original value
convert_list_to_json_array str/None Compact JSON string (spaces stripped) or `None`
get_loop asyncio.AbstractEventLoop The current (or newly created) event loop

Usage Examples

Rounding a Quantity to Exchange Step Size

from src.gateway.binance.helpers import round_step_size

# BTCUSDT LOT_SIZE step is 0.00001
qty = round_step_size(0.123456789, 0.00001)
print(qty)  # 0.12345

Date and Interval Conversion

from src.gateway.binance.helpers import date_to_milliseconds, interval_to_milliseconds

start_ms = date_to_milliseconds("1 Jan 2024")
one_hour_ms = interval_to_milliseconds("1h")   # 3600000
one_day_ms = interval_to_milliseconds("1d")   # 86400000

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment