Skip to content

Commit

Permalink
[CCEX] Implemented all API-s!
Browse files Browse the repository at this point in the history
  • Loading branch information
atlet committed Nov 10, 2016
1 parent 616b3f4 commit e6974d3
Show file tree
Hide file tree
Showing 23 changed files with 1,249 additions and 11 deletions.
5 changes: 5 additions & 0 deletions xchange-ccex/src/main/java/org/knowm/xchange/ccex/CCEX.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.knowm.xchange.ccex.dto.marketdata.CCEXMarkets;
import org.knowm.xchange.ccex.dto.marketdata.CCEXTrades;
import org.knowm.xchange.ccex.dto.ticker.CCEXPairs;
import org.knowm.xchange.ccex.dto.ticker.CCEXTickerResponse;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.utils.jackson.CurrencyPairDeserializer;

Expand All @@ -27,6 +28,10 @@ public interface CCEX {
@GET
@Path("pairs.json")
CCEXPairs getPairs() throws IOException;

@GET
@Path("{lpair}-{rpair}.json")
CCEXTickerResponse getTicker(@PathParam("lpair") String lpair, @PathParam("rpair") String rpair) throws IOException;

/**
* Returns "bids" and "asks". Each is a list of open orders and each order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@
import org.knowm.xchange.ccex.dto.marketdata.CCEXMarket;
import org.knowm.xchange.ccex.dto.marketdata.CCEXTrade;
import org.knowm.xchange.ccex.dto.marketdata.CCEXTrades;
import org.knowm.xchange.ccex.dto.ticker.CCEXPriceResponse;
import org.knowm.xchange.ccex.dto.trade.CCEXOpenorder;
import org.knowm.xchange.ccex.dto.trade.CCEXOrderhistory;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.account.Balance;
import org.knowm.xchange.dto.account.Wallet;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trade;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.dto.marketdata.Trades.TradeSortType;
import org.knowm.xchange.dto.meta.CurrencyMetaData;
import org.knowm.xchange.dto.meta.CurrencyPairMetaData;
import org.knowm.xchange.dto.meta.ExchangeMetaData;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.UserTrade;

public class CCEXAdapters {

Expand Down Expand Up @@ -142,4 +147,70 @@ public static Wallet adaptWallet(List<CCEXBalance> balances) {

return new Wallet(wallets);
}

public static List<LimitOrder> adaptOpenOrders(List<CCEXOpenorder> cCexOpenOrders) {

List<LimitOrder> openOrders = new ArrayList<LimitOrder>();

for (CCEXOpenorder order : cCexOpenOrders) {
openOrders.add(adaptOpenOrder(order));
}

return openOrders;
}

public static LimitOrder adaptOpenOrder(CCEXOpenorder cCEXOpenOrder) {

OrderType type = cCEXOpenOrder.getOrderType().equalsIgnoreCase("LIMIT_SELL") ? OrderType.ASK : OrderType.BID;
String[] currencies = cCEXOpenOrder.getExchange().split("-");
CurrencyPair pair = new CurrencyPair(currencies[1], currencies[0]);

return new LimitOrder(type, cCEXOpenOrder.getQuantityRemaining(), pair, cCEXOpenOrder.getOrderUuid(), null,
cCEXOpenOrder.getLimit());
}

public static List<UserTrade> adaptUserTrades(List<CCEXOrderhistory> cCEXOrderhistory) {

List<UserTrade> trades = new ArrayList<UserTrade>();

for (CCEXOrderhistory cCEXTrade : cCEXOrderhistory) {
trades.add(adaptUserTrade(cCEXTrade));
}
return trades;
}

public static UserTrade adaptUserTrade(CCEXOrderhistory trade) {

String[] currencies = trade.getExchange().split("-");
CurrencyPair currencyPair = new CurrencyPair(currencies[1], currencies[0]);

OrderType orderType = trade.getOrderType().equalsIgnoreCase("LIMIT_BUY") ? OrderType.BID : OrderType.ASK;
BigDecimal amount = trade.getQuantity().subtract(trade.getQuantityRemaining());
Date date = CCEXUtils.toDate(trade.getTimeStamp());
String orderId = String.valueOf(trade.getOrderUuid());

BigDecimal price = trade.getPricePerUnit();

if (price == null) {
price = trade.getLimit();
}

return new UserTrade(orderType, amount, currencyPair, price, date, orderId, orderId, trade.getCommission(),
currencyPair.counter);
}

public static Ticker adaptTicker(CCEXPriceResponse cCEXTicker, CurrencyPair currencyPair) {

BigDecimal last = cCEXTicker.getLastbuy();
BigDecimal bid = cCEXTicker.getBuy();
BigDecimal ask = cCEXTicker.getSell();
BigDecimal high = cCEXTicker.getHigh();
BigDecimal low = cCEXTicker.getLow();
BigDecimal volume = cCEXTicker.getBuysupport();

Date timestamp = new Date(cCEXTicker.getUpdated());

return new Ticker.Builder().currencyPair(currencyPair).last(last).bid(bid).ask(ask).high(high).low(low)
.volume(volume).timestamp(timestamp).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

import org.knowm.xchange.ccex.dto.account.CCEXBalanceResponse;
import org.knowm.xchange.ccex.dto.account.CCEXBalancesResponse;
import org.knowm.xchange.ccex.dto.trade.CCEXBuySellLimitResponse;
import org.knowm.xchange.ccex.dto.trade.CCEXCancelResponse;
import org.knowm.xchange.ccex.dto.trade.CCEXGetopenordersResponse;
import org.knowm.xchange.ccex.dto.trade.CCEXGetorderhistoryResponse;

import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.SynchronizedValueFactory;
Expand All @@ -27,4 +31,23 @@ public interface CCEXAuthenticated extends CCEX {
@Path("api.html?a=getbalance&apikey={apikey}&nonce={nonce}&currency={currency}")
CCEXBalanceResponse getdepositaddress(@PathParam("apikey") String apikey, @HeaderParam("apisign") ParamsDigest signature, @PathParam("nonce") SynchronizedValueFactory<Long> nonce, @PathParam("currency") String currency) throws IOException;

@GET
@Path("api.html?a=getopenorders&apikey={apikey}&nonce={nonce}")
CCEXGetopenordersResponse getopenorders(@PathParam("apikey") String apikey, @HeaderParam("apisign") ParamsDigest signature, @PathParam("nonce") SynchronizedValueFactory<Long> nonce) throws IOException;

@GET
@Path("api.html?a=getorderhistory&apikey={apikey}&nonce={nonce}")
CCEXGetorderhistoryResponse getorderhistory(@PathParam("apikey") String apikey, @HeaderParam("apisign") ParamsDigest signature, @PathParam("nonce") SynchronizedValueFactory<Long> nonce) throws IOException;

@GET
@Path("api.html?a=cancel&apikey={apikey}&nonce={nonce}&uuid={uuid}")
CCEXCancelResponse cancel(@PathParam("apikey") String apikey, @HeaderParam("apisign") ParamsDigest signature, @PathParam("nonce") SynchronizedValueFactory<Long> nonce, @PathParam("uuid") String uuid) throws IOException;

@GET
@Path("api.html?a=buylimit&apikey={apikey}&nonce={nonce}&market={lmarket}-{rmarket}&quantity={quantity}&rate={rate}")
CCEXBuySellLimitResponse buylimit(@PathParam("apikey") String apikey, @HeaderParam("apisign") ParamsDigest signature, @PathParam("nonce") SynchronizedValueFactory<Long> nonce, @PathParam("lmarket") String lmarket, @PathParam("rmarket") String rmarket, @PathParam("quantity") String quantity, @PathParam("rate") String rate) throws IOException;

@GET
@Path("api.html?a=selllimit&apikey={apikey}&nonce={nonce}&market={lmarket}-{rmarket}&quantity={quantity}&rate={rate}")
CCEXBuySellLimitResponse selllimit(@PathParam("apikey") String apikey, @HeaderParam("apisign") ParamsDigest signature, @PathParam("nonce") SynchronizedValueFactory<Long> nonce, @PathParam("lmarket") String lmarket, @PathParam("rmarket") String rmarket, @PathParam("quantity") String quantity, @PathParam("rate") String rate) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.knowm.xchange.ccex.service.pooling.CCEXAccountService;
import org.knowm.xchange.ccex.service.pooling.CCEXMarketDataService;
import org.knowm.xchange.ccex.service.pooling.CCEXMarketDataServiceRaw;
import org.knowm.xchange.ccex.service.pooling.CCEXTradeService;
import org.knowm.xchange.utils.nonce.CurrentTimeNonceFactory;

import si.mazi.rescu.SynchronizedValueFactory;
Expand Down Expand Up @@ -38,7 +39,7 @@ public ExchangeSpecification getDefaultExchangeSpecification() {
@Override
protected void initServices() {
this.pollingMarketDataService = new CCEXMarketDataService(this);
this.pollingTradeService = null;
this.pollingTradeService = new CCEXTradeService(this);
this.pollingAccountService = new CCEXAccountService(this);
}

Expand Down
37 changes: 37 additions & 0 deletions xchange-ccex/src/main/java/org/knowm/xchange/ccex/CCEXUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.knowm.xchange.ccex;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.knowm.xchange.currency.CurrencyPair;

public class CCEXUtils {

private static final Date EPOCH = new Date(0);

private CCEXUtils() {

}

public static String toPairString(CurrencyPair currencyPair) {

return currencyPair.counter.getCurrencyCode().toLowerCase() + "-"
+ currencyPair.base.getCurrencyCode().toLowerCase();
}

public static Date toDate(String datetime) {
SimpleDateFormat sdf;

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

try {
return sdf.parse(datetime);
} catch (ParseException e) {
return EPOCH;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.knowm.xchange.ccex.dto.ticker;

import java.math.BigDecimal;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CCEXPriceResponse {

private BigDecimal high;
private BigDecimal low;
private BigDecimal avg;
private BigDecimal lastbuy;
private BigDecimal lastsell;
private BigDecimal buy;
private BigDecimal sell;
private BigDecimal lastprice;
private BigDecimal buysupport;
private int updated;

public CCEXPriceResponse(
@JsonProperty("high") BigDecimal high,
@JsonProperty("low") BigDecimal low,
@JsonProperty("avg") BigDecimal avg,
@JsonProperty("lastbuy") BigDecimal lastbuy,
@JsonProperty("lastsell") BigDecimal lastsell,
@JsonProperty("buy") BigDecimal buy,
@JsonProperty("sell") BigDecimal sell,
@JsonProperty("lastprice") BigDecimal lastprice,
@JsonProperty("buysupport") BigDecimal buysupport,
@JsonProperty("updated") int updated) {
super();
this.high = high;
this.low = low;
this.avg = avg;
this.lastbuy = lastbuy;
this.lastsell = lastsell;
this.buy = buy;
this.sell = sell;
this.lastprice = lastprice;
this.buysupport = buysupport;
this.updated = updated;
}

public BigDecimal getHigh() {
return high;
}

public void setHigh(BigDecimal high) {
this.high = high;
}

public BigDecimal getLow() {
return low;
}

public void setLow(BigDecimal low) {
this.low = low;
}

public BigDecimal getAvg() {
return avg;
}

public void setAvg(BigDecimal avg) {
this.avg = avg;
}

public BigDecimal getLastbuy() {
return lastbuy;
}

public void setLastbuy(BigDecimal lastbuy) {
this.lastbuy = lastbuy;
}

public BigDecimal getLastsell() {
return lastsell;
}

public void setLastsell(BigDecimal lastsell) {
this.lastsell = lastsell;
}

public BigDecimal getBuy() {
return buy;
}

public void setBuy(BigDecimal buy) {
this.buy = buy;
}

public BigDecimal getSell() {
return sell;
}

public void setSell(BigDecimal sell) {
this.sell = sell;
}

public BigDecimal getLastprice() {
return lastprice;
}

public void setLastprice(BigDecimal lastprice) {
this.lastprice = lastprice;
}

public BigDecimal getBuysupport() {
return buysupport;
}

public void setBuysupport(BigDecimal buysupport) {
this.buysupport = buysupport;
}

public int getUpdated() {
return updated;
}

public void setUpdated(int updated) {
this.updated = updated;
}

@Override
public String toString() {
return "CCEXPriceResponse [high=" + high + ", low=" + low + ", avg=" + avg + ", lastbuy=" + lastbuy
+ ", lastsell=" + lastsell + ", buy=" + buy + ", sell=" + sell + ", lastprice=" + lastprice
+ ", buysupport=" + buysupport + ", updated=" + updated + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.knowm.xchange.ccex.dto.ticker;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CCEXTickerResponse {

private CCEXPriceResponse ticker;

public CCEXTickerResponse(@JsonProperty("ticker") CCEXPriceResponse ticker) {
super();
this.ticker = ticker;
}

public CCEXPriceResponse getTicker() {
return ticker;
}

public void setTicker(CCEXPriceResponse ticker) {
this.ticker = ticker;
}

@Override
public String toString() {
return "CCEXTickerResponse [ticker=" + ticker + "]";
}
}
Loading

0 comments on commit e6974d3

Please sign in to comment.