Skip to content

Commit

Permalink
calendar asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Jul 4, 2024
1 parent 8c3addc commit 899e73c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
48 changes: 48 additions & 0 deletions lib/calendar/resources/quanta/notebook/calendar_debug.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(ns quanta.notebook.calendar-debug
(:require
[taoensso.timbre :as timbre :refer [info warn error]]
[ta.calendar.core :refer [calendar-seq-instant]]))


(->> (calendar-seq-instant [:crypto :d])
(take 3))

(->> (calendar-seq-instant [:crypto :h])
(take 3))

(->> (calendar-seq-instant [:crypto :m15])
(take 3))

(->> (calendar-seq-instant [:crypto :m])
(take 3))


(defn next-dt [calendar]
(let [[market interval] calendar]
(let [dt (->> (calendar-seq-instant calendar)
first)]
[interval dt])))

(defn next-market [market]
(let [intervals [:d :h :m30 :m15 :m]
calendars (map (fn [int]
[market int]) intervals)]
(->> (map next-dt calendars)
(into {}))
;calendars
))

(next-dt [:crypto :d])
(next-dt [:crypto :h])
(next-dt [:crypto :m])
(next-dt [:crypto :m30])


(next-market :crypto)
;; => {:d #inst "2024-07-02T23:59:59.000000000-00:00",
;; :h #inst "2024-07-04T02:00:00.000000000-00:00",
;; :m30 #inst "2024-07-04T02:30:00.000000000-00:00",
;; :m15 #inst "2024-07-04T02:45:00.000000000-00:00",
;; :m #inst "2024-07-04T02:51:00.000000000-00:00"}

;; day should be one day later.
10 changes: 8 additions & 2 deletions lib/calendar/resources/quanta/notebook/calendar_live_time.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
(ns quanta.notebook.calendar-live-time
(:require
[taoensso.timbre :as timbre :refer [info warn error]]
[ta.calendar.core :refer [calendar-seq-instant]]
[manifold.stream :as s]
[ta.calendar.generator :as ct]))

(def s (ct/create-live-calendar-time-generator))

(ct/add-calendar s [:us :m])
(ct/add-calendar s [:crypto :m])
(ct/add-calendar s [:crypto :m15])
(ct/add-calendar s [:crypto :m30])
(ct/add-calendar s [:crypto :h])
(ct/add-calendar s [:forex :m])
(ct/add-calendar s [:eu :m])

(s/consume
(fn [msg]
(info "time event: " msg))
(warn "time event: " msg))
(ct/get-time-stream s))

(ct/remove-calendar s [:eu :m])

(ct/show-calendars s)
(ct/show-calendars s)


4 changes: 4 additions & 0 deletions lib/calendar/src/ta/calendar/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[taoensso.timbre :as timbre :refer [info warn error]]
[manifold.stream :as s]
[chime.core :as chime]
[ta.calendar.validate :refer [calendar-valid?]]
[ta.calendar.core :refer [calendar-seq-instant]]))

(defn create-live-calendar-time-generator
Expand All @@ -27,6 +28,9 @@
true)

(defn add-calendar [this calendar]
(assert (calendar-valid? calendar)
(str "cannot add calendar [ " calendar
"] to time-generator: not a valid calendar!"))
(if (get @(:calendars this) calendar)
(error "cannot add calendar: " calendar " - calendar already exists!")
(let [_ (warn "creating chimes for calendar: " calendar)
Expand Down
42 changes: 39 additions & 3 deletions lib/calendar/src/ta/calendar/validate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,33 @@
[ta.calendar.calendars :refer [calendar-exists?]]
[ta.calendar.interval :refer [interval-exists?]]))

(defn calendar-valid? [calendar]
(let [[calendar-kw interval-kw] calendar]
(cond
(not (vector? calendar))
false

(< (count calendar) 2) ; need at least two arguments in the vector
false

(not (keyword? calendar-kw)) ; calendar-kw needs to be a keyword
false

(not (keyword? interval-kw)) ; interval-kw needs to be a keyword
false

(not (calendar-exists? calendar-kw))
false

(not (interval-exists? interval-kw))
false

:else
true)))

(defn validate-calendar [[calendar-kw interval-kw]]
(assert (calendar-exists? calendar-kw) (str "unknown calendar: " calendar-kw))
(assert (interval-exists? interval-kw) (str "unknown interval: " interval-kw))
(assert (calendar-exists? calendar-kw) (str "calendar-market unknown: " calendar-kw))
(assert (interval-exists? interval-kw) (str "calendar-interval unknown : " interval-kw))
true)

(defn exchange [calendar]
Expand All @@ -18,10 +42,22 @@
(validate-calendar [:us :h])
(validate-calendar [:us :m])
(validate-calendar [:us :d])
(validate-calendar [:us :m37])

(validate-calendar [:us :m15])
(validate-calendar [:us :m30])
; unknown calendar
(validate-calendar [:us :m37])
(validate-calendar [:superlunar-exchange :h])

(calendar-valid? [:us :h])
(calendar-valid? [:us :m])
(calendar-valid? [:us :m :adsf])
(calendar-valid? [:us :m15 :adsf])
(calendar-valid? [:us :m155 :adsf])

; not valid:
(calendar-valid? [:us :m3 :adsf])

(exchange [:us :h])
(interval [:us :h])

Expand Down

0 comments on commit 899e73c

Please sign in to comment.