首页 > 解决方案 > 精度超过为该资产定义的最大值。python-binance 模块

问题描述

我用 python-binance 模块编写了一个交易机器人,该机器人从消息中获取硬币名称,而不是像 take-message 一样开仓 > 在消息中对硬币开多头头寸 > 为该仓位设置限价卖单

机器人可以做前两个,但当它尝试做最后一个时,它会给出错误““精度超过为该资产定义的最大值。” 我在互联网上查找了解决方案,并尝试使用此代码设置 sellPrice

float(str(price).split('.')[0] + "." + str(price).split('.')[1][0:5])+(price*0.3/100)

我从互联网上获取第一部分代码 float(str(price).split('.')[0] + "." + str(price).split('.')[1][0:5])

并从我购买的价格中添加了设置上面的 sellPrice %0.3 的部分

这是我的代码

coinName = "ONEUSDT"
print(coinName)
symbol = coinName
amount = "1"
leverage = "20"
self.client.futures_change_leverage(symbol=symbol, leverage=leverage)
price=float(self.client.get_symbol_ticker(symbol=symbol)["price"])
print(price)
amount = float(amount)
leverage = float(leverage)
quantity =(amount*leverage)/price
quantity = int(quantity)
self.client.futures_create_order(symbol=symbol,side="BUY",type="MARKET",quantity=quantity)
time.sleep(3)
self.client.futures_create_order(symbol=symbol,side="SELL",type="LIMIT",price =float(str(price).split('.')[0] + "." + str(price).split('.')[1][0:5])+(price*0.3/100),quantity=quantity,timeInForce="GTC")

你能帮助我吗 ?

标签: pythonbinance

解决方案


看起来您正在寻找 tick_size,它将显示将价格四舍五入到小数位数。您可以导入一个辅助模块以进行舍入,我们可以在其中运行我们的刻度大小和价格。这些文档可以在这里看到:

币安订单过滤器

我们需要确保从 app.py 顶部的辅助模块导入辅助函数。然后在脚本正文中插入以下函数。假设您的互联网消息正确地给了我们一个价格,那么cost我们可以将变量作为四舍五入的价格插入到我们的最终订单中。我只是在我的解释器中运行了它,它适用于替换值:

from binance.helpers import round_step_size # add at top

cost = float(str(price).split('.')[0] + "." + str(price).split('.')[1][0:5])+(price*0.3/100)

data = self.client.futures_exchange_info() # request data
info = data['symbols'] # pull list of symbols
for x in range(len(info)): # find length of list and run loop
    if info[x]['symbol'] == symbol: # until we find our coin
        a = info[x]["filters"][0]['tickSize'] # break into filters pulling tick size
        cost = round_step_size(cost, float(a)) # convert tick size from string to float, insert in helper func with cost
        print(cost) # run into order parameter as price=cost

为您的编码和交易干杯并祝您好运!


推荐阅读