Skip to content

Commit

Permalink
initial orderbook logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCookieLab committed Mar 14, 2018
1 parent 27469df commit 084eba6
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/cf/client/WSSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ protected void initChannel(SocketChannel ch) {
while (router.isRunning() == true && (startTime + runTimeInMillis > System.currentTimeMillis())) {
TimeUnit.MINUTES.sleep(1);
}

throw new InterruptedException("Runtime exceeded");
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cf.client.poloniex.wss.model;

import com.google.gson.Gson;
import java.util.Map;
import java.util.TreeMap;

/**
*
* @author David
*/
public class PoloniexOrderBook {

public final Map<String, PoloniexOrderBookEntry> bids;
public final Map<String, PoloniexOrderBookEntry> asks;

public PoloniexOrderBook() {
this.bids = new TreeMap<>();
this.asks = new TreeMap<>();
}

public PoloniexOrderBook(Map<String, PoloniexOrderBookEntry> bids, Map<String, PoloniexOrderBookEntry> asks) {
this.bids = bids;
this.asks = asks;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cf.client.poloniex.wss.model;

import com.google.gson.Gson;
import java.math.BigDecimal;

/**
*
* @author David
*/
public class PoloniexOrderBookEntry {

public final String type;
public final BigDecimal rate;
public final BigDecimal amount;

public PoloniexOrderBookEntry(String type, BigDecimal rate, BigDecimal amount) {
this.type = type;
this.rate = rate;
this.amount = amount;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cf.client.poloniex.wss.model;

import com.google.gson.Gson;

/**
*
* @author David
*/
public class PoloniexWSSOrderBookUpdate {

public final Double currencyPair;
public final Double orderNumber;
public final PoloniexOrderBookEntry previousEntry;
public final PoloniexWSSOrderBookUpdate replacementEntry;

public PoloniexWSSOrderBookUpdate(Double currencyPair, Double orderNumber, PoloniexOrderBookEntry previousEntry, PoloniexWSSOrderBookUpdate replacementEntry) {
this.currencyPair = currencyPair;
this.orderNumber = orderNumber;
this.previousEntry = previousEntry;
this.replacementEntry = replacementEntry;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/cf/client/wss/handler/NoOpMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package com.cf.client.wss.handler;

/**
*
* @author David
*/
public class NoOpMessageHandler implements IMessageHandler {

@Override
public void handle(String message) {
// do nothing
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cf.client.wss.handler;

import com.cf.client.poloniex.wss.model.PolonicexWSSOrderBook;
import com.cf.client.poloniex.wss.model.PoloniexWSSOrderBookUpdate;
import com.google.gson.Gson;
import java.util.List;
import org.apache.logging.log4j.LogManager;
Expand All @@ -17,12 +17,11 @@ public class OrderBookMessageHandler implements IMessageHandler {

@Override
public void handle(String message) {
LOG.trace(message);
PolonicexWSSOrderBook orderBook = this.mapMessageToPoloniexOrderBook(message);
LOG.trace(orderBook);
PoloniexWSSOrderBookUpdate orderBook = this.mapMessageToPoloniexOrderBook(message);
LOG.debug(orderBook);
}

protected PolonicexWSSOrderBook mapMessageToPoloniexOrderBook(String message) {
protected PoloniexWSSOrderBookUpdate mapMessageToPoloniexOrderBook(String message) {
List results = GSON.fromJson(message, List.class);
if (results.size() < 3) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public class TickerMessageHandler implements IMessageHandler {

@Override
public void handle(String message) {
LOG.trace(message);
PoloniexWSSTicker ticker = this.mapMessageToPoloniexTicker(message);
LOG.trace(ticker);
LOG.debug(ticker);

}

Expand Down
32 changes: 14 additions & 18 deletions src/main/java/com/cf/example/PoloniexWSSClientExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,37 @@

import com.cf.client.WSSClient;
import com.cf.client.poloniex.wss.model.PoloniexWSSSubscription;
import com.cf.client.wss.handler.LoggerMessageHandler;
import com.cf.client.wss.handler.OrderBookMessageHandler;
import com.cf.client.wss.handler.TickerMessageHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
*
* @author David
*/
public class PoloniexWSSClientExample
{
public class PoloniexWSSClientExample {

private static final Logger LOG = LogManager.getLogger(PoloniexWSSClientExample.class);
private static final String ENDPOINT_URL = "wss://api2.poloniex.com";

public static void main(String[] args)
{
try
{
new PoloniexWSSClientExample().run();
}
catch (Exception ex)
{
public static void main(String[] args) {
try {
new PoloniexWSSClientExample().subscribe();
} catch (InterruptedException ex) {
LOG.info(ex.getMessage());
System.exit(0);
} catch (Exception ex) {
LOG.fatal("An exception occurred when running PoloniexWSSClientExample - {}", ex.getMessage());
System.exit(-1);
}
}

public void run() throws Exception
{
try (WSSClient wssClient = new WSSClient(ENDPOINT_URL))
{
public void subscribe() throws Exception {
try (WSSClient wssClient = new WSSClient(ENDPOINT_URL)) {
wssClient.addSubscription(PoloniexWSSSubscription.USDT_ETH, new OrderBookMessageHandler());
wssClient.addSubscription(PoloniexWSSSubscription.TICKER, new TickerMessageHandler());
wssClient.run(20000);
//wssClient.addSubscription(PoloniexWSSSubscription.TICKER, new TickerMessageHandler());
wssClient.run(60000);
}

}
}

0 comments on commit 084eba6

Please sign in to comment.