The trend detector
The detector answers one question: has the price started moving one way for real, or is this just noise? It uses a test called CUSUM, fed only by the pool's own price, and it fires when the evidence is strong enough, not after a fixed wait.
Step 1: measure the move
On the first swap of every block, the hook reads its price from its reserves, as they were before that swap, and compares it with the price it recorded last time. The result is the block's return: +0.3% means the price rose 0.3%.
Returns are measured as log-returns, so a 1% move counts the same at $100 as at $4,000, and a rise and a fall of the same size weigh the same. Source: PriceLib.sol (opens in a new tab).
Step 2: add up the evidence
The detector keeps a running score for each direction. Every block it adds the return and subtracts a small allowance for noise, called the slack k. The score can never go below zero.
- Small moves, or moves that reverse, are smaller than the slack. The score stays at zero.
- Moves that keep going one way are bigger than the slack. The score climbs.
- When the score crosses the threshold h, a trend is confirmed.
This test is called CUSUM. It is a standard tool for spotting a change as early as possible without reacting to noise, and it has a proven property (Lorden, 1971): for a given rate of false alarms, it keeps the worst-case delay before detecting a real change as short as possible. Source: Cusum.sol (opens in a new tab).
A worked example
Six quiet blocks, then ten blocks that each rise 0.3%. The slack is 0.001 (0.1%), so each trending block adds 0.003 − 0.001 = 0.002 to the score. The threshold is 0.005.
| Block | Return | Evidence | Spread charged |
|---|---|---|---|
| 5 | +0.07% | 0.000 | none |
| 6 | −0.06% | 0.000 | none |
| 7 | +0.30% | 0.002 | none yet |
| 8 | +0.30% | 0.004 | none yet |
| 9 | +0.30% | 0.006 | 0.33% |
| 10 | +0.30% | 0.008 | 1.00% |
| 11 | +0.30% | 0.010 | 1.67% |
| 12 | +0.30% | 0.012 | 2.33% |
The trend starts at block 7. Two blocks later the evidence passes 0.005 and the spread begins, small at first. A stronger move would cross sooner and a weaker one later. That is the point of the design: the moment depends on the data, so nobody can count blocks and time it.
Step 3: check that the move is really one-way
A wild, jumpy market can also build a score without going anywhere. So there is a second check, called directional efficiency (D): how far the price ended up, divided by how much it moved in total.
Evidence only becomes a spread while D is at least 0.25. Below that, the pool treats the movement as chop and charges nothing. D is tracked as a fading average that gives recent blocks more weight, so there is no fixed window an attacker could wait out. Source: DirectionalSignal.sol (opens in a new tab).
Why once per block, before the swap
- Once per block: a trade that is reversed within the same block never reaches the detector. To move it, someone has to hold the price across blocks, where arbitrageurs can trade against them.
- Before the swap: the trade that triggers the reading cannot influence it.
- Capped: one block's return is limited to 20%, so a single freak price cannot swing the score.
The settings in use
| Setting | Value | Meaning |
|---|---|---|
| Slack k | 0.001 | Moves under 0.1% a block count as noise |
| Threshold h | 0.005 | Evidence needed before any spread |
| Evidence cap | 0.02 | The score stops here; the spread is at its maximum |
| Efficiency gate | 0.25 | How one-way the movement must be |
| Memory | 0.9 per block | How quickly old blocks fade from D and volatility |
These were measured, not guessed: they were tested one at a time against four years of real ETH/USDC prices. The reasoning is in the README, section 9.4 (opens in a new tab). All of them are fixed when the pool is deployed and cannot be changed afterwards.