Swap to candle

One venue, one pipeline, every step a view running in this tab. Place a swap and follow it.

Click Buy or Sell, then follow your swap through each view.

Starting the database in this tab...
just ran-

How it works

Trades arrive as raw integers and flags, and your app wants candles. Usually that means a batch job. Here every step is a view that keeps itself up to date.

  1. 1Your swap lands as a raw venue row: integer amounts and a direction flag.

    create table market::raw_swaps {
      id: int8, slot: int8, pool: utf8,
      amount0: int8, amount1: int8, zero_for_one: bool
    }
  2. 2A deferred view joins the pool's decimals and turns it into a trade with a price.

    create deferred view market::normalized {
      id: int8, slot: int8, venue: utf8, price: float8,
      base_volume: float8, quote_volume: float8, is_buy: bool
    } as {
      from market::raw_swaps
      inner join { from market::pools } as p using (pool, p.pool)
      map {
        id, slot, venue: "rdb-usd",
        base_volume: cast(amount0, float8) / math::power(10.0, cast(p_base_decimals, float8)),
        quote_volume: cast(amount1, float8) / math::power(10.0, cast(p_quote_decimals, float8)),
        is_buy: zero_for_one == false
      }
      extend { price: quote_volume / base_volume }
    }
  3. 3Each later view builds on the one before: per-slot VWAP, then volume, TWAP and candles per 10 slots.

    create deferred view market::candles {
      bucket: int8, open: float8, high: float8, low: float8, close: float8
    } as {
      from market::candle_span
      inner join { from market::block } as o using (first_slot, o.slot)
      inner join { from market::block } as c using (last_slot, c.slot)
      map { bucket, open: o_vwap, high, low, close: c_vwap }
    }
  4. 4Nothing refreshes them. Your app just reads market::candles.

    from market::candles sort { bucket } take 8

Subscribe to the candles

Live subscription
create subscription as {
  from market::candles
  take 10
}

Waiting for the first read...

On a server this arrives as a push; in this tab we re-read it every half second

Read the live tables

Integer amounts and a direction flag, before any view touches them.

Raw, as the venue sends it
from market::raw_swaps sort { id } take 4

The same trades, filtered on the fly. Press Buy and run it again.

Only buys
from market::trades filter { is_buy == true } sort { id } take 5

25 RDB or more. Buy 25 or 100 to see your own swap here.

Big trades
from market::trades filter { base_volume >= 25.0 } sort { id } take 5

Slots with the most swaps, read from the per-slot view.

Busiest slots
from market::block sort { swaps } take 3

The share of volume that was buying, computed at read time.

Buy pressure
from market::volume map { bucket, buy_share: buy_volume / volume } take 2

How far price moved inside each candle.

Candle range
from market::candles extend { range: high - low } map { bucket, open, close, range } take 8

One row over every trade.

Everything so far
from market::trades aggregate { swaps: math::count(id), volume: math::sum(quote_volume) }

Examples