Skip to content

Commit

Permalink
added new calendar.core functions
Browse files Browse the repository at this point in the history
fixed-window-open and calendar-seq-prior-open
  • Loading branch information
wizard50 committed Oct 19, 2024
1 parent ff5d46f commit 7dc3fb8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
33 changes: 26 additions & 7 deletions lib/calendar/src/quanta/calendar/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

;; all function should have docstrings and be tested.

(defn now-calendar [calendar-kw]
(defn now-calendar
"returns a zoned-date-time of now with the timezone of the calendar"
[calendar-kw]
(let [calendar (calendar-kw calendars)]
(interval/now-calendar calendar)))

Expand Down Expand Up @@ -97,9 +99,10 @@
current-open-dt (:current-open interval)]
(current-open-dt calendar dt)))

(defn close->open-dt [[calendar-kw interval-kw] & [dt]]
(let [dt (if dt dt (t/now))
calendar (calendar-kw calendars)
(defn close->open-dt
"converts bar close time to bar open time"
[[calendar-kw interval-kw] dt]
(let [calendar (calendar-kw calendars)
interval (interval-kw intervals)
_ (assert calendar)
_ (assert interval)
Expand All @@ -112,9 +115,10 @@
(prior-open-dt calendar aligned-close-dt) ; dt is aligned close -> new candle started. prior open needed
(current-open-dt calendar aligned-close-dt)))) ; dt is not alined close -> unfinished candle. current open needed

(defn open->close-dt [[calendar-kw interval-kw] & [dt]]
(let [dt (if dt dt (t/now))
calendar (calendar-kw calendars)
(defn open->close-dt
"converts bar open time to bar close time"
[[calendar-kw interval-kw] dt]
(let [calendar (calendar-kw calendars)
interval (interval-kw intervals)
_ (assert calendar)
_ (assert interval)
Expand Down Expand Up @@ -143,6 +147,13 @@
prior-fn (partial prior-close [calendar-kw interval-kw])]
(iterate prior-fn cur-dt)))

(defn calendar-seq-prior-open
"like calendar-seq-prior but with bar open time"
[[calendar-kw interval-kw] dt]
(let [cur-dt (current-open [calendar-kw interval-kw] dt)
prior-fn (partial prior-open [calendar-kw interval-kw])]
(iterate prior-fn cur-dt)))

(defn trailing-window
"returns a calendar-seq for a calendar of n rows
if end-dt specified then last date equals end-date,
Expand Down Expand Up @@ -175,6 +186,14 @@
after-start? (fn [dt] (t/>= dt start))]
(take-while after-start? seq)))

(defn fixed-window-open
"like fixed-window but with bar open time
window start and end should be also bar open times"
[[calendar-kw interval-kw] {:keys [start end] :as window}]
(let [seq (calendar-seq-prior-open [calendar-kw interval-kw] end)
after-start? (fn [dt] (t/>= dt start))]
(take-while after-start? seq)))

(defn calendar-seq->range [cal-seq]
{:start (last cal-seq)
:end (first cal-seq)})
Expand Down
52 changes: 50 additions & 2 deletions lib/calendar/test/ta/calendar/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
[tick.core :as t]
[ta.calendar.data.dates :refer :all]
[ta.calendar.calendars :as cal]
[quanta.calendar.core :refer [trailing-window calendar-seq fixed-window
close->open-dt open->close-dt]]))
[quanta.calendar.core :refer [trailing-window calendar-seq
fixed-window fixed-window-open
close->open-dt open->close-dt]]))
(defn print-seq [s]
(for [i (range 0 (count s))]
(println i ": " (nth s i))))
Expand Down Expand Up @@ -812,4 +813,51 @@
(is (= (nth close-dts 9) utc-thursday-23-56))
(is (not (= (nth close-dts 5) utc-friday-00-00)))))
)

(deftest fixed-window-open--crypto
(testing "crypto :m | inside trading day - fixed-window-open"
(let [fixed-window-crypto-m (fixed-window-open [:crypto :m] {:start utc-friday-16-25
:end utc-friday-16-29})]
(is (= (nth fixed-window-crypto-m 0) utc-friday-16-29))
(is (= (nth fixed-window-crypto-m 1) utc-friday-16-28))
(is (= (nth fixed-window-crypto-m 2) utc-friday-16-27))
(is (= (nth fixed-window-crypto-m 3) utc-friday-16-26))
(is (= (nth fixed-window-crypto-m 4) utc-friday-16-25))
(is (not (= (nth fixed-window-crypto-m 0) utc-friday-16-30)))))

(testing "crypto :m | on trading day start - fixed-window-open"
(let [fixed-window-crypto-m (fixed-window-open [:crypto :m] {:start utc-friday-00-00
:end utc-friday-00-04})]
(is (= (nth fixed-window-crypto-m 0) utc-friday-00-04))
(is (= (nth fixed-window-crypto-m 1) utc-friday-00-03))
(is (= (nth fixed-window-crypto-m 2) utc-friday-00-02))
(is (= (nth fixed-window-crypto-m 3) utc-friday-00-01))
(is (= (nth fixed-window-crypto-m 4) utc-friday-00-00))
(is (not (= (nth fixed-window-crypto-m 0) utc-friday-00-05)))))

(testing "crypto :m | on trading day end - fixed-window-open"
(let [fixed-window-crypto-m (fixed-window-open [:crypto :m] {:start utc-thursday-23-56
:end utc-friday-00-00})]
(is (= (nth fixed-window-crypto-m 0) utc-friday-00-00))
(is (= (nth fixed-window-crypto-m 1) utc-thursday-23-59))
(is (= (nth fixed-window-crypto-m 2) utc-thursday-23-58))
(is (= (nth fixed-window-crypto-m 3) utc-thursday-23-57))
(is (= (nth fixed-window-crypto-m 4) utc-thursday-23-56))
(is (not (= (nth fixed-window-crypto-m 0) utc-thursday-23-59-59-999)))))

(testing "crypto :m | over 2 trading days - fixed-window-open"
(let [fixed-window-crypto-m (fixed-window-open [:crypto :m] {:start utc-thursday-23-56
:end utc-friday-00-05})]
(is (= (nth fixed-window-crypto-m 0) utc-friday-00-05))
(is (= (nth fixed-window-crypto-m 1) utc-friday-00-04))
(is (= (nth fixed-window-crypto-m 2) utc-friday-00-03))
(is (= (nth fixed-window-crypto-m 3) utc-friday-00-02))
(is (= (nth fixed-window-crypto-m 4) utc-friday-00-01))
(is (= (nth fixed-window-crypto-m 5) utc-friday-00-00))
(is (= (nth fixed-window-crypto-m 6) utc-thursday-23-59))
(is (= (nth fixed-window-crypto-m 7) utc-thursday-23-58))
(is (= (nth fixed-window-crypto-m 8) utc-thursday-23-57))
(is (= (nth fixed-window-crypto-m 9) utc-thursday-23-56))
(is (not (= (nth fixed-window-crypto-m 5) utc-thursday-23-59-59-999)))))
)
)

0 comments on commit 7dc3fb8

Please sign in to comment.