# Find significant price moves with SDT

> **For AI agents:** the complete documentation index is at [llms.txt](https://questdb.com/docs/llms.txt). Every page is available as markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` request header.

Reduce tick-level quotes to the turning points of the price path, within a tolerance in pips, across all FX pairs in one query using the sdt() window function.

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](/docs/query/sql/subsample/#sdt---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:

```questdb-sql demo title="Keep the turning points of each FX pair, within 2 pips"
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;
```

- `@mid` is the mid price from the top of the book.
- `@pip` is the pip size per row: `0.01` for JPY pairs and `0.0001` for 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 := 1` keeps every pair within 2 pips. For a maximum error `E`,
  use `E / 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:

```questdb-sql demo title="Keep a quote whenever the price enters a new 2-pip band"
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.

:::info Related documentation
- [SUBSAMPLE and SDT](/docs/query/sql/subsample/#sdt---swinging-door-trending)
- [sdt() window function](/docs/query/functions/window-functions/reference/#sdt)
- [DECLARE](/docs/query/sql/declare/)
- [OHLC bars](/docs/cookbook/sql/finance/ohlc/)
- [Downsample GPS tracks with SDT](/docs/cookbook/sql/time-series/downsample-gps-tracks/)
:::
