- What are the opening and closing prices of these stocks?
- When did the highest price occur for this stock?
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.
Two-step aggregation
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:- More efficient because multiple accessors can reuse the same aggregate
- Easier to reason about performance, because aggregation is separate from final computation
- Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
- Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result
Samples
Get candlestick values from tick data
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:Create a continuous aggregate from tick data and roll it up
Create a continuous aggregate on your stock trade data:AAPL for the last month:
Starting from already-aggregated data
If you have a table of pre-aggregated stock data, it might look similar this this format: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:
rollup
and vwap.
Roll up your by-day historical data into weekly buckets and return the Volume
Weighted Average Price:
Available functions
Aggregate
candlestick_agg(): aggregate tick data into an intermediate form for further calculation
Pseudo-aggregate
candlestick(): transform pre-aggregated candlestick data into the correct form to use with candlestick_agg functions
Accessors
open(): get the opening price from a candlestick aggregateopen_time(): get the timestamp of the opening price from a candlestick aggregatehigh(): get the high price from a candlestick aggregatehigh_time(): get the timestamp of the high price from a candlestick aggregatelow(): get the low price from a candlestick aggregatelow_time(): get the timestamp of the low price from a candlestick aggregateclose(): get the closing price from a candlestick aggregateclose_time(): get the timestamp of the closing price from a candlestick aggregatevolume(): get the total volume from a candlestick aggregatevwap(): calculate the volume-weighted average price from a candlestick aggregate
Rollup
rollup(): roll up multiple candlestick aggregates