如何使用 Binance API 和 Python-CCXT 下百分比订单?

kal*_*tus 6 python trading bitcoin binance ccxt

我正在使用 Binance API 来使用 Python 3.6 制作我的交易机器人。和CCXT 库(在这里您可以找到文档)。

他们在其网站上拥有的一项非常有用的功能是能够以当前余额的一定比例下订单:

例如,如果我看着在BTC/USDT加密硬币对,我有50 USDT我的账户,我可以买之间进行选择N的数量BTC或使用100%的帐户的USDT用于购买,因此购买的最高金额BTC,我可以。

我多次阅读文档,但我找不到以任何方式使用 API 执行这些“余额百分比”订单的选项:我唯一能做的就是将 a 传递float给订单函数。这就是我现在下订单的方式:

amount = 0.001
symbol = "BTC/USDT"

def buyorder(amount, symbol): # this makes a market order taking in the amount I defined before, for the pair defined by "symbol"

    type = 'market'  # or 'limit'
    side = 'buy'     # or 'sell'
    params = {}      # extra params and overrides if needed
    order = exchange.create_order(symbol, type, side, amount, params)
Run Code Online (Sandbox Code Playgroud)

有谁知道是否有执行百分比订单的内置功能?如果 API 无法这样做,您会建议一些解决方法吗?

我希望能够给我的当前余额为的API百分比amount,这样我就可以一直使用完整的,而不必更新时费减损

小智 5

使用这样的自定义函数:

 def get_max_position_available():
    to_use = float(exchange.fetch_balance().get('USDT').get('free'))
    price = float(exchange.fetchTicker('BTC/USDT').get('last'))
    decide_position_to_use = to_use / price
    return decide_position_to_use
Run Code Online (Sandbox Code Playgroud)