Mathematical library
The authoritative, versioned formula catalogue — returns, PnL, True Range & ATR, volatility, momentum, moving averages, RSI, breakout and volume.
Clawlas never asks an LLM to compute a number. Prices, returns, volatility, PnL and every indicator come from this deterministic, versioned formula catalogue — pure functions with unit tests, explicit units and a defined treatment of missing data.
Price returns
The intuitive percentage change between two prices — used for trade return, portfolio return and reporting.
- price at time t
- price at the previous step
def simple_return(current: float, previous: float) -> float:
if previous <= 0:
raise ValueError("previous must be > 0")
return (current / previous) - 1.0Additive across periods and symmetric — the basis for volatility and statistical analysis.
Compounds a series of simple returns into one total return.
Trade PnL
Gross PnL for a Spot long is quantity times the price move; net PnL subtracts every cost. Clawlas always reports net.
- filled quantity
- entry and exit prices
- +gross PnL
- -entry feeEntryCost × f_entry
- -exit feeExitValue × f_exit
- -other costsnetwork, bridge, conversion, realized slippage
Net profit over the capital actually committed (entry cost plus the entry fee).
def net_return(net_pnl: float, entry_cost: float, entry_fee: float) -> float:
return net_pnl / (entry_cost + entry_fee)True Range & ATR
The largest of today's range and the gaps to yesterday's close — captures overnight jumps a high-minus-low misses.
- high and low of the current candle
- previous close
Average volatility over n periods (Wilder smoothing is also supported). Feeds stop distance and sizing — never a standalone signal.
def average_true_range(data, period: int = 14):
tr = true_range(data)
return tr.ewm(alpha=1 / period, adjust=False).mean()Volatility
Standard deviation of returns. Local volatility drives stop distance and sizing; annualized volatility is mostly for comparison and reporting.
- periods per year for the timeframe
Volatility is bucketed into regimes per market, venue and timeframe:
| Regime | Condition |
|---|---|
| Low | Current volatility < 30th percentile |
| Normal | 30th ≤ current ≤ 80th percentile |
| High | Current volatility > 80th percentile |
| Extreme | Current volatility > 95th percentile |
Momentum
Percentage move over n periods — the raw momentum measure.
Momentum expressed in units of volatility, so the same threshold means the same thing across calm and wild markets.
How many of the recent returns were positive. A high ratio means an orderly move, not one isolated candle.
- indicator: 1 if true, else 0
Moving averages & RSI
Trend filters and momentum confirmation only — never sufficient standalone entry signals.
- EMA smoothing factor
Momentum confirmation and overextension detection. A given RSI never automatically implies a reversal.
Breakout
Close above the highest high of the prior n candles — the current candle is never part of the prior high.
- previous highest high over n candles
- median volume over n candles
- volume threshold
Volume
Median, not mean — for new and skewed tokens a single print should not move the baseline.
Value traded, comparable across assets — generally more useful than base-unit volume.
- relative volume
- quote volume
- volume acceleration
- volume concentration