some large-ish refactoring

This commit is contained in:
didier amyot committed 2018-07-08 15:41:37 -04:00
1 parent 7fbb3484a1
commit 7ed0ff9071
10 files changed
+142 -140

No files matched your search

+12 -1
View File
@@ -154,7 +154,7 @@ services:
skirmish-api:
image: skirmish
command: ["lein", "run", "webserver"]
command: ["lein", "run", "api-server"]
depends_on:
- kafka-topics-ui # basically, we need all kafka
environment:
@@ -162,3 +162,14 @@ services:
KAFKA_BOOTSTRAP_SERVERS: "kafka1:9092"
ports:
- "1602:1602"
skirmish-exchange-core:
image: skirmish
command: ["lein", "run", "exchange-core"]
depends_on:
- skirmish-api # basically, we need all kafka
environment:
PORT: 1602 # https://en.wikipedia.org/wiki/Euronext_Amsterdam
KAFKA_BOOTSTRAP_SERVERS: "kafka1:9092"
ports:
- "1602:1602"
+2 -2
View File
@@ -1,7 +1,7 @@
(ns skirmish.cli.exchange
(:require [skirmish.match :refer [sell?]]
(:require [skirmish.exchange.match :refer [sell?]]
[skirmish.util :refer :all]
[skirmish.exchange :refer [empty-book create-exchange-for-security]]
[skirmish.exchange.exchange :refer [empty-book create-exchange-for-security]]
[skirmish.order] ;; for spec side effect
[slingshot.slingshot :refer [try+ throw+]]
[clojure.edn :as edn]
+4 -4
View File
@@ -2,13 +2,13 @@
(:require [skirmish.cli.core]
[skirmish.rest.core]
[skirmish.cli.benchmark]
[skirmish.kafka.exchange-simulator]
[aero.core :refer (read-config)])
(:gen-class))
(def apps {:webserver skirmish.rest.core/-main
:benchmark skirmish.cli.benchmark/-main
:kafka-exchange-simulator skirmish.kafka.exchange-simulator/-main})
(def apps {:api-server skirmish.rest.core/api-main
:exchange-core skirmish.rest.core/api-main ;; should be skirmish.exchange.core/api-main
:benchmark skirmish.cli.benchmark/-main})
(defn -main [& argv]
(let [config (-> "config.edn" clojure.java.io/resource read-config)
@@ -1,5 +1,5 @@
(ns skirmish.exchange
(:require [skirmish.match :refer [sell?
(ns skirmish.exchange.exchange
(:require [skirmish.exchange.match :refer [sell?
create-buy-book
create-sell-book
match-order-in-book]]
@@ -1,4 +1,4 @@
(ns skirmish.match
(ns skirmish.exchange.match
(:require [skirmish.util :refer [sorted-list-by]]
[clojure.spec.alpha :as s]
[clojure.edn :as edn]))
+1 -1
View File
@@ -5,7 +5,7 @@
[skirmish.rest.exchange :refer [create-exchanges-api-for
create-routes]]))
(defn -main [config]
(defn api-main [config]
(let [webserver-config (:webserver config)
exchange-apis (create-exchanges-api-for (:securities config))
kafka-producer (kinsky.client/producer {:bootstrap.servers (get-in config [:kafka :bootstrap.servers])}
+1 -2
View File
@@ -1,11 +1,10 @@
(ns skirmish.rest.exchange
(:require [skirmish.exchange :refer [empty-book]]
(:require [skirmish.exchange.exchange :refer [empty-book create-exchange-for-security]]
[org.httpkit.server :refer [run-server with-channel send! close on-close]]
[compojure.route :refer [not-found]]
[compojure.handler :refer [api]]
[compojure.core :refer :all]
[skirmish.order] ;; for spec side effect
[skirmish.exchange :refer [create-exchange-for-security]]
[skirmish.rest.util :refer [build-response build-response-body]]
[clojure.spec.alpha :as s]
[kinsky.client]
@@ -1,8 +1,8 @@
(ns skirmish.exchange-test
(ns skirmish.exchange.exchange-test
(:import [skirmish.PersistentSortedList])
(:require [midje.sweet :refer :all]
[skirmish.exchange :refer :all]
[skirmish.match :refer [create-buy-book
[skirmish.exchange.exchange :refer :all]
[skirmish.exchange.match :refer [create-buy-book
create-sell-book
match-order-in-book]]))
+116
View File
@@ -0,0 +1,116 @@
(ns skirmish.exchange.match-test
(:require [midje.sweet :refer :all]
[skirmish.exchange.match :refer :all]))
(def PersistentSortedList? (comp #(= % skirmish.PersistentSortedList) type))
(facts "matching buy order in sell book"
(fact "Simplest case I."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 101})
order {:type :buy :quantity 10 :price 2 :book.order/id 102}
[trades new-book] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 10 :price 2 :book-id 101 :order-id 102 :type :sell}]))
(fact "Simplest case II."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 103})
order {:type :buy :quantity 10 :price 3 :book.order/id 104}
[trades new-book] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 10 :price 2 :book-id 103 :order-id 104 :type :sell}]))
(fact "Simplest case III."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 105})
order {:type :buy :quantity 8 :price 2 :book.order/id 106}
[trades new-book] (match-order-in-book sell-book order)]
new-book => [{:type :sell :quantity 2 :price 2 :book.order/id 105}]
trades => [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :sell}]))
(fact "Simplest case IV."
(let [sell-book (create-sell-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-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 2 :price 12 :book-id 107 :order-id 109 :type :sell}
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :sell}]))
(fact "Simplest case V."
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 113} {:type :sell :quantity 8 :price 13 :book.order/id 114})
order {:type :buy :quantity 9 :price 13 :book.order/id 115}
[trades new-book] (match-order-in-book sell-book order)]
new-book => (create-sell-book {:type :sell :quantity 1 :price 13 :book.order/id 114})
trades => [#:book.order{:quantity 2 :price 12 :book-id 113 :order-id 115 :type :sell}
#:book.order{:quantity 7 :price 13 :book-id 114 :order-id 115 :type :sell}]))
(fact "Simplest case VI."
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 116} {:type :sell :quantity 8 :price 13 :book.order/id 117})
order {:type :buy :quantity 21 :price 13 :book.order/id 118}
[trades new-book leftover-order] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 2 :price 12 :book-id 116 :order-id 118 :type :sell}
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :sell}]
leftover-order => {:type :buy :quantity 11 :price 13 :book.order/id 118}))
(fact "Simplest case VII."
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 116} {:type :sell :quantity 8 :price 13 :book.order/id 117})
order {:type :buy :quantity 21 :price 10 :book.order/id 118}
[trades new-book leftover-order] (match-order-in-book sell-book order)]
new-book => sell-book
trades => empty?
leftover-order => order)))
(facts "matching sell order in buy book"
(fact "Simplest case I."
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 2 :book.order/id 101})
order {:type :sell :quantity 10 :price 2 :book.order/id 102}
[trades new-book] (match-order-in-book buy-book order)]
new-book => empty?
trades => [#:book.order{:quantity 10 :price 2 :book-id 101 :order-id 102 :type :buy}]))
(fact "Simplest case II."
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 4 :book.order/id 103})
order {:type :sell :quantity 10 :price 3 :book.order/id 104}
[trades new-book] (match-order-in-book buy-book order)]
new-book => []
trades => [#:book.order{:quantity 10 :price 4 :book-id 103 :order-id 104 :type :buy}]))
(fact "Simplest case III."
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 2 :book.order/id 105})
order {:type :sell :quantity 8 :price 2 :book.order/id 106}
[trades new-book] (match-order-in-book buy-book order)]
new-book => [{:type :buy :quantity 2 :price 2 :book.order/id 105}]
trades => [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :buy}]))
(fact "Simplest case IV."
(let [buy-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 107}
{:type :buy :quantity 8 :price 13 :book.order/id 108})
order {:type :sell :quantity 10 :price 13 :book.order/id 109}
[trades new-book] (match-order-in-book buy-book order)]
new-book => []
trades => [#:book.order{:quantity 2 :price 14 :book-id 107 :order-id 109 :type :buy}
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :buy}]))
(fact "Simplest case V."
(let [buy-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 113}
{:type :buy :quantity 8 :price 13 :book.order/id 114})
order {:type :sell :quantity 9 :price 13 :book.order/id 115}
[trades new-book] (match-order-in-book buy-book order)]
new-book => (create-buy-book {:type :buy :quantity 1 :price 13 :book.order/id 114})
trades => [#:book.order{:quantity 2 :price 14 :book-id 113 :order-id 115 :type :buy}
#:book.order{:quantity 7 :price 13 :book-id 114 :order-id 115 :type :buy}]))
(fact "Simplest case VI."
(let [sell-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 116}
{:type :buy :quantity 8 :price 13 :book.order/id 117})
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
[trades new-book leftover-order] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 2 :price 14 :book-id 116 :order-id 118 :type :buy}
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :buy}]
leftover-order => {:type :sell :quantity 11 :price 13 :book.order/id 118})))
(facts "We should write tests for each bug found"
(fact "book type should always be book.PersistentSortedList"
(let [book (create-buy-book)
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
[_ new-book _] (match-order-in-book book order)]
(count new-book) => 0
(type new-book) => skirmish.PersistentSortedList)))
-124
View File
@@ -1,124 +0,0 @@
(ns skirmish.match-test
(:require [midje.sweet :refer :all]
[skirmish.match :refer :all]))
(def PersistentSortedList? (comp #(= % skirmish.PersistentSortedList) type))
(facts "matching buy order in sell book"
(fact "Simplest case I."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 101})
order {:type :buy :quantity 10 :price 2 :book.order/id 102}
[trades new-book] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 10 :price 2 :book-id 101 :order-id 102 :type :sell}]))
(fact "Simplest case II."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 103})
order {:type :buy :quantity 10 :price 3 :book.order/id 104}
[trades new-book] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 10 :price 2 :book-id 103 :order-id 104 :type :sell}]))
(fact "Simplest case III."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 105})
order {:type :buy :quantity 8 :price 2 :book.order/id 106}
[trades new-book] (match-order-in-book sell-book order)]
new-book => [{:type :sell :quantity 2 :price 2 :book.order/id 105}]
trades => [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :sell}]))
(fact "Simplest case IV."
(let [sell-book (create-sell-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-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 2 :price 12 :book-id 107 :order-id 109 :type :sell}
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :sell}]))
(fact "Simplest case V."
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 113} {:type :sell :quantity 8 :price 13 :book.order/id 114})
order {:type :buy :quantity 9 :price 13 :book.order/id 115}
[trades new-book] (match-order-in-book sell-book order)]
new-book => (create-sell-book {:type :sell :quantity 1 :price 13 :book.order/id 114})
trades => [#:book.order{:quantity 2 :price 12 :book-id 113 :order-id 115 :type :sell}
#:book.order{:quantity 7 :price 13 :book-id 114 :order-id 115 :type :sell}]))
(fact "Simplest case VI."
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 116} {:type :sell :quantity 8 :price 13 :book.order/id 117})
order {:type :buy :quantity 21 :price 13 :book.order/id 118}
[trades new-book leftover-order] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 2 :price 12 :book-id 116 :order-id 118 :type :sell}
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :sell}]
leftover-order => {:type :buy :quantity 11 :price 13 :book.order/id 118}))
(fact "Simplest case VII."
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 116} {:type :sell :quantity 8 :price 13 :book.order/id 117})
order {:type :buy :quantity 21 :price 10 :book.order/id 118}
[trades new-book leftover-order] (match-order-in-book sell-book order)]
new-book => sell-book
trades => empty?
leftover-order => order)))
(facts "matching sell order in buy book"
(fact "Simplest case I."
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 2 :book.order/id 101})
order {:type :sell :quantity 10 :price 2 :book.order/id 102}
[trades new-book] (match-order-in-book buy-book order)]
new-book => empty?
trades => [#:book.order{:quantity 10 :price 2 :book-id 101 :order-id 102 :type :buy}]))
(fact "Simplest case II."
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 4 :book.order/id 103})
order {:type :sell :quantity 10 :price 3 :book.order/id 104}
[trades new-book] (match-order-in-book buy-book order)]
new-book => []
trades => [#:book.order{:quantity 10 :price 4 :book-id 103 :order-id 104 :type :buy}]))
(fact "Simplest case III."
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 2 :book.order/id 105})
order {:type :sell :quantity 8 :price 2 :book.order/id 106}
[trades new-book] (match-order-in-book buy-book order)]
new-book => [{:type :buy :quantity 2 :price 2 :book.order/id 105}]
trades => [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :buy}]))
(fact "Simplest case IV."
(let [buy-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 107}
{:type :buy :quantity 8 :price 13 :book.order/id 108})
order {:type :sell :quantity 10 :price 13 :book.order/id 109}
[trades new-book] (match-order-in-book buy-book order)]
new-book => []
trades => [#:book.order{:quantity 2 :price 14 :book-id 107 :order-id 109 :type :buy}
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :buy}]))
(fact "Simplest case V."
(let [buy-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 113}
{:type :buy :quantity 8 :price 13 :book.order/id 114})
order {:type :sell :quantity 9 :price 13 :book.order/id 115}
[trades new-book] (match-order-in-book buy-book order)]
new-book => (create-buy-book {:type :buy :quantity 1 :price 13 :book.order/id 114})
trades => [#:book.order{:quantity 2 :price 14 :book-id 113 :order-id 115 :type :buy}
#:book.order{:quantity 7 :price 13 :book-id 114 :order-id 115 :type :buy}]))
(fact "Simplest case VI."
(let [sell-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 116}
{:type :buy :quantity 8 :price 13 :book.order/id 117})
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
[trades new-book leftover-order] (match-order-in-book sell-book order)]
new-book => empty?
trades => [#:book.order{:quantity 2 :price 14 :book-id 116 :order-id 118 :type :buy}
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :buy}]
leftover-order => {:type :sell :quantity 11 :price 13 :book.order/id 118})))
(facts "We should write tests for each bug found"
(fact "book type should always be book.PersistentSortedList"
(let [book (create-buy-book)
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
[_ new-book _] (match-order-in-book book order)]
(count new-book) => 0
(type new-book) => skirmish.PersistentSortedList)))
;(fact "should not fuck up"
; (let [order-1 {:type :buy :quantity 4 :price 4 :book.order/id 5}
; order-2 {:type :sell :quantity 2 :price 3 :book.order/id 6}
; book (create-buy-book order-1)
; expt-trade #:book.order{}
; [trades new-book new-order] (match-order-in-book book order-2)]
; trades => 1
; new-order => nil?
; new-book => {})))