Formulas
Reference math
Technical formulas and calculations used by the bot. Reference material for advanced users who want to understand the exact math behind grid spacing, grid distance , rebalancing, indicators, and risk limits.
Grid Spacing
Grid levels use geometric (percentage-based) spacing, not linear spacing:
level_price = anchor_price × (1 + spacingPct/100)^n
Where
n is the level number (positive for sells, negative for buys).Example: With anchor at $2,000 and
spacingPct: 0.37:- Level +1: $2,000 × 1.0037 = $2,007.40
- Level +5: $2,000 × 1.0037^5 = $2,037.14
- Level -3: $2,000 × 1.0037^-3 = $1,977.92
Grid distance
Grid distance determines how far price can move before one side depletes:
slots = floor(inventoryUsd ÷ orderSizeUsd)
long grid distance = ((1 + spacingPct/100)^slots - 1) × 100
short grid distance = (1 - (1 + spacingPct/100)^-slots) × 100
Example: With $500 seeded inventory and 0.37% spacing:
Long: $500 ÷ $10.00 = 50 slots → 20.3% upside distance
Short: $500 ÷ $5.50 = 90 slots → 28.3% downside distance
Short grid distance cannot exceed 100% because price cannot fall below zero.
Rebalancing Distribution Rate
The rate at which excess position is corrected:
rate = clamp(baseRate × pivotRatio / ratio, baseRate, maxDistributionRate)
Where:
baseRate= 2.5% (fixed)pivotRatio= config value (default 10.0)ratio= current imbalance / orderSizeUsd- Result is clamped between
baseRateandmaxDistributionRate
Behavior:
- Small imbalances (low ratio) → higher rate → resolve quickly
- Large imbalances (high ratio) → lower rate → unwind gradually
Example: With
pivotRatio: 10.0, maxDistributionRate: 20.0, baseRate: 2.5%:Setup:
baseRate = 2.5% · pivotRatio = 10.0 · maxDistributionRate = 20.0%| Imbalance ratio | Raw result | Clamped? | Applied rate |
|---|---|---|---|
| 0.8 | 2.5% × 10.0 / 0.8 = 31.25% | Yes (cap at 20%) | 20.0% |
| 1.5 | 2.5% × 10.0 / 1.5 = 16.7% | No | 16.7% |
| 2.0 | 2.5% × 10.0 / 2.0 = 12.5% | No | 12.5% |
| 5.0 | 2.5% × 10.0 / 5.0 = 5.0% | No | 5.0% |
| 50 | 2.5% × 10.0 / 50 = 0.5% | Yes (floor at 2.5%) | 2.5% |
ROE% (Return on Equity)
Used by the Position Balancer to check profitability:
Long position:
ROE% = (markPrice - avgEntryPrice) / avgEntryPrice × 100
Short position:
ROE% = (avgEntryPrice - markPrice) / avgEntryPrice × 100
When
avgEntryPrice is zero (no position), ROE returns zero.Position Utilization
Used by Position Balancer tiers. Utilization is based on net exposure:
netExposure = abs(positionUsd - counterpartPositionUsd)
utilization = netExposure / maxNetExposureUsd
Example: Long $760, Short $200, with
maxNetExposureUsd: 1000:netExposure = abs($760 - $200) = $560
utilization = $560 / $1,000 = 0.56 (56%)
This would trigger the 50% tier (multiplier 1.25) but not the 75% tier.
Hedge Guard Activation
The Hedge Guard activates based on position ratio:
Entry condition:
longUsd < shortUsd × entryThresholdPct
Exit condition:
longUsd > shortUsd × exitThresholdPct
Setup:
entryThresholdPct = 0.667 · exitThresholdPct = 0.9| Your long | Your short | Long/Short ratio | Condition | Result |
|---|---|---|---|---|
| $400 | $700 | 0.57 | 0.57 < 0.667 (entry) | Activates |
| $600 | $700 | 0.86 | 0.86 < 0.9 (exit) | Stays active |
| $650 | $700 | 0.93 | 0.93 > 0.9 (exit) | Deactivates |
Indicator Calculations
RSI (Relative Strength Index)
Standard RSI formula with configurable period (
rsiLength):RSI = 100 - (100 / (1 + RS))
RS = average gain / average loss over period
Bollinger Bands
middle = SMA(price, bbLength)
upper = middle + (stdDev × bbStdDev)
lower = middle - (stdDev × bbStdDev)
Price below lower band = oversold signal (for longs)
Price above upper band = overbought signal (for shorts)
WaveTrend
A momentum oscillator combining channel and smoothing:
ap = (high + low + close) / 3
esa = EMA(ap, wtChannelLen)
d = EMA(|ap - esa|, wtChannelLen)
ci = (ap - esa) / (0.015 × d)
wt = EMA(ci, wtAverageLen)
signal = SMA(wt, wtSmaLen)
WT below
wtOversoldThreshold = oversold
WT above wtOverboughtThreshold = overbought
Momentum = direction wt is moving relative to signal linePnD Detection
Pump-and-dump protection triggers when:
closeFillCount ≥ closeFillsThreshold within withinSeconds
Example: With
closeFillsThreshold: 8, withinSeconds: 60:If 8 close orders fill within 60 seconds (~3% move at 0.37% spacing), the bot enters cooldown.
Effective Leverage
In Bybit hedge cross margin, long and short positions net each other. Only the difference between them drives effective leverage:
effectiveLeverage = abs(longUsd - shortUsd) / walletBalance
Setup: wallet balance = $500
| Your long | Your short | Net exposure | Effective leverage |
|---|---|---|---|
| $500 | $500 | $0 | 0x |
| $1,000 | $600 | $400 | 0.8x |
| $1,500 | $500 | $1,000 | 2x |
Note: Bybit's displayed per-position leverage is not the same as effective leverage across both sides.