Skip to content

Financial analysis overview

Perform analysis of financial asset data

Perform analysis of financial asset data. These specialized hyperfunctions make it easier to write financial analysis queries that involve candlestick data.

They help you answer questions such as:

  • What are the opening and closing prices of these stocks?
  • When did the highest price occur for this stock?

This function group uses the two-step aggregation pattern. In addition to the usual aggregate function, candlestick_agg, it also includes the pseudo-aggregate function candlestick. candlestick_agg produces a candlestick aggregate from raw tick data, which can then be used with the accessor and rollup functions in this group. candlestick takes pre-aggregated data and transforms it into the same format that candlestick_agg produces. This allows you to use the accessors and rollups with existing candlestick data.

This group of functions uses the two-step aggregation pattern.

Rather than calculating the final result in one step, you first create an intermediate aggregate by using the aggregate function.

Then, use any of the accessors on the intermediate aggregate to calculate a final result. You can also roll up multiple intermediate aggregates with the rollup functions.

The two-step aggregation pattern has several advantages:

  1. More efficient because multiple accessors can reuse the same aggregate
  2. Easier to reason about performance, because aggregation is separate from final computation
  3. Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
  4. Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result

To learn more, see the blog post on two-step aggregates.

Query your tick data table for the opening, high, low, and closing prices, and the trading volume, for each 1 hour period in the last day:

SELECT
time_bucket('1 hour'::interval, "time") AS ts,
symbol,
open(candlestick_agg("time", price, volume)),
high(candlestick_agg("time", price, volume)),
low(candlestick_agg("time", price, volume)),
close(candlestick_agg("time", price, volume)),
volume(candlestick_agg("time", price, volume))
FROM crypto_ticks
WHERE "time" > now() - '1 day'::interval
GROUP BY ts, symbol
;
-- or
WITH cs AS (
SELECT time_bucket('1 hour'::interval, "time") AS hourly_bucket,
symbol,
candlestick_agg("time", price, volume) AS candlestick
FROM crypto_ticks
WHERE "time" > now() - '1 day'::interval
GROUP BY hourly_bucket, symbol
)
SELECT hourly_bucket,
symbol,
open(candlestick),
high(candlestick),
low(candlestick),
close(candlestick),
volume(candlestick)
FROM cs
;

Create a continuous aggregate from tick data and roll it up

Section titled “Create a continuous aggregate from tick data and roll it up”

Create a continuous aggregate on your stock trade data:

CREATE MATERIALIZED VIEW candlestick
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 minute'::interval, "time") AS ts,
symbol,
candlestick_agg("time", price, volume) AS candlestick
FROM crypto_ticks
GROUP BY ts, symbol
;

Query your by-minute continuous aggregate over stock trade data for the opening, high, low, and closing (OHLC) prices, along with their timestamps, in the last hour:

SELECT ts,
symbol,
open_time(candlestick),
open(candlestick),
high_time(candlestick),
high(candlestick),
low_time(candlestick),
low(candlestick),
close_time(candlestick),
close(candlestick)
FROM candlestick
WHERE ts > now() - '1 hour'::interval
;

Roll up your by-minute continuous aggregate into daily buckets and return the Volume Weighted Average Price for AAPL for the last month:

SELECT
time_bucket('1 day'::interval, ts) AS daily_bucket,
symbol,
vwap(rollup(candlestick))
FROM candlestick
WHERE symbol = 'AAPL'
AND ts > now() - '1 month'::interval
GROUP BY daily_bucket
ORDER BY daily_bucket
;

Roll up your by-minute continuous aggregate into hourly buckets and return the the opening, high, low, and closing prices and the volume for each 1 hour period in the last day:

SELECT
time_bucket('1 hour'::interval, ts) AS hourly_bucket,
symbol,
open(rollup(candlestick)),
high(rollup(candlestick)),
low(rollup(candlestick)),
close(rollup(candlestick)),
volume(rollup(candlestick))
FROM candlestick
WHERE ts > now() - '1 day'::interval
GROUP BY hourly_bucket
;

If you have a table of pre-aggregated stock data, it might look similar this this format:

ts │ symbol │ open │ high │ low │ close │ volume
────────────────────────┼────────┼────────┼────────┼────────┼────────┼──────────
2022-11-17 00:00:00-05 │ VTI │ 195.67197.9195.45197.493704700
2022-11-16 00:00:00-05 │ VTI │ 199.45199.72198.03198.322905000
2022-11-15 00:00:00-05 │ VTI │ 201.5202.14198.34200.364606200
2022-11-14 00:00:00-05 │ VTI │ 199.26200.92198.21198.354248200
2022-11-11 00:00:00-05 │ VTI │ 198.58200.7197.82200.164538500
2022-11-10 00:00:00-05 │ VTI │ 194.35198.31193.65198.143981600
2022-11-09 00:00:00-05 │ VTI │ 190.46191.04187.21187.5313959600
2022-11-08 00:00:00-05 │ VTI │ 191.25193.31189.42191.664847500
2022-11-07 00:00:00-05 │ VTI │ 189.59190.97188.47190.663420000
2022-11-04 00:00:00-04 │ VTI │ 189.32190.3185.75188.943584600
2022-11-03 00:00:00-04 │ VTI │ 186.5188.09185.13186.543935600
2022-11-02 00:00:00-04 │ VTI │ 193.07195.27188.29188.344686000
2022-11-01 00:00:00-04 │ VTI │ 196196.44192.76193.439873800
2022-10-31 00:00:00-04 │ VTI │ 193.99195.17193.51194.035053900
2022-10-28 00:00:00-04 │ VTI │ 190.84195.53190.74195.293178800
2022-10-27 00:00:00-04 │ VTI │ 192.46193.47190.61190.853556300
2022-10-26 00:00:00-04 │ VTI │ 191.26194.64191.26191.754091100
2022-10-25 00:00:00-04 │ VTI │ 189.57193.16189.53192.943287100
2022-10-24 00:00:00-04 │ VTI │ 188.38190.12186.69189.514527800
2022-10-21 00:00:00-04 │ VTI │ 182.99187.78182.29187.493381200
2022-10-20 00:00:00-04 │ VTI │ 184.54186.99182.81183.272636200
2022-10-19 00:00:00-04 │ VTI │ 185.25186.64183.34184.872589100
2022-10-18 00:00:00-04 │ VTI │ 188.14188.7184.71186.463906800

You can use the candlestick function to transform the data into a form that you’ll be able pass to all of the accessors and rollup functions. To show that your data is preserved, this example shows how these accessors return a table that looks just like your data:

SELECT
ts,
symbol,
open(candlestick),
high(candlestick),
low(candlestick),
close(candlestick),
volume(candlestick)
FROM (
SELECT
ts,
symbol,
candlestick(ts, open, high, low, close, volume)
FROM historical_data
) AS _(ts, symbol, candlestick);
;
-- or
WITH cs AS (
SELECT ts
symbol,
candlestick(ts, open, high, low, close, volume)
FROM historical_data
)
SELECT
ts
symbol,
open(candlestick),
high(candlestick),
low(candlestick),
close(candlestick),
volume(candlestick)
FROM cs
;

The advantage of transforming your data into the candlestick aggergate form is that you can then use other functions in this group, such as rollup and vwap.

Roll up your by-day historical data into weekly buckets and return the Volume Weighted Average Price:

SELECT
time_bucket('1 week'::interval, ts) AS weekly_bucket,
symbol,
vwap(rollup(candlestick))
FROM (
SELECT
ts,
symbol,
candlestick(ts, open, high, low, close, volume)
FROM historical_data
) AS _(ts, symbol, candlestick)
GROUP BY weekly_bucket, symbol
;
  • candlestick_agg(): aggregate tick data into an intermediate form for further calculation
  • candlestick(): transform pre-aggregated candlestick data into the correct form to use with candlestick_agg functions
  • open(): get the opening price from a candlestick aggregate
  • open_time(): get the timestamp of the opening price from a candlestick aggregate
  • high(): get the high price from a candlestick aggregate
  • high_time(): get the timestamp of the high price from a candlestick aggregate
  • low(): get the low price from a candlestick aggregate
  • low_time(): get the timestamp of the low price from a candlestick aggregate
  • close(): get the closing price from a candlestick aggregate
  • close_time(): get the timestamp of the closing price from a candlestick aggregate
  • volume(): get the total volume from a candlestick aggregate
  • vwap(): calculate the volume-weighted average price from a candlestick aggregate
  • rollup(): roll up multiple candlestick aggregates