Find significant price moves with SDT
Reduce tens of thousands of quotes per symbol to the points where the price path changes direction, within a tolerance in pips. OHLC bars reduce tick data by time. This recipe reduces it by price movement, keeping real quotes with their original timestamps.
The problem
The market_data table on the demo receives around 220,000 order book
updates per hour for each of 30 FX pairs. To chart a long range, find turning
points, or feed a strategy that reacts to moves larger than a few pips, most
of those updates add nothing: they sit on a line between the quotes around
them.
A common first idea is to keep a quote only when the price changes by more than N pips. That needs the price of the last kept quote, which depends on earlier decisions, so window functions cannot express it exactly.
Solution: SDT on the mid price in pips
Swinging Door Trending
(SDT) keeps the quotes needed so that a straight line in time between kept
quotes stays within a tolerance of every quote in between. The window form
supports PARTITION BY, so one query covers every symbol:
DECLARE
@range := '$now-1h..$now',
@mid := (best_bid + best_ask) / 2,
@pip := CASE WHEN symbol LIKE '%JPY' THEN 0.01 ELSE 0.0001 END,
@tol_pips := 1
WITH flagged AS (
SELECT timestamp, symbol, @mid AS mid,
sdt(timestamp, @mid / @pip, @tol_pips)
OVER (PARTITION BY symbol ORDER BY timestamp) AS keep
FROM market_data
WHERE timestamp IN @range
)
SELECT timestamp, symbol, mid
FROM flagged
WHERE keep;
@midis the mid price from the top of the book.@pipis the pip size per row:0.01for JPY pairs and0.0001for the rest. Dividing the mid price by it expresses every pair in pips, so a single tolerance fits all of them.sdt()needs a constant tolerance, so it cannot take a per-symbol value directly.- The reconstruction error of SDT is bounded by
2 * compdev, so@tol_pips := 1keeps every pair within 2 pips. For a maximum errorE, useE / 2.
On the demo this keeps about 1.2% of the rows: roughly 2,700 quotes per symbol per hour.
Turning points, not price changes
SDT does not keep "every quote where the price moved by N pips". It keeps the quotes where the trend changes:
- A steady drift, even a large one, keeps very few quotes, because a straight line between its ends stays within the tolerance.
- A reversal larger than the tolerance keeps a quote at the turn, even if the price ends up where it started.
- Noise smaller than the tolerance keeps nothing.
To keep a quote whenever the price moves N pips from the last kept quote, SDT is the wrong tool. Use the grid filter below, or a client-side loop.
The naive alternative: price grid crossings
Window functions can approximate "keep a quote when the price moves N pips" by splitting prices into bands of N pips and keeping a quote whenever the band changes:
DECLARE
@range := '$now-1h..$now',
@mid := (best_bid + best_ask) / 2,
@pip := CASE WHEN symbol LIKE '%JPY' THEN 0.01 ELSE 0.0001 END,
@band_pips := 2
WITH bands AS (
SELECT timestamp, symbol, @mid AS mid,
floor(@mid / @pip / @band_pips) AS band,
lag(floor(@mid / @pip / @band_pips))
OVER (PARTITION BY symbol ORDER BY timestamp) AS prev_band
FROM market_data
WHERE timestamp IN @range
)
SELECT timestamp, symbol, mid
FROM bands
WHERE prev_band IS NULL OR band <> prev_band;
Holding the last kept price until the next one, this stays within one band of the real price, so it also has a bound. It needs far more rows to keep it:
- It keeps more than twice as many rows. On the demo it keeps about 2.8% of the rows against 1.2% for SDT with a similar 2-pip tolerance, and runs slightly slower.
- A price that wobbles across a band edge is kept on every wobble. About a quarter of the kept quotes only return to the band of the quote kept two steps earlier.
- Steady drifts keep a quote per band, where SDT keeps the two ends.
Analysis, not storage
Use these results for charts, turning points, and signals, not to delete ticks. Volume, VWAP, trade counts, and any audit or best execution review need every row.