Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Bittrex Exchange #1215

Merged
merged 13 commits into from
Jan 24, 2018
110 changes: 85 additions & 25 deletions extensions/exchanges/bittrex/exchange.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var bittrex_authed = require('node.bittrex.api'),
bittrex_public = require('node.bittrex.api'),
path = require('path'),
var bittrex_authed = require('node-bittrex-api'),
bittrex_public = require('node-bittrex-api'),
moment = require('moment'),
n = require('numbro'),
colors = require('colors')
n = require('numbro')



/**
Expand Down Expand Up @@ -32,10 +31,11 @@ module.exports = function container(get, set, clear) {
}

function retry(method, args, error) {
var timeout
if (error.message.match(/Rate limit exceeded/)) {
var timeout = 10000
timeout = 10000
} else {
var timeout = 2500
timeout = 2500
}

console.error(('\nBittrex API error - unable to call ' + method + ' (' + error.message + '), retrying in ' + timeout / 1000 + 's').red)
Expand All @@ -61,7 +61,15 @@ module.exports = function container(get, set, clear) {
market: joinProduct(opts.product_id)
}

bittrex_public.getmarkethistory(args, function( data ) {
bittrex_public.getmarkethistory(args, function( data, err) {
if (err != null && data == null)
{
data = {}
data.message = err.message
data.success = err.success
data.result = err.result
}

if (!shownWarning) {
console.log('please note: the bittrex api does not support backfilling (trade/paper only).')
console.log('please note: make sure to set the --period_length=1m to make sure data for trade/paper is fetched.')
Expand Down Expand Up @@ -95,7 +103,7 @@ module.exports = function container(get, set, clear) {
}
})
} catch (e) {
return retry('getTrades', func_args, {message: 'Error: ' + e});
return retry('getTrades', func_args, {message: 'Error: ' + e})
}
cb(null, trades)
})
Expand All @@ -104,8 +112,17 @@ module.exports = function container(get, set, clear) {
getBalance: function (opts, cb) {
var args = [].slice.call(arguments)

bittrex_authed.getbalances(function( data ) {
if (typeof data !== 'object') {
bittrex_authed.getbalances(function( data,err ) {
if (err != null && data == null)
{
data = {}
data.message = err.message
data.success = err.success
data.result = err.result
}


if (typeof data !== 'object' ) {
console.log('bittrex API (getbalances) had an abnormal response, quitting.')
return cb(null, [])
}
Expand All @@ -128,20 +145,20 @@ module.exports = function container(get, set, clear) {
if(opts.last_signal === 'buy') {
if (_balance['Currency'] === opts.currency.toUpperCase()) {
balance.currency = n(_balance.Available).format('0.00000000'),
balance.currency_hold = 0
balance.currency_hold = 0
}
if (_balance['Currency'] === opts.asset.toUpperCase()) {
balance.asset = n(_balance.Available).format('0.00000000'),
balance.asset_hold = 0
balance.asset_hold = 0
}
} else {
if (_balance['Currency'] === opts.asset.toUpperCase()) {
balance.asset = n(_balance.Available).format('0.00000000'),
balance.asset_hold = 0
balance.asset_hold = 0
}
if (_balance['Currency'] === opts.currency.toUpperCase()) {
balance.currency = n(_balance.Available).format('0.00000000'),
balance.currency_hold = 0
balance.currency_hold = 0
}
}
})
Expand All @@ -153,7 +170,15 @@ module.exports = function container(get, set, clear) {
var args = {
market: joinProduct(opts.product_id)
}
bittrex_public.getticker(args, function( data ) {
bittrex_public.getticker(args, function( data, err ) {
if (err != null && data == null)
{
data = {}
data.message = err.message
data.success = err.success
data.result = err.result
}

if (typeof data !== 'object') {
console.log('bittrex API (getticker) had an abnormal response, quitting.')
return cb(null, [])
Expand All @@ -175,10 +200,18 @@ module.exports = function container(get, set, clear) {
},

cancelOrder: function (opts, cb) {
var args = [].slice.call(arguments)
bittrex_authed.cancel({
uuid: opts.order_id
}, function( data ) {
}, function( data,err ) {
if (err != null && data == null)
{
data = {}
data.message = err.message
data.success = err.success
data.result = err.result
}


if (typeof data !== 'object') {
console.log('bittrex API (cancel) had an abnormal response, quitting.')
return cb(null, [])
Expand All @@ -203,23 +236,42 @@ module.exports = function container(get, set, clear) {
rate: opts.price
}

if(!'order_type' in opts || !opts.order_type) {
if(!('order_type' in opts) || !opts.order_type) {
opts.order_type = 'maker'
}

var fn = function(data) {
var fn = function(data,err) {
if (err != null && data == null)
{
data = {}
data.message = err.message
data.success = err.success
data.result = err.result
}
if (err && err.message)
{
if (err.message =='MIN_TRADE_REQUIREMENT_NOT_MET')
{
let returnResult = {
reject_reason:'balance',
status:'rejected'
}
return cb(null, returnResult)
}
}

if (typeof data !== 'object') {
console.log('bittrex API (trade) had an abnormal response, quitting.')
return cb(null, [])
return cb(null, {})
}

if(!data.success) {
if (data.message && data.message.match(recoverableErrors)) {
return retry('trade', args, data.message)
}
console.log(data.message)
return cb(null, [])
}


var order = {
id: data && data.result ? data.result.uuid : null,
Expand Down Expand Up @@ -269,7 +321,15 @@ module.exports = function container(get, set, clear) {
var params = {
uuid: opts.order_id
}
bittrex_authed.getorder(params, function (data) {
bittrex_authed.getorder(params, function (data,err) {
if (err != null && data == null)
{
data = {}
data.message = err.message
data.success = err.success
data.result = err.result
}

if (typeof data !== 'object') {
console.log('bittrex API (getorder) had an abnormal response, quitting.')
return cb(null, [])
Expand Down Expand Up @@ -302,7 +362,7 @@ module.exports = function container(get, set, clear) {

// return the property used for range querying.
getCursor: function (trade) {
return (trade.time || trade);
return (trade.time || trade)
}
}
return exchange
Expand Down
2 changes: 1 addition & 1 deletion extensions/exchanges/bittrex/update-products.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

var bittrex = require('node.bittrex.api')
var bittrex = require('node-bittrex-api')

var mapping
var products = []
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
"minimist": "^1.2.0",
"moment": "^2.18.1",
"mongodb": "^3.0.1",
"node-bittrex-api": "^0.8.2",
"node-prowl": "^0.1.7",
"node-sass": "^4.7.2",
"node-telegram-bot-api": "^0.30.0",
"node.bittrex.api": "^1.0.0",
"number-abbreviate": "^2.0.0",
"numbro": "highvelocityspace/numbro",
"path": "^0.12.7",
Expand Down