我在influxdb中有一些tick数据存储,协议行如下,(注:lastPrice是当时的实时价格,totalVolume是当天开始的累计交易数量(
tick,contract=FU2301 lastPrice=10.2,totalVolume=100
tick,contract=FU2301 lastPrice=10.1,totalVolume=110
tick,contract=FU2301 lastPrice=10.3,totalVolume=150
...
tick,contract=FU2301 lastPrice=9.8,totalVolume=290
我正试图从蜱虫数据中计算1减去OHLCV。(注:这里的数量是每减一笔的交易数量(
time measurement contract open high low close volume
----
xxxx tick FU2301 10.2 10.3 9.0 9.3 10
xxxx tick FU2301 10.2 10.3 9.1 9.2 40
xxxx tick FU2301 10.1 10.4 9.0 9.1 20
所以我的通量脚本如下,但它没有按预期工作。
data = from(bucket: "mk_data_test")
|> range(start: -12h, stop: now())
|> filter(fn: (r) => r["_measurement"] == "tick")
|> filter(fn: (r) => r["contract"] == "FU2301")
|> window(every: 1m)
volumeData = data |> filter(fn: (r) => r["_field"] == "totalVolume")
priceData = data |> filter(fn: (r) => r["_field"] == "lastPrice")
max = priceData |> max() |> set(key: "_field", value: "max")
min = priceData |> min() |> set(key: "_field", value: "min")
open = priceData |> first() |> set(key: "_field", value: "open")
close = priceData |> last() |> set(key: "_field", value: "close")
// this expression didn't work excepted
volume = volumeData |> first() |> difference() |> set(key: "_field", value: "volume")
union(tables: [max, min, open, close, volume])
|> pivot(rowKey: ["_start"], columnKey: ["_field"], valueColumn: "_value")
您是否对卷数据集的行号感到困惑?默认情况下;对于每个有n行的输入表,difference((输出一个有n-1行的表">
如果您想将卷数据集与最大、最小、打开和关闭对齐,可以打开keepFirst标志:
volume = volumeData |> difference(keepFirst: true) |> set(key: "_field", value: "volume")
点击此处查看更多详细信息。