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

errmsg: 'E11000 duplicate key error collection: zenbot4.trades index: _id_ dup key: #1513

Open DrMabuse-2017 opened 6 years ago

DrMabuse-2017 commented 6 years ago

System information

Hello guys... The zenbot works for binance.com very well.

Now i test the trade for bittrex.com...and will running 2 trades-commands (binance + bittrex) When i start first time the bittrex trade i become a Error "duplicate key error". But the zenbot still works.

./zenbot.sh trade bittrex.XVG-BTC --debug --conf=conf.bittrex.js --strategy trend_ema --buy_pct=98 --sell_pct=30 --period=3m --min_periods=56 --trend_ema=15 --neutral_rate=0.06 --period_length=1m fetching pre-roll data: Please note: the bittrex api does not support backfilling directly. Backfill is indirectly supported thru the use of a hybrid system that combines a low resolution long term market of about 10 days and a short term high res market of the last 1-5 minutes. Please note: make sure to set the --period_length=1m to make sure data for trade/paper is fetched.

{ MongoError: E11000 duplicate key error collection: zenbot4.trades index: id dup key: { : "bittrex.XVG-BTC-1521354017437" } name: 'MongoError', message: 'E11000 duplicate key error collection: zenbot4.trades index: id dup key: { : "bittrex.XVG-BTC-1521354017437" }', driver: true, index: 0, code: 11000, errmsg: 'E11000 duplicate key error collection: zenbot4.trades index: id dup key: { : "bittrex.XVG-BTC-1521354017437" }' }

If i kill and start the same trade-command for the second time, then this error does not come.

The conf.bittrex.js is a copy from conf.binance.js but different port.

Many Thanks Mario

defkev commented 6 years ago

Can confirm.

Even true i have only seen this once on a single period.save by now (on a node running 10 bots simultaneously with tens of thousands of periods saved) and i wasn't able to reproduce it since then.

Even added some additional debugging in my repo (see 0ed0b6bb01e17c590f139c0e85599ad1228a5d9c) to hopefully get some more info out of it but so far it hasn't happened again.

For clarification regarding the error:

First of all it doesn't really make sense as we are using save with _id defined which simply means

create the record or update it if it already exists

Now considering we use crypto.randomBytes to generate a (hopefully uniqe) _id and the randomBytes implementation in node simply uses OpenSSL's RAND_bytes which in turn relies on /dev/urandom (which uses the systems IO) it would theoretically (like-super-extremely-unlikely but still) be possible on a low-IO system for two consecutive calls of randomBytes to actually really return the same value.

:boom:

Well, this still doesn't explain why the mongo driver isn't simply updating the record and instead decides to throw an exception.

One possible workaround could be to increase the size used by randomBytes (currently its 4) to increase the uniqueness of our _id but then again taking what i wrote above into account i am not even sure if this would fix the problem for good.

DrMabuse-2017 commented 6 years ago

@defkev ok thanks

you running 10 bots? oha...

I tested many command-lines with different params. But i loose everytime money. Can sou send me a good command-line for binance and bittrex? I think that could help me

Best Regards Mario