简体   繁体   English

如何在每笔交易中使用您账户余额的百分比? 币安python

[英]How to use a percentage of your account balance per trade? Binance-python

As far as I am aware the only way to create a new futures order is with client.futures_create_order().据我所知,创建新期货订单的唯一方法是使用 client.futures_create_order()。 The issue is this requires you to give it the exact amount of contracts you would like to buy.问题是这需要你给它你想要购买的合同的确切数量。 I am using BUSD to make all my trades, so I am wondering if there is any way to give it a BUSD amount or a percentage of the BUSD I have, instead of the amount of order contracts?我正在使用 BUSD 进行所有交易,所以我想知道是否有任何方法可以给它一个 BUSD 金额或我拥有的 BUSD 的百分比,而不是订单合约的金额? If the answer is no then is there any way to calculate how many contracts to buy with a specified amount of BUSD or a percentage of the BUSD you have in your wallet.如果答案是否定的,那么有什么方法可以计算出用指定数量的 BUSD 或您钱包中 BUSD 的百分比购买多少份合约。

I am currently using client.futures_create_order() to execute trades.我目前正在使用 client.futures_create_order() 来执行交易。 I am using Binance hedge mode so to place a Long I use, side = 'BUY',positionSide: 'LONG'.我正在使用 Binance 对冲模式,以便放置我使用的多头,side = 'BUY',positionSide: 'LONG'。 Then to sell I keep the positionSide as 'LONG' but switch the side to 'SELL' and opposite for shorts.然后卖出,我将 positionSide 保持为“多头”,但将一侧切换为“卖出”,而空头则相反。 One of the requirements of using the create order is listing the amount of order contracts you want to buy or sell.使用创建订单的要求之一是列出您要购买或出售的订单合约的数量。 But I would like to use 5% of my BUSD account balance per trade.但我想在每笔交易中使用我 BUSD 账户余额的 5%。 I found a way to pull the price of the symbol I want to trade and then calculate how much 5% of my account balance is into BUSD then finally multiply it by the symbol price to get the number of order contracts to buy.我找到了一种方法来提取我要交易的代码的价格,然后计算我的账户余额的 5% 有多少用于 BUSD,最后将它乘以代码价格以获得要购买的订单合约的数量。 But even that comes with its own issues, one being rounding.但即使这样也有其自身的问题,其中之一就是四舍五入。 If I am buying DOGE Binance wont let me buy less than one contract so would need it to round to the nearest whole number.如果我要购买 DOGE,币安不会让我购买少于一份合约,因此需要将其四舍五入到最接近的整数。 But if I am buying BTC obviously rounding to a whole number isn't helpful.但如果我购买 BTC,显然四舍五入到整数没有帮助。 Even if we can some how get around this issue there is one more.即使我们可以解决这个问题,还有一个问题。 I am making more than one trade at a time, so lets say I place a Long with 5% of my BUSD balance which at the time lets say is $5.我一次进行不止一笔交易,所以假设我用我 BUSD 余额的 5% 做多,当时假设是 5 美元。 Within the time of buying and selling my account balance will change.在买入和卖出的时间内,我的账户余额会发生变化。 So when it goes to place the sell order it again uses 5% but now that only $4.5 so there is still $0.5 in the trade open.因此,当它下达卖单时,它再次使用 5%,但现在只有 4.5 美元,所以在未平仓交易中仍有 0.5 美元。 If using a percentage isn't possible, being able to use a fixed amount of BUSD would still be a huge benefit over using a contract amount.如果无法使用百分比,那么能够使用固定数量的 BUSD 仍然比使用合同金额有巨大的好处。

for people that are having this issue I figured out a solution, it's not perfect but it works for me.对于遇到此问题的人,我想出了一个解决方案,它并不完美,但对我有用。

FIL3MIN_original_quantity=None

@app.route('/test', methods = ['POST'])
def test():
    data = json.loads(request.data)
    side = data['order_action']
    positionSide = data['positionSide']
    command = data['command']

global FIL3MIN_original_quantity

    if command == "FIL3MINBUY":
            key = "https://api.binance.com/api/v3/ticker/price?symbol=FILBUSD"
            hey = requests.get(key)  
            hey = hey.json()
            sym_price =(f"{hey['price']}")   
            ticker_price = float(sym_price)
            acc_balance = client.futures_account_balance()[8]
            balance_value = acc_balance['balance']
            balance_value = float(balance_value)
            BUSD_quantity = balance_value * 0.02
            quantitys = BUSD_quantity/ticker_price
            quantityss =quantitys*20  
            quantityss = round(quantityss, 1) 
            executed_order = client.futures_create_order(symbol='FILBUSD',side=side,positionSide=positionSide,
            type=ORDER_TYPE_MARKET, quantity=quantityss)   
            FIL3MIN_original_quantity = executed_order['origQty']
            if executed_order: return {"code": "success", "message": "order executed BUY",
            "amount bought": FIL3MIN_original_quantity}
            else: return{"code": "error","message": "order failed"}
        if command == "FIL3MINSELL":
            if FIL3MIN_original_quantity == None:return("No open position to close")
            else:executed_order2 = client.futures_create_order(symbol='FILBUSD',side=side,positionSide = positionSide ,
            type=ORDER_TYPE_MARKET, quantity=FIL3MIN_original_quantity)
            FIL3MIN_original_quantity=None
            if executed_order2: return {"code": "success","message": "order executed SELL"}
            else:return {"code": "error","message": "order failed"}

I know this looks like a complete mess but it works for me.我知道这看起来一团糟,但对我有用。 to simplify what is going on the progragam first looks at the message it is give in which looks something like this.为了简化正在发生的事情,程序首先查看它给出的消息,看起来像这样。 place the buy order.下订单。

{"passphrase": "abc123","order_action": "BUY","positionSide": "LONG", "command": "FIL3MINBUY"}

place the sell order.下卖单。

{"passphrase": "abc123","order_action": "SELL","positionSide": "LONG", "command": "FIL3MINSELL"}

from that line of code it receives form trading view it then calculated the current price of the symbol which in this case is FILBUSD.它从交易视图接收的那行代码中计算出符号的当前价格,在本例中为 FILBUSD。 Then gets my current BUSD account balance to figure out what 2% is.然后获取我当前的 BUSD 账户余额,以计算出 2% 是多少。 It then converts 2% of BUSD into the amount of contracts then rounds it.然后它将 BUSD 的 2% 转换为合约数量,然后四舍五入。 Next it places the buy order and remembers the amount of contracts that were bought and stores it in FIL3MIN_original_quantity.接下来它会下买单并记住已购买的合约数量并将其存储在 FIL3MIN_original_quantity 中。 Then once it receives the sell order from trading view it pulls the amount of contracts and places the sell order.然后,一旦它从交易视图中收到卖单,它就会提取合约数量并下达卖单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM