diff --git a/lib/calendar/src/ta/calendar/compress.clj b/lib/calendar/src/ta/calendar/compress.clj index 3e4e7f7c..2e4267c9 100644 --- a/lib/calendar/src/ta/calendar/compress.clj +++ b/lib/calendar/src/ta/calendar/compress.clj @@ -4,7 +4,8 @@ [tech.v3.datatype :as dtype] [tech.v3.datatype.functional :as dfn] [tablecloth.api :as tc] - [ta.helper.date-ds :as h])) + [ta.helper.date-ds :as h] + [ta.calendar.core :refer [current-close2]])) (def midnight (t/time "00:00:00")) @@ -59,6 +60,11 @@ (let [date-group-col (dtype/emap year-end-date :zoned-date-time (:date ds))] (tc/add-column ds :date-group date-group-col))) +(defn add-date-group-calendar [ds calendar] + (let [group-fn (partial current-close2 calendar) + date-group-col (dtype/emap group-fn :zoned-date-time (:date ds))] + (tc/add-column ds :date-group date-group-col))) + ;; COMPRESS (defn compress-ds [grouped-ds] @@ -87,11 +93,21 @@ (count)))}) (tc/rename-columns {:date-group :date}))) +(defn compress-to-calendar [ds calendar] + (-> ds + (add-date-group-calendar calendar) + compress-ds)) + (comment (year-month->date 2021 04) + ;; => #time/zoned-date-time "2021-04-30T00:00Z[UTC]" + (month-end-date (t/instant)) + ;; => #time/zoned-date-time "2024-07-31T00:00Z[UTC]" + (month-end-date (t/instant "2023-01-01T15:30:00Z")) + ;; => #time/zoned-date-time "2023-01-31T00:00Z[UTC]" (def ds (tc/dataset [{:date (t/instant "2021-01-01T15:30:00Z") :open 100 @@ -121,6 +137,63 @@ (add-date-group-month) (compress-ds)) + (def ds-intraday (tc/dataset [{:date (t/instant "2024-07-04T14:25:00Z") + :open 100 + :high 110 + :low 90 + :close 105 + :volume 100} + {:date (t/instant "2024-07-04T14:46:00Z") + :open 106 + :high 115 + :low 101 + :close 109 + :volume 100} + {:date (t/instant "2024-07-04T14:59:00Z") + :open 110 + :high 121 + :low 105 + :close 116 + :volume 100}])) + + (-> ds-intraday + (compress-to-calendar [:crypto :m15])) + ;; | :date | :open | :high | :low | :close | :volume | :count | + ;; |------------------------|------:|------:|-----:|-------:|--------:|-------:| + ;; | 2024-07-04T14:15Z[UTC] | 100 | 110 | 90 | 105 | 100 | 1 | + ;; | 2024-07-04T14:45Z[UTC] | 106 | 121 | 101 | 116 | 200 | 2 | + + (-> ds-intraday + (compress-to-calendar [:crypto :m30])) + ;; | :date | :open | :high | :low | :close | :volume | :count | + ;; |------------------------|------:|------:|-----:|-------:|--------:|-------:| + ;; | 2024-07-04T14:00Z[UTC] | 100 | 110 | 90 | 105 | 100 | 1 | + ;; | 2024-07-04T14:30Z[UTC] | 106 | 121 | 101 | 116 | 200 | 2 | + + (-> ds-intraday + (compress-to-calendar [:crypto :h])) + ;; => _unnamed [1 7]: + ;; + ;; | :date | :open | :high | :low | :close | :volume | :count | + ;; |------------------------|------:|------:|-----:|-------:|--------:|-------:| + ;; | 2024-07-04T14:00Z[UTC] | 100 | 121 | 90 | 116 | 300 | 3 | + + (-> ds-intraday + (compress-to-calendar [:crypto :d])) + ;; => _unnamed [1 7]: + ;; + ;; | :date | :open | :high | :low | :close | :volume | :count | + ;; |---------------------------|------:|------:|-----:|-------:|--------:|-------:| + ;; | 2024-07-03T23:59:59Z[UTC] | 100 | 121 | 90 | 116 | 300 | 3 | + + (-> ds-intraday + (compress-to-calendar [:crypto :W])) + ;; => _unnamed [1 7]: + ;; + ;; | :date | :open | :high | :low | :close | :volume | :count | + ;; |---------------------------|------:|------:|-----:|-------:|--------:|-------:| + ;; | 2024-06-30T23:59:59Z[UTC] | 100 | 121 | 90 | 116 | 300 | 3 | + ; ) diff --git a/lib/calendar/src/ta/calendar/core.clj b/lib/calendar/src/ta/calendar/core.clj index 516c8f19..f7aa5c6c 100644 --- a/lib/calendar/src/ta/calendar/core.clj +++ b/lib/calendar/src/ta/calendar/core.clj @@ -41,6 +41,19 @@ (current-close-dt calendar dt) (current-close-dt calendar (t/now))))) +(defn current-close2 [[calendar-kw interval-kw] dt] + ; 2 reasons for this variation: + ; 1. calendar argument vector (not two args) this is our new syntax + ; 2. no optional arg, so in compression nothign goes wrong. + ; current-close should be replaced by this version. + (let [calendar (calendar-kw calendars) + interval (interval-kw intervals) + _ (assert calendar) + _ (assert interval) + current-close-dt (:current-close interval) + _ (assert dt "current close dt is nil.")] + (current-close-dt calendar dt))) + (defn calendar-seq ; todo: [cal interval] instead of 2 parameter ([calendar-kw interval-kw] (let [cur-dt (current-close calendar-kw interval-kw)] diff --git a/lib/calendar/src/ta/calendar/interval/month.clj b/lib/calendar/src/ta/calendar/interval/month.clj index 1b5b7cb1..e1c1186b 100644 --- a/lib/calendar/src/ta/calendar/interval/month.clj +++ b/lib/calendar/src/ta/calendar/interval/month.clj @@ -66,7 +66,11 @@ (current-close (:us cal/calendars) (t/in (t/date-time "2024-03-04T12:00:00") "America/New_York")) (current-close (:us cal/calendars) (t/in (t/date-time "2024-02-04T12:00:00") "America/New_York")) + + (current-close (:us cal/calendars) (t/in (t/date-time "2024-01-31T16:59:59") "America/New_York")) (current-close (:us cal/calendars) (t/in (t/date-time "2024-01-31T17:00:00") "America/New_York")) + (current-close (:us cal/calendars) (t/in (t/date-time "2024-01-31T17:00:01") "America/New_York")) + (month-close (:us cal/calendars) (t/date "2024-03-04")) (t/first-day-of-month (t/date "2024-03-04"))