Skip to content

Commit

Permalink
Merge pull request #5 from EconomicSL/add-double-auction-trait
Browse files Browse the repository at this point in the history
Add double auction trait
  • Loading branch information
David R. Pugh committed Mar 23, 2017
2 parents 7f10cd5 + f05b986 commit 0ee8a0d
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 15 deletions.
8 changes: 7 additions & 1 deletion build.sbt
@@ -1,3 +1,5 @@
import xsbti.compile

name := "auctions"

version := "0.1.0-alpha"
Expand All @@ -8,4 +10,8 @@ scalaVersion := "2.12.1"
scalacOptions ++= Seq(
"-feature", // tells the compiler to provide information about misused language features
"-language:implicitConversions" // eliminates the need to import implicit conversions for each usage
)
)


// In our project Java depends on Scala, but not the other way round!
compileOrder := CompileOrder.ScalaThenJava
77 changes: 77 additions & 0 deletions pom.xml
@@ -0,0 +1,77 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.economicsl</groupId>
<artifactId>auctions</artifactId>
<packaging>jar</packaging>
<version>0.1.0-alpha</version>
<name>auctions</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.12.1</scala.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</project>
100 changes: 100 additions & 0 deletions src/main/java/org/economicsl/auctions/singleunit/JDoubleAuction.java
@@ -0,0 +1,100 @@
// Copyright (c) 2017 Robert Bosch GmbH
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.economicsl.auctions.singleunit;

import org.economicsl.auctions.Price;
import org.economicsl.auctions.Tradable;
import org.economicsl.auctions.singleunit.pricing.PricingRule;
import scala.Option;
import scala.Tuple2;
import scala.collection.JavaConverters;
import scala.collection.immutable.Stream;
import scala.math.Ordering;

import java.util.List;
import java.util.Optional;

public class JDoubleAuction<T extends Tradable> {

private DoubleAuction<T> auction = DoubleAuction$.MODULE$.withUniformPricing(
LimitAskOrder$.MODULE$.<LimitAskOrder<T>>ordering(),
LimitBidOrder$.MODULE$.<LimitBidOrder<T>>ordering().reverse()
);

public class ClearResult<T extends Tradable> {
private JDoubleAuction<T> auction;
private List<Fill<T>> fills;

public ClearResult(List<Fill<T>> _fills, JDoubleAuction<T> _auction) {
auction = _auction;
fills = _fills;
}

public JDoubleAuction<T> getAuction() {
return auction;
}

public List<Fill<T>> getFills() {
return fills;
}
}

private JDoubleAuction() {
}

private JDoubleAuction(DoubleAuction<T> _auction) {
auction = _auction;
}

public JDoubleAuction<T> insert(LimitAskOrder<T> order) {
return new JDoubleAuction<T>(auction.insert(order));
}

public JDoubleAuction<T> insert(LimitBidOrder<T> order) {
return new JDoubleAuction<T>(auction.insert(order));
}

public JDoubleAuction<T> remove(LimitAskOrder<T> order) {
return new JDoubleAuction<T>(auction.remove(order));
}

public JDoubleAuction<T> remove(LimitBidOrder<T> order) {
return new JDoubleAuction<T>(auction.remove(order));
}

public Optional<ClearResult<T>> clear(PricingRule<T, Price> p) {
Tuple2<Option<Stream<Fill<T>>>, DoubleAuction<T>> clear = auction.clear(p);
Option<Stream<Fill<T>>> streamOption = clear._1();
if(streamOption.isDefined()) {
List<Fill<T>> fills = JavaConverters.seqAsJavaListConverter(clear._1().get()).asJava();
JDoubleAuction<T> newAuction = new JDoubleAuction<T>(clear._2());
return Optional.of(new ClearResult<T>(fills, newAuction));
}
return Optional.empty();
}

public static <T extends Tradable> JDoubleAuction<T> withUniformPricing(
Ordering<LimitAskOrder<T>> askOrdering,
Ordering<LimitBidOrder<T>> bidOrdering) {
return new JDoubleAuction<T>(DoubleAuction$.MODULE$.withUniformPricing(askOrdering, bidOrdering));
}

public static <T extends Tradable> JDoubleAuction<T> withDiscriminatoryPricing(
Ordering<LimitAskOrder<T>> askOrdering,
Ordering<LimitBidOrder<T>> bidOrdering) {
return new JDoubleAuction<T>(DoubleAuction$.MODULE$.withDiscriminatoryPricing(askOrdering, bidOrdering));
}
}
@@ -0,0 +1,45 @@
// Copyright (c) 2017 Robert Bosch GmbH
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.economicsl.auctions.singleunit;

import org.economicsl.auctions.singleunit.pricing.WeightedAveragePricingRule;

import java.util.Optional;
import java.util.UUID;

public class JDoubleAuctionTest {

public static void main( String[] args ) {
JDoubleAuction<Service> auction = JDoubleAuction.withUniformPricing(
LimitAskOrder$.MODULE$.<LimitAskOrder<Service>>ordering(),
LimitBidOrder$.MODULE$.<LimitBidOrder<Service>>ordering().reverse());

LimitBidOrder<Service> bid1 = LimitBidOrder$.MODULE$.apply(UUID.randomUUID(), 10.0, new Service());
LimitAskOrder<Service> ask1 = LimitAskOrder$.MODULE$.apply(UUID.randomUUID(), 5.0, new Service());
auction = auction.insert(bid1);
auction = auction.insert(ask1);
LimitBidOrder<Service> bid2 = LimitBidOrder$.MODULE$.apply(UUID.randomUUID(), 3.0, new Service());
LimitAskOrder<Service> ask2 = LimitAskOrder$.MODULE$.apply(UUID.randomUUID(), 12.0, new Service());
auction = auction.insert(bid2);
auction = auction.insert(ask2);
Optional<JDoubleAuction<Service>.ClearResult<Service>> clear = auction.clear(new WeightedAveragePricingRule<Service>(1.0));
if(clear.isPresent()) {
JDoubleAuction<Service>.ClearResult<Service> result = clear.get();
auction = result.getAuction();
result.getFills().forEach(fill -> System.out.println(fill));
}
}
}
@@ -0,0 +1,46 @@
// Copyright (c) 2017 Robert Bosch GmbH
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.economicsl.auctions.singleunit;

import org.economicsl.auctions.singleunit.orderbooks.FourHeapOrderBook;
import org.economicsl.auctions.singleunit.orderbooks.FourHeapOrderBook$;
import scala.Tuple2;
import scala.collection.JavaConverters;
import scala.collection.immutable.Stream;

import java.util.List;
import java.util.UUID;

public class OrderBookTest {
public static void main(String[] args) {
FourHeapOrderBook<Service> orderbook = FourHeapOrderBook$.MODULE$.empty(
LimitAskOrder$.MODULE$.<LimitAskOrder<Service>>ordering(),
LimitBidOrder$.MODULE$.<LimitBidOrder<Service>>ordering()
);

Service service = new Service();

LimitBidOrder<Service> bid1 = LimitBidOrder$.MODULE$.apply(UUID.randomUUID(), 5.0, service);
LimitAskOrder<Service> ask1 = LimitAskOrder$.MODULE$.apply(UUID.randomUUID(), 5.0, service);

orderbook = orderbook.$plus(bid1);
orderbook = orderbook.$plus(ask1);

Tuple2<Stream<Tuple2<LimitAskOrder<Service>, LimitBidOrder<Service>>>, FourHeapOrderBook<Service>> tuple = orderbook.takeAllMatched();
List<Tuple2<LimitAskOrder<Service>, LimitBidOrder<Service>>> matchedOrders = JavaConverters.seqAsJavaList(tuple._1());
matchedOrders.forEach(t -> System.out.println(t));
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/economicsl/auctions/singleunit/Service.java
@@ -0,0 +1,21 @@
// Copyright (c) 2017 Robert Bosch GmbH
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.economicsl.auctions.singleunit;

import org.economicsl.auctions.Tradable;

public class Service implements Tradable {
}

0 comments on commit 0ee8a0d

Please sign in to comment.