Skip to content

Instantly share code, notes, and snippets.

@carboleum
Created June 9, 2020 08:26
Show Gist options
  • Save carboleum/7587988063b263ce2aa5e3fee1492fe6 to your computer and use it in GitHub Desktop.
Save carboleum/7587988063b263ce2aa5e3fee1492fe6 to your computer and use it in GitHub Desktop.
Relative Strength Index
;; use rolling-map and average (https://gist.github.com/carboleum/c329d255746a7659cb01ba0305c1d2f0)
(defun rsi (n prices)
(let ((gain (let ((cumul 0))
(mapcar #'(lambda (x) (setq cumul (/ (+ (* cumul (1- n)) x) n)))
(cdr (rolling-map 2 #'(lambda (&rest list)
(if (apply #'< list) (abs (apply #'- list)) 0))
prices)))))
(loss (let ((cumul 0))
(mapcar #'(lambda (x) (setq cumul (/ (+ (* cumul (1- n)) x) n)))
(cdr (rolling-map 2 #'(lambda (&rest list)
(if (apply #'> list) (abs (apply #'- list)) 0))
prices))))))
(mapcar #'(lambda (u d) (- 100 (/ 100 (1+ (/ u d)))))
(append (list (apply #'average (subseq gain 0 n))) (subseq gain n))
(append (list (apply #'average (subseq loss 0 n))) (subseq loss n)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment