1

所以我在尝试使用 ccxt 进行交易时不断收到此消息:

“此请求的时间戳无效。”

我已经阅读了 ccxt 手册,它说要覆盖我应该使用的随机数:

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

但我只是不明白如何使这项工作。我不是一个 python 程序员太先进。

任何帮助绝对值得赞赏

这是我试图运行的代码:

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

这是我得到的错误:

回溯(最近一次调用最后):文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 209 行,在 fetch response = opener.open (request, timeout=int(self.timeout / 1000)) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py”,第 471 行,打开响应 = meth (请求,响应)文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py”,第 581 行,在 http_response 'http',请求,响应,代码,味精, hdrs) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py”,第 509 行,错误返回 self._call_chain(*args) 文件“C:\Users\ Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py”,第 443 行,在 _call_chain 结果 = func(*args) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\urllib\request.py”,第 589 行,在 http_error_default 中引发 HTTPError(req.full_url,代码, msg, hdrs, fp) urllib.error.HTTPError: HTTP 错误 400: 错误请求

在处理上述异常的过程中,又出现了一个异常:

回溯(最后一次调用):文件“”,第 1 行,在 MomentumTrader('ETH','BTC') 文件“C:/Users/Scott/Desktop/MomentumTraderFifth.py”,第 199 行,在 MomentumTrader BuyAll(currencyPair ) 文件“C:/Users/Scott/Desktop/MomentumTraderFifth.py”,第 89 行,BuyAll binance.createLimitBuyOrder(symbol,amount,orderbook['asks'][orderBookPosition][0]) 文件“C:\Users\ Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 646 行,在 createLimitBuyOrder 中返回 self.create_limit_buy_order(market, amount, price, params) 文件“C:\Users\ Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 634 行,在 create_limit_buy_order return self.create_order(market, 'limit', '购买'、金额、价格、参数)文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py”,第 986 行,在 create_order response = self. privatePostOrder(self.extend(order, params)) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py”,第 1049 行,在请求响应中 = self.fetch(url, method, headers, body) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 244 行,在 fetch self .raise_error(error, url, method, e, details) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 181 行,在 raise_error详细信息,ccxt.errors.ExchangeNotAvailable:binance POST\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py”,第 986 行,在 create_order response = self.privatePostOrder(self.extend(order, params)) 文件中“ C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py",第 1049 行,在请求响应 = self.fetch(url, method, headers, body) 文件中“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 244 行,在 fetch self.raise_error(error, url, method, e, details)文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py”,第 986 行,在 create_order response = self.privatePostOrder(self.extend(order, params)) 文件中“ C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py",第 1049 行,在请求响应 = self.fetch(url, method, headers, body) 文件中“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 244 行,在 fetch self.raise_error(error, url, method, e, details)文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py”,第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST第 986 行,在 create_order response = self.privatePostOrder(self.extend(order, params)) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py ”,第 1049 行,在请求响应 = self.fetch(url, method, headers, body) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange. py”,第 244 行,在 fetch self.raise_error(error, url, method, e, details) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange .py”,第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST第 986 行,在 create_order response = self.privatePostOrder(self.extend(order, params)) File "C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py ”,第 1049 行,在请求响应 = self.fetch(url, method, headers, body) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange. py”,第 244 行,在 fetch self.raise_error(error, url, method, e, details) 文件“C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange .py”,第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py",第 1049 行,在请求响应 = self.fetch(url, method, headers, body) File "C :\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 244 行,在 fetch self.raise_error(error, url, method, e, details) 文件中" C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchanges.py",第 1049 行,在请求响应 = self.fetch(url, method, headers, body) File "C :\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 244 行,在 fetch self.raise_error(error, url, method, e, details) 文件中" C:\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST\Users\Scott\AppData\Local\Programs\Python\Python35\lib\site-packages\ccxt\exchange.py",第 181 行,在 raise_error 详细信息中,ccxt.errors.ExchangeNotAvailable:binance POSThttps://www.binance.com/api/v1/order 400 错误请求(可能的原因:无效的 API 密钥、错误或旧的 nonce、交易所关闭或离线、维护中、DDoS 保护、速率限制、{"code ":-1021,"msg":"此请求的时间戳无效。"})

4

1 回答 1

1

请仔细阅读 ccxt 库手册中的故障排除部分。这是由于过期的密钥。您是否尝试过使用另一个密钥对?

此外,对于 Bitfinex,不需要将 nonce 设置为毫秒,默认情况下它已经以毫秒为单位。试试这个示例,看看它是否适合您:

import ccxt

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

print(bitfinex.fetch_balance())

如果它确实有效,那么您应该创建一个新的密钥对,从那里开始一切都应该正常。不用担心,如果您创建新的密钥对,您的帐户不会丢失资金。

于 2017-08-30T16:10:40.490 回答