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

Binance: Add OHLC candle backfilling #1614

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 72 additions & 44 deletions extensions/exchanges/binance/exchange.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const ccxt = require('ccxt')
, path = require('path')
// eslint-disable-next-line no-unused-vars
, colors = require('colors')
, _ = require('lodash')
, path = require('path')
// eslint-disable-next-line no-unused-vars
, colors = require('colors')
, _ = require('lodash')

module.exports = function bittrex (conf) {
var public_client, authed_client
let firstRun = true
let allowGetMarketCall = true

function publicClient () {
if (!public_client) public_client = new ccxt.binance({ 'apiKey': '', 'secret': '' })
Expand All @@ -23,11 +25,11 @@ module.exports = function bittrex (conf) {
}

/**
* Convert BNB-BTC to BNB/BTC
*
* @param product_id BNB-BTC
* @returns {string}
*/
* Convert BNB-BTC to BNB/BTC
*
* @param product_id BNB-BTC
* @returns {string}
*/
function joinProduct(product_id) {
let split = product_id.split('-')
return split[0] + '/' + split[1]
Expand Down Expand Up @@ -59,14 +61,12 @@ module.exports = function bittrex (conf) {

getTrades: function (opts, cb) {
var func_args = [].slice.call(arguments)

, trades = []
, maxTime = 0
var client = publicClient()
var args = {}
if (opts.from) {
args.startTime = opts.from
}
if (opts.to) {
args.endTime = opts.to
}
if (opts.from) args.startTime = opts.from
if (opts.to) args.endTime = opts.to
if (args.startTime && !args.endTime) {
// add 12 hours
args.endTime = parseInt(args.startTime, 10) + 3600000
Expand All @@ -75,23 +75,51 @@ module.exports = function bittrex (conf) {
// subtract 12 hours
args.startTime = parseInt(args.endTime, 10) - 3600000
}

var client = publicClient()
client.fetchTrades(joinProduct(opts.product_id), undefined, undefined, args).then(result => {
var trades = result.map(function (trade) {
return {
trade_id: trade.id,
time: trade.timestamp,
size: parseFloat(trade.amount),
price: parseFloat(trade.price),
side: trade.side
}
if (allowGetMarketCall != true) {
cb(null, [])
return null
}
if (firstRun) {
client.fetchOHLCV(joinProduct(opts.product_id), args.timeframe, opts.from).then(result => {
var lastVal = 0
trades = result.map(function(trade) {
let buySell = parseFloat(trade[4]) > lastVal ? 'buy' : 'sell'
lastVal = parseFloat(trade[4])
if (Number(trade[0]) > maxTime) maxTime = Number(trade[0])
return {
trade_id: trade[0]+''+ (trade[5]+'').slice(-2) + (trade[4]+'').slice(-2),
time: trade[0],
size: parseFloat(trade[5]),
price: parseFloat(trade[4]),
side: buySell
}
})
cb(null, trades)
}).catch(function(error) {
firstRun = false
allowGetMarketCall = false
setTimeout(()=>{allowGetMarketCall = true}, 5000)
console.error('[OHLCV] An error occurred', error)
return retry('getTrades', func_args, error)
})
cb(null, trades)
}).catch(function (error) {
console.error('An error occurred', error)
return retry('getTrades', func_args)
})
}
else {
client.fetchTrades(joinProduct(opts.product_id), undefined, undefined, args).then(result => {
var trades = result.map(function (trade) {
return {
trade_id: trade.id,
time: trade.timestamp,
size: parseFloat(trade.amount),
price: parseFloat(trade.price),
side: trade.side
}
})
cb(null, trades)
}).catch(function (error) {
console.error('An error occurred', error)
return retry('getTrades', func_args)
})
}
},

getBalance: function (opts, cb) {
Expand All @@ -111,10 +139,10 @@ module.exports = function bittrex (conf) {
})
cb(null, balance)
})
.catch(function (error) {
console.error('An error occurred', error)
return retry('getBalance', func_args)
})
.catch(function (error) {
console.error('An error occurred', error)
return retry('getBalance', func_args)
})
},

getQuote: function (opts, cb) {
Expand All @@ -123,10 +151,10 @@ module.exports = function bittrex (conf) {
client.fetchTicker(joinProduct(opts.product_id)).then(result => {
cb(null, { bid: result.bid, ask: result.ask })
})
.catch(function (error) {
console.error('An error occurred', error)
return retry('getQuote', func_args)
})
.catch(function (error) {
console.error('An error occurred', error)
return retry('getQuote', func_args)
})
},

getDepth: function (opts, cb) {
Expand All @@ -135,10 +163,10 @@ module.exports = function bittrex (conf) {
client.fetchOrderBook(joinProduct(opts.product_id), {limit: opts.limit}).then(result => {
cb(null, result)
})
.catch(function(error) {
console.error('An error ocurred', error)
return retry('getDepth', func_args)
})
.catch(function(error) {
console.error('An error ocurred', error)
return retry('getDepth', func_args)
})
},

cancelOrder: function (opts, cb) {
Expand Down