Skip to content

Commit

Permalink
improved time complexity of sma
Browse files Browse the repository at this point in the history
  • Loading branch information
bortexz committed Dec 13, 2022
1 parent 61825ea commit b8c07dd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
33 changes: 26 additions & 7 deletions src/bortexz/tacos.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)))))))
(ts/moving-average src k er-period)))))))
2 changes: 1 addition & 1 deletion test/bortexz/tacos_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

0 comments on commit b8c07dd

Please sign in to comment.