Last active
June 4, 2020 13:33
-
-
Save brulint/c329d255746a7659cb01ba0305c1d2f0 to your computer and use it in GitHub Desktop.
function to calculate moving average, bollinger bands and so on
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
(defun rolling-map (n func list &optional (sequence '()) (return '())) | |
(cond ((eq list '()) return) | |
(t (setq sequence (append sequence (list (car list)))) | |
(if (> (length sequence) n) (setq sequence (cdr sequence))) | |
(setq return (append return (list (apply func sequence)))) | |
(rolling-map n func (cdr list) sequence return)))) | |
(defun average (&rest list) (/ (apply '+ list) (length list))) | |
(rolling-map 3 'list '(1 1 2 3 5 8 13 21 34)) | |
((1) (1 1) (1 1 2) (1 2 3) (2 3 5) (3 5 8) (5 8 13) (8 13 21) (13 21 34)) | |
(rolling-map 3 'average '(1 1 2 3 5 8 13 21 34)) | |
(1 1 4/3 2 10/3 16/3 26/3 14 68/3) | |
; list sequence return | |
; (1 1 2 3 5 8 13 21 34) NIL NIL | |
; (1 2 3 5 8 13 21 34) (1) (1) | |
; (2 3 5 8 13 21 34) (1 1) (1 1) | |
; (3 5 8 13 21 34) (1 1 2) (1 1 4/3) | |
; (5 8 13 21 34) (1 2 3) (1 1 4/3 2) | |
; (8 13 21 34) (2 3 5) (1 1 4/3 2 10/3) | |
; (13 21 34) (3 5 8) (1 1 4/3 2 10/3 16/3) | |
; (21 34) (5 8 13) (1 1 4/3 2 10/3 16/3 26/3) | |
; (34) (8 13 21) (1 1 4/3 2 10/3 16/3 26/3 14) | |
; NIL (13 21 34) (1 1 4/3 2 10/3 16/3 26/3 14 68/3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment