Bläddra i källkod

[finance] hold buy

Innokentiy Enikeev 8 år sedan
förälder
incheckning
1ae3a285c5
1 ändrade filer med 35 tillägg och 7 borttagningar
  1. 35 7
      plugins/finance.lisp

+ 35 - 7
plugins/finance.lisp

@@ -135,9 +135,9 @@
                            symbol)
      when last append last))
 
-(defun db/get-finance-at (symbol ts)
-  (db-single "select price from finance_ticker where symbol = ? and ts <= ? order by ts desc limit 1"
-             symbol ts))
+(defun db/get-finance-at (symbol ts &optional (window +DAY+))
+  (db-single "select price from finance_ticker where symbol = ? and ts between ? and ? order by ts desc limit 1"
+             symbol (- ts window) ts))
 
 (defun db/get-chat-symbols (&optional chat-id)
   (mapcar #'car
@@ -246,12 +246,40 @@
           (get-symbol-label (agets info :symbol))
           (format-ts (local-time:unix-to-timestamp (agets info :open-at)))
           (agets info :open)
-	  (if (> (agets info :income) 0) "📉 " "📉")
+	  (if (> (or (agets info :income) 0) 0) "📈" "📉")
           (if (agets info :open-p) "now"
 	      (format-ts (local-time:unix-to-timestamp (agets info :close-at))))
-          (agets info :close)
-          (agets info :income)
-          (* 100 (agets info :roi))))
+          (or (agets info :close) 0)
+          (or (agets info :income) 0)
+          (* 100 (or (agets info :roi) 0))))
+
+(defun handle-hodl-show (chat-id)
+  (let ((orders (db/orders-get chat-id)))
+    (bot-send-message chat-id
+		      (if orders (format nil "~{~A~%~} 😇"
+					 (loop for order in orders for idx from 1
+					    collect (print-order-info idx (get-order-info order))))
+			  "Нет ходлеров :(")
+		                            :parse-mode "markdown")))
+
+(defun handle-hodl-buy (chat-id from args)
+  (handler-case
+      (let ((symbol (get-label-symbol (nth 1 args)))
+	    (amount (parse-float (nth 2 args)))
+	    (open (parse-float (nth 3 args)))
+	    (open-at (if (nth 4 args) (local-time:parse-timestring (nth 4 args)) (local-time:now))))
+	(db/order-buy chat-id (agets from "id") (agets from "username")
+		      symbol (princ-to-string amount) (princ-to-string open)
+		      (local-time:timestamp-to-unix open-at))
+	(handle-hodl-show chat-id))
+    (error (e)
+      (log:error "~A" e)
+      (bot-send-message chat-id "/hodl buy <SYM> <AMT> <PRICE> [YYYY-MM-DD]"))))
+
+(def-message-cmd-handler handle-hodl (:hodl :hodlers)
+  (cond
+    ((equal (car args) "buy") (handle-hodl-buy chat-id (agets message "from") args))
+    (:otherwise (handle-hodl-show chat-id))))
 
 (def-message-cmd-handler handle-hodl (:hodl :hodlers)
   (let ((orders (db/orders-get chat-id)))