Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns watcher.core
- (:require [postal.core :as postal]
- [postal.sendmail :as local]
- [postal.smtp :as smtp]
- [clj-http.client :as client]
- [clojure.data.json :as json]
- [clj-time.local :as l]
- [watcher.polo :as polo])
- (:require [watcher.log :as log])
- (:gen-class))
- ;;;;;; constants ;;;;;;
- (def zoho (read-string (slurp "zoho.edn"))) ; mail config
- (def tr 0.000)
- (def trminus (* tr -1))
- (def sec 1000)
- (def minute (* 60 sec))
- (def waitp (* 3 minute))
- (def totalwait (* 3 60)) ; 3hours. totalwait * waitp
- (def intervalstep 30) ; every 30 minutes notify
- (def title "btcusd notify")
- (def titleshutdown "btcusd watch")
- (def titlestartup "startup btcusd watch")
- (def recp "benjamin.l.cordes@gmail.com")
- ;(def recp "happygeorge22@gmail.com")
- (def frommail "ben@metaexchange.io")
- (def btcusd-price (atom -1))
- (def btcusd-prev (atom -1))
- (def okprice (atom -1))
- (def delta (atom -1))
- (def maxd (atom -1))
- (def mind (atom 1))
- (def notfirstrun (atom false))
- (def polobids (atom []))
- (def poloasks (atom []))
- (def ptimestamp (atom nil))
- (def ptimestamp-prev (atom nil))
- (defn sendmailinfo
- "send signup via email"
- [info subj]
- (log/info "sending mail " info)
- (postal/send-message
- zoho
- {:from frommail
- :to [recp]
- :subject subj
- :body (str info)}))
- ;;;;;; feeds ;;;;;;
- (defn btce []
- (json/read-str (get (client/get "https://btc-e.com/api/3/ticker/btc_usd" {:query-params {}}) :body)))
- (defn okcoin []
- (json/read-str (get (client/get "https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd" {:query-params {}}) :body)))
- ;;;;;; utils ;;;;;;
- (defn parse-number
- "Reads a number from a string. Returns nil if not a number."
- [s]
- (if (re-find #"^-?\d+\.?\d*$" s)
- (read-string s)))
- (defn formatDouble [t]
- (format "%.3f" (double (* t 100))))
- (defn prettyob [orders]
- (def allstring (atom ""))
- (doseq [x orders]
- (let [p (parse-number (nth x 0))
- q (nth x 1)]
- (str p " #" q)
- ;(reset! allstring (str @allstring p " " (/ 1 p) " # " q " " "\n"))))
- (reset! allstring (str @allstring p " " (/ 1 p)))))
- @allstring)
- (defn roc [cur prev]
- "rate of change"
- (let [d (- cur prev)]
- (/ d cur)))
- (defn currentROC []
- "current rate of change"
- (roc @btcusd-price @btcusd-prev))
- (defn logcurrent []
- (def sttr (atom ""))
- (reset! sttr (str @sttr "delta: " (formatDouble (currentROC)) "%" " btc: " @btcusd-price
- " okcoin " @okprice
- " last: " @btcusd-prev "\n"));" timestamp: " @ptimestamp "\n"))
- (reset! sttr (str @sttr "bids " (prettyob @polobids) "\n"))
- (reset! sttr (str @sttr "asks " (prettyob @poloasks) "\n"))
- @sttr)
- (defn mailstr [t]
- (str (formatDouble t) " % in the last " (/ waitp 1000)
- " seconds. \n" "btcusd: " @btcusd-price "\nlast " @btcusd-prev)
- "\n" (l/local-now))
- (defn sendmailtresh []
- (sendmailinfo (logcurrent) title))
- ;(str (mailstr @delta)
- ; "\nbids " @polobids "\nasks: " @poloasks) title))
- ;;;;;; updates ;;;;;;
- (defn updateob []
- (let [ob (polo/orderbook)]
- ;(println ob)
- (reset! poloasks (take 1 (get ob "asks")))
- (reset! polobids (take 1 (get ob "bids")))))
- ;(log/info (prettyob @poloasks))
- ;(log/info (prettyob @polobids))
- ;(if @nfirst
- ; (sendmailinfo @sttr "watch")))
- (defn updates [new-price]
- ;(log/info "updates")
- (reset! delta (currentROC))
- (reset! btcusd-prev @btcusd-price)
- (reset! btcusd-price new-price)
- (if (> @delta @maxd)
- (reset! maxd @delta))
- (if (< @delta @mind)
- (reset! mind @delta))
- (updateob)
- (log/info (logcurrent))
- (if (not @notfirstrun)
- (reset! notfirstrun true)))
- (defn updateaction []
- ;lost more than tr
- ;(log/info "check delta " @delta)
- (if (and (< @delta trminus)) ; (> @delta -0.5) (< @delta 0.5))
- (sendmailtresh)
- (log/info "no significant change" (formatDouble @delta))))
- (defn update-timestamp [new-ts]
- (reset! ptimestamp-prev @ptimestamp)
- (reset! ptimestamp new-ts))
- (defn pricewatch []
- (let [v (btce)]
- (updates (get-in v ["btc_usd" "last"]))
- (update-timestamp (get-in v ["btc_usd" "updated"])))
- (let [curprice (get-in (okcoin) ["ticker" "last"])]
- (reset! okprice curprice))
- ;(log/info " okcoin " @okprice))
- (updateaction)
- )
- (defn send-interval [i]
- (log/info "interval function")
- (sendmailinfo (str "price: " @btcusd-price ". previous: " @btcusd-prev
- ". ROC: " (format "%.3f" (currentROC)) ". min ROC: " @mind) "btcusd interval"))
- (defn doseq-interval
- "a function to do things in a thread in steps. no threadpool"
- [f coll interval]
- (doseq [i coll]
- (f)
- ;(if (and (> i 0) (= (mod i intervalstep) 0))
- ; (send-interval i))
- (Thread/sleep interval)))
- ;;;;;; main thread ;;;;;;
- (defn startup []
- ;(sendmailinfo "startup btcusd watcher" titlestartup)
- (log/info "starting watcher app"))
- (defn shutdown []
- (log/info "shutdown watcher app")
- (sendmailinfo "shutdown" titleshutdown))
- (defn -main
- "main"
- [& args]
- (log/info "startup")
- (startup)
- (doseq-interval pricewatch (range totalwait) waitp)
- (shutdown))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement