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

时间:2019-07-31 00:48:24

标签: python bitcoin trading binance ccxt

我正在使用Binance API来使我的交易机器人使用Python 3.6。和CCXT library(在这里找到docs)。

他们网站上的一个非常有用的功能是能够按您当前余额的一定百分比下订单:

例如,如果我正在查看BTC/USDT个加密硬币对,并且我的帐户中有50 USDT,则可以选择购买N数量的BTC还是选择使用我的帐户100%中的USDT进行购买,因此,我可以购买的最大金额为BTC

我阅读了许多文档,但是我找不到以任何方式使用API​​执行这些“余额百分比”订单的选项:我唯一能做的就是将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)

有人知道是否有内置功能可以做 百分比顺序?如果API无法解决问题,您会建议一些解决方法吗?

我希望能够将我的当前余额的API百分比作为amount给出,因此我总是可以使用全部余额而不必在费用减少时进行更新

2 个答案:

答案 0 :(得分:1)

 use a function like this

 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

答案 1 :(得分:1)

我不知道有任何币安 API 函数可以做到这一点,但您可以尝试这样的操作:

# You ask for the balance
balance= client.get_asset_balance(asset='USDT')

# set the percentage or fraction you want to invest in each order
portion_balance = float(balance['free']) * 0.35

# you assign the created variable in the quantity of your order
sell_market = client.order_market_sell(
    symbol= 'ETHUSDT',
    quantity= portion_balance)

你好;)