python ccxt

时间:2017-08-30 15:19:34

标签: python bitcoin trading nonce ccxt

所以当我尝试使用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

以下是我遇到的错误:

  

Traceback(最近一次调用最后一次):文件   “C:\用户\斯科特\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ ccxt \ exchange.py”   第209行,取指令       response = opener.open(request,timeout = int(self.timeout / 1000))文件   “C:\用户\斯科特\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \的urllib \ request.py”   第471行,公开       response = meth(req,response)文件“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行,错误       return 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,code,msg,hdrs,fp)urllib.error.HTTPError:HTTP错误400:错误请求

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件“”,第1行,in          MomentumTrader('ETH','BTC')文件“C:/Users/Scott/Desktop/MomentumTraderFifth.py”,第199行,in   MomentumTrader       BuyAll(currencyPair)文件“C:/Users/Scott/Desktop/MomentumTraderFifth.py”,第89行,在BuyAll       binance.createLimitBuyOrder(符号,量,手持订单[ '询问'] [orderBookPosition] [0])   文件   “C:\用户\斯科特\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ ccxt \ exchange.py”   第646行,在createLimitBuyOrder中       return self.create_limit_buy_order(market,amount,price,params)文件   “C:\用户\斯科特\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ ccxt \ exchange.py”   第634行,在create_limit_buy_order中       return self.create_order(market,'limit','buy',amount,price,params)File   “C:\用户\斯科特\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ ccxt \ exchanges.py”   第986行,在create_order中       response = self.privatePostOrder(self.extend(order,params))文件   “C:\用户\斯科特\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ ccxt \ exchanges.py”   第1049行,请求中       response = self.fetch(url,method,headers,body)文件“C:\ Users \ Scott \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ ccxt \ exchange.py”,   第244行,取指       self.raise_error(错误,url,方法,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错误请求(可能)   原因:无效的API密钥,坏的或旧的nonce,交换失败或   离线,维护,DDoS保护,速率限制,   {“code”: - 1021,“msg”:“此请求的时间戳无效。”})

1 个答案:

答案 0 :(得分:2)

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

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

import ccxt

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

print(bitfinex.fetch_balance())

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