Hi, I am trying to create Candlestick from data I enter from binance, they are 1m timeframe candles.
When querying the data it does not take more than 0.07s
But when I implement some of the functions that I found on this website for example this code, the cpu usage goes way up
data = from(bucket: "crypto_app")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "candles")
|> filter(fn: (r) => r["exchange"] == "Binance")
|> filter(fn: (r) => r["symbol"] == "BTC-USDT")
|> window(every: 5m)
|> reduce(fn: (r, accumulator) => ({
indexLow:
if (r._field=="low") then
accumulator.indexLow+1
else
accumulator.indexLow,
indexOpen:
if (r._field=="open") then
accumulator.indexOpen+1
else
accumulator.indexOpen,
open:
if (r._field=="open") then
if (accumulator.indexOpen==0) then
float(v:r._value )
else
accumulator.open
else
accumulator.open
,
high:
if (r._field=="high") then
if(r._value>accumulator.high ) then
float(v:r._value )
else
accumulator.high
else
accumulator.high
,
low:
if (r._field=="low") then
if(r._value<accumulator.low or accumulator.indexLow==0.0) then
float(v:r._value )
else
accumulator.low
else
accumulator.low,
close:
if (r._field=="close") then
float(v:r._value )
else
accumulator.close,
volume:
if (r._field=="volume") then
float(v:r._value )+accumulator.volume
else
accumulator.volume
}),
identity: {indexLow:0,indexOpen:0,open: 0.0,high: 0.0,low: 0.0,close: 0.0,volume: 0.0})
|> drop(columns: ["indexOpen","indexLow"])
|> group(columns:["symbol"])
|> yield(name: "candle")
Another code I found
data = from(bucket:"crypto_app")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "candles")
|> filter(fn: (r) => r["symbol"] == "BTC-USDT")
open = data |> filter(fn: (r) => r._field == "open") |> aggregateWindow(every: 5m, fn: first) |> drop(columns: ["_start", "_stop", "_measurement", "_field"])
close = data |> filter(fn: (r) => r._field == "close") |> aggregateWindow(every: 5m, fn: last) |> drop(columns: ["_start", "_stop", "_measurement", "_field"])
low = data |> filter(fn: (r) => r._field == "low") |> aggregateWindow(every: 5m, fn: min) |> drop(columns: ["_start", "_stop", "_measurement", "_field"])
high = data |> filter(fn: (r) => r._field == "high") |> aggregateWindow(every: 5m, fn: max) |> drop(columns: ["_start", "_stop", "_measurement", "_field"])
OC = join(tables: {o: open, c: close}, on: ["_time", "symbol"])
HL = join(tables: {h: high, l: low}, on: ["_time", "symbol"])
OCLH = join(tables: {OC: OC, HL: HL}, on: ["_time", "symbol"])
|> group(columns:["symbol"])
|> rename(columns: {_value_c: "c", _value_h:"h", _value_l:"l", _value_o:"o"})
|> yield(name: "mean")
In this way I entered the information using a python script from the csv in binance collection
point = (
dot(“candles”)
.tag(“exchange”, “Binance”)
.tag(“symbol”, “BTC-USDT”)
.tag(“interval”, “1m”)
.tag(“topic”, “crypto/candles/Binance/BTC-USDT/1m”)
.field(“start”, row[0].astype(‘int64’))
.field(“stop”, row[6].astype(‘int64’))
.field(“open”, row[1])
.field(“high”, row[2])
.field(“minimum”, row[3])
.field(“close”, row[4])
.field(“volume”, row[5])
.time(row[0].astype(‘int64’), “ms”),
)
1722470400000,64628.01000000,64670.01000000,64601.00000000,64634.01000000,36.77990000,1722470459999,2377477.74237530,3399,13.19798000,853026.66151230,0
sometimes it takes more than 50sec or never ends.
I have no idea what I am doing wrong