Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of BTC Central API #419

Merged
merged 6 commits into from
May 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<module>xchange-bitfinex</module>
<module>xchange-bitstamp</module>
<module>xchange-blockchain</module>
<module>xchange-btccentral</module>
<module>xchange-btcchina</module>
<module>xchange-btce</module>
<module>xchange-bter</module>
Expand Down
2 changes: 2 additions & 0 deletions xchange-btccentral/api-specification.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/Paymium/api-documentation

35 changes: 35 additions & 0 deletions xchange-btccentral/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.xeiam.xchange</groupId>
<artifactId>xchange-parent</artifactId>
<version>2.0.1-SNAPSHOT</version>
</parent>

<artifactId>xchange-btccentral</artifactId>

<name>XChange Bitcoin Central</name>
<description>XChange implementation for Bitcoin Central exchange</description>

<url>http://xeiam.com</url>
<inceptionYear>2012</inceptionYear>

<organization>
<name>Xeiam, LLC</name>
<url>http://xeiam.com</url>
</organization>

<!-- Parent provides default configuration for dependencies -->
<dependencies>

<dependency>
<groupId>com.xeiam.xchange</groupId>
<artifactId>xchange-core</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>

</dependencies>

</project>
37 changes: 37 additions & 0 deletions xchange-btccentral/src/main/java/com/xeiam/xchange/BTCCentral.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.xeiam.xchange;

import com.xeiam.xchange.dto.marketdata.BTCCentralMarketDepth;
import com.xeiam.xchange.dto.marketdata.BTCCentralTicker;
import com.xeiam.xchange.dto.marketdata.BTCCentralTrade;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.IOException;

/**
* @author kpysniak
*/
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public interface BTCCentral {


/**
*
* @return BTCCentral ticker
* @throws IOException
*/
@GET
@Path("ticker/")
public BTCCentralTicker getBTCCentralTicker() throws IOException;

@GET
@Path("depth/")
public BTCCentralMarketDepth getOrderBook() throws IOException;

@GET
@Path("trades/")
public BTCCentralTrade[] getTrades() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.xeiam.xchange;

import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.Order.OrderType;
import com.xeiam.xchange.dto.marketdata.*;
import com.xeiam.xchange.dto.trade.LimitOrder;
import com.xeiam.xchange.utils.DateUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author kpysniak
*/
public class BTCCentralAdapters {

/**
* Singleton
*/
private BTCCentralAdapters() { }

/**
* Adapts a BTCCentralTicker to a Ticker Object
*
* @param btcCentralTicker The exchange specific ticker
* @param currencyPair (e.g. BTC/USD)
* @return The ticker
*/
public static Ticker adaptTicker(BTCCentralTicker btcCentralTicker, CurrencyPair currencyPair) {

BigDecimal bid = btcCentralTicker.getBid();
BigDecimal ask = btcCentralTicker.getAsk();
BigDecimal high = btcCentralTicker.getHigh();
BigDecimal low = btcCentralTicker.getLow();
BigDecimal volume = btcCentralTicker.getVolume();
Date timestamp = new Date(btcCentralTicker.getAt() * 1000L);

return Ticker.TickerBuilder.newInstance().withCurrencyPair(currencyPair)
.withBid(bid).withAsk(ask).withHigh(high).withLow(low).withVolume(volume)
.withTimestamp(timestamp).build();
}

/**
*
* @param marketDepth
* @param currencyPair
* @return new order book
*/
public static OrderBook adaptMarketDepth(BTCCentralMarketDepth marketDepth, CurrencyPair currencyPair) {
List<LimitOrder> asks = adaptMarketOrderToLimitOrder(marketDepth.getAsks(), OrderType.ASK, currencyPair);
List<LimitOrder> bids = adaptMarketOrderToLimitOrder(marketDepth.getBids(), OrderType.BID, currencyPair);

// TODO
// What timestamp should be used? Latest/Earliest/Current one?
return new OrderBook(new Date(), asks, bids);
}

/**
*
* @param btcCentralMarketOrders
* @param orderType
* @param currencyPair
*/
private static List<LimitOrder> adaptMarketOrderToLimitOrder(BTCCentralMarketOrder[] btcCentralMarketOrders,
OrderType orderType, CurrencyPair currencyPair) {

List<LimitOrder> orders = new ArrayList<LimitOrder>(btcCentralMarketOrders.length);

for (BTCCentralMarketOrder btcCentralMarketOrder : btcCentralMarketOrders) {
LimitOrder limitOrder = new LimitOrder(orderType, btcCentralMarketOrder.getAmount(), currencyPair, null, new Date(btcCentralMarketOrder.getTimestamp()), btcCentralMarketOrder.getPrice());
orders.add(limitOrder);
}

return orders;
}

public static Trades adaptTrade(BTCCentralTrade[] btcCentralTrades, CurrencyPair currencyPair) {
List<Trade> trades = new ArrayList<Trade>();

for (BTCCentralTrade btcCentralTrade : btcCentralTrades) {
Trade trade = new Trade(null, btcCentralTrade.getTraded_btc(), currencyPair,
btcCentralTrade.getPrice(), new Date(btcCentralTrade.getCreated_at_int()),
btcCentralTrade.getUuid().toString(), btcCentralTrade.getUuid().toString());

trades.add(trade);
}

return new Trades(trades, Trades.TradeSortType.SortByTimestamp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.xeiam.xchange;

import com.xeiam.xchange.service.polling.BTCCentralMarketDataService;

/**
* @author kpysniak
*/
public class BTCCentralExchange extends BaseExchange implements Exchange {

@Override
public ExchangeSpecification getDefaultExchangeSpecification() {
ExchangeSpecification exchangeSpecification = new ExchangeSpecification(this.getClass().getCanonicalName());
exchangeSpecification.setSslUri("https://bitcoin-central.net/api/v1/data/eur");
exchangeSpecification.setHost("bitcoin-central.net");
exchangeSpecification.setPort(80);
exchangeSpecification.setExchangeName("Bitcoin-Central");
exchangeSpecification.setExchangeDescription("Bitcoin-Central is a Bitcoin exchange registered and maintained by a company based in Paris, France.");

return exchangeSpecification;
}

@Override
public void applySpecification(ExchangeSpecification exchangeSpecification) {

super.applySpecification(exchangeSpecification);
this.pollingMarketDataService = new BTCCentralMarketDataService(exchangeSpecification);
this.pollingTradeService = null;
this.pollingAccountService = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.xeiam.xchange.dto;

/**
* @author kpysniak
*/
public abstract class BTCCentralBaseResponse {

private final String error;

protected BTCCentralBaseResponse(String error) {

this.error = error;
}

public String getError() {

return error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.xeiam.xchange.dto.marketdata;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Arrays;

/**
* @author kpysniak
*/
public class BTCCentralMarketDepth {

private final BTCCentralMarketOrder[] bids;
private final BTCCentralMarketOrder[] asks;

/**
*
* @param bids
* @param asks
*/
public BTCCentralMarketDepth(@JsonProperty("bids") BTCCentralMarketOrder[] bids,
@JsonProperty("asks") BTCCentralMarketOrder[] asks) {
this.bids = bids;
this.asks = asks;
}

public BTCCentralMarketOrder[] getBids() {
return bids;
}

public BTCCentralMarketOrder[] getAsks() {
return asks;
}

@Override
public String toString() {
return "BTCCentralMarketDepth{" +
"bids=" + Arrays.toString(bids) +
", asks=" + Arrays.toString(asks) +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.xeiam.xchange.dto.marketdata;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;

/**
* @author kpysniak
*/
public class BTCCentralMarketOrder {

private final String currency;
private final long timestamp;
private final BigDecimal price;
private final BigDecimal amount;

public BTCCentralMarketOrder(@JsonProperty("currency") String currency,
@JsonProperty("timestamp") long timestamp,
@JsonProperty("price") BigDecimal price,
@JsonProperty("amount") BigDecimal amount) {
this.currency = currency;
this.timestamp = timestamp;
this.price = price;
this.amount = amount;
}

public String getCurrency() {
return currency;
}

public long getTimestamp() {
return timestamp;
}

public BigDecimal getPrice() {
return price;
}

public BigDecimal getAmount() {
return amount;
}

@Override
public String toString() {
return "BTCCentralMarketOrder{" +
"currency='" + currency + '\'' +
", timestamp=" + timestamp +
", price=" + price +
", amount=" + amount +
'}';
}
}
Loading