This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-05-02
Channels
- # announcements (31)
- # babashka (19)
- # beginners (90)
- # biff (1)
- # calva (172)
- # cider (3)
- # cljsrn (3)
- # clojure (84)
- # clojure-australia (1)
- # clojure-europe (11)
- # clojurescript (48)
- # css (1)
- # data-science (9)
- # datalog (3)
- # emacs (16)
- # events (2)
- # fulcro (8)
- # jackdaw (31)
- # luminus (3)
- # malli (3)
- # off-topic (13)
- # onyx (3)
- # random (1)
- # remote-jobs (2)
- # shadow-cljs (88)
- # vim (1)
- # xtdb (13)
can someone tell me how I can convert http://tech.ml dataset back to clojure? dataset->data gives me vectors, but I want a row by row representation.
@UCSJVFV35
tablecloth.api/rows
has various output formats.
On a lower level, tech.v3.dataset/mapseq-reader
returns a Reader (in the dtype-next
sense) of rows as maps.
I have a structure that looks like
{:UC48G7530085 [{:price 693.4965 :unixtimestamp 16893454}
{:price 700.3344 :unixtimestamp 16893455}
{:price 702.4444 :unixtimestamp 16893456}]}
And I need to create data for a candlestick chart which shows
• min price
• max price
• open price
• close price
• open and close timestamp
for slices of 1 min in time (I should ahve around 1 price per second).
I know how I would do this in R in a data.frame, but I must use this structure for an experiment i’m running. Any data people here have a suggestion for how to do this in clojure? I’m not sure how to create a sequence of timestamps at precise/even 1min increments e.g. 12:00:00, 12:01:00
and not 12:00:53, 12:01:53
from unix timestampsYou can use a library like juxt/tick
(require '[tick.alpha.api :as t])
(let [now (t/now)
now' (t/truncate now :minutes)]
(t/range
(t/beginning now')
(t/end (t/instant (t/at (t/tomorrow) (t/midnight))))
(t/new-duration 1 :minutes)))
=> (#time/instant”2021-05-02T23:17:00Z” #time/instant”2021-05-02T23:18:00Z” #time/instant”2021-05-02T23:19:00Z” #time/instant”2021-05-02T23:20:00Z” #time/instant”2021-05-02T23:21:00Z” #time/instant”2021-05-02T23:22:00Z” …. )
@U3RGL6XNF might have something to say about this time-related thread.
(defn candlestick-data
"Returns a vector of maps with the data to build a candlestick chart"
[price-quotes]
(->> price-quotes
(mapv #(assoc-in % [:truncated-dt] (t/truncate (:datetime %) :minutes)))
(group-by :truncated-dt)
(filter (fn [[_ v]] (> (count v) 1)))
(into {})
(reduce-kv
(fn [m k v]
(let [high-price (max-key-val :price v)
low-price (min-key-val :price v)
candle-start-unix (min-key-val :unixtimestamp v)
candle-end-unix (max-key-val :unixtimestamp v)
open-price (-> v
first
:price)
close-price (-> v
peek
:price)
n-quotes (count v)
res {:high-price high-price
:low-price low-price
:open-price open-price
:close-price close-price
:candle-start k
:candle-end (t/>> k (t/new-duration 1 :minutes))
:candle-start-unix candle-start-unix
:candle-end-unix candle-end-unix
:n-quotes n-quotes}]
(conj m res)))
[])))
worked for me 😄 not sure if it’s the “best” solution but the whole API is responding in 3ms so I’m happy and again impressed with Clj
juxt/tick
is dope! It’s exactly what I was looking for 🙂 let’s me work with unix timestamps as much as possible until the last minute@UFPEDL1LY Very glad to hear!