gazbert / bxbot

A simple Bitcoin trading bot written in Java.
MIT License
779 stars 275 forks source link

Invalid Nonce #57

Closed 151henry151 closed 6 years ago

151henry151 commented 7 years ago

It was running fine yesterday, but when I turned it off and made some changes in the configuration (lowered the minimum percentage gain and raised the amount to purchase at a time) and restarted it, I got these errors. I don't know if this is really a bug/issue with the code, or if it is just user error, but I can't find any place to ask so I'm putting this here in the hopes of somebody helping me resolve this issue.


Exchange Adapter:

com.gazbert.bxbot.exchanges.GeminiExchangeAdapter


Event Time:

Mon Jun 26 09:09:44 EDT 2017


Event Details:

A FATAL error has occurred in Exchange Adapter! Details: Failed to connect to Exchange due to unexpected IO error. ErrorStream Response: {"result":"error","reason":"InvalidNonce","message":"Nonce '1498482583' has not increased since your last call to the Gemini API."} Cause: java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.gemini.com/v1/balances


Action Taken:

The bot will shutdown NOW! Check the bot logs for more information.


Stacktrace:

com.gazbert.bxbot.trading.api.TradingApiException: Failed to connect to Exchange due to unexpected IO error. ErrorStream Response: {"result":"error","reason":"InvalidNonce","message":"Nonce '1498482583' has not increased since your last call to the Gemini API."} at com.gazbert.bxbot.exchanges.AbstractExchangeAdapter.sendNetworkRequest(AbstractExchangeAdapter.java:250) at com.gazbert.bxbot.exchanges.GeminiExchangeAdapter.sendAuthenticatedRequestToExchange(GeminiExchangeAdapter.java:783) at com.gazbert.bxbot.exchanges.GeminiExchangeAdapter.getBalanceInfo(GeminiExchangeAdapter.java:441) at com.gazbert.bxbot.core.engine.TradingEngine.isEmergencyStopLimitBreached(TradingEngine.java:351) at com.gazbert.bxbot.core.engine.TradingEngine.runMainControlLoop(TradingEngine.java:224) at com.gazbert.bxbot.core.engine.TradingEngine.start(TradingEngine.java:194) at com.gazbert.bxbot.BXBot.run(BXBot.java:55) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) at com.gazbert.bxbot.BXBot.main(BXBot.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.gemini.com/v1/balances at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at com.gazbert.bxbot.exchanges.AbstractExchangeAdapter.sendNetworkRequest(AbstractExchangeAdapter.java:184) ... 21 more

21098 [main] 2017-06-26 09:09:44,829 FATAL TradingEngine runMainControlLoop() - BX-bot is shutting down NOW! 21104 [main] 2017-06-26 09:09:44,835 INFO BXBot logStarted() - Started BXBot in 19.528 seconds (JVM running for 21.146)`

151henry151 commented 7 years ago

I'm now able to run the script again, I issued myself a new API key and it works with the new key, however I'm still not understanding what caused the issue or how to avoid it in the future, so I'm leaving this open for the moment. Apologies if this is not a real bug and merely a misuse/misunderstanding on my end.

gazbert commented 7 years ago

Hi Henry, thanks for reporting.

The nonce value is seeded off a unique time and incremented for every API call made to the exchange. It shouldn't be possible to send the same nonce from the adapter, but there could be a bug in there somewhere! Or it could be an issue with the exchange.

If it happens again, change the logging of the bot to debug as described here: https://github.com/gazbert/bxbot#logging The logs will dump out the nonce value in the request, so we can check if it's a duplicate. You'll need to restart the bot for the changes to take place.

I'll take a look at the code too. Not seen this with Gemini, but I think I've seen it once or twice with BTC-e.

Changing the API keys shouldn't affect the nonce handling.

I'll keep the issue open for now until we can reproduce it.

151henry151 commented 7 years ago

Hi gazbert, I realize this is probably not the correct channel for this question but couldn't find any help elsewhere, in https://github.com/gazbert/bxbot/blob/master/config/markets.xml I was trying to switch BTC and USD so that I can run the ExampleScalpingStrategy but with BTC/USD swapped, i.e. with USD as the base currency and BTC as the countercurrency. I thought it would be as simple as switching the word BTC for USD in two places in the markets.xml config file, but that didn't work, I'm hoping you might give me some guidance on how I could achieve that result.

151henry151 commented 7 years ago

I've encountered the bug again and logged the errors here, hopefully this information is helpful.

http://termbin.com/2tux

gazbert commented 7 years ago

Hi Henry

I've had a look at the logs you linked to, but there's no error in there relating to an invalid nonce - is this the correct log file?

I've got a bot running on Gemini right now (since 25 June) and not seen the nonce issue yet - it's hitting the exchange every 10s. Will keep an eye on it...

gazbert commented 6 years ago

Closing this as not able to reproduce.

@151henry151 - Please re-open the issue with logs etc if it happens again.