buy orders work.

This commit is contained in:
didier amyot committed 2018-05-31 13:58:55 -04:00
1 parent 1fa719ffe3
commit 3ed03675dd
2 files changed
+102 -51

No files matched your search

+40 -22
View File
@@ -17,19 +17,19 @@
;; sell-book contains all the sell orders. The second
;; called buy-book contains all the buy orders.
(defn create-book [& args]
(let [cmp #(< (:price %1) (:price %2))]
(defn create-sell-book [& args]
(let [cmp #(compare (:price %1) (:price %2))]
(apply sorted-list-by cmp args)))
(defn create-buy-book [& args]
(let [cmp #(compare (:price %2) (:price %1))]
(apply sorted-list-by cmp args)))
(defn sorted-set-rest [s]
(print "=>" s)
(if (empty? s)
s
(disj s (first s))))
(def book {:sell (create-book) :buy (create-book)})
(def book {:sell (create-sell-book) :buy (create-sell-book)})
(defn buy? [order]
@@ -43,23 +43,35 @@
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)})
(defn create-sell-trade-factory [order-to-fulfill]
(fn [trade book-order]
(let [already-fulfilled (:book.order/already-fulfilled trade 0)
qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) already-fulfilled))
can-fulfill? (<= (:price book-order) (:price order-to-fulfill))]
(when (and can-fulfill? (< 0 qtn-to-fulfill))
#:book.order{:type :sell
:quantity qtn-to-fulfill
:already-fulfilled (+ already-fulfilled qtn-to-fulfill)
:price (min (:price book-order) (:price order-to-fulfill))
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)}))))
(defn sell? [order]
(= (:type order) :sell))
(defn sell-order-fullfillable? [book-order order]
(>= (:price book-order) (:price order)))
(defn buy-order-fullfillable? [book-order order]
(<= (:price book-order) (:price order)))
(defn create-trade-factory [order-to-fulfill]
(let [trade-type (if (sell? order-to-fulfill) :buy :sell)
trade-price (if (sell? order-to-fulfill) max min)
fulfillable? (if (sell? order-to-fulfill) sell-order-fullfillable? buy-order-fullfillable?)]
(fn [trade book-order]
(let [already-fulfilled (:book.order/already-fulfilled trade 0)
qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) already-fulfilled))
can-fulfill? (fulfillable? book-order order-to-fulfill)]
(when (and can-fulfill? (< 0 qtn-to-fulfill))
#:book.order{:type trade-type
:quantity qtn-to-fulfill
:already-fulfilled (+ already-fulfilled qtn-to-fulfill)
:price (trade-price (:price book-order) (:price order-to-fulfill))
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)})))))
(defn create-sell-trades [order-to-fulfill sell-book]
(let [create-sell-trade (create-sell-trade-factory order-to-fulfill)
trades (reductions create-sell-trade {} sell-book)
(let [create-trade (create-trade-factory order-to-fulfill)
trades (reductions create-trade {} sell-book)
ret (rest (take-while some? trades))]
(map #(dissoc % :book.order/already-fulfilled) ret)))
@@ -92,6 +104,12 @@
new-buy-order (leftover-order order trades)]
[trades new-book new-buy-order]))
(defn match-sell-order-in-buy-book [buy-book order]
(let [trades (create-sell-trades order buy-book)
new-book (compute-new-sell-book-after-trades buy-book trades)
new-buy-order (leftover-order order trades)]
[trades new-book new-buy-order]))
(defn place-buy-order [book order]
(match-buy-order-in-sell-book (:sell book) order)