python ccxt中的Nonce覆盖

所以当我尝试使用ccxt进行交易时,我会继续收到此消息:

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

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

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

这是我得到的错误:

Traceback(最近一次调用最后一次):文件“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(req,response)文件“C:\ Users \ Scott \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ urllib \ request.py”,第581行,http_response'htt',请求,响应,代码,消息,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 result = 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:han期间请求错误对上述异常进行了处理,发生了另一个异常:Traceback(最近一次调用最后一次):MomentumTrader中的文件“”,第1行('ETH','BTC')文件“C:/Users/Scott/Desktop/MomentumTraderFifth.py “,第199行,在MomentumTrader BuyAll(currencyPair)文件”C:/Users/Scott/Desktop/MomentumTraderFifth.py“,第89行,在BuyAll binance.createLimitBuyOrder(符号,金额,订单['问道'] [orderBookPosition] [ 0])文件“C:\ Users \ Scott \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ ccxt \ exchange.py”,第646行,在createLimitBuyOrder中返回self.create_limit_buy_order(市场,金额,价格) ,params)文件“C:\ Users \ Scott \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ ccxt \ exchange.py”,第634行,在create_limit_buy_order中返回self.create_order(市场,'限制' ,'买',金额,价格,参数)文件“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,方法, Headers ,正文)文件”C:\用户\ Scott \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ ccxt \ exchange.py“,第244行,获取self.raise_error(错误,网址,方法,e,详细信息)文件”C: \ Users \ Scott \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ ccxt \ exchange.py“,第181行,在raise_error详细信息中,ccxt.errors.ExchangeNotAvailable:binance POST https://www.binance .com / api / v1 / order 400 Bad Request(可能的原因:无效的API密钥,坏的或旧的nonce,交换失败或离线,维护,DDoS保护,速率限制,{“代码”: - 1021,“msg “:”此请求的时间戳无效 . “})

回答(1)

2 years ago

请从ccxt库手册中仔细阅读Troubleshooting部分 . 这是由于过期的密钥 . 你用另一个密钥对试了吗?

此外,对于Bitfinex,不需要将nonce设置为毫秒,默认情况下它是以毫秒为单位 . 试试这个样本,看看它是否适合你:

import ccxt

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

print(bitfinex.fetch_balance())

如果它确实有效,那么你应该创建一个全新的密钥对,从那里一切都应该是正常的 . 不用担心,如果您创建新密钥对,则不会在您的帐户上放松资金 .