【问题标题】:python-binance how to set tp/sl, quantitypython-binance如何设置tp/sl,数量
【发布时间】:2021-09-03 23:08:54
【问题描述】:

我在项目中使用了一个名为“python-binance”的模块 它在开仓时效果很好,但是我找不到正确关闭它们的方法。

from config import Connect
import time
class Main:
  def __init__(self):
    self.client = Connect().make_connection()
    print("logged in")
    amount = float(input("Hesaptaki para miktarı:"))
    symbol = input("Coin İsmi:")
    orderType= input("Emir tipi(1,2):")
    leverage = int(input("Kaldıraç boyutu:"))
    self.client.futures_change_leverage(symbol=symbol, leverage=leverage)
    price=float(self.client.get_symbol_ticker(symbol=symbol)["price"])
    print(price)
    quantity = amount*leverage*price
    quantity = int(quantity)
    print(quantity)
    if orderType == "2":
        Price = float(input("Limit fiyatı:"))
        print(Price)
        sellPrice= Price+(Price*0.5/100)
        self.client.futures_create_order(symbol=symbol,type="LIMIT",timeInForce="GTC",side="BUY",price=Price,quantity=quantity)
        self.client.futures_create_order(symbol=symbol,type="LIMIT",timeInForce="GTC",side="SELL",price=sellPrice,quantity=quantity)

    else:
        Price=self.client.get_symbol_ticker(symbol=symbol)["price"]
        Price=float(Price)
        print(Price)
        sellPrice = Price+(Price*0.5/100)
        self.client.futures_create_order(symbol=symbol,type="MARKET",side="BUY",quantity=quantity,)
        self.client.futures_create_order(symbol=symbol,type="LIMIT",timeInForce="GTC",side="SELL",price=sellPrice,quantity=quantity)

    print(self.client.futures_position_information(symbol=symbol))
    print(self.client.futures_get_position_mode())

Main()

这是我的代码,当我尝试按照代码开多头头寸或做空其开仓头寸时,它的一部分起作用

            self.client.futures_create_order(symbol=symbol,type="MARKET",side="BUY",quantity=quantity,)

但是在这段代码之后,我想给出另一个代码来设置限价单来关闭这个订单 如果你知道期货交易系统,我想用这个 api 设置 tp/sl 点 我该怎么做呢

我已经告诉过这段代码有一些错误,其中另一个是数量,我也需要帮助我想用任何硬币自动设置数量,但我找不到公式以及如何编码到了

感谢您的帮助

【问题讨论】:

    标签: python api binance


    【解决方案1】:

    首先,我认为您应该在打开和关闭脚本之间添加睡眠。因为您需要在发送关闭之前放置它。

    self.client.futures_create_order(symbol=symbol,type="MARKET",side="BUY",quantity=quantity,)
     
    => sleep (10)  
    self.client.futures_create_order(symbol=symbol,type="LIMIT",timeInForce="GTC",side="SELL",price=sellPrice,quantity=quantity)
    

    其次,我认为最好的方式也是发送订单,类型为 TAKE_PROFIT_MARKET 和 STOP_MARKET。不要忘记参数 stopPrice 和 closePosition= True。

    【讨论】:

    • 睡眠是关键,谢谢!
    【解决方案2】:

    创建平仓订单的非常简单的方法是获取订单历史记录并对该订单进行罚款并通过在对面持仓来平仓

    x=None
    i=-1 # for latest trade
    while x==None:
         trade=client.futures_account_trades()[i] #it fetch details of latest trade
         if trade['symbol']==symbol and trade['quantity']==quantity:
              price_t=trade['price'] # fetching price
              price_prec=len(str(price_t)[str(price_t).index(".")+1:])           
              if trade['side']=='BUY':
                    side='SELL'
                    new_price=float(input('Enter new price'))
              else:
                    side='BUY'
                    new_price=float(input('Enter new price'))
              client.futures_create_order(symbol=currency_symbol, side=side, type='LIMIT', quantity=quantity,price=new_price,timeInForce='GTC',ReduceOnly=True) 
              x='something'
         else:
              i=i-1
    
    

    这可能会帮助你关闭药水 并且您的代码中有关于数量的错误,数量应计算为(数量*杠杆)/价格

    【讨论】:

    • 好吧,当我在买入订单显示“精度超过为该资产定义的最大值”后尝试打开卖出限价订单时出现问题。我该如何解决这个问题
    猜你喜欢
    • 2022-11-20
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多