Skip to content

Commit

Permalink
✨ New account endpoint, stoploss example
Browse files Browse the repository at this point in the history
  • Loading branch information
joeri-vv committed Nov 12, 2020
1 parent 8bef6a3 commit 471b5ae
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This is the Java wrapper for the Bitvavo API. This project can be used to build
* Cancel Orders [REST](https://github.com/bitvavo/java-bitvavo-api#cancel-orders) [Websocket](https://github.com/bitvavo/java-bitvavo-api#cancel-orders-1)
* Orders Open [REST](https://github.com/bitvavo/java-bitvavo-api#get-orders-open) [Websocket](https://github.com/bitvavo/java-bitvavo-api#get-orders-open-1)
* Trades [REST](https://github.com/bitvavo/java-bitvavo-api#get-trades) [Websocket](https://github.com/bitvavo/java-bitvavo-api#get-trades-1)
* Account [REST](https://github.com/bitvavo/java-bitvavo-api#get-account) [Websocket](https://github.com/bitvavo/java-bitvavo-api#get-account-1)
* Balance [REST](https://github.com/bitvavo/java-bitvavo-api#get-balance) [Websocket](https://github.com/bitvavo/java-bitvavo-api#get-balance-1)
* Deposit Assets [REST](https://github.com/bitvavo/java-bitvavo-api#deposit-assets) [Websocket](https://github.com/bitvavo/java-bitvavo-api#deposit-assets-1)
* Withdraw Assets [REST](https://github.com/bitvavo/java-bitvavo-api#withdraw-assets) [Websocket](https://github.com/bitvavo/java-bitvavo-api#withdraw-assets-1)
Expand Down Expand Up @@ -616,7 +617,9 @@ for(int i = 0; i < response.length(); i ++) {
When placing an order, make sure that the correct optional parameters are set. For a limit order it is required to set both the amount and price. A market order is valid if either the amount or the amountQuote has been set.
```java
// optional parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection),
// both: timeInForce, selfTradePrevention, responseRequired
// stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
// stopLossLimit/takeProfitLimit:(amount, price, postOnly, triggerType, triggerReference, triggerAmount)
// all orderTypes: timeInForce, selfTradePrevention, responseRequired
System.out.println(bitvavo.placeOrder("BTC-EUR", "sell", "limit",
new JSONObject("{ amount: 0.1, price: 4000 }")).toString(2));
```
Expand Down Expand Up @@ -654,7 +657,8 @@ System.out.println(bitvavo.placeOrder("BTC-EUR", "sell", "limit",
When updating an order make sure that at least one of the optional parameters has been set. Otherwise nothing can be updated.
```java
// Optional parameters: limit:(amount, amountRemaining, price, timeInForce, selfTradePrevention, postOnly)
// (set at least 1) (responseRequired can be set as well, but does not update anything)
// untriggered stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
// stopLossLimit/takeProfitLimit: (amount, price, postOnly, triggerType, triggerReference, triggerAmount)
System.out.println(bitvavo.updateOrder("BTC-EUR", "81080b09-2415-44e3-b61c-50ffca4a0221",
new JSONObject("{ amount: 0.2 }")));
```
Expand Down Expand Up @@ -974,6 +978,24 @@ for(int i = 0; i < response.length(); i ++) {
```
</details>

#### Get account
```java
System.out.println(bitvavo.account().toString(2));
```
<details>
<summary>View Response</summary>

```java
{
"fees": {
"taker": "0.0025",
"maker": "0.0015",
"volume": "100"
}
}
```
</details>

#### Get balance
Returns the balance for this account.
```java
Expand Down Expand Up @@ -1780,7 +1802,9 @@ ws.ticker24h(new JSONObject(), new WebsocketClientEndpoint.MessageHandler() {
When placing an order, make sure that the correct optional parameters are set. For a limit order it is required to set both the amount and price. A market order is valid if either the amount or the amountQuote has been set.
```java
// optional parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection),
// both: timeInForce, selfTradePrevention, responseRequired
// stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
// stopLossLimit/takeProfitLimit:(amount, price, postOnly, triggerType, triggerReference, triggerAmount)
// all orderTypes: timeInForce, selfTradePrevention, responseRequired
ws.placeOrder("BTC-EUR", "sell", "limit", new JSONObject("{ amount: 0.1, price: 4000 }"),
new WebsocketClientEndpoint.MessageHandler() {
public void handleMessage(JSONObject responseObject) {
Expand Down Expand Up @@ -1822,7 +1846,8 @@ ws.placeOrder("BTC-EUR", "sell", "limit", new JSONObject("{ amount: 0.1, price:
When updating an order make sure that at least one of the optional parameters has been set. Otherwise nothing can be updated.
```java
// Optional parameters: limit:(amount, amountRemaining, price, timeInForce, selfTradePrevention, postOnly)
// (set at least 1) (responseRequired can be set as well, but does not update anything)
// untriggered stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
// stopLossLimit/takeProfitLimit: (amount, price, postOnly, triggerType, triggerReference, triggerAmount)
ws.updateOrder("BTC-EUR", "81080b09-2415-44e3-b61c-50ffca4a0221", new JSONObject("{ amount: 0.2 }"),
new WebsocketClientEndpoint.MessageHandler() {
public void handleMessage(JSONObject responseObject) {
Expand Down Expand Up @@ -2170,6 +2195,29 @@ ws.trades("BTC-EUR", new JSONObject(), new WebsocketClientEndpoint.MessageHandle
```
</details>

#### Get account
```java
ws.account(new WebsocketClientEndpoint.MessageHandler() {
public void handleMessage(JSONObject responseObject) {
JSONObject response = responseObject.getJSONObject("response");
System.out.println(response.toString(2));
}
});
```
<details>
<summary>View Response</summary>

```java
{
"fees": {
"taker": "0.0025",
"maker": "0.0015",
"volume": "100"
}
}
```
</details>

#### Get balance
Returns the balance for this account.
```java
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/com/bitvavo/api/Bitvavo.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ public JSONArray ticker24h(JSONObject options) {
* @param market The market for which the order should be created
* @param side is this a buy or sell order
* @param orderType is this a limit or market order
* @param body optional body parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection), both: timeInForce, selfTradePrevention, responseRequired
* @param body optional body parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection)
* stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
* stopLossLimit/takeProfitLimit:(amount, price, postOnly, triggerType, triggerReference, triggerAmount)
* all orderTypes: timeInForce, selfTradePrevention, responseRequired
* @return JSONObject response, get status of the order through response.getString("status")
*/
public JSONObject placeOrder(String market, String side, String orderType, JSONObject body) {
Expand Down Expand Up @@ -582,7 +585,8 @@ public JSONObject getOrder(String market, String orderId) {
* @param market the market the order resides on
* @param orderId the id of the order which should be updated
* @param body optional body parameters: limit:(amount, amountRemaining, price, timeInForce, selfTradePrevention, postOnly)
* (set at least 1) (responseRequired can be set as well, but does not update anything)
* untriggered stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
* stopLossLimit/takeProfitLimit: (amount, price, postOnly, triggerType, triggerReference, triggerAmount)
* @return JSONObject response, get status of the order through response.getString("status")
*/
public JSONObject updateOrder(String market, String orderId, JSONObject body) {
Expand Down Expand Up @@ -649,6 +653,14 @@ public JSONArray trades(String market, JSONObject options) {
return privateRequestArray("/trades", postfix, "GET", new JSONObject());
}

/**
* Return the fee tier for an account
* @return JSONObject response, get taker fee through: response.getJSONObject("fees").getString("taker")
*/
public JSONObject account() {
return privateRequest("/account", "", "GET", new JSONObject());
}

/**
* Returns the balance for an account
* @param options optional parameters: symbol
Expand Down Expand Up @@ -908,7 +920,10 @@ public void tickerBook(JSONObject options, WebsocketClientEndpoint.MessageHandle
* @param market market on which the order should be created
* @param side is this a sell or buy order
* @param orderType is this a limit or market order
* @param body optional body parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection), both: timeInForce, selfTradePrevention, responseRequired
* @param body optional body parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection)
* stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
* stopLossLimit/takeProfitLimit:(amount, price, postOnly, triggerType, triggerReference, triggerAmount)
* all orderTypes: timeInForce, selfTradePrevention, responseRequired
* @param msgHandler callback
* @return JSONObject response, get order object through response.getJSONObject("response")
*/
Expand Down Expand Up @@ -944,7 +959,8 @@ public void getOrder(String market, String orderId, WebsocketClientEndpoint.Mess
* @param market market on which the order should be updated
* @param orderId the order which should be updated
* @param body optional body parameters: limit:(amount, amountRemaining, price, timeInForce, selfTradePrevention, postOnly)
* (set at least 1) (responseRequired can be set as well, but does not update anything)
* untriggered stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
* stopLossLimit/takeProfitLimit: (amount, price, postOnly, triggerType, triggerReference, triggerAmount)
* @param msgHandler callback
* @return JSONObject response, get order object through response.getJSONObject("response")
*/
Expand Down Expand Up @@ -1029,6 +1045,18 @@ public void trades(String market, JSONObject options, WebsocketClientEndpoint.Me
doSendPrivate(options);
}

/**
* Returns the fee tier for an account
*
* @param msgHandler callback
* @return JSONObject response, get taker fee through response.getJSONObject("response").getJSONObject("fees").getString("taker")
*/
public void account(WebsocketClientEndpoint.MessageHandler msgHandler) {
ws.addAccountHandler(msgHandler);
JSONObject options = new JSONObject("{ action: privateGetAccount }");
doSendPrivate(options);
}

/**
* Returns the balance for an account
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/bitvavo/api/WebsocketClientEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class WebsocketClientEndpoint {
private MessageHandler getOrdersHandler;
private MessageHandler getOrdersOpenHandler;
private MessageHandler getTradesHandler;
private MessageHandler getAccountHandler;
private MessageHandler balanceHandler;
private MessageHandler depositAssetsHandler;
private MessageHandler withdrawAssetsHandler;
Expand Down Expand Up @@ -550,6 +551,11 @@ else if(response.getString("action").equals("privateGetTrades")) {
this.getTradesHandler.handleMessage(response);
}
}
else if(response.getString("action").equals("privateGetAccount")) {
if (this.getAccountHandler != null) {
this.getAccountHandler.handleMessage(response);
}
}
else if(response.getString("action").equals("privateGetBalance")) {
if(this.balanceHandler != null) {
this.balanceHandler.handleMessage(response);
Expand Down Expand Up @@ -657,6 +663,10 @@ public void addGetTradesHandler(MessageHandler msgHandler) {
this.getTradesHandler = msgHandler;
}

public void addAccountHandler(MessageHandler msgHandler) {
this.getAccountHandler = msgHandler;
}

public void addBalanceHandler(MessageHandler msgHandler) {
this.balanceHandler = msgHandler;
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/bitvavo/api/example/example.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public static void testREST(Bitvavo bitvavo) {
JSONArray response;

int remaining = bitvavo.getRemainingLimit();
if (remaining > 0) {
System.out.println("remaining limit is " + remaining);
System.out.println(bitvavo.time().toString(2));
}
System.out.println("remaining limit is " + remaining);

// response = bitvavo.markets(new JSONObject());
// for(int i = 0; i < response.length(); i ++) {
Expand Down Expand Up @@ -74,6 +71,8 @@ public static void testREST(Bitvavo bitvavo) {
// }

// System.out.println(bitvavo.placeOrder("BTC-EUR", "sell", "limit", new JSONObject("{ amount: 0.1, price: 4000 }")).toString(2));

// System.out.println(bitvavo.placeOrder("BTC-EUR", "sell", "stopLoss", new JSONObject("{ amount: 0.1, triggerType: price, triggerReference: lastTrade, triggerAmount: 5000 }")).toString(2));

// System.out.println(bitvavo.getOrder("BTC-EUR", "afa9da1c-edb9-4245-9271-3549147845a1").toString(2));

Expand Down Expand Up @@ -101,6 +100,8 @@ public static void testREST(Bitvavo bitvavo) {
// System.out.println(response.getJSONObject(i).toString(2));
// }

// System.out.println(bitvavo.account().toString(2));

// response = bitvavo.balance(new JSONObject());
// for(int i = 0; i < response.length(); i ++) {
// System.out.println(response.getJSONObject(i).toString(2));
Expand Down Expand Up @@ -265,6 +266,13 @@ public void handleMessage(JSONObject responseObject) {
// }
// });

// ws.account(new WebsocketClientEndpoint.MessageHandler() {
// public void handleMessage(JSONObject responseObject) {
// JSONObject response = responseObject.getJSONObject("response");
// System.out.println(response.toString(2));
// }
// });

// ws.balance(new JSONObject(), new WebsocketClientEndpoint.MessageHandler() {
// public void handleMessage(JSONObject responseObject) {
// JSONArray response = responseObject.getJSONArray("response");
Expand Down

0 comments on commit 471b5ae

Please sign in to comment.