diff --git a/.gitignore b/.gitignore
index 39be296..37685b9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,4 @@ pom.xml.asc
.hgignore
.hg/
.idea/
+book.iml
diff --git a/book.iml b/book.iml
index 909f158..4da9514 100644
--- a/book.iml
+++ b/book.iml
@@ -15,7 +15,9 @@
-
+
+
+
\ No newline at end of file
diff --git a/src/book/core.clj b/src/book/core.clj
index af0d2a4..5043080 100644
--- a/src/book/core.clj
+++ b/src/book/core.clj
@@ -31,21 +31,23 @@
(defn create-sell-trade-factory [order-to-fulfill]
(fn [trade book-order]
- (let [qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) (:already-fulfilled trade 0)))
- can-fulfill? (< (:price book-order) (:price order-to-fulfill))]
- (when (and can-fulfill? (< qtn-to-fulfill 0))
+ (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 create-sell-trades [order-to-fulfill sell-book]
- (take-while nil?
- (reductions (create-sell-trade-factory order-to-fulfill) sell-book)))
-
-
+ (let [create-sell-trade (create-sell-trade-factory order-to-fulfill)
+ trades (reductions create-sell-trade {} sell-book)
+ ret (rest (take-while some? trades))]
+ (map #(dissoc % :book.order/already-fulfilled) ret)))
(defn partial-trade? [trade order]
(not= (:quantity order) (:book.order/quantity trade)))
@@ -65,7 +67,7 @@
(defn match-buy-order-in-sell-book [sell-book order]
- (let [trades (create-trade order (first sell-book))
+ (let [trades-1 [(create-trade order (first sell-book))]
trades (create-sell-trades order sell-book)
new-book (compute-new-sell-book-after-trades sell-book trades)]
[trades new-book]))
diff --git a/test/book/core_test.clj b/test/book/core_test.clj
index aad22d4..accc385 100644
--- a/test/book/core_test.clj
+++ b/test/book/core_test.clj
@@ -4,6 +4,7 @@
+
(deftest test-match-buy-order-in-sell-book
(testing "Simplest case."
(let [sell-book (create-book {:type :sell :quantity 10 :price 2 :book.order/id 101})
@@ -27,9 +28,9 @@
(is (= trades [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :sell}]))))
(testing "Simplest case IV."
- (let [sell-book (create-book {:type :sell :quantity 2 :price 12 :book.order/id 107} {:type :sell :quantity 8 :price 12 :book.order/id 108})
- order {:type :buy :quantity 10 :price 12 :book.order/id 109}
+ (let [sell-book (create-book {:type :sell :quantity 2 :price 12 :book.order/id 107} {:type :sell :quantity 8 :price 13 :book.order/id 108})
+ order {:type :buy :quantity 10 :price 13 :book.order/id 109}
[trades new-book] (match-buy-order-in-sell-book sell-book order)]
(is (= new-book []))
(is (= trades [#:book.order{:quantity 2 :price 12 :book-id 107 :order-id 109 :type :sell}
- #:book.order{:quantity 8 :price 12 :book-id 108 :order-id 109 :type :sell}])))))
+ #:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :sell}])))))