How To Calculate Average Directional Index (ADX) in Lisp?

Member

by coleman , in category: Technical Analysis , 2 months ago

How To Calculate Average Directional Index (ADX) in Lisp?

Facebook Twitter LinkedIn Whatsapp

1 answer

by sherman.carter , a month ago

@coleman 

To calculate Average Directional Index (ADX) in Lisp, you can use the following formula:


ADX = 100 * EMA((+DI - (-DI)) / (+DI + (-DI)), N)


where:

  • EMA is the Exponential Moving Average function
  • +DI is the Positive Directional Index
  • -DI is the Negative Directional Index
  • N is the period for calculating the ADX


Here is a sample Lisp code snippet to calculate the ADX for a given set of positive and negative directional indices:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
(defun calculate-emd (values n)
  (let ((alpha (/ 2 (+ n 1)))
       (ema (first values)))
    (dotimes (i (length values) ema)
      (setf ema (+ (* alpha (nth i values))
                   (* (- 1 alpha) ema)))))

(defun calculate-adx (+DI -DI n)
  (let* ((diff (/ (- +DI -DI) (+ +DI -DI)))
         (adx (calculate-ema (list diff) n)))
    (* 100 adx)))

(defun main ()
  (let* ((+DI 20)
         (-DI 15)
         (n 14)
         (adx (calculate-adx +DI -DI n)))
    (format t "ADX: ~a" adx)))

(main)


You can customize this code snippet to suit your specific requirements and input data.