Doğan Ali ŞAN
Doğan Ali ŞAN

Reputation: 11

python-binance how to set tp/sl, quantity

There is a module named "python-binance" I used for my project and it is working very well on opening positions however I couldn't find a way to close them properly.

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()

this is my code and part of it works when I try to open a long position or short its opening it following the code

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

but after this code I wanna give another code that puts a limit order to close this order if u know the tradings system in futures I am trying to set tp/sl point with this api how can I do that

I already told this code have some errors another one of them is quantity and I need help with that too I want to set the quantity automatic with any coin but I couldn't find the formula and how to code it could u look at that to

thanks for your helps

Upvotes: 0

Views: 3415

Answers (2)

Eric
Eric

Reputation: 9

First, I think you should add sleep between open and close script. Because you need it placed before sending close.

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)

Secondly, I think the best way also is to send order, with type TAKE_PROFIT_MARKET and STOP_MARKET. Don't forget the parameter stopPrice and closePosition= True.

Upvotes: 1

KrisH Jodu
KrisH Jodu

Reputation: 114

Very simple way to crate close position order is fetching order history and fining that order and closing by taking position in opposite side

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

This might help you to close potion And there is error in your code about quantity, quantity should calculate as (amount*leverage)/price

Upvotes: 0

Related Questions