O'Reilly logo

Clojure for Finance by Timothy Washington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing our equation

So far, our moving-average function only gives us a partitioned list of prices (or ticks). Now, we only need to visit a sliced list at each increment and calculate each increment's price average. The reduce function provides a way of accumulating (or folding over) each item in a list and producing a result based on those values. We'll take our partitioned list and have reduce apply an accumulator function on each sublist. The form looks roughly like this:

(reduce (fn [accumulated-result-so-far each-item-list] 
          ;; ** single line comments come after semi-colons
          ;; do stuff, return accumulated result 
          ;; for each item iteration 
         )
    initial-list-of-accumulated-values
    initial-list-of-input-values)

Our accumulator function only has ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required