How To Compute On-Balance Volume (OBV) using Clojure?

by stevie_prohaska , in category: Technical Analysis , 9 months ago

How To Compute On-Balance Volume (OBV) using Clojure?

Facebook Twitter LinkedIn Whatsapp

2 answers

by bridgette , 8 months ago

@stevie_prohaska 

To compute On-Balance Volume (OBV) using Clojure, you can follow these steps:

  1. Create a function that takes a vector of price data as input.
  2. Initialize a variable to store the OBV value.
  3. Iterate through the price data vector, and for each price point: a. Check if the current price is higher than the previous price. If yes, add the volume to the OBV value. b. If the current price is lower than the previous price, subtract the volume from the OBV value.
  4. Return the final OBV value.


Here's an example implementation of the above steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
(defn calculate-obv [price-data]
  (loop [obv 0, prev-price 0, prices (rest price-data), volumes (rest volumes)]
    (if (empty? prices)
      obv
      (let [price (first prices)
            volume (first volumes)
            obv-change (if (> price prev-price) volume
                        (if (< price prev-price) (- volume) 0))]
        (recur (+ obv obv-change) price (rest prices) (rest volumes)))))

(def price-data [10 12 11 13 14])
(def volumes [1000 800 1200 1500 2000])
(def obv (calculate-obv price-data))
(println obv)


In this example, we define a function calculate-obv that takes the price data vector and volume data vector as input. We then iterate through the price data vector and calculate the OBV value by adding or subtracting the volume based on the price movement. Finally, we print the computed OBV value.


You can adjust the input data and further customize the implementation based on your specific requirements.

by darby_thiel , 6 months ago

@stevie_prohaska 

The explanation and code given above seems to contain a mistake in the function definition, as well as in the loop syntax. The corrected code to compute the On-Balance Volume (OBV) using Clojure is below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
(defn calculate-obv [price-data volumes]
  (loop [obv 0 prev-price 0 prices (rest price-data) volumes (rest volumes)]
    (if (empty? prices)
      obv
      (let [price (first prices)
            volume (first volumes)
            obv-change (if (> price prev-price) volume
                        (if (< price prev-price) (- volume) 0))]
        (recur (+ obv obv-change) price (rest prices) (rest volumes)))))

(def price-data [10 12 11 13 14])
(def volumes [1000 800 1200 1500 2000])
(def obv (calculate-obv price-data volumes))
(println obv)


In this corrected code, we define the function calculate-obv that takes the price data vector and volume data vector as input. We then iterate through the price data vector and calculate the OBV value by adding or subtracting the volume based on the price movement. Finally, we print the computed OBV value.


You can adjust the input data and further customize the implementation based on your specific requirements.