├── .classpath ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs ├── org.eclipse.m2e.core.prefs └── org.eclipse.wst.common.project.facet.core.xml ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml ├── src ├── main │ ├── java │ │ └── fractalfintech │ │ │ └── orderbook │ │ │ ├── CustomController.java │ │ │ ├── MarketController.java │ │ │ ├── MarketList.java │ │ │ ├── Order.java │ │ │ ├── OrderBook.java │ │ │ ├── OrderItemDao.java │ │ │ └── OrderbookApplication.java │ └── resources │ │ ├── application-h2.properties │ │ ├── application.properties │ │ ├── data-h2.sql │ │ ├── log4j.properties │ │ └── schema-h2.sql └── test │ └── java │ └── fractalfintech │ └── orderbook │ ├── MarketControllerIT.java │ ├── MarketControllerTest.java │ ├── OrderbookApplicationTests.java │ ├── TestMarket.java │ └── TestOrder.java └── test.sh /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | orderbook 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.common.project.facet.core.builder 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.m2e.core.maven2Builder 20 | 21 | 22 | 23 | 24 | org.springframework.ide.eclipse.core.springbuilder 25 | 26 | 27 | 28 | 29 | org.springframework.ide.eclipse.boot.validation.springbootbuilder 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.ide.eclipse.core.springnature 36 | org.eclipse.jdt.core.javanature 37 | org.eclipse.m2e.core.maven2Nature 38 | org.eclipse.wst.common.project.facet.core.nature 39 | 40 | 41 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.8 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Order Book 2 | Order Book is a subsystem of an exchange, normally is called the trade engine. 3 | 4 | It is at its initial stage of development at the moment. 5 | 6 | ## Purpose 7 | 8 | - There is not much open source trade engine out there at the moment, with that, this project would like to address that by the creation of this project. 9 | - Have a trade engine that can be used by anyone wanting to understand what a trade engine looks like, or simply use it in an exchange system. 10 | 11 | ## Objective 12 | 13 | - Be able to handle large amount of transactions per hour using cheap/affordable off-the-shelf resources. 14 | 15 | ## Building It 16 | 17 | This project uses Maven to build the project. 18 | 19 | ``` 20 | mvn package 21 | ``` 22 | 23 | To run the package: 24 | 25 | ``` 26 | java -jar target/orderbook-0.0.1-SNAPSHOT.jar 27 | ``` 28 | 29 | ## Test Using Curl 30 | 31 | Add item to order book list 32 | ``` 33 | curl http://localhost:8090/market/add/item/diner0 34 | ``` 35 | 36 | Get list 37 | ``` 38 | curl http://localhost:8090/market/list/get 39 | ``` 40 | 41 | Bid transaction 42 | ``` 43 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"100", "qty":"20"}' -X POST http://localhost:8090/market/bid/add 44 | ``` 45 | 46 | Offer transaction 47 | ``` 48 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"100", "qty":"10"}' -X POST http://localhost:8090/market/offer/add 49 | ``` 50 | 51 | 52 | Get market bid list 53 | ``` 54 | curl -H "Content-Type: application/json" -d '{"name":"diner0"}' -X POST http://localhost:8090/market/bid/get 55 | ``` 56 | 57 | Get market offer list 58 | ``` 59 | curl -H "Content-Type: application/json" -d '{"name":"diner0"}' -X POST http://localhost:8090/market/offer/get 60 | ``` 61 | 62 | Check out the file test.sh for more information on testing. 63 | 64 | ## Dev Environment Requirements 65 | 66 | - Linux (Ubuntu) OS 67 | - Java SDK 68 | - Spring Boot STS (Eclipse) 69 | - Maven 70 | - Curl 71 | 72 | 73 | 74 | ## Refences 75 | 76 | - https://en.wikipedia.org/wiki/Order_book_(trading) 77 | - https://www.youtube.com/watch?v=zeg3B7gMCNQ 78 | - https://www.investopedia.com/terms/o/order-book.asp 79 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | fractalfintech 7 | orderbook 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | orderbook 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.1.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-actuator 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-data-jpa 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | io.springfox 47 | springfox-swagger2 48 | 2.4.0 49 | 50 | 51 | io.springfox 52 | springfox-swagger-ui 53 | 2.4.0 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-devtools 58 | runtime 59 | 60 | 61 | com.h2database 62 | h2 63 | runtime 64 | 65 | 66 | 67 | org.codehaus.jackson 68 | jackson-mapper-asl 69 | 1.9.13 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | spring-snapshots 78 | Spring Snapshots 79 | https://repo.spring.io/snapshot 80 | 81 | true 82 | 83 | 84 | 85 | spring-milestones 86 | Spring Milestones 87 | https://repo.spring.io/milestone 88 | 89 | false 90 | 91 | 92 | 93 | 94 | 95 | 96 | spring-snapshots 97 | Spring Snapshots 98 | https://repo.spring.io/snapshot 99 | 100 | true 101 | 102 | 103 | 104 | spring-milestones 105 | Spring Milestones 106 | https://repo.spring.io/milestone 107 | 108 | false 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | org.springframework.boot 118 | spring-boot-maven-plugin 119 | 120 | 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/CustomController.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | @RestController 18 | public class CustomController { 19 | 20 | private final Logger logger = LoggerFactory.getLogger(getClass()); 21 | private MarketList marketList; 22 | 23 | public CustomController() 24 | { 25 | marketList = new MarketList(); 26 | marketList.Add("Test"); 27 | } 28 | 29 | @RequestMapping(value = "/custom", method = RequestMethod.POST) 30 | public String custom() { 31 | //final String username = SecurityContextHolder.getContext().getAuthentication().getName(); 32 | //logger.info(username); 33 | //return "Welcome, " + username; 34 | return "Custom message here."; 35 | } 36 | 37 | @RequestMapping(value = "/add/item/{name}", method = RequestMethod.GET) 38 | public String AddMarketItem(@PathVariable(value="name") String name) { 39 | marketList.Add(name); 40 | return "success"; 41 | } 42 | 43 | @RequestMapping(value = "/list", method = RequestMethod.GET) 44 | @ResponseBody 45 | public List GetMarketList() { 46 | List list = marketList.GetList(); 47 | return list; 48 | } 49 | 50 | @RequestMapping(value = "/bid/add", method = RequestMethod.POST,consumes="application/json") 51 | public ResponseEntity AddMarketBid(@RequestBody OrderItemDao bid) { 52 | logger.info("MarketController bid name : {}", bid.getName()); 53 | logger.info("MarketController bid price : {}", bid.getPrice()); 54 | logger.info("MarketController bid qty : {}", bid.getQty()); 55 | marketList.AddBid(bid); 56 | return new ResponseEntity("success: bid added.", HttpStatus.OK); 57 | } 58 | 59 | @RequestMapping(value = "/offer/add", method = RequestMethod.POST,consumes="application/json") 60 | public ResponseEntity AddMarketOffer(@RequestBody OrderItemDao offer) { 61 | logger.info("MarketController offer name : {}", offer.getName()); 62 | logger.info("MarketController offer price : {}", offer.getPrice()); 63 | logger.info("MarketController offer qty : {}", offer.getQty()); 64 | marketList.AddOffer(offer); 65 | return new ResponseEntity("success: offer added.", HttpStatus.OK); 66 | } 67 | 68 | @RequestMapping(value = "/offer/get", method = RequestMethod.POST,consumes="application/json") 69 | public ResponseEntity>> GetMarketOffer(@RequestBody OrderItemDao offer) { 70 | logger.info("MarketController GetMarketOffer name : {}", offer.getName()); 71 | Map> list = marketList.GetOfferMap(offer); 72 | return new ResponseEntity>>(list, HttpStatus.OK); 73 | } 74 | 75 | @RequestMapping(value = "/bid/get", method = RequestMethod.POST,consumes="application/json") 76 | public ResponseEntity>> GetMarketBid(@RequestBody OrderItemDao bid) { 77 | logger.info("MarketController GetMarketBid name : {}", bid.getName()); 78 | Map> list = marketList.GetBidMap(bid); 79 | return new ResponseEntity>>(list, HttpStatus.OK); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/MarketController.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import fractalfintech.orderbook.OrderBook; 4 | import fractalfintech.orderbook.MarketList; 5 | import fractalfintech.orderbook.Order; 6 | import fractalfintech.orderbook.OrderItemDao; 7 | 8 | 9 | import org.springframework.web.bind.annotation.RestController; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RequestMethod; 12 | import org.springframework.web.bind.annotation.ResponseBody; 13 | import org.springframework.web.bind.annotation.PathVariable; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.http.HttpStatus; 17 | import org.springframework.http.ResponseEntity; 18 | import org.springframework.web.bind.annotation.ModelAttribute; 19 | import org.springframework.web.bind.annotation.PostMapping; 20 | import org.springframework.web.bind.annotation.RequestBody; 21 | 22 | import java.lang.Double; 23 | 24 | import java.util.Collections; 25 | import java.util.HashMap; 26 | import java.util.LinkedList; 27 | import java.util.List; 28 | import java.util.Map; 29 | import java.util.PriorityQueue; 30 | import java.util.Queue; 31 | 32 | 33 | @RestController 34 | public class MarketController 35 | { 36 | private final Logger logger = LoggerFactory.getLogger(MarketController.class); 37 | private MarketList marketList; 38 | 39 | // Initializes marketplace 40 | public MarketController() 41 | { 42 | marketList = new MarketList(); 43 | marketList.Add("Test"); 44 | } 45 | 46 | 47 | @RequestMapping("/market/add/item/{name}") 48 | public String AddMarketItem(@PathVariable(value="name") String name) { 49 | marketList.Add(name); 50 | return "success"; 51 | } 52 | 53 | @RequestMapping("/market/list/get") 54 | @ResponseBody 55 | public List GetMarketList() { 56 | List list = marketList.GetList(); 57 | return list; 58 | } 59 | 60 | @RequestMapping(value = "/market/bid/add", method = RequestMethod.POST,consumes="application/json") 61 | public ResponseEntity AddMarketBid(@RequestBody OrderItemDao bid) { 62 | logger.info("MarketController bid name : {}", bid.getName()); 63 | logger.info("MarketController bid price : {}", bid.getPrice()); 64 | logger.info("MarketController bid qty : {}", bid.getQty()); 65 | marketList.AddBid(bid); 66 | return new ResponseEntity("success: bid added.", HttpStatus.OK); 67 | } 68 | 69 | 70 | 71 | @RequestMapping(value = "/market/offer/add", method = RequestMethod.POST,consumes="application/json") 72 | public ResponseEntity AddMarketOffer(@RequestBody OrderItemDao offer) { 73 | logger.info("MarketController offer name : {}", offer.getName()); 74 | logger.info("MarketController offer price : {}", offer.getPrice()); 75 | logger.info("MarketController offer qty : {}", offer.getQty()); 76 | marketList.AddOffer(offer); 77 | return new ResponseEntity("success: offer added.", HttpStatus.OK); 78 | } 79 | 80 | @RequestMapping(value = "/market/offer/get", method = RequestMethod.POST,consumes="application/json") 81 | public ResponseEntity>> GetMarketOffer(@RequestBody OrderItemDao offer) { 82 | logger.info("MarketController GetMarketOffer name : {}", offer.getName()); 83 | Map> list = marketList.GetOfferMap(offer); 84 | return new ResponseEntity>>(list, HttpStatus.OK); 85 | } 86 | 87 | 88 | @RequestMapping(value = "/market/bid/get", method = RequestMethod.POST,consumes="application/json") 89 | public ResponseEntity>> GetMarketBid(@RequestBody OrderItemDao bid) { 90 | logger.info("MarketController GetMarketBid name : {}", bid.getName()); 91 | Map> list = marketList.GetBidMap(bid); 92 | return new ResponseEntity>>(list, HttpStatus.OK); 93 | } 94 | 95 | 96 | @RequestMapping("/") 97 | public String index() { 98 | return "Greetings from Spring Boot!"; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/MarketList.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | //import fractalfintech.orderbook.OrderBook; 4 | //import fractalfintech.orderbook.Order; 5 | //import fractalfintech.orderbook.OrderItemDao; 6 | 7 | 8 | import org.springframework.web.bind.annotation.RestController; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | 13 | import java.lang.Double; 14 | import java.util.ArrayList; 15 | import java.util.Collections; 16 | import java.util.HashMap; 17 | import java.util.LinkedList; 18 | import java.util.List; 19 | import java.util.Map; 20 | import java.util.PriorityQueue; 21 | import java.util.Queue; 22 | 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | 26 | 27 | public class MarketList 28 | { 29 | private final Logger logger = LoggerFactory.getLogger(MarketList.class); 30 | private OrderBook market; 31 | private Map orderBooks = null; 32 | 33 | public MarketList() 34 | { 35 | orderBooks = new HashMap(); 36 | market = new OrderBook("Test"); 37 | orderBooks.put("Test", market); 38 | } 39 | 40 | public List GetList() 41 | { 42 | List list = new ArrayList(orderBooks.keySet()); 43 | return list; 44 | } 45 | 46 | public void Add(String name) 47 | { 48 | if (!orderBooks.containsKey(name)) { 49 | market = new OrderBook(name); 50 | orderBooks.put(name, market); 51 | } 52 | } 53 | 54 | public void AddBid(OrderItemDao bid) 55 | { 56 | logger.info("MarketList AddBid name : {}", bid.getName()); 57 | logger.info("MarketList AddBid price : {}", bid.getPrice()); 58 | logger.info("MarketList AddBid qty : {}", bid.getQty()); 59 | if (orderBooks.containsKey(bid.getName())) { 60 | OrderBook book = orderBooks.get(bid.getName()); 61 | book.addBid(bid.getPrice(), bid.getQty()); 62 | } 63 | } 64 | 65 | public void AddOffer(OrderItemDao offer) 66 | { 67 | logger.info("MarketList AddOffer name : {}", offer.getName()); 68 | logger.info("MarketList AddOffer price : {}", offer.getPrice()); 69 | logger.info("MarketList AddOffer qty : {}", offer.getQty()); 70 | if (orderBooks.containsKey(offer.getName())) { 71 | OrderBook book = orderBooks.get(offer.getName()); 72 | book.addOffer(offer.getPrice(), offer.getQty()); 73 | } 74 | } 75 | 76 | public Map> GetBidMap(OrderItemDao bid) 77 | { 78 | logger.info("MarketList GetBidMap name : {}", bid.getName()); 79 | 80 | if (orderBooks.containsKey(bid.getName())) { 81 | OrderBook book = orderBooks.get(bid.getName()); 82 | Map> bidMap = book.getBidMap(); 83 | return bidMap; 84 | } 85 | return null; 86 | } 87 | 88 | public Map> GetOfferMap(OrderItemDao offer) 89 | { 90 | logger.info("MarketList GetOfferMap name : {}", offer.getName()); 91 | 92 | if (orderBooks.containsKey(offer.getName())) { 93 | OrderBook book = orderBooks.get(offer.getName()); 94 | Map> offerMap = book.getOfferMap(); 95 | return offerMap; 96 | } 97 | return null; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/Order.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | public class Order 4 | { 5 | private double price; 6 | private int quantity; 7 | 8 | public Order(double price, int quantity) 9 | { 10 | this.price = price; 11 | this.quantity = quantity; 12 | } 13 | 14 | public double getPrice() 15 | { 16 | return this.price; 17 | } 18 | 19 | public void setPrice(double newPrice) 20 | { 21 | this.price = newPrice; 22 | } 23 | 24 | public int getQuantity() 25 | { 26 | return this.quantity; 27 | } 28 | 29 | public void setQuantity(int newQuantity) 30 | { 31 | this.quantity = newQuantity; 32 | } 33 | 34 | public String toString() 35 | { 36 | return this.price + " " + this.quantity; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/OrderBook.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import fractalfintech.orderbook.Order; 4 | 5 | import java.lang.Double; 6 | 7 | import java.util.Collections; 8 | import java.util.HashMap; 9 | import java.util.LinkedList; 10 | import java.util.List; 11 | import java.util.Map; 12 | import java.util.PriorityQueue; 13 | import java.util.Queue; 14 | 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | public class OrderBook 19 | { 20 | private final Logger logger = LoggerFactory.getLogger(OrderBook.class); 21 | 22 | private String itemName; 23 | // Maps use List as value to hold multiple values with same hash 24 | private Map> bidMap = null; 25 | private Map> offerMap = null; 26 | 27 | private Queue bidMaxPriceList = null; 28 | private Queue offerMinPriceList = null; 29 | 30 | // Initializes marketplace 31 | public OrderBook(String name) 32 | { 33 | bidMap = new HashMap>(); 34 | offerMap = new HashMap>(); 35 | 36 | bidMaxPriceList = new PriorityQueue(30, Collections.reverseOrder()); // top is maximum bid price 37 | offerMinPriceList = new PriorityQueue(); // top is minimum offer price 38 | 39 | this.itemName = name; 40 | } 41 | 42 | public void setName(String name) 43 | { 44 | this.itemName = name; 45 | } 46 | 47 | /* Adds bid to map by hashing the price, then 48 | * adding bid to list located in that hash bucket 49 | */ 50 | public void addBid(double price, int quantity) 51 | { 52 | logger.info("addBid qty : {}", quantity); 53 | logger.info("addBid price : {}", price); 54 | List bucket = getBucket(bidMap, price); 55 | Order newBid = new Order(price, quantity); 56 | bucket.add(newBid); 57 | bidMap.put(newBid.getPrice(), bucket); 58 | bidMaxPriceList.add(price); 59 | matchOrders(); 60 | } 61 | 62 | /* Adds offer to map by hashing the price, then 63 | * adding offer to list located in that hash bucket 64 | */ 65 | public void addOffer(double price, int quantity) 66 | { 67 | List bucket = getBucket(offerMap, price); 68 | Order newOffer = new Order(price, quantity); 69 | logger.info("addOffer qty : {}", newOffer.getQuantity()); 70 | logger.info("addOffer price : {}", newOffer.getPrice()); 71 | logger.info("offerMap size : {}", bucket.size()); 72 | bucket.add(newOffer); 73 | logger.info("offerMap after insert size : {}", bucket.size()); 74 | offerMap.put(newOffer.getPrice(), bucket); 75 | offerMinPriceList.add(price); 76 | matchOrders(); 77 | } 78 | 79 | // Returns bucket list if price match, otherwise returns new list 80 | public List getBucket(Map> hashmap, Double price) 81 | { 82 | List bucket; 83 | if(hashmap.containsKey(price)) 84 | { 85 | bucket = hashmap.get(price); 86 | } 87 | else 88 | { 89 | bucket = new LinkedList(); 90 | } 91 | return bucket; 92 | } 93 | 94 | // Matches offers and bids based on pricetime priority 95 | public void matchOrders() 96 | { 97 | List bidBucket = null; 98 | List offerBucket = null; 99 | Double lowestOffer = null; 100 | Double highestBid = null; 101 | boolean finished = false; 102 | 103 | while(!finished) 104 | { 105 | // Peek because we don't want to remove the top element until the order is closed 106 | highestBid = bidMaxPriceList.peek(); 107 | lowestOffer = offerMinPriceList.peek(); 108 | 109 | // No possible trade if either list is empty or no bid higher than an offer 110 | if(lowestOffer == null || highestBid == null || lowestOffer > highestBid) 111 | { 112 | finished = true; 113 | logger.info("OrderBook matchOrders finished = true"); 114 | } 115 | else 116 | { 117 | // Gets buckets for both maps 118 | bidBucket = bidMap.get(bidMaxPriceList.peek()); 119 | offerBucket = offerMap.get(offerMinPriceList.peek()); 120 | 121 | // Gets first element from each bucket since they're the oldest 122 | int bidQuantity = bidBucket.get(0).getQuantity(); 123 | int offerQuantity = offerBucket.get(0).getQuantity(); 124 | 125 | if(bidQuantity > offerQuantity) 126 | { 127 | logger.info("bidQuantity > offerQuantity"); 128 | System.out.println(successfulTrade(offerQuantity, lowestOffer)); 129 | 130 | // Decrements quantity in bid 131 | bidQuantity -= offerQuantity; 132 | bidBucket.get(0).setQuantity(bidQuantity); 133 | logger.info("bidQuantity remaining qty : {}", bidQuantity); 134 | 135 | // Closes previous offer 136 | offerBucket.remove(0); 137 | offerMinPriceList.remove(); 138 | } 139 | else if(offerQuantity > bidQuantity) 140 | { 141 | logger.info("bidQuantity < offerQuantity"); 142 | System.out.println(successfulTrade(bidQuantity, lowestOffer)); 143 | 144 | // Decrements quantity in offer 145 | offerQuantity -= bidQuantity; 146 | offerBucket.get(0).setQuantity(offerQuantity); 147 | logger.info("offerQuantity remaining qty : {}", offerQuantity); 148 | 149 | // Closes previous bid 150 | bidBucket.remove(0); 151 | bidMaxPriceList.remove(); 152 | } 153 | else 154 | { 155 | // bidQuantity is an arbitrary choice because both quantities are equal. 156 | // lowestOffer is chosen because it's the price at which the trade is made. 157 | System.out.println(successfulTrade(bidQuantity, lowestOffer)); 158 | 159 | // Removes bid and offer because they're both closed 160 | bidBucket.remove(0); 161 | bidMaxPriceList.remove(); 162 | offerBucket.remove(0); 163 | offerMinPriceList.remove(); 164 | } 165 | } 166 | } 167 | } 168 | 169 | // Returns the string printed for a successful trade. 170 | public String successfulTrade(int quantity, double price) 171 | { 172 | logger.info("successfulTrade bidQuantity : {}", quantity); 173 | logger.info("successfulTrade lowestOffer : {}", price); 174 | return quantity + " shares traded for $" + price + " per share."; 175 | } 176 | 177 | // Prints the remaining trades from input map after close of market. 178 | private void printFailedTrades(Map> hashmap, String type) 179 | { 180 | for (Double key : hashmap.keySet()) 181 | { 182 | List bucket = hashmap.get(key); 183 | 184 | for(Order order : bucket) 185 | { 186 | System.out.println(type + order.getQuantity() + " shares for $" + order.getPrice() + " per share failed to trade."); 187 | } 188 | } 189 | } 190 | 191 | // Signifies that the market is open. 192 | public void openMarket() 193 | { 194 | System.out.println("Market opens."); 195 | } 196 | 197 | public void closeMarket() 198 | { 199 | System.out.println("Market closes."); 200 | printFailedTrades(bidMap, "Bid for "); 201 | printFailedTrades(offerMap, "Offer of "); 202 | } 203 | 204 | public Map> getBidMap() 205 | { 206 | return bidMap; 207 | } 208 | 209 | public Map> getOfferMap() 210 | { 211 | return offerMap; 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/OrderItemDao.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | public class OrderItemDao { 4 | 5 | private String name; 6 | private double price; 7 | private int qty; 8 | private String type; 9 | private String transactionType; 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getType() { 20 | return type; 21 | } 22 | 23 | public void setType(String transactionType) { 24 | this.transactionType = transactionType; 25 | } 26 | 27 | public String getTranType() { 28 | return transactionType; 29 | } 30 | 31 | public void setTranType(String type) { 32 | this.type = type; 33 | } 34 | 35 | public int getQty() { 36 | return qty; 37 | } 38 | 39 | public void setQty(int qty) { 40 | this.qty = qty; 41 | } 42 | 43 | public double getPrice() { 44 | return price; 45 | } 46 | 47 | public void setPrice(double price) { 48 | this.price = price; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/fractalfintech/orderbook/OrderbookApplication.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | public class OrderbookApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(OrderbookApplication.class, args); 12 | } 13 | 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/resources/application-h2.properties: -------------------------------------------------------------------------------- 1 | # In addition, Spring Boot processes the schema-${platform}.sql and data-${platform}.sql 2 | # files (if present), where platform is the value of spring.datasource.platform. 3 | # This allows you to switch to database-specific scripts if necessary. 4 | # For example, you might choose to set it to the vendor name of the 5 | # database (hsqldb, h2, oracle, mysql, postgresql, and so on). 6 | 7 | spring.jpa.generate-ddl=true 8 | spring.jpa.hibernate.ddl-auto=create-drop 9 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 10 | 11 | spring.datasource.initialize=true 12 | spring.datasource.platform=h2 13 | spring.datasource.continue-on-error=true 14 | spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 15 | spring.datasource.driverClassName=org.h2.Driver 16 | spring.datasource.username=sa 17 | spring.datasource.password= 18 | 19 | # Enabling H2 Console. http://www.springboottutorial.com/spring-boot-and-spring-jdbc-with-h2 20 | spring.h2.console.enabled=true 21 | 22 | 23 | #Spring Boot automatically creates the schema of an embedded DataSource. 24 | #This behavior can be customized by using the spring.datasource.initialization-mode 25 | #property (and it can also be always or never). 26 | spring.datasource.initialization-mode=always 27 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.web=DEBUG 2 | #logging.level.org.hibernate=ERROR 3 | 4 | spring.profiles.active=h2 5 | server.port = 8090 6 | -------------------------------------------------------------------------------- /src/main/resources/data-h2.sql: -------------------------------------------------------------------------------- 1 | insert into person 2 | values(10001,'Ranga', 'E1234567'); 3 | 4 | insert into person 5 | values(10002,'Ravi', 'A1234568'); 6 | -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | #log4j.rootLogger=DEBUG, STDOUT 2 | #log4j.logger.deng=INFO 3 | #log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender 4 | #log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout 5 | #log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 6 | -------------------------------------------------------------------------------- /src/main/resources/schema-h2.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE TABLE IF NOT EXISTS bidder 3 | ( 4 | id integer not null, 5 | guid varchar(64) not null, 6 | name varchar(64) not null, 7 | create_date date not null, 8 | primary key(id) 9 | ); 10 | 11 | CREATE TABLE IF NOT EXISTS bid 12 | ( 13 | id integer not null, 14 | bidder_id integer not null, 15 | bid_price float not null, 16 | bid_qty integer not null, 17 | orig_qty integer not null, 18 | limit_price float not null, 19 | create_date date not null, 20 | primary key(id) 21 | ); 22 | 23 | CREATE TABLE IF NOT EXISTS offer 24 | ( 25 | id integer not null, 26 | bidder_id integer not null, 27 | offer_price float not null, 28 | offer_qty integer not null, 29 | orig_qty integer not null, 30 | create_date date not null, 31 | primary key(id) 32 | ); 33 | 34 | CREATE TABLE IF NOT EXISTS offer_ledger 35 | ( 36 | id integer not null, 37 | price_point float not null, 38 | db_offer_id integer not null, 39 | db_offer_qty integer not null, 40 | cr_bid_id integer not null, 41 | cr_bid_qty integer not null, 42 | create_date date not null, 43 | primary key(id) 44 | ); 45 | 46 | 47 | CREATE TABLE IF NOT EXISTS person 48 | ( 49 | id integer not null, 50 | name varchar(255) not null, 51 | passport_number varchar(255) not null, 52 | primary key(id) 53 | ); 54 | -------------------------------------------------------------------------------- /src/test/java/fractalfintech/orderbook/MarketControllerIT.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | 6 | import java.net.URL; 7 | 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.boot.test.context.SpringBootTest; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.boot.web.server.LocalServerPort; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.test.context.junit4.SpringRunner; 17 | 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 20 | public class MarketControllerIT { 21 | 22 | @LocalServerPort 23 | private int port; 24 | 25 | private URL base; 26 | 27 | @Autowired 28 | private TestRestTemplate template; 29 | 30 | @Before 31 | public void setUp() throws Exception { 32 | this.base = new URL("http://localhost:" + port + "/"); 33 | } 34 | 35 | @Test 36 | public void getHello() throws Exception { 37 | ResponseEntity response = template.getForEntity(base.toString(), 38 | String.class); 39 | assertThat(response.getBody(), equalTo("Greetings from Spring Boot!")); 40 | } 41 | 42 | @Test 43 | public void getCustom() throws Exception { 44 | ResponseEntity response = template.getForEntity(base.toString()+"custom", 45 | String.class); 46 | assertThat(response.getBody(), equalTo("Custom message here.")); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/fractalfintech/orderbook/MarketControllerTest.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import static org.hamcrest.Matchers.equalTo; 4 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 5 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.http.MediaType; 13 | import org.springframework.test.context.junit4.SpringRunner; 14 | import org.springframework.test.web.servlet.MockMvc; 15 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 16 | 17 | import org.codehaus.jackson.JsonGenerationException; 18 | import org.codehaus.jackson.map.JsonMappingException; 19 | import org.codehaus.jackson.map.ObjectMapper; 20 | 21 | @RunWith(SpringRunner.class) 22 | @SpringBootTest 23 | @AutoConfigureMockMvc 24 | public class MarketControllerTest { 25 | 26 | @Autowired 27 | private MockMvc mvc; 28 | 29 | @Test 30 | public void getHello() throws Exception { 31 | mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) 32 | .andExpect(status().isOk()) 33 | .andExpect(content().string(equalTo("Greetings from Spring Boot!"))); 34 | } 35 | 36 | @Test 37 | public void addMarketItem() throws Exception { 38 | mvc.perform(MockMvcRequestBuilders.get("/add/item/apple").accept(MediaType.APPLICATION_JSON)) 39 | .andExpect(status().isOk()) 40 | .andExpect(content().string(equalTo("success"))); 41 | } 42 | 43 | public static String asJsonString(final Object obj) { 44 | try { 45 | final ObjectMapper mapper = new ObjectMapper(); 46 | final String jsonContent = mapper.writeValueAsString(obj); 47 | return jsonContent; 48 | } catch (Exception e) { 49 | throw new RuntimeException(e); 50 | } 51 | } 52 | 53 | @Test 54 | public void addBid() throws Exception { 55 | OrderItemDao marketItemDao = new OrderItemDao(); 56 | marketItemDao.setName("Apple"); 57 | marketItemDao.setPrice(148.78); 58 | marketItemDao.setQty(25); 59 | marketItemDao.setType("Market"); 60 | marketItemDao.setTranType("Buy"); 61 | mvc.perform(MockMvcRequestBuilders.post("/bid/add") 62 | .content(asJsonString(marketItemDao)) 63 | .contentType(MediaType.APPLICATION_JSON) 64 | .accept(MediaType.APPLICATION_JSON)) 65 | .andExpect(status().isOk()) 66 | .andExpect(content().string(equalTo("success: bid added."))); 67 | } 68 | 69 | @Test 70 | public void addOffer() throws Exception { 71 | OrderItemDao marketItemDao = new OrderItemDao(); 72 | marketItemDao.setName("Apple"); 73 | marketItemDao.setPrice(158.78); 74 | marketItemDao.setQty(20); 75 | marketItemDao.setType("Market"); 76 | marketItemDao.setTranType("Sell"); 77 | mvc.perform(MockMvcRequestBuilders.post("/offer/add") 78 | .content(asJsonString(marketItemDao)) 79 | .contentType(MediaType.APPLICATION_JSON) 80 | .accept(MediaType.APPLICATION_JSON)) 81 | .andExpect(status().isOk()) 82 | .andExpect(content().string(equalTo("success: offer added."))); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/fractalfintech/orderbook/OrderbookApplicationTests.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import static org.junit.Assert.*; 4 | import org.junit.Test; 5 | import org.junit.Before; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | import fractalfintech.orderbook.OrderBook; 11 | import fractalfintech.orderbook.Order; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest 15 | public class OrderbookApplicationTests { 16 | 17 | private OrderBook market; 18 | 19 | @Before 20 | public void initMarket() 21 | { 22 | market = new OrderBook("Test"); 23 | } 24 | 25 | @Test 26 | public void contextLoads() { 27 | } 28 | 29 | @Test 30 | public void addNewBidShouldCorrectlyAddNewBid2() 31 | { 32 | initMarket(); 33 | assertTrue(market.getBidMap().isEmpty()); 34 | market.addBid(12.0, 1); 35 | assertTrue(market.getBidMap().containsKey(12.0)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/fractalfintech/orderbook/TestMarket.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | import java.lang.Exception; 9 | 10 | import java.util.List; 11 | import java.util.LinkedList; 12 | 13 | import fractalfintech.orderbook.OrderBook; 14 | import fractalfintech.orderbook.Order; 15 | 16 | public class TestMarket 17 | { 18 | private OrderBook market; 19 | 20 | @Before 21 | public void initMarket() 22 | { 23 | market = new OrderBook("Test"); 24 | } 25 | 26 | /******************************************* 27 | * 28 | * addBid Tests 29 | * 30 | *******************************************/ 31 | 32 | @Test 33 | public void addNewBidShouldCorrectlyAddNewBid() 34 | { 35 | initMarket(); 36 | assertTrue(market.getBidMap().isEmpty()); 37 | market.addBid(12.0, 1); 38 | assertTrue(market.getBidMap().containsKey(12.0)); 39 | } 40 | 41 | @Test 42 | public void addDuplicateBidPriceShouldCorrectlyAddNewBid() 43 | { 44 | initMarket(); 45 | assertTrue(market.getBidMap().isEmpty()); 46 | market.addBid(12.0, 1); 47 | market.addBid(12.0, 2); 48 | 49 | // Checks to see if corresponding elements have same quantities. 50 | assertEquals(1, market.getBidMap().get(12.0).get(0).getQuantity()); 51 | assertEquals(2, market.getBidMap().get(12.0).get(1).getQuantity()); 52 | } 53 | 54 | /******************************************* 55 | * 56 | * addOffer Tests 57 | * 58 | *******************************************/ 59 | 60 | @Test 61 | public void addNewOfferShouldCorrectlyAddNewOffer() 62 | { 63 | initMarket(); 64 | assertTrue(market.getOfferMap().isEmpty()); 65 | market.addOffer(12.0, 1); 66 | assertTrue(market.getOfferMap().containsKey(12.0)); 67 | } 68 | 69 | @Test 70 | public void addDuplicateOfferPriceShouldCorrectlyAddNewOffer() 71 | { 72 | initMarket(); 73 | assertTrue(market.getOfferMap().isEmpty()); 74 | market.addOffer(12.0, 1); 75 | market.addOffer(12.0, 2); 76 | 77 | // Checks to see if corresponding elements have same quantities. 78 | assertEquals(1, market.getOfferMap().get(12.0).get(0).getQuantity()); 79 | assertEquals(2, market.getOfferMap().get(12.0).get(1).getQuantity()); 80 | } 81 | 82 | /******************************************* 83 | * 84 | * getBucket Test 85 | * 86 | *******************************************/ 87 | 88 | @Test 89 | public void getBucketShouldReturnCorrectList() 90 | { 91 | initMarket(); 92 | assertTrue(market.getOfferMap().isEmpty()); 93 | market.addOffer(12.0, 1); 94 | market.addOffer(12.0, 2); 95 | 96 | // Checks to see if corresponding bucket elements have same quantities. 97 | assertEquals(1, market.getBucket(market.getOfferMap(), 12.0).get(0).getQuantity()); 98 | assertEquals(2, market.getBucket(market.getOfferMap(), 12.0).get(1).getQuantity()); 99 | } 100 | 101 | /******************************************* 102 | * 103 | * matchOrders Tests 104 | * 105 | *******************************************/ 106 | 107 | @Test 108 | public void BidQuantityShouldCorrectlyDecrementWhenGreaterThanOfferQuantity() 109 | { 110 | initMarket(); 111 | market.addOffer(12.0, 6); 112 | market.addBid(12.0, 9); 113 | market.matchOrders(); 114 | assertEquals(3, market.getBidMap().get(12.0).get(0).getQuantity()); // Bid correctly decremented 115 | assertTrue(market.getOfferMap().get(12.0).isEmpty()); // Offer correctly closed 116 | } 117 | 118 | @Test 119 | public void OfferQuantityShouldCorrectlyDecrementWhenGreaterThanBidQuantity() 120 | { 121 | initMarket(); 122 | market.addBid(12.0, 5); 123 | market.addOffer(12.0, 10); 124 | market.matchOrders(); 125 | assertEquals(5, market.getOfferMap().get(12.0).get(0).getQuantity()); // Offer correctly decremented 126 | assertTrue(market.getBidMap().get(12.0).isEmpty()); // Bid correctly closed 127 | } 128 | 129 | @Test 130 | public void BothQuantitiesEqualShouldCorrectlyRemoveBoth() 131 | { 132 | initMarket(); 133 | market.addBid(12.0, 5); 134 | market.addOffer(12.0, 5); 135 | market.matchOrders(); 136 | assertTrue(market.getBidMap().get(12.0).isEmpty()); // Bid correctly closed 137 | assertTrue(market.getOfferMap().get(12.0).isEmpty()); // Offer correctly closed 138 | } 139 | 140 | @Test 141 | public void BidWithValueAndNoOffersShouldStayTheSame() 142 | { 143 | initMarket(); 144 | market.addBid(12.0, 5); 145 | market.matchOrders(); 146 | assertEquals(5, market.getBidMap().get(12.0).get(0).getQuantity()); // Bid still has same value 147 | assertTrue(market.getOfferMap().get(12.0) == null); // Offer still null 148 | } 149 | 150 | @Test 151 | public void OfferWithValueAndNoBidsShouldStayTheSame() 152 | { 153 | initMarket(); 154 | market.addOffer(12.0, 5); 155 | market.matchOrders(); 156 | assertEquals(5, market.getOfferMap().get(12.0).get(0).getQuantity()); // Offer still has same value 157 | assertTrue(market.getBidMap().get(12.0) == null); // Bid still null 158 | } 159 | 160 | @Test 161 | public void OfferPriceHigherThanBidPriceShouldStayTheSame() 162 | { 163 | initMarket(); 164 | market.addOffer(12.0, 7); 165 | market.addBid(6.0, 5); 166 | market.matchOrders(); 167 | assertEquals(7, market.getOfferMap().get(12.0).get(0).getQuantity()); // Offer still has same value 168 | assertEquals(5, market.getBidMap().get(6.0).get(0).getQuantity()); // Bid still has same value 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/test/java/fractalfintech/orderbook/TestOrder.java: -------------------------------------------------------------------------------- 1 | package fractalfintech.orderbook; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | import fractalfintech.orderbook.Order; 9 | 10 | public class TestOrder 11 | { 12 | private Order order; 13 | 14 | @Test 15 | public void getPriceShouldReturnCorrectPrice() 16 | { 17 | order = new Order(12.0, 1); 18 | // margin of error 19 | assertEquals(12.0, order.getPrice(), .00000000000001); 20 | } 21 | 22 | @Test 23 | public void setPriceShouldCorrectlySetPrice() 24 | { 25 | order = new Order(12.0, 1); 26 | order.setPrice(15.0); 27 | assertEquals(15.0, order.getPrice(), .00000000000001); 28 | } 29 | 30 | @Test 31 | public void getQuantityShouldReturnCorrectQuantity() 32 | { 33 | order = new Order(12.0, 1); 34 | assertEquals(1, order.getQuantity()); 35 | } 36 | 37 | @Test 38 | public void setQuantityShouldCorrectlySetQuantity() 39 | { 40 | order = new Order(12.0, 1); 41 | order.setQuantity(5); 42 | assertEquals(5, order.getQuantity()); 43 | } 44 | 45 | @Test 46 | public void toStringShouldReturnCorrectStringFormat() 47 | { 48 | order = new Order(12.0, 1); 49 | assertEquals("12.0 1", order.toString()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | curl http://localhost:8080/market/add/item/diner0 4 | 5 | 6 | printf "\n" 7 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"101", "qty":"20"}' -X POST http://localhost:8080/market/bid/add 8 | 9 | printf "\n" 10 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"100", "qty":"12"}' -X POST http://localhost:8080/market/bid/add 11 | 12 | printf "\n" 13 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"98", "qty":"12"}' -X POST http://localhost:8080/market/bid/add 14 | 15 | printf "\n" 16 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"97", "qty":"24"}' -X POST http://localhost:8080/market/bid/add 17 | 18 | 19 | printf "\n" 20 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"99", "qty":"15"}' -X POST http://localhost:8080/market/offer/add 21 | 22 | printf "\n" 23 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"100", "qty":"10"}' -X POST http://localhost:8080/market/offer/add 24 | 25 | printf "\n" 26 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"98", "qty":"5"}' -X POST http://localhost:8080/market/offer/add 27 | 28 | printf "\n" 29 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"97.5", "qty":"9"}' -X POST http://localhost:8080/market/offer/add 30 | 31 | printf "\n" 32 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"97", "qty":"18"}' -X POST http://localhost:8080/market/offer/add 33 | 34 | printf "\n" 35 | curl -H "Content-Type: application/json" -d '{"name":"diner0", "price":"110", "qty":"21"}' -X POST http://localhost:8080/market/offer/add 36 | 37 | 38 | printf "\nBid List\n" 39 | curl -H "Content-Type: application/json" -d '{"name":"diner0"}' -X POST http://localhost:8080/market/bid/get 40 | 41 | printf "\nOffer List\n" 42 | curl -H "Content-Type: application/json" -d '{"name":"diner0"}' -X POST http://localhost:8080/market/offer/get 43 | 44 | 45 | --------------------------------------------------------------------------------