forked from knowm/XChange
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created unit tests for the BitfinexAdapters class in order to verify …
…its correct behavior regarding the timestamp conversion of BigDecimal (given by Bitfinex) to Date (as used in xchange).
- Loading branch information
apaygh
committed
Oct 30, 2014
1 parent
5ceefb7
commit b835f34
Showing
1 changed file
with
187 additions
and
0 deletions.
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
xchange-bitfinex/src/test/java/com/xeiam/xchange/bitfinex/v1/BitfinexAdaptersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2014 Xeiam, LLC. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.xeiam.xchange.bitfinex.v1; | ||
|
||
import com.xeiam.xchange.bitfinex.v1.dto.marketdata.BitfinexLevel; | ||
import com.xeiam.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse; | ||
import com.xeiam.xchange.bitfinex.v1.dto.trade.BitfinexTradeResponse; | ||
import com.xeiam.xchange.currency.CurrencyPair; | ||
import com.xeiam.xchange.dto.Order; | ||
import com.xeiam.xchange.dto.Order.OrderType; | ||
import com.xeiam.xchange.dto.marketdata.Trade; | ||
import com.xeiam.xchange.dto.marketdata.Trades; | ||
import com.xeiam.xchange.dto.trade.LimitOrder; | ||
import com.xeiam.xchange.dto.trade.OpenOrders; | ||
import java.math.BigDecimal; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class BitfinexAdaptersTest { | ||
|
||
private final static String MARKET = "bitfinex"; | ||
private final static String EXCHANGE = "exchange"; | ||
private final static String SYMBOL = "BTCUSD"; | ||
|
||
@Test | ||
public void testAdaptOrdersToOrdersContainer() { | ||
|
||
BitfinexLevel[] levels = initLevels(); | ||
BitfinexAdapters.OrdersContainer container = BitfinexAdapters.adaptOrders(levels, CurrencyPair.BTC_USD, OrderType.BID); | ||
|
||
BitfinexLevel lastLevel = levels[levels.length - 1]; | ||
assertEquals(lastLevel.getTimestamp().multiply(new BigDecimal(1000l)).longValue(), container.getTimestamp()); | ||
assertEquals(container.getLimitOrders().size(), levels.length); | ||
|
||
for (int i = 0; i < levels.length; i++) { | ||
LimitOrder order = container.getLimitOrders().get(i); | ||
long expectedTimestampMillis = levels[i].getTimestamp().multiply(new BigDecimal(1000l)).longValue(); | ||
|
||
assertEquals(levels[i].getAmount(), order.getTradableAmount()); | ||
assertEquals(expectedTimestampMillis, order.getTimestamp().getTime()); | ||
assertEquals(levels[i].getPrice(), order.getLimitPrice()); | ||
} | ||
} | ||
|
||
/** | ||
* Create 60 {@link BitfinexLevel}s. The values increase as the array index does. The timestamps | ||
* increase by 1 second + 1 minute + 1 hour + 1 day in order to test the correct handling of the | ||
* given timestamp. | ||
* | ||
* @return The generated responses. | ||
*/ | ||
private BitfinexLevel[] initLevels() { | ||
|
||
BitfinexLevel[] responses = new BitfinexLevel[60]; | ||
|
||
for (int i = 0; i < responses.length; i++) { | ||
BigDecimal price = new BigDecimal(350l + i); | ||
BigDecimal timestamp = new BigDecimal("1414669893.823615468").add(new BigDecimal(i * (1 + 60 + 60 * 60 + 60 * 60 * 24))); | ||
BigDecimal amount = new BigDecimal(1l + i); | ||
responses[i] = new BitfinexLevel(price, amount, timestamp); | ||
} | ||
|
||
return responses; | ||
} | ||
|
||
@Test | ||
public void testAdaptOrdersToOpenOrders() { | ||
|
||
BitfinexOrderStatusResponse[] responses = initOrderStatusResponses(); | ||
OpenOrders orders = BitfinexAdapters.adaptOrders(responses); | ||
assertEquals(orders.getOpenOrders().size(), responses.length); | ||
|
||
for (int i = 0; i < responses.length; i++) { | ||
LimitOrder order = orders.getOpenOrders().get(i); | ||
long expectedTimestampMillis = responses[i].getTimestamp().multiply(new BigDecimal(1000l)).longValue(); | ||
Order.OrderType expectedOrderType = responses[i].getSide().equalsIgnoreCase("buy") | ||
? Order.OrderType.BID | ||
: Order.OrderType.ASK; | ||
|
||
assertEquals(String.valueOf(responses[i].getId()), order.getId()); | ||
assertEquals(responses[i].getRemainingAmount(), order.getTradableAmount()); | ||
assertEquals(BitfinexAdapters.adaptCurrencyPair(SYMBOL), order.getCurrencyPair()); | ||
assertEquals(expectedOrderType, order.getType()); | ||
assertEquals(expectedTimestampMillis, order.getTimestamp().getTime()); | ||
assertEquals(responses[i].getPrice(), order.getLimitPrice()); | ||
} | ||
} | ||
|
||
/** | ||
* Create 60 {@link BitfinexOrderStatusResponse}s. The values increase as array index does. The | ||
* timestamps increase by 1 second + 1 minute + 1 hour + 1 day in order to test the correct | ||
* handling of the given timestamp. | ||
* | ||
* @return The generated responses. | ||
*/ | ||
private BitfinexOrderStatusResponse[] initOrderStatusResponses() { | ||
|
||
BitfinexOrderStatusResponse[] responses = new BitfinexOrderStatusResponse[60]; | ||
|
||
for (int i = 0; i < responses.length; i++) { | ||
BigDecimal price = new BigDecimal(350l + i); | ||
BigDecimal avgExecutionPrice = price.add(new BigDecimal(0.25 * i)); | ||
String side = i % 2 == 0 ? "buy" : "sell"; | ||
String type = "limit"; | ||
BigDecimal timestamp = new BigDecimal("1414658239.41373654").add(new BigDecimal(i * (1 + 60 + 60 * 60 + 60 * 60 * 24))); | ||
boolean isLive = false; | ||
boolean isCancelled = false; | ||
boolean wasForced = false; | ||
BigDecimal originalAmount = new BigDecimal("70"); | ||
BigDecimal remainingAmount = originalAmount.subtract(new BigDecimal(i * 1)); | ||
BigDecimal executedAmount = originalAmount.subtract(remainingAmount); | ||
responses[i] = new BitfinexOrderStatusResponse(i, SYMBOL, EXCHANGE, price, avgExecutionPrice, side, type, timestamp, isLive, isCancelled, wasForced, originalAmount, remainingAmount, executedAmount); | ||
} | ||
|
||
return responses; | ||
} | ||
|
||
@Test | ||
public void testAdaptTradeHistory() { | ||
|
||
BitfinexTradeResponse[] responses = initTradeResponses(); | ||
Trades trades = BitfinexAdapters.adaptTradeHistory(responses, SYMBOL); | ||
assertEquals(trades.getTrades().size(), responses.length); | ||
|
||
for (int i = 0; i < responses.length; i++) { | ||
Trade trade = trades.getTrades().get(i); | ||
long expectedTimestampMillis = responses[i].getTimestamp().multiply(new BigDecimal(1000l)).longValue(); | ||
Order.OrderType expectedOrderType = responses[i].getType().equalsIgnoreCase("buy") | ||
? OrderType.BID | ||
: OrderType.ASK; | ||
|
||
assertEquals(responses[i].getPrice(), trade.getPrice()); | ||
assertEquals(responses[i].getAmount(), trade.getTradableAmount()); | ||
assertEquals(BitfinexAdapters.adaptCurrencyPair(SYMBOL), trade.getCurrencyPair()); | ||
assertEquals(expectedTimestampMillis, trade.getTimestamp().getTime()); | ||
assertEquals(expectedOrderType, trade.getType()); | ||
assertEquals(responses[i].getTradeId(), trade.getId()); | ||
} | ||
} | ||
|
||
/** | ||
* Create 60 {@link BitfinexTradeResponse}s. The values increase as array index does. The | ||
* timestamps increase by 1 second + 1 minute + 1 hour + 1 day in order to test the correct | ||
* handling of the given timestamp. | ||
* | ||
* @return The generated responses. | ||
*/ | ||
private BitfinexTradeResponse[] initTradeResponses() { | ||
|
||
BitfinexTradeResponse[] responses = new BitfinexTradeResponse[60]; | ||
int tradeId = 2000; | ||
int orderId = 1000; | ||
|
||
for (int i = 0; i < responses.length; i++) { | ||
BigDecimal price = new BigDecimal(350l + i); | ||
BigDecimal amount = new BigDecimal(1l + i); | ||
BigDecimal timestamp = new BigDecimal("1414658239.41373654").add(new BigDecimal(i * (1 + 60 + 60 * 60 + 60 * 60 * 24))); | ||
String type = i % 2 == 0 ? "buy" : "sell"; | ||
String tradeIdString = String.valueOf(tradeId++); | ||
String orderIdString = String.valueOf(orderId++); | ||
responses[i] = new BitfinexTradeResponse(price, amount, timestamp, MARKET, type, tradeIdString, orderIdString); | ||
} | ||
|
||
return responses; | ||
} | ||
} |