Created
January 24, 2018 18:35
-
-
Save talvasconcelos/4fdb25f51ea34ad457ddcbb60156b330 to your computer and use it in GitHub Desktop.
Binance Example Methods
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
const api = require('binance') | |
const client = new api.BinanceRest({ | |
key: 'key', // Get this from your account on binance.com | |
secret: 'secret', // Same for this | |
timeout: 15000, // Optional, defaults to 15000, is the request time out in milliseconds | |
recvWindow: 10000, // Optional, defaults to 5000, increase if you're getting timestamp errors | |
disableBeautification: false | |
}) | |
//methods are inside a class... | |
// get candles for a pair | |
getCandles(pair){ | |
return new Promise(resolve => { | |
this.client.klines({ | |
symbol: pair, | |
interval: '15m', | |
limit: 30 | |
}).then(res => { | |
console.log(output) | |
resolve(output) | |
}) | |
.catch(err => console.error(err)) | |
}) | |
} | |
//Place order | |
addOrder(options) { | |
let self = this | |
return this.client.testOrder({ | |
symbol: self.product, | |
side: options.side, | |
type: 'LIMIT', | |
price: options.price, | |
timeInForce: 'GTC', | |
quantity: options.quantity, | |
timestamp: new Date().getTime() | |
}) | |
.then(data => { | |
console.log(data) | |
return data | |
}) | |
.catch(err => console.error(err)) | |
} | |
//Buy | |
buy(pair) { | |
let qty = this.roundToNearest(/*quote balance*/0.01 / price) | |
this.addOrder({ | |
side: 'BUY', | |
quantity: qty, | |
price: 0.01255 //example price | |
}) | |
} | |
//Sell | |
sell() { | |
let qty = this.roundToNearest(/*asset balance*/ 14.678241) | |
this.addOrder({ | |
side: 'SELL', | |
quantity: qty, | |
price: 0.0255 | |
}) | |
} | |
//Rounding based on Binance's filters for minimal quantity [taken from zenbot] | |
roundToNearest(numToRound) { | |
let numToRoundTo = this._minQty // minimum quantity from binance for specified pair (1, 0.1, 0.001) | |
numToRoundTo = 1 / (numToRoundTo) | |
return Math.floor(numToRound * numToRoundTo) / numToRoundTo | |
} | |
fetch_balances() { | |
console.log('starting fetch_balances') | |
let self = this | |
return new Promise((resolve, reject) => { | |
self.client.account() | |
.then(data => { | |
let asset = self.product.split(self._base)[0] | |
//self._balances = data.balances | |
self._tradebalance.base = data.balances.find(b => b.asset === self._base).free | |
self._tradebalance.asset = data.balances.find(b => b.asset === asset).free | |
resolve(data) | |
}) | |
.catch(err => console.error(err)) | |
}) | |
} | |
fetch_ticker() { | |
let self = this | |
return new Promise(resolve => { | |
self.client.ticker24hr(self.product, (err, data) => { | |
if(err) { | |
self._ticker.errors.push({ | |
timestamp: new Date(), | |
error: err | |
}) | |
return resolve(null) | |
} | |
self._ticker.updated = new Date() | |
self._ticker.data = data | |
self._last_price = data.lastPrice | |
resolve(data) | |
}) | |
}) | |
} | |
listen_to_messages() { | |
let feed = this._websocket | |
if(!feed) return | |
if(this.product){ | |
feed.onTrade(this.product, msg => { | |
this._stats = { | |
updated: new Date(), | |
last_msg: msg | |
} | |
this._last_price = msg.price | |
}) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment