Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
#11 klines and aggTrades fix and test coverage with options
Browse files Browse the repository at this point in the history
  • Loading branch information
cutwebapps committed Jan 13, 2018
1 parent e3c4285 commit 6ac18ec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/webcerebrium/binance/api/BinanceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public List<BinanceAggregatedTrades> aggTrades(BinanceSymbol symbol, int limit,
String u = baseUrl + "v1/aggTrades?symbol=" + symbol.get() + "&limit=" + limit;
if (options != null) {
for (String optionKey : options.keySet()) {
if (!optionKey.equals("fromId") ||
!optionKey.equals("startTime") ||
if (!optionKey.equals("fromId") &&
!optionKey.equals("startTime") &&
!optionKey.equals("endTime")) {
throw new BinanceApiException("Invalid aggTrades option, only fromId, startTime, endTime are allowed");
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public List<BinanceCandlestick> klines(BinanceSymbol symbol, BinanceInterval int
String u = baseUrl + "v1/klines?symbol=" + symbol.get() + "&interval=" + interval.toString() + "&limit=" + limit;
if (options != null) {
for (String optionKey : options.keySet()) {
if (!optionKey.equals("startTime") || !optionKey.equals("endTime")) {
if (!optionKey.equals("startTime") && !optionKey.equals("endTime")) {
throw new BinanceApiException("Invalid klines option, only startTime, endTime are allowed");
}
u += "&" + optionKey + "=" + options.get(optionKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Released under the MIT License
* ============================================================ */

import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.webcerebrium.binance.datatype.BinanceAggregatedTrades;
Expand All @@ -20,6 +21,9 @@
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -69,6 +73,30 @@ public void testAggTradesEndpoint() throws Exception, BinanceApiException {
assertTrue("First Trade should contain timestamp", trade.getTimestamp() > 0);
}

@Test
public void testAggTradesEndpointWithOptions() throws Exception, BinanceApiException {
// picking interval of last 15 minutes
Long timeEnd = new Date().getTime();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.MINUTE, -15);
Long timeStart = cal.getTime().getTime();

Map<String, Long> options = ImmutableMap.of("startTime", timeStart, "endTime", timeEnd);
List<BinanceAggregatedTrades> binanceAggregatedTrades = binanceApi.aggTrades(symbol, 5, options);

assertTrue("Aggregated trades array should be received", binanceAggregatedTrades.size() > 0);
// check human-looking getters for the first picked trade
BinanceAggregatedTrades trade = binanceAggregatedTrades.get(0);

assertTrue("First Trade should contain tradeId", trade.getTradeId() > 0);
assertTrue("First Trade should contain price", trade.getPrice().compareTo(BigDecimal.ZERO) > 0);
assertTrue("First Trade should contain quantity", trade.getQuantity().compareTo(BigDecimal.ZERO) > 0);
assertTrue("First Trade should contain firstTradeId", trade.getFirstTradeId() > 0);
assertTrue("First Trade should contain lastTradeId", trade.getLastTradeId() > 0);
assertTrue("First Trade should contain timestamp", trade.getTimestamp() > 0);
}

@Test
public void testIntervalsAreConvertedToStrings() throws Exception {
assertTrue("15min check", BinanceInterval.FIFTEEN_MIN.toString().equals("15m"));
Expand Down Expand Up @@ -97,6 +125,34 @@ public void testKlinesEndpoint() throws Exception, BinanceApiException {
assertNotNull("Candlestick should contain takerBuyQuoteAssetVolume", firstCandlestick.getTakerBuyQuoteAssetVolume());
}

@Test
public void testKlinesEndpointWithOptions() throws Exception, BinanceApiException {
// picking interval of last 3 days
Long timeEnd = new Date().getTime();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.DATE, -3);
Long timeStart = cal.getTime().getTime();

Map<String, Long> options = ImmutableMap.of("startTime", timeStart, "endTime", timeEnd);
List<BinanceCandlestick> klines = binanceApi.klines(symbol, BinanceInterval.FIFTEEN_MIN, 50, options);
assertTrue("Klines should return non-empty array of candlesticks", klines.size() > 0);

BinanceCandlestick firstCandlestick = klines.get(0);
log.info(firstCandlestick.toString());
assertNotNull("Candlestick should contain open", firstCandlestick.getOpen());
assertNotNull("Candlestick should contain high", firstCandlestick.getHigh());
assertNotNull("Candlestick should contain low", firstCandlestick.getLow());
assertNotNull("Candlestick should contain close", firstCandlestick.getClose());
assertNotNull("Candlestick should contain openTime", firstCandlestick.getOpenTime());
assertNotNull("Candlestick should contain closeTime", firstCandlestick.getCloseTime());
assertNotNull("Candlestick should contain numberOfTrades", firstCandlestick.getNumberOfTrades());
assertNotNull("Candlestick should contain volume", firstCandlestick.getVolume());
assertNotNull("Candlestick should contain quoteAssetVolume", firstCandlestick.getQuoteAssetVolume());
assertNotNull("Candlestick should contain takerBuyBaseAssetVolume", firstCandlestick.getTakerBuyBaseAssetVolume());
assertNotNull("Candlestick should contain takerBuyQuoteAssetVolume", firstCandlestick.getTakerBuyQuoteAssetVolume());
}

@Test
public void testTicker24hrEndpoint() throws Exception, BinanceApiException {
JsonObject jsonObject = binanceApi.ticker24hr(symbol);
Expand Down

0 comments on commit 6ac18ec

Please sign in to comment.