Zero-Rebuild Binance USDโ-M Futures Adapter Pattern ยท Exact LOT_SIZE/PRICE_FILTER Normalizer ยท Pre-Trade Risk Gate ยท Replit Codebase Audit.
wss://stream.binancefuture.com/ws
Adapter Pattern Active: Strategy, Feature Engine & Risk Gate remain 100% untouched when toggling Demo โ Live credentials.
LOT_SIZE QtynewClientOrderIdโ
Normalized Entry/SL: โ
| Symbol | Best Bid | Best Ask | OBI Imbalance | Regime Signal |
|---|
Toggle items below to inspect critical failure modes commonly found in early Replit crypto bots and our exact Python fixes.
Drop-in modular architecture for your Replit workspace: switch `ASTRA_ENV=testnet` โ `ASTRA_ENV=live` via Replit Secrets with zero strategy edits.
from dataclasses import dataclass
from decimal import Decimal, ROUND_DOWN
import hashlib, hmac, os, time
from typing import Literal
EnvMode = Literal["testnet", "live"]
@dataclass(frozen=True)
class BinanceFuturesConfig:
mode: EnvMode
rest_base: str
ws_base: str
api_key: str
api_secret: str
@classmethod
def from_replit_secrets(cls) -> "BinanceFuturesConfig":
mode: EnvMode = "live" if os.getenv("ASTRA_ENV", "testnet").lower() == "live" else "testnet"
if mode == "live":
if os.getenv("ASTRA_LIVE_ARMED") != "true":
raise RuntimeError("Refusing LIVE mainnet boot: set ASTRA_LIVE_ARMED=true explicitly.")
return cls(
mode="live",
rest_base="https://fapi.binance.com",
ws_base="wss://fstream.binance.com/ws",
api_key=os.environ["BINANCE_LIVE_API_KEY"].strip(),
api_secret=os.environ["BINANCE_LIVE_API_SECRET"].strip(),
)
return cls(
mode="testnet",
rest_base="https://testnet.binancefuture.com",
ws_base="wss://stream.binancefuture.com/ws",
api_key=os.environ["BINANCE_TESTNET_API_KEY"].strip(),
api_secret=os.environ["BINANCE_TESTNET_API_SECRET"].strip(),
)
def normalize_step(value: Decimal, step: Decimal) -> Decimal:
"""Prevents Binance -1111 Precision & Filter failure: LOT_SIZE / PRICE_FILTER."""
return (value // step) * step.quantize(step, rounding=ROUND_DOWN)
def deterministic_client_order_id(mode: str, symbol: str, side: str, signal_ts_bucket: int) -> str:
digest = hashlib.sha256(f"{mode}:{symbol}:{side}:{signal_ts_bucket}".encode()).hexdigest()[:10]
return f"astra-{mode[:4]}-{symbol.lower()}-{digest}"