Skip to content

Commit 8feabb9

Browse files
committed
[btcturk] marketdata api implementation
1 parent a9fd3f5 commit 8feabb9

30 files changed

Lines changed: 19054 additions & 8 deletions

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<module>xchange-vaultoro</module>
130130
<module>xchange-vircurex</module>
131131
<module>xchange-yobit</module>
132+
<module>xchange-btcturk</module>
132133
</modules>
133134

134135
<repositories>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
The documentation for BTCTurk BTCTrader's white label exchange platform API.
2+
================================
3+
4+
Public API
5+
6+
7+
Documentation
8+
-------------
9+
https://github.com/BTCTrader/broker-api-docs
10+
11+
12+
Ticker
13+
------
14+
https://www.btcturk.com/api/ticker
15+
16+
17+
Orders
18+
------
19+
https://www.btcturk.com/api/orderbook?pairSymbol=BTCTRY
20+
21+
22+
Trades
23+
------
24+
https://www.btcturk.com/api/trades?pairSymbol=BTCTRY&last=COUNT (Max. value for count parameter is 500)
25+
26+
27+
REQUEST LIMITS
28+
------
29+
.../api/ticker requests are limited to 10 requests per 100 miliseconds.
30+
Other requests are limited to 1 request per 100 miliseconds.
31+
If you make more than 50 consequent unauthorized requests your IP address will be blocked
32+
33+
34+
Trading Fees
35+
------
36+
30 day trading volume in Turkish Lira | Maker Fee | Taker Fee
37+
0 - 100,000 | 0,20% (0.002) + KDV | 0,35% (0.0035) + KDV
38+
100,000 - 500,000 | 0,15% (0.0015) + KDV | 0,30% (0.003) + KDV
39+
500,000 + | 0,10% (0.001) + KDV | 0,20% (0.002) + KDV
40+
41+
42+
Testing
43+
------
44+
You can use the testing platform to test the APIs. The balances on the test sites are not real and do not represent any real value. The testing platforms work on Bitcoin TESTNET and you can deposit TESTNET coins to your account. The testing platforms are:
45+
46+
[BTCTurk Test] (https://btctrader-broker-btcturk.azurewebsites.net/)
47+
Important Our mobile applications will not work with the testing platforms. They can only be synced with our live platforms.

xchange-btcturk/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>xchange-parent</artifactId>
7+
<groupId>org.knowm.xchange</groupId>
8+
<version>4.3.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>xchange-btcturk</artifactId>
13+
14+
<name>XChange BTC Turk</name>
15+
<description>XChange implementation for the BTC Turk Exchange</description>
16+
<url>http://knowm.org/open-source/xchange/</url>
17+
<inceptionYear>2012</inceptionYear>
18+
19+
<organization>
20+
<name>Knowm Inc.</name>
21+
<url>http://knowm.org/open-source/xchange/</url>
22+
</organization>
23+
24+
<developers>
25+
<developer>
26+
<name>Semih Unaldi</name>
27+
<email>semihunaldi@gmail.com</email>
28+
</developer>
29+
</developers>
30+
31+
<!-- Parent provides default configuration for dependencies -->
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.knowm.xchange</groupId>
35+
<artifactId>xchange-core</artifactId>
36+
<version>4.3.1-SNAPSHOT</version>
37+
</dependency>
38+
</dependencies>
39+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.knowm.xchange.btcturk;
2+
3+
import java.io.IOException;
4+
import java.util.Objects;
5+
6+
import javax.ws.rs.GET;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.Produces;
9+
import javax.ws.rs.QueryParam;
10+
import javax.ws.rs.core.MediaType;
11+
12+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkOHLC;
13+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkOrderBook;
14+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkTicker;
15+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkTrade;
16+
import org.knowm.xchange.currency.CurrencyPair;
17+
import org.knowm.xchange.utils.jackson.CurrencyPairDeserializer;
18+
19+
/**
20+
* @author semihunaldi
21+
*/
22+
23+
@Path("api/")
24+
@Produces(MediaType.APPLICATION_JSON)
25+
public interface BTCTurk {
26+
27+
@GET
28+
@Path("ticker/")
29+
BTCTurkTicker getTicker(@QueryParam("pairSymbol") Pair pairSymbol) throws IOException;
30+
31+
@GET
32+
@Path("orderbook/")
33+
BTCTurkOrderBook getOrderBook(@QueryParam("pairSymbol") Pair pairSymbol) throws IOException;
34+
35+
@GET
36+
@Path("trades/")
37+
BTCTurkTrade[] getTrades(@QueryParam("pairSymbol") Pair pairSymbol, @QueryParam("last") Integer last) throws IOException;
38+
39+
@GET
40+
@Path("ohlcdata/")
41+
BTCTurkOHLC[] getOHLC(@QueryParam("pairSymbol") Pair pairSymbol, @QueryParam("last") Integer last) throws IOException;
42+
43+
class Pair {
44+
public final CurrencyPair pair;
45+
46+
public Pair(CurrencyPair pair) {
47+
this.pair = pair;
48+
}
49+
50+
public Pair(String pair) {
51+
this(CurrencyPairDeserializer.getCurrencyPairFromString(pair));
52+
}
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
return this == o || !(o == null || getClass() != o.getClass()) && Objects.equals(pair, ((Pair) o).pair);
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return Objects.hash(pair);
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return pair == null ? "" : String.format("%s%s", pair.base.getCurrencyCode(), pair.counter.getCurrencyCode());
67+
}
68+
}
69+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package org.knowm.xchange.btcturk;
2+
3+
import java.math.BigDecimal;
4+
import java.text.MessageFormat;
5+
import java.util.ArrayList;
6+
import java.util.Date;
7+
import java.util.List;
8+
9+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkOrderBook;
10+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkTicker;
11+
import org.knowm.xchange.btcturk.dto.marketdata.BTCTurkTrade;
12+
import org.knowm.xchange.currency.CurrencyPair;
13+
import org.knowm.xchange.dto.Order;
14+
import org.knowm.xchange.dto.marketdata.OrderBook;
15+
import org.knowm.xchange.dto.marketdata.Ticker;
16+
import org.knowm.xchange.dto.marketdata.Trade;
17+
import org.knowm.xchange.dto.marketdata.Trades;
18+
import org.knowm.xchange.dto.trade.LimitOrder;
19+
import org.knowm.xchange.utils.DateUtils;
20+
21+
/**
22+
* @author semihunaldi
23+
* Various adapters for converting from BTCTurk DTOs to XChange DTOs
24+
*/
25+
public final class BTCTurkAdapters {
26+
27+
private BTCTurkAdapters() {
28+
}
29+
30+
/**
31+
* Adapts a BTCTurkTicker to a Ticker Object
32+
*
33+
* @param btcTurkTicker The exchange specific ticker
34+
* @return The ticker
35+
*/
36+
public static Ticker adaptTicker(BTCTurkTicker btcTurkTicker) {
37+
BTCTurk.Pair pair = btcTurkTicker.getPair();
38+
BigDecimal high = btcTurkTicker.getHigh();
39+
BigDecimal last = btcTurkTicker.getLast();
40+
Date timestamp = new Date(btcTurkTicker.getTimestamp());
41+
BigDecimal bid = btcTurkTicker.getBid();
42+
BigDecimal volume = btcTurkTicker.getVolume();
43+
BigDecimal low = btcTurkTicker.getLow();
44+
BigDecimal ask = btcTurkTicker.getAsk();
45+
BigDecimal open = btcTurkTicker.getOpen();
46+
BigDecimal average = btcTurkTicker.getAverage();
47+
48+
return new Ticker.Builder()
49+
.currencyPair(pair != null ? pair.pair : null)
50+
.last(last)
51+
.bid(bid)
52+
.ask(ask)
53+
.high(high)
54+
.low(low)
55+
.vwap(average)
56+
.open(open)
57+
.volume(volume)
58+
.timestamp(timestamp).build();
59+
}
60+
61+
/**
62+
* Adapts a BTCTurkTrade[] to a Trades Object
63+
*
64+
* @param btcTurkTrades The BTCTurk trades
65+
* @param currencyPair (e.g. BTC/TRY)
66+
* @return The XChange Trades
67+
*/
68+
public static Trades adaptTrades(BTCTurkTrade[] btcTurkTrades, CurrencyPair currencyPair) {
69+
List<Trade> trades = new ArrayList<>();
70+
int lastTradeId = 0;
71+
for (BTCTurkTrade btcTurkTrade : btcTurkTrades) {
72+
if (btcTurkTrade.getTid() > lastTradeId) {
73+
lastTradeId = btcTurkTrade.getTid();
74+
}
75+
trades.add(adaptTrade(btcTurkTrade, currencyPair, 1));
76+
}
77+
return new Trades(trades, lastTradeId, Trades.TradeSortType.SortByID);
78+
}
79+
80+
/**
81+
* Adapts a BTCTurkTrade to a Trade Object
82+
*
83+
* @param btcTurkTrade The BTCTurkTrade trade
84+
* @param currencyPair (e.g. BTC/TRY)
85+
* @param timeScale polled order books provide a timestamp in seconds, stream in ms
86+
* @return The XChange Trade
87+
*/
88+
public static Trade adaptTrade(BTCTurkTrade btcTurkTrade, CurrencyPair currencyPair, int timeScale) {
89+
90+
final String tradeId = String.valueOf(btcTurkTrade.getTid());
91+
Date date = DateUtils.fromMillisUtc(btcTurkTrade.getDate() * timeScale);// polled order books provide a timestamp in seconds, stream in ms
92+
return new Trade(null, btcTurkTrade.getAmount(), currencyPair, btcTurkTrade.getPrice(), date, tradeId);
93+
}
94+
95+
/**
96+
* Adapts org.knowm.xchange.btcturk.dto.marketdata.BTCTurkOrderBook to a OrderBook Object
97+
*
98+
* @param currencyPair (e.g. BTC/TRY)
99+
* @return The XChange OrderBook
100+
*/
101+
public static OrderBook adaptOrderBook(BTCTurkOrderBook btcTurkOrderBook, CurrencyPair currencyPair) {
102+
List<LimitOrder> asks = createOrders(currencyPair, Order.OrderType.ASK, btcTurkOrderBook.getAsks());
103+
List<LimitOrder> bids = createOrders(currencyPair, Order.OrderType.BID, btcTurkOrderBook.getBids());
104+
return new OrderBook(btcTurkOrderBook.getTimestamp(), asks, bids);
105+
}
106+
107+
public static List<LimitOrder> createOrders(CurrencyPair currencyPair, Order.OrderType orderType, List<List<BigDecimal>> orders) {
108+
List<LimitOrder> limitOrders = new ArrayList<>();
109+
for (List<BigDecimal> ask : orders) {
110+
checkArgument(ask.size() == 2, "Expected a pair (price, amount) but got {0} elements.", ask.size());
111+
limitOrders.add(createOrder(currencyPair, ask, orderType));
112+
}
113+
return limitOrders;
114+
}
115+
116+
public static LimitOrder createOrder(CurrencyPair currencyPair, List<BigDecimal> priceAndAmount, Order.OrderType orderType) {
117+
118+
return new LimitOrder(orderType, priceAndAmount.get(1), currencyPair, "", null, priceAndAmount.get(0));
119+
}
120+
121+
public static void checkArgument(boolean argument, String msgPattern, Object... msgArgs) {
122+
if (!argument) {
123+
throw new IllegalArgumentException(MessageFormat.format(msgPattern, msgArgs));
124+
}
125+
}
126+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.knowm.xchange.btcturk;
2+
3+
import org.knowm.xchange.BaseExchange;
4+
import org.knowm.xchange.Exchange;
5+
import org.knowm.xchange.ExchangeSpecification;
6+
import org.knowm.xchange.btcturk.service.BTCTurkMarketDataService;
7+
import org.knowm.xchange.utils.nonce.CurrentTimeNonceFactory;
8+
9+
import si.mazi.rescu.SynchronizedValueFactory;
10+
11+
/**
12+
* @author semihunaldi
13+
*/
14+
public class BTCTurkExchange extends BaseExchange implements Exchange {
15+
16+
private SynchronizedValueFactory<Long> nonceFactory = new CurrentTimeNonceFactory();
17+
18+
@Override
19+
protected void initServices() {
20+
21+
this.marketDataService = new BTCTurkMarketDataService(this);
22+
}
23+
24+
@Override
25+
public ExchangeSpecification getDefaultExchangeSpecification() {
26+
27+
ExchangeSpecification exchangeSpecification = new ExchangeSpecification(this.getClass().getCanonicalName());
28+
exchangeSpecification.setSslUri("https://www.btcturk.com");
29+
exchangeSpecification.setHost("www.btcturk.com");
30+
exchangeSpecification.setPort(80);
31+
exchangeSpecification.setExchangeName("BTCTurk");
32+
exchangeSpecification.setExchangeDescription("BTCTurk is a Bitcoin & Etherium exchange registered in Turkey.");
33+
return exchangeSpecification;
34+
}
35+
36+
@Override
37+
public SynchronizedValueFactory<Long> getNonceFactory() {
38+
return nonceFactory;
39+
}
40+
}

0 commit comments

Comments
 (0)