Nonce Override in python ccxt

2.9k Views Asked by At

so I keep recieveing this message when trying to place trades using ccxt:

"Timestamp for this request is not valid."

I've read the ccxt manual and it says to override the nonce I should use:

class MyBitfinex (ccxt.bitfinex):
    def nonce (self):
        return self.milliseconds ()

But I just don't understand how to make this work. I'm not too advanced of a python programmer.

any help is definitely appreciated

Here is the code I am trying to get to run:

def BuyAll (symbol):

base = markets[symbol]['base']
quote = markets[symbol]['quote']
Balances = binance.fetchBalance()
quoteBalance = Balances[quote]['free']
stopOut = 0
orderBookPosition = 0

while quoteBalance > stopOut:
    print('quote balance',quoteBalance)
    runningBalance = quoteBalance - stopOut
    orderbook = binance.fetchOrderBook(symbol)


    if (orderbook['asks'][orderBookPosition][1])*(orderbook['asks'][orderBookPosition][0])< runningBalance:
        if base == 'BNB':
            quantity = truncate(orderbook['asks'][orderBookPosition][1],0)
        else:
            quantity = truncate(orderbook['asks'][orderBookPosition][1],3)
        print('quantity',quantity)
        binance.createLimitBuyOrder(symbol,quantity,orderbook['asks'][orderBookPosition][0])
        print('Bought' + ' ' + symbol)
        stopOut += (orderbook['asks'][orderBookPosition][1])*(orderbook['asks'][orderBookPosition][0])

    elif (runningBalance)/(orderbook['asks'][orderBookPosition][0]) > 0:

        if base == 'BNB':
            amount = int((runningBalance)/(orderbook['asks'][orderBookPosition][0]))

        else:
            amount = truncate((runningBalance)/(orderbook['asks'][orderBookPosition][0]),3)

        print('amount',amount)
        binance.createLimitBuyOrder(symbol,amount,orderbook['asks'][orderBookPosition][0])
        print('Bought' + ' ' + symbol)
        stopOut += (orderbook['asks'][orderBookPosition][1])*(orderbook['asks'][orderBookPosition][0])
    orderBookPosition += 1

Here is the error I am getting:

Traceback (most recent call last): File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py", line 209, in fetch response = opener.open(request, timeout=int(self.timeout / 1000)) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 471, in open response = meth(req, response) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 581, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 509, in error return self._call_chain(*args) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 443, in _call_chain result = func(*args) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 589, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in MomentumTrader('ETH','BTC') File "C:/Users/Scott/Desktop/MomentumTraderFifth.py", line 199, in MomentumTrader BuyAll(currencyPair) File "C:/Users/Scott/Desktop/MomentumTraderFifth.py", line 89, in BuyAll binance.createLimitBuyOrder(symbol,amount,orderbook['asks'][orderBookPosition][0]) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py", line 646, in createLimitBuyOrder return self.create_limit_buy_order(market, amount, price, params) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py", line 634, in create_limit_buy_order return self.create_order(market, 'limit', 'buy', amount, price, params) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py", line 986, in create_order response = self.privatePostOrder(self.extend(order, params)) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py", line 1049, in request response = self.fetch(url, method, headers, body) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py", line 244, in fetch self.raise_error(error, url, method, e, details) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py", line 181, in raise_error details, ccxt.errors.ExchangeNotAvailable: binance POST https://www.binance.com/api/v1/order 400 Bad Request (possible reasons: invalid API keys, bad or old nonce, exchange is down or offline, on maintenance, DDoS protection, rate-limiting, {"code":-1021,"msg":"Timestamp for this request is not valid."})

1

There are 1 best solutions below

5
On

Please, read the Troubleshooting section very carefully from ccxt library Manual. This is due to expired keys. Did you try it with another keypair?

Also, for Bitfinex, setting the nonce to milliseconds is not needed, it is in milliseconds already by default. Try this sample and see if it works for you:

import ccxt

bitfinex = ccxt.bitfinex ({
    'apiKey': '4FlEDtxDl35gdEiobnfZ72vJeZteE4Bb7JdvqzjIjHq',
    'secret': 'D4DXM8DZdHuAq9YptUsb42aWT1XBnGlIJgLi8a7tzFH',
})

print(bitfinex.fetch_balance())

If it does work then you should create a fresh new keypair and everything should be normal from there on. Don't worry, you won't loose funds on your account if you create a new keypair.