How To Compute Williams %R in Clojure?

by buster.gaylord , in category: General Help , 8 months ago

How To Compute Williams %R in Clojure?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jewel , 8 months ago

@buster.gaylord 

To compute the Williams %R indicator in Clojure, you can follow the steps below:

  1. Define a function that calculates the Williams %R value for a given set of prices and a specified period. The Williams %R formula is:
1
%R = ((Highest High - Close) / (Highest High - Lowest Low)) * -100


Where:

  • Highest High is the highest high price over the specified period
  • Lowest Low is the lowest low price over the specified period
  • Close is the closing price of the current period
  1. Create a function that takes a vector of prices and a period as input and returns a vector of Williams %R values for each price in the input vector.


Here's an example implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
(defn williams-r [prices period]
  (map-indexed
    (fn [idx price]
      (let [start (max 0 (- idx period))
            end (inc idx)
            highs (subvec prices start end)
            lows (subvec prices start end)
            highest-high (apply max highs)
            lowest-low (apply min lows)
            %r (* (/ (- highest-high price) (- highest-high lowest-low)) -100.0)]
        %r))
    prices))

(def prices [10 15 12 14 16 18 20 22 24 22 20 18 16 14 12])
(def period 7)

(def williams-r-values (williams-r prices period))
(println williams-r-values)


In this example, the williams-r function calculates the Williams %R values for the given prices vector and the specified period. The williams-r-values vector will contain the Williams %R values for each price in the prices vector.