Skip to content

Instantly share code, notes, and snippets.

@castis
Created September 22, 2022 11:23
Show Gist options
  • Save castis/8e35d8d9129558a09a004e715b468c0d to your computer and use it in GitHub Desktop.
Save castis/8e35d8d9129558a09a004e715b468c0d to your computer and use it in GitHub Desktop.
Flux OHLCV query
base = from(bucket: "stocks")
|> range(start: _start)
|> filter(fn: (r) => r._measurement == "stock" and r.symbol == _symbol)
get = (field, fn) =>
base
|> filter(fn: (r) => r._field == field)
|> aggregateWindow(every: _every, fn: fn, createEmpty: true)
union(tables: [
get(field: "o", fn: first),
get(field: "h", fn: max),
get(field: "l", fn: min),
get(field: "c", fn: last),
get(field: "v", fn: sum)
])
|> group()
|> pivot(
rowKey: ["_time"],
columnKey: ["_field"],
valueColumn: "_value"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment