How To Calculate Stochastic Oscillator using Lisp?

by shirley.reilly , in category: Technical Analysis , 2 months ago

How To Calculate Stochastic Oscillator using Lisp?

Facebook Twitter LinkedIn Whatsapp

1 answer

Member

by morton , 2 months ago

@shirley.reilly 

Here is a sample code snippet in Lisp to calculate the Stochastic Oscillator for a given set of data points:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(defun calculate_stochastic_oscillator (data-points k-period d-period)
  (let* ((lowest-low (reduce #'min data-points))
         (highest-high (reduce #'max data-points))
         (%k-values '())
         (%d-values '()))
    
    (dotimes (i (- (length data-points) k-period))
      (let ((current-data (subseq data-points i (+ i k-period))))
        (let ((current-lowest (reduce #'min current-data))
              (current-highest (reduce #'max current-data))
              (%k-value (/ (- (car (last current-data)) current-lowest) (- current-highest current-lowest))))
          (push %k-value %k-values))))
          
    (dotimes (i (- (length %k-values) d-period))
      (let ((current-data (subseq %k-values i (+ i d-period))))
        (let ((%d-value (/ (reduce #'+ current-data) d-period)))
          (push %d-value %d-values))))
    
    (list %k-values %d-values)))

;; Example usage:
(setq data-points '(14 15 16 17 18 19 18 17 16 15))
(calculate_stochastic_oscillator data-points 5 3)


In this code snippet, we define a function calculate_stochastic_oscillator that takes in a list of data points, the K-period, and the D-period as input parameters. The function then calculates the %K and %D values based on the Stochastic Oscillator formula. The function returns a list of %K values and %D values.


You can call this function with your data points and specify the K-period and D-period to get the Stochastic Oscillator values.