python - 精度超过为该资产定义的最大值。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")
你能帮助我吗 ?
解决方案
看起来您正在寻找 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
为您的编码和交易干杯并祝您好运!
推荐阅读
- javascript - AXIOS:有没有办法在没有 FormData 的情况下发布对象?
- python - 设置训练/测试拆分以用于 Conv 神经网络
- azure-application-insights - Kusto 使用 id 连接行并将内容显示为数组
- python - 为什么 pyautogui 不会在绘图软件中突出显示和使用键盘动作?(这可以解决吗)
- node.js - ModuleNotFoundError:当我在本地测试我的烧瓶/反应应用程序时没有名为“tensorflow”的模块(使用 procfile 命令)
- ios - iOS 语音框架 EARErrorDomain 代码=0
- ios - `UIScreen.main.snapshotView` 在设备上显示为空白
- c# - C#修改下拉列表中的项目
- json - 使用 JSON 有效负载和文件调用 .NET Core API
- javascript - 异步/等待 Discord.js Node.js Javascript JS