1
0

finance.lisp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (in-package #:chatikbot)
  2. (defparameter +yahoo-url+ "https://query.yahooapis.com/v1/public/yql" "Yahoo Finance API endpoint")
  3. (defparameter +yahoo-query+ "select Name,Rate from yahoo.finance.xchange where pair in (~{\"~A\"~^,~})")
  4. (defvar *rate-pairs* '("USDRUB" "EURRUB" "GBPRUB"))
  5. (defun get-rates (&optional (pairs *rate-pairs*))
  6. (let ((response (yason:parse
  7. (flexi-streams:octets-to-string
  8. (drakma:http-request +yahoo-url+
  9. :parameters (append '(("format" . "json")
  10. ("env" . "store://datatables.org/alltableswithkeys"))
  11. (list (cons "q" (format nil +yahoo-query+ pairs)))))
  12. :external-format :utf-8)
  13. :object-as :alist)))
  14. (when (aget "error" response)
  15. (error "Error in rates request"))
  16. (loop for rate in (aget "rate" (aget "results" (aget "query" response)))
  17. collect (cons (aget "Name" rate)
  18. (read-from-string (aget "Rate" rate))))))
  19. (defun get-serie (series name)
  20. (loop for (time . rates) in series
  21. when (aget name rates)
  22. collect (list time (aget name rates))))
  23. (defun make-chart (series &key (usd t) (eur t) (gbp t))
  24. (let ((flat (remove-if #'null (if (alexandria:circular-list-p series)
  25. (flat-circular series)
  26. series))))
  27. (adw-charting:with-line-chart (1200 900)
  28. (when usd (adw-charting:add-series "USD/RUB" (get-serie flat "USD/RUB")))
  29. (when eur (adw-charting:add-series "EUR/RUB" (get-serie flat "EUR/RUB")))
  30. (when gbp (adw-charting:add-series "GBP/RUB" (get-serie flat "GBP/RUB")))
  31. (adw-charting:set-axis
  32. :x "Time" :draw-gridlines-p t
  33. :label-formatter #'(lambda (v) (local-time:format-timestring nil (local-time:unix-to-timestamp v)
  34. :format '((:hour 2) ":" (:min 2)))))
  35. (adw-charting:set-axis :y "RUB" :draw-gridlines-p t :label-formatter "~,2F")
  36. (adw-charting:save-file "chart.png"))))