Skip to content
This repository was archived by the owner on Feb 16, 2020. It is now read-only.

Support for Bitfinex Importing (take 2) #1145

Closed
wants to merge 46 commits into from
Closed

Support for Bitfinex Importing (take 2) #1145

wants to merge 46 commits into from

Conversation

cmroche
Copy link
Contributor

@cmroche cmroche commented Sep 21, 2017

  • What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

(feature) Support for importing trade data from Bitfinex

  • What is the current behavior? (You can also link to an open issue here)

Issue #918

  • What is the new behavior (if this is a feature change)?

Bitfinex import is functional though due to the high volume nature of this exchange it should be noted that those imports can be very slow and large.

The importer uses a 2.5 second delay between requests to prevent coming up against the API rate limit. While the Bitfinex API documentation says that you can either do 60 or 90 requests per second (depending on the source in their website) in practice I'm finding it to be a little bit lower than that.

Since it is only possible to import historical data using the v2 api, I've update the bitfinex-api-node library and made minor changes for compatibility to the initialization code. The regular exchange functions still use the v1 API as v2 is considered beta and a bit of a pain to change over.

  • Other information:

Bitfinex's new API for trades uses a time range for the import, start and end must be specified for the query to work, and results are limited to a max of 1000 per query. However if the time range exceeds the record limit the API will return results from the end point first heading towards start, that is will return more recent trades first. Because of this approach it is necessary to do the import by iterating backwards, however since this doesn't work with many modules the system will substep by breaking the import into sequential time ranges, then reverse iterate to fill them before sending to the dispatcher.

@streetbits
Copy link

@cmroche Receiving this error when trying to import data from bitfinex

/home/gekko3/importers/exchanges/bitfinex.js:24
    this.bitfinex.makePublicRequest(path, (err, data) => {
                  ^

TypeError: this.bitfinex.makePublicRequest is not a function
    at Trader.Fetcher.getTrades (/home/gekko3/importers/exchanges/bitfinex.js:24:19)
    at Trader.bound [as getTrades] (/home/gekko3/node_modules/lodash/dist/lodash.js:729:21)
    at Object.fetch (/home/gekko3/importers/exchanges/bitfinex.js:71:17)
    at Market.get (/home/gekko3/core/markets/importer.js:95:16)
    at Market.bound [as get] (/home/gekko3/node_modules/lodash/dist/lodash.js:729:21)
    at new Market (/home/gekko3/core/markets/importer.js:80:8)
    at /home/gekko3/core/pipeline.js:187:20
    at /home/gekko3/node_modules/async/dist/async.js:3853:9
    at /home/gekko3/node_modules/async/dist/async.js:484:16
    at replenish (/home/gekko3/node_modules/async/dist/async.js:1025:25)
RECEIVED ERROR IN IMPORT 9603528732250863
Child process has died.

@cmroche
Copy link
Contributor Author

cmroche commented Sep 26, 2017

@streetbits You probably need to npm install as the bitfinex-node-api package was updated to support this.

@streetbits
Copy link

Worked like a charm, thanks man you're the best

This is noticeable when grabing BCH before August.
@PhatDoan
Copy link

PhatDoan commented Sep 30, 2017

@streetbits Could you please show me the step how you got it import? I tried but it still shows this error. Gekko can use Bitfinex exchange in watch mode.

Do I need to do anything else? Thank you.

$ node gekko --config myconfig.js --import

2017-09-30 08:29:42 (INFO):	Setting up Gekko in importer mode
2017-09-30 08:29:42 (INFO):	
2017-09-30 08:29:42 (WARN):	The plugin Trading Advisor does not support the mode importer. It has been disabled.
2017-09-30 08:29:42 (WARN):	The plugin Paper Trader does not support the mode importer. It has been disabled.
2017-09-30 08:29:42 (WARN):	The plugin Performance Analyzer does not support the mode importer. It has been disabled.
2017-09-30 08:29:42 (DEBUG):	No end date specified for importing, setting to 2017-09-30 01:29:42

 ERROR: The exchange Bitfinex does not provide full history (or Gekko doesn't support importing it)

@cmroche
Copy link
Contributor Author

cmroche commented Sep 30, 2017

@PhatDoan It looks like you didn't apply the changes correctly. The exchanges/bitfinex.js file should have an update with providesFullHistory: true which can be seen in the changes for the pull request.

@PhatDoan
Copy link

Ah my bad. Thank you!

@askmike
Copy link
Owner

askmike commented Sep 30, 2017

Great stuff, Though I am gettting rate limited when I run this (see below). Would you mind adding a delay between fetches?

Here is the error (I logged from your monkey patched getTrades):

2017-09-30 18:34:29 (DEBUG):  Processing 2512 new trades. From 2017-08-16 20:59:57 UTC to 2017-08-16 22:59:46 UTC. (2 hours)
2017-09-30 18:34:33 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502845200000&end=1502931600000
2017-09-30 18:34:36 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502843136000&end=1502929536000
2017-09-30 18:34:38 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502842349000&end=1502928749000
2017-09-30 18:34:41 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502841683000&end=1502928083000
2017-09-30 18:34:44 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502840311000&end=1502926711000
2017-09-30 18:34:46 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502839841000&end=1502926241000
2017-09-30 18:34:49 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502838137000&end=1502924537000
2017-09-30 18:34:50 (DEBUG):  Processing 3116 new trades. From 2017-08-16 23:00:04 UTC to 2017-08-17 00:59:46 UTC. (2 hours)
2017-09-30 18:34:53 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502852400000&end=1502938800000
2017-09-30 18:34:56 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502849793000&end=1502936193000
2017-09-30 18:34:59 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502848264000&end=1502934664000
2017-09-30 18:34:59 (DEBUG):  ERROR Error: StatusCodeError: 429 - ["error",11010,"ratelimit: error"]
    at rp.then.catch (/Users/pear/projects/gekko/node_modules/bitfinex-api-node/rest2.js:78:26)
    at tryCatcher (/Users/pear/projects/gekko/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:689:18)
    at Async._drainQueue (/Users/pear/projects/gekko/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/Users/pear/projects/gekko/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/Users/pear/projects/gekko/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)
2017-09-30 18:34:59 (DEBUG):  Bitfinex returned an error, retrying..
2017-09-30 18:35:09 (DEBUG):  Querying trades with: trades/tBTCUSD/hist?limit=1000&start=1502848264000&end=1502934664000
2017-09-30 18:35:09 (DEBUG):  ERROR Error: StatusCodeError: 429 - ["error",11010,"ratelimit: error"]
    at rp.then.catch (/Users/pear/projects/gekko/node_modules/bitfinex-api-node/rest2.js:78:26)
    at tryCatcher (/Users/pear/projects/gekko/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/pear/projects/gekko/node_modules/bluebird/js/release/promise.js:689:18)
    at Async._drainQueue (/Users/pear/projects/gekko/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/Users/pear/projects/gekko/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/Users/pear/projects/gekko/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)
2017-09-30 18:35:09 (DEBUG):  Bitfinex returned an error, retrying..

@cmroche
Copy link
Contributor Author

cmroche commented Sep 30, 2017

@askmike There already is a rate limiter, I set it to 2.5 seconds based on testing my local system (which admittedly is an older and slower computer). I bumped it up a second so that should fix it.

@cmroche
Copy link
Contributor Author

cmroche commented Sep 30, 2017

FYI this PR now showing a lot of commits since I rebased develop onto this branch instead of merging, sorry in advance.

@askmike
Copy link
Owner

askmike commented Oct 1, 2017

Yes that seems to work fine, if someone runs into this I will just bump the timer some more. Thanks a lot :)

@askmike
Copy link
Owner

askmike commented Oct 1, 2017

Thanks a lot :) I wasn't able to merge automatically so have done it manually. It's now all in the latest develop!

@askmike askmike closed this Oct 1, 2017
@cmroche cmroche deleted the bitfinex_import branch October 1, 2017 13:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants