Skip to content

Commit

Permalink
#152 : Simulate fees in Try Mode adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
gazbert committed Aug 20, 2022
1 parent 80ea3aa commit adc85fb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 82 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ The [`TryModeExchangeAdapter`](./bxbot-exchanges/src/main/java/com/gazbert/bxbot
configured by default to delegate public API calls to the
[`BitstampExchangeAdapter`](./bxbot-exchanges/src/main/java/com/gazbert/bxbot/exchanges/BitstampExchangeAdapter.java).
It simulates the private API (order management) calls; it's good for testing your initial setup and
paper trading, but you'll eventually want to send live orders to the exchange! You'll still need to
configure your API credentials for Bitstamp because the adapter makes an authenticated API call to
get the buy/sell fees from the [/balance](https://www.bitstamp.net/api/#account-balance) endpoint.

``paper trading, but you'll eventually want to send live orders to the exchange!

The configuration below shows how to live-trade with Bitstamp:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,32 @@ public class TryModeExchangeAdapter extends AbstractExchangeAdapter implements E
private static final Logger LOG = LogManager.getLogger();

private static final String SIMULATED_COUNTER_CURRENCY_PROPERTY_NAME = "simulatedCounterCurrency";
private static final String COUNTER_CURRENCY_START_BALANCE_PROPERTY_NAME =
"counterCurrencyStartingBalance";
private static final String SIMULATED_COUNTER_CURRENCY_START_BALANCE_PROPERTY_NAME =
"simulatedCounterCurrencyStartingBalance";

private static final String SIMULATED_BASE_CURRENCY_PROPERTY_NAME = "simulatedBaseCurrency";
private static final String BASE_CURRENCY_START_BALANCE_PROPERTY_NAME =
"baseCurrencyStartingBalance";
private static final String SIMULATED_BASE_CURRENCY_START_BALANCE_PROPERTY_NAME =
"simulatedBaseCurrencyStartingBalance";

private static final String SIMULATED_SELL_FEE_PROPERTY_NAME = "simulatedSellFee";
private static final String SIMULATED_BUY_FEE_PROPERTY_NAME = "simulatedBuyFee";

private static final String DELEGATE_ADAPTER_CLASS_PROPERTY_NAME = "delegateAdapter";

private String simulatedBaseCurrency;
private BigDecimal simulatedBaseCurrencyBalance;

private String simulatedCounterCurrency;
private BigDecimal counterCurrencyBalance;
private BigDecimal simulatedCounterCurrencyBalance;

private BigDecimal simulatedSellFee;
private BigDecimal simulatedBuyFee;

private String delegateExchangeClassName;

private ExchangeAdapter delegateExchangeAdapter;

private OpenOrder currentOpenOrder;
private BigDecimal baseCurrencyBalance;
private boolean isOpenOrderCheckReentering;

@Override
Expand Down Expand Up @@ -173,31 +183,21 @@ public BigDecimal getLatestMarketPrice(String marketId)
@Override
public BalanceInfo getBalanceInfo() {
final HashMap<String, BigDecimal> availableBalances = new HashMap<>();
availableBalances.put(simulatedBaseCurrency, baseCurrencyBalance);
availableBalances.put(simulatedCounterCurrency, counterCurrencyBalance);
availableBalances.put(simulatedBaseCurrency, simulatedBaseCurrencyBalance);
availableBalances.put(simulatedCounterCurrency, simulatedCounterCurrencyBalance);
final BalanceInfo currentBalance = new BalanceInfoImpl(availableBalances, new HashMap<>());
LOG.info(() -> "Return the following simulated balances: " + currentBalance);
return currentBalance;
}

@Override
public BigDecimal getPercentageOfBuyOrderTakenForExchangeFee(String marketId)
throws TradingApiException, ExchangeNetworkException {
LOG.info(
() ->
"Delegate 'getPercentageOfBuyOrderTakenForExchangeFee'"
+ "to the configured delegation exchange adapter.");
return delegateExchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(marketId);
public BigDecimal getPercentageOfBuyOrderTakenForExchangeFee(String marketId) {
return simulatedBuyFee;
}

@Override
public BigDecimal getPercentageOfSellOrderTakenForExchangeFee(String marketId)
throws TradingApiException, ExchangeNetworkException {
LOG.info(
() ->
"Delegate 'getPercentageOfSellOrderTakenForExchangeFee'"
+ "to the configured delegation exchange adapter.");
return delegateExchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(marketId);
public BigDecimal getPercentageOfSellOrderTakenForExchangeFee(String marketId) {
return simulatedSellFee;
}

@Override
Expand All @@ -215,24 +215,33 @@ private void setOtherConfig(ExchangeConfig exchangeConfig) {
LOG.info(() -> "Base currency to be simulated:" + simulatedBaseCurrency);

final String startingBaseBalanceInConfig =
getOtherConfigItem(otherConfig, BASE_CURRENCY_START_BALANCE_PROPERTY_NAME);
baseCurrencyBalance = new BigDecimal(startingBaseBalanceInConfig);
getOtherConfigItem(otherConfig, SIMULATED_BASE_CURRENCY_START_BALANCE_PROPERTY_NAME);
simulatedBaseCurrencyBalance = new BigDecimal(startingBaseBalanceInConfig);
LOG.info(
() ->
"Base currency balance at simulation start in BigDecimal format: "
+ baseCurrencyBalance);
+ simulatedBaseCurrencyBalance);

simulatedCounterCurrency =
getOtherConfigItem(otherConfig, SIMULATED_COUNTER_CURRENCY_PROPERTY_NAME);
LOG.info(() -> "Counter currency to be simulated:" + simulatedCounterCurrency);

final String startingBalanceInConfig =
getOtherConfigItem(otherConfig, COUNTER_CURRENCY_START_BALANCE_PROPERTY_NAME);
counterCurrencyBalance = new BigDecimal(startingBalanceInConfig);
getOtherConfigItem(otherConfig, SIMULATED_COUNTER_CURRENCY_START_BALANCE_PROPERTY_NAME);
simulatedCounterCurrencyBalance = new BigDecimal(startingBalanceInConfig);
LOG.info(
() ->
"Counter currency balance at simulation start in BigDecimal format: "
+ counterCurrencyBalance);
+ simulatedCounterCurrencyBalance);

final String sellFeeInConfig =
getOtherConfigItem(otherConfig, SIMULATED_SELL_FEE_PROPERTY_NAME);
simulatedSellFee = new BigDecimal(sellFeeInConfig);
LOG.info(() -> "Sell Fee at simulation start in BigDecimal format: " + simulatedSellFee);

final String buyFeeInConfig = getOtherConfigItem(otherConfig, SIMULATED_BUY_FEE_PROPERTY_NAME);
simulatedBuyFee = new BigDecimal(buyFeeInConfig);
LOG.info(() -> "Buy Fee at simulation start in BigDecimal format: " + simulatedBuyFee);

delegateExchangeClassName =
getOtherConfigItem(otherConfig, DELEGATE_ADAPTER_CLASS_PROPERTY_NAME);
Expand All @@ -250,6 +259,7 @@ private void initializeAdapterDelegation(ExchangeConfig config) {
delegateExchangeAdapter.init(config);
}

@SuppressWarnings("unchecked")
private ExchangeAdapter createDelegateExchangeAdapter() {
LOG.info(() -> "Creating the delegate exchange adapter '" + delegateExchangeClassName + "'...");
try {
Expand Down Expand Up @@ -308,8 +318,9 @@ private void checkOpenSellOrderExecution(String marketId)
final BigDecimal buyFees =
getPercentageOfSellOrderTakenForExchangeFee(marketId).multiply(orderPrice);
final BigDecimal netOrderPrice = orderPrice.subtract(buyFees);
counterCurrencyBalance = counterCurrencyBalance.add(netOrderPrice);
baseCurrencyBalance = baseCurrencyBalance.subtract(currentOpenOrder.getOriginalQuantity());
simulatedCounterCurrencyBalance = simulatedCounterCurrencyBalance.add(netOrderPrice);
simulatedBaseCurrencyBalance =
simulatedBaseCurrencyBalance.subtract(currentOpenOrder.getOriginalQuantity());
currentOpenOrder = null;
}
}
Expand All @@ -326,8 +337,9 @@ private void checkOpenBuyOrderExecution(String marketId)
final BigDecimal buyFees =
getPercentageOfBuyOrderTakenForExchangeFee(marketId).multiply(orderPrice);
final BigDecimal netOrderPrice = orderPrice.add(buyFees);
counterCurrencyBalance = counterCurrencyBalance.subtract(netOrderPrice);
baseCurrencyBalance = baseCurrencyBalance.add(currentOpenOrder.getOriginalQuantity());
simulatedCounterCurrencyBalance = simulatedCounterCurrencyBalance.subtract(netOrderPrice);
simulatedBaseCurrencyBalance =
simulatedBaseCurrencyBalance.add(currentOpenOrder.getOriginalQuantity());
currentOpenOrder = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ public class TestTryModeExchangeAdapter extends AbstractExchangeAdapter {
private static final BigDecimal SELL_ORDER_QUANTITY = new BigDecimal("0.03");
private static final String UNRECOGNISED_ORDER_ID = "80894263";

private static final BigDecimal PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE =
new BigDecimal("0.0025");
private static final BigDecimal PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE =
new BigDecimal("0.0024");

private static final BigDecimal LAST = new BigDecimal("18789.58");
private static final BigDecimal BID = new BigDecimal("18778.25");
private static final BigDecimal ASK = new BigDecimal("18783.33");
Expand Down Expand Up @@ -160,6 +155,13 @@ public class TestTryModeExchangeAdapter extends AbstractExchangeAdapter {
private static final String COUNTER_CURRENCY = "USD";
private static final String COUNTER_CURRENCY_STARTING_BALANCE = "100.0";

private static final String SIMULATED_SELL_FEE = "0.2";
private static final String SIMULATED_BUY_FEE = "0.1";
private static final BigDecimal PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE =
new BigDecimal(SIMULATED_SELL_FEE);
private static final BigDecimal PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE =
new BigDecimal(SIMULATED_BUY_FEE);

private static final String CLIENT_ID = "clientId123";
private static final String KEY = "key123";
private static final String SECRET = "notGonnaTellYa";
Expand All @@ -185,17 +187,20 @@ public void setupForEachTest() {

OtherConfig otherConfig = PowerMock.createMock(OtherConfig.class);
expect(otherConfig.getItem("simulatedBaseCurrency")).andReturn(BASE_CURRENCY).atLeastOnce();
expect(otherConfig.getItem("baseCurrencyStartingBalance"))
expect(otherConfig.getItem("simulatedBaseCurrencyStartingBalance"))
.andReturn(BASE_CURRENCY_STARTING_BALANCE)
.atLeastOnce();

expect(otherConfig.getItem("simulatedCounterCurrency"))
.andReturn(COUNTER_CURRENCY)
.atLeastOnce();
expect(otherConfig.getItem("counterCurrencyStartingBalance"))
expect(otherConfig.getItem("simulatedCounterCurrencyStartingBalance"))
.andReturn(COUNTER_CURRENCY_STARTING_BALANCE)
.atLeastOnce();

expect(otherConfig.getItem("simulatedSellFee")).andReturn(SIMULATED_SELL_FEE).atLeastOnce();
expect(otherConfig.getItem("simulatedBuyFee")).andReturn(SIMULATED_BUY_FEE).atLeastOnce();

expect(otherConfig.getItem("delegateAdapter")).andReturn(DELEGATE_ADAPTER).atLeastOnce();

authenticationConfig = PowerMock.createMock(AuthenticationConfig.class);
Expand Down Expand Up @@ -303,12 +308,6 @@ public void testCreateBuyOrderThatFillsInstantly() throws Exception {
PowerMock.expectPrivate(delegateExchangeAdapter, MOCKED_GET_TICKER_METHOD, eq(MARKET_ID))
.andReturn(tickerResponse);

PowerMock.expectPrivate(
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);
Expand Down Expand Up @@ -375,12 +374,6 @@ public void testCreateSellOrderThatFillsInstantly() throws Exception {
PowerMock.expectPrivate(delegateExchangeAdapter, MOCKED_GET_TICKER_METHOD, eq(MARKET_ID))
.andReturn(tickerResponse);

PowerMock.expectPrivate(
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);
Expand Down Expand Up @@ -609,12 +602,6 @@ public void testGettingYourOpenOrdersWhenSellOrderFilled() throws Exception {
PowerMock.expectPrivate(delegateExchangeAdapter, MOCKED_GET_TICKER_METHOD, eq(MARKET_ID))
.andReturn(tickerResponse);

PowerMock.expectPrivate(
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);
Expand Down Expand Up @@ -714,12 +701,6 @@ public void testGettingYourOpenOrdersWhenBuyOrderFilled() throws Exception {
PowerMock.expectPrivate(delegateExchangeAdapter, MOCKED_GET_TICKER_METHOD, eq(MARKET_ID))
.andReturn(tickerResponse);

PowerMock.expectPrivate(
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);
Expand Down Expand Up @@ -908,17 +889,12 @@ public void testGettingBalanceInfoSuccessfully() throws Exception {

@Test
public void testGettingExchangeBuyingFeeSuccessfully() throws Exception {

final BitstampExchangeAdapter delegateExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
BitstampExchangeAdapter.class,
MOCKED_GET_PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE);

PowerMock.expectPrivate(
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);
Expand All @@ -931,7 +907,7 @@ public void testGettingExchangeBuyingFeeSuccessfully() throws Exception {
tryModeExchangeAdapter.init(exchangeConfig);

final BigDecimal buyPercentageFee =
delegateExchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(MARKET_ID);
tryModeExchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(MARKET_ID);
assertEquals(0, buyPercentageFee.compareTo(PERCENTAGE_OF_BUY_ORDER_TAKEN_FOR_EXCHANGE_FEE));

PowerMock.verifyAll();
Expand All @@ -943,17 +919,12 @@ public void testGettingExchangeBuyingFeeSuccessfully() throws Exception {

@Test
public void testGettingExchangeSellingFeeSuccessfully() throws Exception {

final BitstampExchangeAdapter delegateExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
BitstampExchangeAdapter.class,
MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE);

PowerMock.expectPrivate(
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);
Expand Down
10 changes: 8 additions & 2 deletions config/exchange.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ exchange:
simulatedCounterCurrency: USD

# The starting balance for the simulation. The simulation starts with this amount for counter currency.
counterCurrencyStartingBalance: 100
simulatedCounterCurrencyStartingBalance: 100

# The base currency which is simulated. This must match your chosen market base currency.
simulatedBaseCurrency: BTC

# The starting balance for the simulation. The simulation starts with this amount for base currency.
baseCurrencyStartingBalance: 2
simulatedBaseCurrencyStartingBalance: 2

# The exchange sell fee percentage for the simulation.
simulatedSellFee: 0.1

# The exchange buy fee percentage for the simulation.
simulatedBuyFee: 0.1

# The adapter which should be used for public API calls.
# All special config values for this adapter must be given in this otherConfig section as well.
Expand Down

0 comments on commit adc85fb

Please sign in to comment.