Public
Remixed
Bybit perps bot via ccxt: order-flow strategy + safety rails
binancecronfuturestelegramtradingbybit
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.

Bybit USDT Perpetual Futures Bot

A real-money Bybit perpetual-futures trading bot with a real scored strategy (no placeholder points) and a full set of safety rails. It is a fork of the Binance bot, moved to Bybit's linear USDT perps to avoid the region restrictions and ban risk that some Binance accounts face.

⚠️ This trades real money. It places live market orders, sets 5x leverage, and parks stop-loss / take-profit brackets on Bybit USDT perpetuals. Start with a tiny funded account and watch it in paper/forward-test mode first.

Files

FileRole
main.tsCron entrypoint — scans, scores, and trades on Bybit
strategy.tsOrder-flow scalping signal from HVN/LVN + footprint imbalance
flow.tsBuilds the volume profile (POC, HVN, LVN, Core Zone) + footprint from trades
safety.tsKill switch, daily loss/trade caps, drawdown guard, dedup, cooldown, reconcile
config.tsAll tunable settings in one place
telegram.tsTelegram alerts
control.tsHTTP webhook — status + remote kill switch
README.mdThis file

Market universe

Every 2 hours the bot re-scans the whole Bybit linear-perp market and rebuilds its watchlist. A symbol is included only if it meets all of:

  • 24h quote volume ≥ MIN_VOLUME_USD ($20M)
  • It is a USDT perpetual (/USDT:USDT)
  • Base is not BTC or ETH (EXCLUDED_BASES)
  • Price ≤ MAX_BASE_PRICE_USD ($1,000)

Strategy (Option B) — Order Flow (HVN / LVN / Footprint)

The strategy is built on order flow, not lagging indicators. For each symbol it pulls up to the last 2h of trades and:

  1. Volume profile — buckets trade volume into 40 price bins → finds:
    • POC (Point of Control): the price level with the most traded volume
    • HVN (High Volume Nodes): bins ≥ 1.5× average volume — strong support/resistance that price gravitates to
    • LVN (Low Volume Nodes): bins ≤ 0.3× average volume — weak zones price slices through quickly
    • Core Zone (value area): the range containing 70% of traded volume
  2. Footprint — for the latest 15m candle, sums buy-volume vs sell-volume at the aggressor level:
    • Delta = buy − sell volume
    • Imbalance = (buy − sell) / (buy + sell), in −1..1

Signal logic:

  • Long when price is trading off an HVN support and the footprint shows a taker-buy imbalance (delta > 0, imbalance > 0.25).
  • Short when price is rejecting off an HVN resistance with taker-sell imbalance.
  • LVN proximity adds "fuel" points (room to run through the weak zone).

Gates: the setup must be off an HVN and backed by a strong footprint imbalance — never a blind one-sided entry. Trades only at score ≥ 70. Stop = 25% of the observed trading range; target = 2× stop distance (R:R 1:2).

Order placement on Bybit

Unlike Binance (which needs three separate orders — entry, STOP_MARKET, TAKE_PROFIT_MARKET), Bybit lets us attach the stop-loss and take-profit to the entry order itself as an atomic bracket. main.ts passes stopLoss / takeProfit (with triggerPrice) in the createOrder params, so the position is protected the instant it fills — no window where it sits naked. ccxt auto-sets Bybit's triggerDirection from the side/price relationship. Margin mode is isolated; leverage is set per-symbol before entry.

Safety rails (Option C)

  • Kill switch — halts all new entries. Flip it via the control webhook (CONTROL_TOKEN) or by setting KILL_SWITCH-style DB state.
  • Daily loss cap — stops opening new trades once realized loss hits MAX_DAILY_LOSS_USD (default $50).
  • Daily trade capMAX_DAILY_TRADES (default 10) per UTC day.
  • Drawdown guard — stops new entries once unrealized loss on open positions hits MAX_DRAWDOWN_USD (default $100).
  • Max open tradesMAX_OPEN_TRADES (default 2), counted correctly for shorts (|contracts| > 0).
  • Position dedup — never opens a second position on a symbol already held.
  • Cooldown — no re-entry on a symbol for SYMBOL_COOLDOWN_MS after a trade.
  • Reconciliation — each run syncs the DB ledger with Bybit's live positions, booking closed positions' PnL and updating marks.

Setup

  1. Set up a Bybit API key (read + trade permissions; futures enabled): https://www.bybit.com/app/user/api-management
  2. Set env vars on this val:
    • BYBIT_API_KEY, BYBIT_SECRET — Bybit futures API credentials
    • TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID — alerts (optional)
    • CONTROL_TOKEN — secret to authorize kill-switch toggles (optional)
  3. Confirm the interval schedule on main.ts (default: every 15 min).
  4. Test with a tiny balance first. Watch Telegram + the control webhook before trusting it with real size.

Control webhook

  • GET → JSON status (kill switch, today's PnL/trades, open positions)
  • POST {"action":"kill","state":true} with header X-Control-Token: <token> → flips the kill switch. Refused unless CONTROL_TOKEN is set.