Last active
August 17, 2024 07:16
-
-
Save brulint/7587988063b263ce2aa5e3fee1492fe6 to your computer and use it in GitHub Desktop.
Relative Strength Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; use rolling-map and average (https://gist.github.com/brulint/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