DeviaVir / zenbot

Zenbot is a command-line cryptocurrency trading bot using Node.js and MongoDB.
MIT License
8.22k stars 2.04k forks source link

Operations on Bitfinex not working #2551

Open Utfard opened 3 years ago

Utfard commented 3 years ago

System information

Describe the problem

Paper or real trading not working at all crashing with the error below. I added here the output for balance check as it gave the same error.

Source code / Error logs

$ node zenbot balance bitfinex.BTC-USD
[ 171567,
  'ticker',
  TradingTicker {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    _fields:
     { symbol: 0,
       bid: 1,
       bidSize: 2,
       ask: 3,
       askSize: 4,
       dailyChange: 5,
       dailyChangePerc: 6,
       lastPrice: 7,
       volume: 8,
       high: 9,
       low: 10 },
    _boolFields: [],
    symbol: 'tBTCUSD',
    bid: 18371,
    bidSize: 44.73358897,
    ask: 18374,
    askSize: 53.957501529999995,
    dailyChange: -163,
    dailyChangePerc: -0.0088,
    lastPrice: 18375,
    volume: 4230.82474971,
    high: 18630,
    low: 17935 },
  filterOverride: [ 'tBTCUSD' ] ]
[ 6580,
  'trades',
  PublicTrade {
    '0':
     { id: 540536227,
       mts: 1607636262755,
       amount: 0.62909999,
       price: 18375 },
    '1':
     { id: 540536225,
       mts: 1607636262755,
       amount: 0.1,
       price: 18374.218128 },
    '2':
     { id: 540536224,
       mts: 1607636262755,
       amount: 0.27090001,
       price: 18374 },
    '3':
     { id: 540536223,
       mts: 1607636262009,
       amount: -0.005,
       price: 18373 },
    '4':
     { id: 540536222,
       mts: 1607636254869,
       amount: 0.00863852,
       price: 18374 },
    '5':
     { id: 540536220,
       mts: 1607636253536,
       amount: 0.04683474,
       price: 18374 },
    '6':
     { id: 540536218,
       mts: 1607636253536,
       amount: 0.00538928,
       price: 18373.50953466 },
    '7':
     { id: 540536217,
       mts: 1607636253536,
       amount: 0.1306504,
       price: 18373 },
    '8':
     { id: 540536215,
       mts: 1607636253536,
       amount: 0.05387459,
       price: 18372.85957542 },
    '9':
     { id: 540536213,
       mts: 1607636253536,
       amount: 0.01704173,
       price: 18372.29935 },
    '10':
     { id: 540536212,
       mts: 1607636253536,
       amount: 0.47530925,
       price: 18372 },
    '11':
     { id: 540536211,
       mts: 1607636253536,
       amount: 0.27090001,
       price: 18372 },
    '12':
     { id: 540536210,
       mts: 1607636242489,
       amount: -0.6823254,
       price: 18366 },
    '13':
     { id: 540536209,
       mts: 1607636242489,
       amount: -0.7967,
       price: 18366 },
    '14':
     { id: 540536208,
       mts: 1607636242489,
       amount: -0.2205,
       price: 18368 },
    '15':
     { id: 540536207,
       mts: 1607636242489,
       amount: -1.4273,
       price: 18369 },
    '16':
     { id: 540536205,
       mts: 1607636242489,
       amount: -0.0493756,
       price: 18369.21753903 },
    '17':
     { id: 540536204,
       mts: 1607636242489,
       amount: -0.209553,
       price: 18370 },
    '18':
     { id: 540536203, mts: 1607636242489, amount: -0.25, price: 18370 },
    '19':
     { id: 540536202,
       mts: 1607636242489,
       amount: -0.0006,
       price: 18373 },
    '20':
     { id: 540536201, mts: 1607636242489, amount: -0.11, price: 18373 },
    '21':
     { id: 540536199,
       mts: 1607636224100,
       amount: 0.02104576,
       price: 18370.380572 },
    '22':
     { id: 540536198, mts: 1607636223989, amount: -0.01, price: 18370 },
    '23':
     { id: 540536197,
       mts: 1607636217823,
       amount: -0.005,
       price: 18370 },
    '24':
     { id: 540536195,
       mts: 1607636213481,
       amount: -0.00449899,
       price: 18368.0035269 },
    '25':
     { id: 540536193,
       mts: 1607636213481,
       amount: -0.00538928,
       price: 18368.19081718 },
    '26':
     { id: 540536191,
       mts: 1607636213481,
       amount: -0.01704173,
       price: 18368.461794 },
    '27':
     { id: 540536190,
       mts: 1607636213481,
       amount: -0.02307,
       price: 18374 },
    '28':
     { id: 540536186,
       mts: 1607636203570,
       amount: -0.005,
       price: 18374 },
    '29':
     { id: 540536185,
       mts: 1607636199008,
       amount: -0.005,
       price: 18374 },
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    _fields: { id: 0, mts: 1, amount: 2, price: 3 },
    _boolFields: [],
    _collection:
     [ [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object] ],
    length: 30,
    [Symbol(Symbol.iterator)]: [Function] },
  filterOverride: [ 'tBTCUSD' ] ]
/home/muthabor/git/zenbot/extensions/exchanges/bitfinex/exchange.js:32
`    trades.forEach(function (trade) {`
           ^

TypeError: trades.forEach is not a function
    at wsUpdateTrades (/home/muthabor/git/zenbot/extensions/exchanges/bitfinex/exchange.js:32:12)
    at ws_client.onTrades (/home/muthabor/git/zenbot/extensions/exchanges/bitfinex/exchange.js:227:44)
    at listeners.forEach (/home/muthabor/git/zenbot/node_modules/bitfinex-api-node/lib/transports/ws2.js:1223:9)
    at Array.forEach (<anonymous>)
    at Function._notifyListenerGroup (/home/muthabor/git/zenbot/node_modules/bitfinex-api-node/lib/transports/ws2.js:1219:15)
    at WSv2._propagateMessageToListeners (/home/muthabor/git/zenbot/node_modules/bitfinex-api-node/lib/transports/ws2.js:1171:12)
    at WSv2._handleTradeMessage (/home/muthabor/git/zenbot/node_modules/bitfinex-api-node/lib/transports/ws2.js:997:10)
    at WSv2._handleChannelMessage (/home/muthabor/git/zenbot/node_modules/bitfinex-api-node/lib/transports/ws2.js:761:12)
    at WSv2._onWSMessage (/home/muthabor/git/zenbot/node_modules/bitfinex-api-node/lib/transports/ws2.js:729:12)
    at WebSocket.emit (events.js:198:13)
    at Receiver.receiverOnMessage (/home/muthabor/git/zenbot/node_modules/ws/lib/websocket.js:825:20)
    at Receiver.emit (events.js:198:13)
    at Receiver.dataMessage (/home/muthabor/git/zenbot/node_modules/ws/lib/receiver.js:437:14)
    at Receiver.getData (/home/muthabor/git/zenbot/node_modules/ws/lib/receiver.js:367:17)
    at Receiver.startLoop (/home/muthabor/git/zenbot/node_modules/ws/lib/receiver.js:143:22)
    at Receiver._write (/home/muthabor/git/zenbot/node_modules/ws/lib/receiver.js:78:10)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Receiver.Writable.write (_stream_writable.js:299:11)
    at TLSSocket.socketOnData (/home/muthabor/git/zenbot/node_modules/ws/lib/websocket.js:900:35)
    at TLSSocket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
Utfard commented 3 years ago

Anyone else having this issue ?

Spritkopf commented 3 years ago

I am having the exact same issue. Using docker-compose in Manjaro, getting "TypeError: trades.forEach is not a function"

Have I written custom code (as opposed to using zenbot vanilla):NO OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Manjaro 20.2.1 Zenbot version (commit ref, or version):4.1.0 ( git ref 016ab678f657b4e17882ef84783938ddb18c7f2) Zenbot branch: unstable NodeJS version: Python version (when using a python script): Exact command to reproduce (include everything): docker-compose exec server zenbot balance bitfinex.XRP-USD (after docker-compose up -d) Did I make any changes to conf-sample.js?: copied to conf.js, only added bitfinex API key

Note: I modified the docker-compose.yml to override the entrypoint so that zenbot is not started automatically when starting the container. I added this to the "server" service:

entrypoint: /bin/sh
    command: ""
gergap commented 3 years ago

I have the same problem.

gergap commented 3 years ago

Hi again,

I commented out the code in function wsUpdates in extensions/exchanges/bitfinex/exchange.js. This way it at least runs. I'm not sure about the consequences. Obviously you cannot get a trade list this way.

I was able to trigger a manual orders this way, e.g. "./zenbot sell " In interactive mode it does not work, but I think that's another problem.

I'm still testing this in manual mode with small amounts, before I use automatic trading.

Osolemio commented 3 years ago

Hi all, Have the same issue. Now testing is in progress. What I have changed in exchange.js:

When wsUpdateTrades is called, trades may come as an object with _collection, or without it (single trade). So:


function wsUpdateTrades(pair, trades) {

    if (trades["id"]) {
    var newTrade = {
        trade_id: Number(trades.id),
        time: Number(trades.mts),
        size: Math.abs(trades.amount),
        price: Number(trades.price),
        side: trades.amount > 0 ? 'buy' : 'sell'
      }
      ws_trades.push(newTrade)
    } else if (trades["_collection"])
    {
    const collection_trades = trades["_collection"]
    collection_trades.forEach(function (trade) {
      var newTrade = {
        trade_id: Number(trade.id),
        time: Number(trade.mts),
        size: Math.abs(trade.amount),
        price: Number(trade.price),
        side: trade.amount > 0 ? 'buy' : 'sell'
      }
      ws_trades.push(newTrade)
    })

    }

    if (ws_trades.length > 1010)
      ws_trades.shift()
  }

then, in the wsMessage function, I removed the line: console.log(message) It should have been as if(so.debug) obviously. Otherwise, it interferes with screen output.

Then, walletUpdate was called too often. It caused websocket blocking and no new trades were coming. Changed timeout: function waitForCalc(method, args, cb) { setTimeout(function () { exchange[method].call(exchange, args, cb) }, 2000) }

And after these changes, I would drop all the collections: trades and sessions etc in mongo db and start from the empty.

Osolemio commented 3 years ago

Another bug fix: After some time, or when the terminal was inactive (not in the focus), the process.stdout.write stopped printing while the trades were updating. I suggest it cased by the timers. So, it's seemed to work better with this changes in engine.js:

function writeReport (is_progress, blink_off) {

    if ((so.mode === 'sim' || so.mode === 'train') && !so.verbose) {

      if(so.silent) return

      is_progress = true

    }
    else if (is_progress && typeof blink_off === 'undefined' && s.vol_since_last_blink) {
      s.vol_since_last_blink = 0
      const ref = setTimeout(function () {
        writeReport(true, true)
      }, 200)

      ref.unref()

      const ref1 = setTimeout(function () {
        writeReport(true, false)
      }, 400)

      ref1.unref()

      const ref2 = setTimeout(function () {
        writeReport(true, true)
      }, 600)

      ref2.unref()
      const ref3 = setTimeout(function () {
        writeReport(true, false)
      }, 800)

      ref3.unref()
    }
    ........................

I did the same trick for the queues everywhere in exchange.js with all setTimeout

tekknikk commented 2 years ago

@Osolemio you rock