diff --git a/src/pricebot/core.clj b/src/pricebot/core.clj index a029038..2950173 100644 --- a/src/pricebot/core.clj +++ b/src/pricebot/core.clj @@ -1,16 +1,15 @@ (ns pricebot.core (:require [clojure.core.async :as async :refer [go go-loop >! >!! date<]] - [fudo-clojure.result :as result :refer [map-success send-success dispatch-result success? unwrap]] + [fudo-clojure.common :refer [date<]] + [fudo-clojure.result :as result :refer [map-success send-success]] [fudo-clojure.logging :as log] [clojure.string :as str] [clojure.math.numeric-tower :refer [floor]] [clojure.tools.cli :refer [parse-opts]] + [clojure.pprint :refer [pprint]] [camel-snake-kebab.core :refer [->SCREAMING_SNAKE_CASE]]) (:import java.math.RoundingMode java.time.format.DateTimeFormatter @@ -33,7 +32,7 @@ :or {delay 60 buffer-size 10}}] (let [stop-chan (chan) - price-chan (chan 10) + price-chan (chan buffer-size) client (coinbase-pro/connect :hostname hostname) get-price #(client/get-market-price! client currency)] (reify @@ -52,15 +51,6 @@ IPriceChannel (get-price-channel [_] price-chan)))) -;; Bots can have a list of price checkers that take current price & price -;; history, and potentially return a message to send to the channel - -(defn- lpthru [label o] - (println (str "****** " label)) - (clojure.pprint/pprint o) - (println "******") - o) - (let [formatter (.withZone DateTimeFormatter/ISO_LOCAL_TIME (ZoneId/systemDefault))] (defn- format-instant [inst] (.format formatter inst))) @@ -84,7 +74,7 @@ (swap! mute-until (fn [_] (.plus (:instant curr) mute-duration)))))) (swap! prev (fn [_] curr))))) -(defn- minimize-time-gap [prices threshold target] +(defn- minimize-time-gap [prices target] (let [smaller-duration? (fn [a b] (> 0 (.compareTo a b))) get-gap (fn [m] (Duration/between target (:instant m)))] (when-let [initial-measure (first prices)] @@ -106,8 +96,8 @@ close-enough? (fn [target t] (date< (.minus target threshold) t (.plus target threshold)))] (fn [curr] (let [target-time (.minus (:instant curr) duration) - prev (minimize-time-gap @prices threshold target-time)] - (if prev + prev (minimize-time-gap @prices target-time)] + (when prev (let [diff-pct (.divide (- (:price curr) (:price prev)) (:price curr) RoundingMode/HALF_EVEN)] (if (close-enough? target-time (:instant prev)) (when (> (Math/abs diff-pct) percentage) @@ -141,7 +131,7 @@ (let [chan (get-price-channel @price-chan)] (go-loop [measure ( bebot-auth-token-file slurp str/trim-newline) - bebot-channel-id - notify-user) - checks (create-checks logger) - bot (reify-bot exchange-host (-> currency str/lower-case keyword)) - shutdown (chan)] - (.addShutdownHook (Runtime/getRuntime) - (Thread. (fn [] - (println "Stopping pricebot...") - (stop! bot) - (>!! shutdown true)))) - (log/notify! logger (str "Hi! I'm going to start watching the price of " (name currency) " for you.")) - (start! bot) - ( bebot-auth-token-file slurp str/trim-newline) + bebot-channel-id + notify-user) + checks (create-checks logger) + bot (reify-bot exchange-host + (-> currency str/lower-case keyword) + checks) + shutdown (chan)] + (.addShutdownHook (Runtime/getRuntime) + (Thread. (fn [] + (println "Stopping pricebot...") + (stop! bot) + (>!! shutdown true)))) + (log/notify! logger (str "Hi! I'm going to start watching the price of " (name currency) " for you.")) + (start! bot) + (