Skip to content

Commit

Permalink
Merge pull request #4 from bitvavo/account_endpoint_stoploss
Browse files Browse the repository at this point in the history
✨ New account endpoint, stoploss example
  • Loading branch information
joeri-vv authored Oct 30, 2020
2 parents 724fc9f + 212da35 commit ba3c853
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 10 deletions.
80 changes: 74 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This is the Node.js wrapper for the Bitvavo API. This project can be used to bui
* Cancel Orders [REST](#cancel-orders) [Websocket](#cancel-orders-1)
* Orders Open [REST](#get-orders-open) [Websocket](#get-orders-open-1)
* Trades [REST](#get-trades) [Websocket](#get-trades-1)
* Account [REST](#get-account) [Websocket](#get-account-1)
* Balance [REST](#get-balance) [Websocket](#get-balance-1)
* Deposit Assets [REST](#deposit-assets) [Websocket](#deposit-assets-1)
* Withdraw Assets [REST](#withdraw-assets) [Websocket](#withdraw-assets-1)
Expand Down Expand Up @@ -703,7 +704,9 @@ When placing an order, make sure that the correct optional parameters are set. F
```javascript
// Function with callback
// 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
bitvavo.placeOrder('BTC-EUR', 'sell', 'limit', { amount: '1', price: '3000' }, (error, response) => {
if (error === null) {
console.log(response)
Expand All @@ -714,7 +717,9 @@ bitvavo.placeOrder('BTC-EUR', 'sell', 'limit', { amount: '1', price: '3000' }, (

// Function with promise
// 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
try {
let response = await bitvavo.placeOrder('BTC-EUR', 'sell', 'limit', { 'amount': '1', 'price': 3000 })
console.log(response)
Expand Down Expand Up @@ -754,7 +759,9 @@ try {
When updating an order make sure that at least one of the optional parameters has been set. Otherwise nothing can be updated.
```javascript
// Function with callback
// Optional parameters: limit:(amount, price, timeInForce, selfTradePrevention, postOnly) (set at least 1)
// Optional parameters: limit:(amount, amountRemaining, price, timeInForce, selfTradePrevention, postOnly)
// untriggered stopLoss/takeProfit:(amount, amountQuote, disableMarketProtection, triggerType, triggerReference, triggerAmount)
// stopLossLimit/takeProfitLimit: (amount, price, postOnly, triggerType, triggerReference, triggerAmount)
bitvavo.updateOrder('BTC-EUR', 'bb4076a3-d7b6-4bf6-aa35-b12f14fcb092', { price: 4500 }, (error, response) => {
if (error === null) {
console.log(response)
Expand All @@ -765,7 +772,8 @@ bitvavo.updateOrder('BTC-EUR', 'bb4076a3-d7b6-4bf6-aa35-b12f14fcb092', { price:

// Function with promise
// 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)
try {
let response = await bitvavo.updateOrder('BTC-EUR', 'bb4076a3-d7b6-4bf6-aa35-b12f14fcb092', { amount: 0.2 })
console.log(response)
Expand Down Expand Up @@ -1175,6 +1183,40 @@ try {
```
</details>

#### Get account
Returns the fee tier for this account.
```javascript
// Function with callback
bitvavo.account((error, response) => {
if (error == null) {
console.log(response)
} else {
console.log(error)
}
})

// Function with promise
try {
let response = await bitvavo.account()
console.log(response)
} catch (error) {
console.log(error)
}
```
<details>
<summary>View Response</summary>

```javascript
{
fees: {
taker: '0.0025'
maker: '0.0015'
volume: '100.00'
}
}
```
</details>

#### Get balance
Returns the balance for this account.
```javascript
Expand Down Expand Up @@ -1913,7 +1955,9 @@ bitvavo.getEmitter().on('placeOrder', (response) => {
})

// 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
bitvavo.websocket.placeOrder('BTC-EUR', 'buy', 'limit', { amount: 0.1, price: 5000 })
```
<details>
Expand Down Expand Up @@ -1951,7 +1995,8 @@ bitvavo.getEmitter().on('updateOrder', (response) => {
})

// 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)
bitvavo.websocket.updateOrder('BTC-EUR', 'bb4076a3-d7b6-4bf6-aa35-b12f14fcb092', { amount: '0.2' })
```
<details>
Expand Down Expand Up @@ -2279,6 +2324,29 @@ bitvavo.websocket.trades('BTC-EUR', {})
```
</details>

#### Get account
Returns the fee tier for this account.
```javascript
bitvavo.getEmitter().on('account', (response) => {
console.log(response)
})

bitvavo.websocket.account()
```
<details>
<summary>View Response</summary>

```javascript
{
fees: {
taker: '0.0025'
maker: '0.0015'
volume: '100'
}
}
```
</details>

#### Get balance
Returns the balance for this account.
```javascript
Expand Down
31 changes: 31 additions & 0 deletions example/testApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ let testPlaceOrder = async () => {
} catch (error) {
console.log(error)
}

// StopLoss
bitvavo.placeOrder('BTC-EUR', 'sell', 'stopLoss', { amount: '0.1', triggerType: 'price', triggerReference: 'lastTrade', triggerAmount: '5000' }, (error, response) => {
if (error === null) {
console.log(response)
} else {
console.log(error)
}
})
}

let testGetOrder = async () => {
Expand Down Expand Up @@ -348,6 +357,23 @@ let testTrades = async () => {
}
}

let testAccount = async () => {
bitvavo.account((error, response) => {
if (error == null) {
console.log(response)
} else {
console.log(error)
}
})

try {
let response = await bitvavo.account()
console.log(response)
} catch (error) {
console.log(error)
}
}

let testBalance = async () => {
bitvavo.balance({}, (error, response) => {
if (error == null) {
Expand Down Expand Up @@ -468,6 +494,7 @@ let testRestApi = async () => {

// testTrades()

// testAccount()
// testBalance()
// testDepositAssets()
// testWithdrawAssets()
Expand Down Expand Up @@ -529,6 +556,9 @@ let websocketSetListeners = async () => {
emitter.on('trades', (response) => {
console.log('TradesResponse', response)
})
emitter.on('account', (response) => {
console.log('AccountResponse', response)
})
emitter.on('balance', (response) => {
console.log('BalanceResponse', response)
})
Expand Down Expand Up @@ -571,6 +601,7 @@ let testWebSockets = async () => {
// bitvavo.websocket.ordersOpen({ market: 'BTC-EUR' })
// bitvavo.websocket.trades('BTC-EUR', {})

// bitvavo.websocket.account()
// bitvavo.websocket.balance({})
// bitvavo.websocket.depositAssets('BTC')
// bitvavo.websocket.withdrawAssets('BTC', '1', 'BitcoinAddress', {})
Expand Down
29 changes: 25 additions & 4 deletions node-bitvavo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ let api = function Bitvavo () {
case 'privateGetTrades':
emitter.emit('trades', response.response)
break
case 'privateGetAccount':
emitter.emit('account', response.response)
break
case 'privateGetBalance':
emitter.emit('balance', response.response)
break
Expand Down Expand Up @@ -535,7 +538,10 @@ let api = function Bitvavo () {
return publicRequest((base + '/ticker/24h' + postfix), callback)
},

// Optional body parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection), both: timeInForce, selfTradePrevention, responseRequired
// 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
placeOrder: function (market = '', side = '', orderType = '', body = {}, callback = false) {
body.market = market
body.side = side
Expand All @@ -550,7 +556,8 @@ let api = function Bitvavo () {
},

// 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)
updateOrder: function (market = '', orderId = '', body = {}, callback = false) {
body.market = market
body.orderId = orderId
Expand Down Expand Up @@ -593,6 +600,10 @@ let api = function Bitvavo () {
return privateRequest('/trades', postfix, callback)
},

account: function (callback = false) {
return privateRequest('/account', '', callback)
},

// options: symbol
balance: function (options = {}, callback = false) {
let postfix = createPostfix(options)
Expand Down Expand Up @@ -726,7 +737,10 @@ let api = function Bitvavo () {
doSendPublic(this.websocket, JSON.stringify(options))
},

// Optional body parameters: limit:(amount, price, postOnly), market:(amount, amountQuote, disableMarketProtection), both: timeInForce, selfTradePrevention, responseRequired
// 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
placeOrder: async function (market = '', side = '', orderType = '', body = {}) {
await this.checkSocket()
body.action = 'privateCreateOrder'
Expand All @@ -745,7 +759,8 @@ let api = function Bitvavo () {
},

// 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)
updateOrder: async function (market = '', orderId = '', body = {}) {
await this.checkSocket()
body.action = 'privateUpdateOrder'
Expand Down Expand Up @@ -792,6 +807,12 @@ let api = function Bitvavo () {
doSendPrivate(this.websocket, JSON.stringify(options))
},

account: async function () {
await this.checkSocket()
options = { action: 'privateGetAccount' }
doSendPrivate(this.websocket, JSON.stringify(options))
},

// options: symbol
balance: async function (options = {}) {
await this.checkSocket()
Expand Down
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "bitvavo",
"version": "1.2.0",
"description": "This is the NodeJS wrapper for the Bitvavo API",
"main": "node-bitvavo-api.js",
"directories": {
"example": "example"
},
"dependencies": {
"events": "^3.0.0",
"request": "^2.88.0",
"ws": "^6.1.2"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bitvavo/node-bitvavo-api.git"
},
"keywords": [
"Node",
"Bitvavo"
],
"author": "Bitvavo",
"license": "ISC",
"bugs": {
"url": "https://github.com/bitvavo/node-bitvavo-api/issues"
},
"homepage": "https://github.com/bitvavo/node-bitvavo-api#readme"
}

0 comments on commit ba3c853

Please sign in to comment.