From b8c07dd501636ae64bba69fb6ef83c0d7cc27708 Mon Sep 17 00:00:00 2001 From: bortexz Date: Tue, 13 Dec 2022 14:15:06 +0100 Subject: [PATCH] improved time complexity of sma --- CHANGELOG.md | 4 ++++ README.md | 4 ++-- build.clj | 2 +- src/bortexz/tacos.clj | 33 ++++++++++++++++++++++++++------- test/bortexz/tacos_test.clj | 2 +- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10abe26..632cf07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Change Log All notable changes to this project will be documented in this file. +## 0.1.0 - 2022-12-13 +### Changed +- `simple-moving-average` now works on logarithmic time, but it assumes only latest time points are added. For adding late arriving time points, the complete tail (from early time point to latest) needs to be recomputed. Most of the time only latest time points arrive. + ## 0.0.2 - 2022-09-30 ### Fix - docstring of `timeseries.keep-latest` to be consistent with code behaviour. diff --git a/README.md b/README.md index cd8bffd..3702180 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ Collection of timeseries technical analysis indicators as [graphcom](https://git ### Clojure CLI/deps.edn ```clojure -io.github.bortexz/tacos {:mvn/version "0.0.2"} +io.github.bortexz/tacos {:mvn/version "0.1.0"} ``` ### Leiningen/Boot ```clojure -[io.github.bortexz/tacos "0.0.2"] +[io.github.bortexz/tacos "0.1.0"] ``` ## Description diff --git a/build.clj b/build.clj index 1931de5..452c0fd 100644 --- a/build.clj +++ b/build.clj @@ -3,7 +3,7 @@ (:require [org.corfield.build :as bb])) (def lib 'io.github.bortexz/tacos) -(def version "0.0.2") +(def version "0.1.0") (defn- gha-output [k v] diff --git a/src/bortexz/tacos.clj b/src/bortexz/tacos.clj index cb2a8fb..e23a5e6 100644 --- a/src/bortexz/tacos.clj +++ b/src/bortexz/tacos.clj @@ -262,13 +262,32 @@ - `src` Opts: - - `period`" + - `period` + + Notes: + - Assumes that only latest time points are added (either new later timestamp, or replacement of current latest). + This should be the case most of the time. For earlier than latest arriving time points, you need to recompute all + from earlier time point to latest for correct results, by specifying them on the timeline. + + - Keeps an internal summatory to work on log-n (n=size of timeseries) instead of reducing tail all the time." [tl {:keys [src] :as sources} {:keys [period]}] - (derived - tl - sources - (fn simple-moving-average- [_ {:keys [src]} k] - (ts/moving-average src k period)))) + (let [acc (derived + tl + sources + (fn [x {:keys [src]} k] + (let [to-remove (ts/shift src k (- period) {:vf val}) + to-add (get src k) + prev-k (ts/shift src k -1 {:vf key}) + prev (when prev-k (get x prev-k)) + {:keys [sum]} (or prev {:sum 0 :ready? false})] + {:sum (when to-add (+ (or sum 0) to-add (if to-remove (- to-remove) 0))) + :ready? (some? to-remove)})))] + (derived + tl + {:acc acc} + (fn [_ {:keys [acc]} k] + (let [{:keys [sum ready?]} (get acc k)] + (when ready? (/ sum period))))))) (defn weighted-moving-average "Reference: @@ -1291,4 +1310,4 @@ :let [prev-kama (get kama (ts/shift src k -1 {:vf key}))]] (if prev-kama (+ prev-kama (* curr-sc (- curr-p prev-kama))) - (ts/moving-average src k er-period))))))) \ No newline at end of file + (ts/moving-average src k er-period))))))) diff --git a/test/bortexz/tacos_test.clj b/test/bortexz/tacos_test.clj index e239621..eba2a25 100644 --- a/test/bortexz/tacos_test.clj +++ b/test/bortexz/tacos_test.clj @@ -137,4 +137,4 @@ (comment ; manual test, with portal and Tradingview (tap> (update-vals indicators-vals (fn [i] (into (sorted-map) i)))) - (tap> (into (sorted-map) (::cci indicators-vals)))) + (tap> (into (sorted-map) (::ma indicators-vals))))