Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. (ns watcher.core
  2. (:require [postal.core :as postal]
  3. [postal.sendmail :as local]
  4. [postal.smtp :as smtp]
  5. [clj-http.client :as client]
  6. [clojure.data.json :as json]
  7. [clj-time.local :as l]
  8. [watcher.polo :as polo])
  9. (:require [watcher.log :as log])
  10. (:gen-class))
  11.  
  12.  
  13. ;;;;;; constants ;;;;;;
  14.  
  15. (def zoho (read-string (slurp "zoho.edn"))) ; mail config
  16.  
  17. (def tr 0.000)
  18. (def trminus (* tr -1))
  19.  
  20. (def sec 1000)
  21. (def minute (* 60 sec))
  22. (def waitp (* 3 minute))
  23. (def totalwait (* 3 60)) ; 3hours. totalwait * waitp
  24. (def intervalstep 30) ; every 30 minutes notify
  25. (def title "btcusd notify")
  26. (def titleshutdown "btcusd watch")
  27. (def titlestartup "startup btcusd watch")
  28. (def recp "benjamin.l.cordes@gmail.com")
  29. ;(def recp "happygeorge22@gmail.com")
  30. (def frommail "ben@metaexchange.io")
  31.  
  32. (def btcusd-price (atom -1))
  33. (def btcusd-prev (atom -1))
  34. (def okprice (atom -1))
  35. (def delta (atom -1))
  36. (def maxd (atom -1))
  37. (def mind (atom 1))
  38. (def notfirstrun (atom false))
  39. (def polobids (atom []))
  40. (def poloasks (atom []))
  41.  
  42. (def ptimestamp (atom nil))
  43. (def ptimestamp-prev (atom nil))
  44.  
  45. (defn sendmailinfo
  46. "send signup via email"
  47. [info subj]
  48. (log/info "sending mail " info)
  49. (postal/send-message
  50. zoho
  51. {:from frommail
  52. :to [recp]
  53. :subject subj
  54. :body (str info)}))
  55.  
  56.  
  57. ;;;;;; feeds ;;;;;;
  58.  
  59. (defn btce []
  60. (json/read-str (get (client/get "https://btc-e.com/api/3/ticker/btc_usd" {:query-params {}}) :body)))
  61.  
  62. (defn okcoin []
  63. (json/read-str (get (client/get "https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd" {:query-params {}}) :body)))
  64.  
  65.  
  66. ;;;;;; utils ;;;;;;
  67.  
  68. (defn parse-number
  69. "Reads a number from a string. Returns nil if not a number."
  70. [s]
  71. (if (re-find #"^-?\d+\.?\d*$" s)
  72. (read-string s)))
  73.  
  74.  
  75. (defn formatDouble [t]
  76. (format "%.3f" (double (* t 100))))
  77.  
  78.  
  79. (defn prettyob [orders]
  80. (def allstring (atom ""))
  81. (doseq [x orders]
  82. (let [p (parse-number (nth x 0))
  83. q (nth x 1)]
  84. (str p " #" q)
  85. ;(reset! allstring (str @allstring p " " (/ 1 p) " # " q " " "\n"))))
  86. (reset! allstring (str @allstring p " " (/ 1 p)))))
  87. @allstring)
  88.  
  89. (defn roc [cur prev]
  90. "rate of change"
  91. (let [d (- cur prev)]
  92. (/ d cur)))
  93.  
  94. (defn currentROC []
  95. "current rate of change"
  96. (roc @btcusd-price @btcusd-prev))
  97.  
  98. (defn logcurrent []
  99. (def sttr (atom ""))
  100. (reset! sttr (str @sttr "delta: " (formatDouble (currentROC)) "%" " btc: " @btcusd-price
  101. " okcoin " @okprice
  102. " last: " @btcusd-prev "\n"));" timestamp: " @ptimestamp "\n"))
  103.  
  104. (reset! sttr (str @sttr "bids " (prettyob @polobids) "\n"))
  105. (reset! sttr (str @sttr "asks " (prettyob @poloasks) "\n"))
  106. @sttr)
  107.  
  108. (defn mailstr [t]
  109. (str (formatDouble t) " % in the last " (/ waitp 1000)
  110. " seconds. \n" "btcusd: " @btcusd-price "\nlast " @btcusd-prev)
  111. "\n" (l/local-now))
  112.  
  113. (defn sendmailtresh []
  114. (sendmailinfo (logcurrent) title))
  115.  
  116. ;(str (mailstr @delta)
  117. ; "\nbids " @polobids "\nasks: " @poloasks) title))
  118.  
  119.  
  120. ;;;;;; updates ;;;;;;
  121.  
  122. (defn updateob []
  123. (let [ob (polo/orderbook)]
  124. ;(println ob)
  125. (reset! poloasks (take 1 (get ob "asks")))
  126. (reset! polobids (take 1 (get ob "bids")))))
  127.  
  128.  
  129. ;(log/info (prettyob @poloasks))
  130. ;(log/info (prettyob @polobids))
  131.  
  132. ;(if @nfirst
  133. ; (sendmailinfo @sttr "watch")))
  134.  
  135.  
  136. (defn updates [new-price]
  137. ;(log/info "updates")
  138. (reset! delta (currentROC))
  139. (reset! btcusd-prev @btcusd-price)
  140. (reset! btcusd-price new-price)
  141.  
  142. (if (> @delta @maxd)
  143. (reset! maxd @delta))
  144.  
  145. (if (< @delta @mind)
  146. (reset! mind @delta))
  147.  
  148. (updateob)
  149.  
  150. (log/info (logcurrent))
  151.  
  152. (if (not @notfirstrun)
  153. (reset! notfirstrun true)))
  154.  
  155. (defn updateaction []
  156. ;lost more than tr
  157. ;(log/info "check delta " @delta)
  158. (if (and (< @delta trminus)) ; (> @delta -0.5) (< @delta 0.5))
  159. (sendmailtresh)
  160. (log/info "no significant change" (formatDouble @delta))))
  161.  
  162. (defn update-timestamp [new-ts]
  163. (reset! ptimestamp-prev @ptimestamp)
  164. (reset! ptimestamp new-ts))
  165.  
  166. (defn pricewatch []
  167. (let [v (btce)]
  168. (updates (get-in v ["btc_usd" "last"]))
  169. (update-timestamp (get-in v ["btc_usd" "updated"])))
  170.  
  171. (let [curprice (get-in (okcoin) ["ticker" "last"])]
  172. (reset! okprice curprice))
  173. ;(log/info " okcoin " @okprice))
  174.  
  175. (updateaction)
  176. )
  177.  
  178. (defn send-interval [i]
  179. (log/info "interval function")
  180. (sendmailinfo (str "price: " @btcusd-price ". previous: " @btcusd-prev
  181. ". ROC: " (format "%.3f" (currentROC)) ". min ROC: " @mind) "btcusd interval"))
  182.  
  183. (defn doseq-interval
  184. "a function to do things in a thread in steps. no threadpool"
  185. [f coll interval]
  186. (doseq [i coll]
  187. (f)
  188. ;(if (and (> i 0) (= (mod i intervalstep) 0))
  189. ; (send-interval i))
  190. (Thread/sleep interval)))
  191.  
  192. ;;;;;; main thread ;;;;;;
  193.  
  194. (defn startup []
  195. ;(sendmailinfo "startup btcusd watcher" titlestartup)
  196. (log/info "starting watcher app"))
  197.  
  198. (defn shutdown []
  199. (log/info "shutdown watcher app")
  200. (sendmailinfo "shutdown" titleshutdown))
  201.  
  202. (defn -main
  203. "main"
  204. [& args]
  205. (log/info "startup")
  206. (startup)
  207. (doseq-interval pricewatch (range totalwait) waitp)
  208. (shutdown))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement