Skip to content

Commit

Permalink
Merge pull request #43 from clojure-quant/clean_close_time
Browse files Browse the repository at this point in the history
changed bar close time behavior at midnight. now it has no longer to …
  • Loading branch information
wizard50 authored Oct 30, 2024
2 parents 92eff6a + 60643b4 commit 2cc98e2
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 221 deletions.
142 changes: 77 additions & 65 deletions lib/calendar/dev/src/dev/calendar_seq.clj
Original file line number Diff line number Diff line change
Expand Up @@ -109,39 +109,39 @@
(calendar-seq-prior [:crypto :d])
(take 10))
;=>
;(#time/zoned-date-time"2024-10-01T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-29T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-28T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-27T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-26T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-25T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-24T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-23T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-09-22T23:59:59.999999999Z[UTC]")
;(#time/zoned-date-time"2024-10-01T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-30T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-29T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-28T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-27T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-26T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-25T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-24T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-23T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-22T00:00Z[UTC]")


(-> (fixed-window [:crypto :h] {:start (t/instant "2024-01-01T01:00:00Z")
:end (t/instant "2024-01-01T23:59:59.999999999Z")})
:end (t/instant "2024-01-02T00:00:00Z")})
count)
; => 24

(-> (fixed-window [:crypto :h] {:start (t/instant "2024-01-01T01:00:00Z")
:end (t/instant "2024-01-01T23:00:00Z")})
:end (t/instant "2024-01-01T23:59:59.999999999Z")})
count)
; => 23


(-> (fixed-window [:crypto :d] {:start (t/instant "2024-01-01T23:59:59.999999999Z")
:end (t/instant "2024-01-07T23:59:59.999999999Z")}))
(-> (fixed-window [:crypto :d] {:start (t/instant "2024-01-01T00:00:00Z")
:end (t/instant "2024-01-07T00:00:00Z")}))
;=>
;(#time/zoned-date-time"2024-01-07T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-06T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-05T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-04T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-03T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-02T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-01T23:59:59.999999999Z[UTC]")
;(#time/zoned-date-time"2024-01-07T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-06T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-05T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-04T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-03T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-01T00:00Z[UTC]")

(-> (fixed-window-open [:crypto :d] {:start (t/instant "2024-09-01T00:00:00Z")
:end (t/instant "2024-09-08T00:00:00Z")}))
Expand All @@ -164,56 +164,68 @@
; #time/zoned-date-time"2024-01-03T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T00:00Z[UTC]")

(-> (fixed-window [:crypto :h] {:start (t/instant "2024-01-01T23:59:59.999999999Z")
:end (t/instant "2024-01-02T23:59:59.999999999Z")}))
;=>
;(#time/zoned-date-time"2024-01-02T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-01-02T23:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T22:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T21:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T20:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T19:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T18:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T17:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T16:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T15:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T14:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T13:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T12:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T11:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T10:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T09:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T08:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T07:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T06:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T05:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T04:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T03:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T02:00Z[UTC]"
; #time/zoned-date-time"2024-01-02T01:00Z[UTC]"
; #time/zoned-date-time"2024-01-01T23:59:59.999999999Z[UTC]")


(->> (calendar-seq-prior [:crypto :d] (t/instant "2024-01-02T23:59:59.999999999Z"))
(take 1))
; => (#time/zoned-date-time"2024-01-02T23:59:59.999999999Z[UTC]")
(->> (t/in (t/instant "2024-01-02T00:00:00Z") "UTC")
(calendar-seq-prior [:crypto :d])
(take 2))
;=> (#time/zoned-date-time"2024-01-02T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-01T00:00Z[UTC]")

(->> (t/in (t/date-time "2024-09-08T00:00:00") "UTC")
(->> (t/in (t/instant "2024-01-02T00:00:00Z") "UTC")
(calendar-seq-prior-open [:crypto :d])
(take 1))
;=> (#time/zoned-date-time"2024-09-08T00:00Z[UTC]")
(take 2))
;=> (#time/zoned-date-time"2024-01-02T00:00Z[UTC]"
; #time/zoned-date-time"2024-01-01T00:00Z[UTC]")

(->> (t/in (t/date-time "2024-01-02T23:59:59.999999999") "UTC")
(calendar-seq-prior-open [:crypto :d])
(take 1))
;=> (#time/zoned-date-time"2024-01-02T00:00Z[UTC]")

(fixed-window-open [:crypto :d] {:start (t/in (t/date-time "2024-02-05T23:59:59.999999999") "UTC")
:end (t/in (t/date-time "2024-02-11T23:59:59.999999999") "UTC")})
(->> (t/in (t/date-time "2024-10-01T00:05:00") "UTC")
(calendar-seq-prior [:crypto :m])
(take 10))
;=>
;(#time/zoned-date-time"2024-10-01T00:05Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:04Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:03Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:02Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:01Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:59Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:58Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:57Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:56Z[UTC]")

(-> (fixed-window [:crypto :m] {:start (t/instant "2024-09-30T23:56:00Z")
:end (t/instant "2024-10-01T00:05:00Z")}))
;=>
;(#time/zoned-date-time"2024-10-01T00:05Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:04Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:03Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:02Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:01Z[UTC]"
; #time/zoned-date-time"2024-10-01T00:00Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:59Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:58Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:57Z[UTC]"
; #time/zoned-date-time"2024-09-30T23:56Z[UTC]")

(fixed-window-open [:crypto :m] {:start (t/in (t/date-time "2024-02-05T23:55:00") "UTC")
:end (t/in (t/date-time "2024-02-05T23:59:00") "UTC")})
;=>
;(#time/zoned-date-time"2024-02-11T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-10T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-08T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-07T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-06T00:00Z[UTC]")
;(#time/zoned-date-time"2024-02-05T23:59Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:58Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:57Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:56Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:55Z[UTC]")

(->> (t/in (t/date-time "2024-02-05T23:59:00") "UTC")
(calendar-seq-prior-open [:crypto :m])
(take 5))
;=>
;(#time/zoned-date-time"2024-02-05T23:59Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:58Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:57Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:56Z[UTC]"
; #time/zoned-date-time"2024-02-05T23:55Z[UTC]")

40 changes: 36 additions & 4 deletions lib/calendar/dev/src/dev/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

(current-close [:crypto :m]
(t/in (t/date-time "2024-02-09T00:00:00") "UTC"))
;=> #time/zoned-date-time"2024-02-08T23:59:59.999999999Z[UTC]"
;=> #time/zoned-date-time"2024-02-09T00:00Z[UTC]"

(let [calendar (:us calendars)
interval (:m intervals)
Expand Down Expand Up @@ -89,13 +89,13 @@
interval (:d intervals)
current-close-dt (:current-close interval)]
(current-close-dt calendar (t/in (t/date-time "2024-02-08T23:59:59.999999999") "UTC")))
;=> #time/zoned-date-time"2024-02-08T23:59:59.999999999Z[UTC]"
;=> #time/zoned-date-time"2024-02-08T00:00Z[UTC]"

(let [calendar (:crypto calendars)
interval (:d intervals)
current-open-dt (:current-open interval)]
(current-open-dt calendar (t/in (t/date-time "2024-02-08T23:59:59.999999999") "UTC")))
; => #time/zoned-date-time"2024-02-08T00:00Z[UTC]"
;=> #time/zoned-date-time"2024-02-08T00:00Z[UTC]"

(let [t (t/instant "2024-02-08T23:59:59.999999999Z")]
(trading-open-time (:crypto calendars) t))
Expand All @@ -106,4 +106,36 @@
interval (:h4 intervals)
current-close-dt (:current-close interval)]
(current-close-dt calendar (t/in (t/date-time "2024-02-10T12:00:00") "UTC")))
; => #time/zoned-date-time"2024-02-10T12:00Z[UTC]"
; => #time/zoned-date-time"2024-02-10T12:00Z[UTC]"


(let [calendar (:crypto calendars)
interval (:m intervals)
current-open-dt (:current-open interval)]
(current-open-dt calendar (t/in (t/date-time "2024-02-08T23:59:00") "UTC")))
;=> #time/zoned-date-time"2024-02-08T23:59Z[UTC]"

(let [calendar (:crypto calendars)
interval (:d intervals)
current-open-dt (:current-open interval)]
(current-open-dt calendar (t/in (t/date-time "2024-02-08T12:00:00") "UTC")))
;=> #time/zoned-date-time"2024-02-08T00:00Z[UTC]"

(let [calendar (:crypto calendars)
interval (:d intervals)
next-close (:next-close interval)]
(next-close calendar (t/in (t/date-time "2024-02-08T00:00:00") "UTC")))
;=> #time/zoned-date-time"2024-02-09T00:00Z[UTC]"


(let [calendar (:crypto calendars)
interval (:d intervals)
next-close (:next-close interval)]
(next-close calendar (t/in (t/date-time "2024-02-07T23:59:59") "UTC")))
;=> #time/zoned-date-time"2024-02-08T00:00Z[UTC]"

(let [calendar (:crypto calendars)
interval (:m intervals)
next-close (:next-close interval)]
(next-close calendar (t/in (t/date-time "2024-02-07T23:59:59") "UTC")))
;=> #time/zoned-date-time"2024-02-08T00:00Z[UTC]"
82 changes: 51 additions & 31 deletions lib/calendar/dev/src/dev/open_close_conversion.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(ns dev.open-close-conversion
(:require
[ta.calendar.core :refer [close->open-dt open->close-dt]]
[quanta.calendar.core :refer [close->open-dt open->close-dt fixed-window fixed-window-open]]
[dev.utils :refer [to-utc]]
[tick.core :as t]
[ta.calendar.core :refer [fixed-window]]))
[tick.core :as t]))

;2024-02-08 => Thu
(close->open-dt [:us :m] (t/in (t/date-time "2024-02-09T12:34") "America/New_York"))
Expand All @@ -15,19 +14,24 @@
(close->open-dt [:us :d] (t/in (t/date-time "2024-02-08T17:00") "America/New_York"))
;=> #time/zoned-date-time"2024-02-08T09:00-05:00[America/New_York]"

(close->open-dt [:crypto :d] (t/in (t/date-time "2024-02-13T12:00:00") "UTC"))

; edge cases - day close crypto
(open->close-dt [:crypto :m] (to-utc "2024-02-08T23:58:00"))
;=> #time/zoned-date-time"2024-02-08T23:59Z[UTC]"

(open->close-dt [:crypto :m] (to-utc "2024-02-08T23:59:00"))
;=> #time/zoned-date-time"2024-02-08T23:59:59.999999999Z[UTC]"
;=> #time/zoned-date-time"2024-02-09T00:00Z[UTC]"

(open->close-dt [:crypto :m] (to-utc "2024-02-09T00:00:00"))
;=> #time/zoned-date-time"2024-02-09T00:01Z[UTC]"

(close->open-dt [:crypto :m] (to-utc "2024-02-08T23:59:59.999999999"))
(close->open-dt [:crypto :m] (to-utc "2024-02-09T00:00:00"))
;=> #time/zoned-date-time"2024-02-08T23:59Z[UTC]"

(open->close-dt [:crypto :d] (to-utc "2024-02-08T12:00:00"))
;=> #time/zoned-date-time"2024-02-09T00:00Z[UTC]"

;;
;; 16:25 - 16:30
(def open-dt-window-crypto-m__16-25 (fixed-window [:crypto :m] {:start (to-utc "2024-02-09T16:25:00")
Expand All @@ -51,11 +55,10 @@

;;
;; 00:00 - 00:05
(def open-dt-window-crypto-m__00-00 (-> (fixed-window [:crypto :m] {:start (to-utc "2024-02-09T00:00:00")
(def open-dt-window-crypto-m__00-00 (-> (fixed-window-open [:crypto :m] {:start (to-utc "2024-02-09T00:00:00")
:end (to-utc "2024-02-09T00:04:00")})
(concat [(to-utc "2024-02-09T00:00:00")])
(vec)))
; NOTE: we are generating the date-times with close iterator functions. 00:00 is no close time, this is why its missing and we have to add it explicit
(vec (concat open-dt-window-crypto-m__00-00))
;=>
;[#time/zoned-date-time"2024-02-09T00:04Z[UTC]"
Expand All @@ -74,35 +77,38 @@


;;
;; 23:55 - 23:59:59.999
(def open-dt-window-crypto-m__23-55 (fixed-window [:crypto :m] {:start (to-utc "2024-02-08T23:55:00")
:end (to-utc "2024-02-08T23:59:00")}))
(vec open-dt-window-crypto-m__23-55)
;; 23:55 - 00:05
(def open-dt-window-crypto-m__23-55-00-05 (fixed-window-open [:crypto :m] {:start (to-utc "2024-02-08T23:55:00")
:end (to-utc "2024-02-09T00:04:00")}))
(vec open-dt-window-crypto-m__23-55-00-05)
;=>
;[#time/zoned-date-time"2024-02-08T23:59Z[UTC]"
;[#time/zoned-date-time"2024-02-09T00:04Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:03Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:02Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:01Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:59Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:58Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:57Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:56Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:55Z[UTC]"]

(map #(open->close-dt [:crypto :m] %) open-dt-window-crypto-m__23-55)
(map #(open->close-dt [:crypto :m] %) open-dt-window-crypto-m__23-55-00-05)
;=>
;(#time/zoned-date-time"2024-02-08T23:59:59.999999999Z[UTC]"
;(#time/zoned-date-time"2024-02-09T00:05Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:04Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:03Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:02Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:01Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:59Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:58Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:57Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:56Z[UTC]")


;;
;; 23:55 - 00:05
; NOTE: we are generating the date-times with close iterator functions. 00:00 is no close time, this is why its missing and we have to add it explicit
(def open-dt-window-crypto-m__23-55-00-05 (concat (fixed-window [:crypto :m] {:start (to-utc "2024-02-09T00:00:00")
:end (to-utc "2024-02-09T00:04:00")})
[(to-utc "2024-02-09T00:00:00")]
(fixed-window [:crypto :m] {:start (to-utc "2024-02-08T23:55:00")
:end (to-utc "2024-02-08T23:59:00")})))
(vec open-dt-window-crypto-m__23-55-00-05)
(def close-dt-window-crypto-m__23-55-00-05 (fixed-window [:crypto :m] {:start (to-utc "2024-02-08T23:55:00")
:end (to-utc "2024-02-09T00:04:00")}))
(vec close-dt-window-crypto-m__23-55-00-05)
;=>
;[#time/zoned-date-time"2024-02-09T00:04Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:03Z[UTC]"
Expand All @@ -115,15 +121,29 @@
; #time/zoned-date-time"2024-02-08T23:56Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:55Z[UTC]"]

(map #(open->close-dt [:crypto :m] %) open-dt-window-crypto-m__23-55-00-05)
;=>
;(#time/zoned-date-time"2024-02-09T00:05Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:04Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:03Z[UTC]"
(map #(close->open-dt [:crypto :m] %) close-dt-window-crypto-m__23-55-00-05)
;(#time/zoned-date-time"2024-02-09T00:03Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:02Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:01Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:59:59.999999999Z[UTC]"
; #time/zoned-date-time"2024-02-09T00:00Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:59Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:58Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:57Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:56Z[UTC]")
; #time/zoned-date-time"2024-02-08T23:56Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:55Z[UTC]"
; #time/zoned-date-time"2024-02-08T23:54Z[UTC]")


(map #(open->close-dt [:crypto :m] %)
(fixed-window [:crypto :m] {:start (t/in (t/date-time "2024-02-08T23:55:00") "UTC")
:end (t/in (t/date-time "2024-02-08T23:59:00") "UTC")}))

(map #(open->close-dt [:crypto :m] %)
(fixed-window [:crypto :m] {:start (t/in (t/date-time "2024-02-08T23:56:00") "UTC")
:end (t/in (t/date-time "2024-02-09T00:05:00") "UTC")}))


(map #(open->close-dt [:crypto :m] %)
(fixed-window-open [:crypto :m] {:start (t/in (t/date-time "2024-02-05T23:55:00") "UTC")
:end (t/in (t/date-time "2024-02-05T23:59:00") "UTC")}))

Loading

0 comments on commit 2cc98e2

Please sign in to comment.