简体   繁体   中英

How to place a Futures Market order with Take Profit and Stop Loss using python-binance

I'm struggling to place Take Profit and Stop Loss orders using python-binance and can't understand how to set up TP & SL % For now I only found how to place base orders like:

Market order

from binance_f import RequestClient
from binance_f.constant.test import *
from binance_f.base.printobject import *
from binance_f.model.constant import *

client = RequestClient(api_key='api_key',
                       secret_key='secret_key',
                       url='https://testnet.binancefuture.com')

print(client.post_order(symbol="BTCUSDT", ordertype="MARKET", side="SELL",
                        positionSide="BOTH", quantity=0.001))

Limit order

from binance_f import RequestClient
from binance_f.constant.test import *
from binance_f.base.printobject import *
from binance_f.model.constant import *

client = RequestClient(api_key='api_key',
                       secret_key='secret_key',
                       url='https://testnet.binancefuture.com')

print(client.post_order(symbol="BTCUSDT", ordertype="LIMIT", side="BUY",
                        positionSide="BOTH", quantity=0.01, timeInForce="GTC",
                        price=48680))
from binance.client import Client
from binance.helpers import round_step_size

api_key = ''
api_secret = ''
client = Client(api_key, api_secret)
symbol = 'KNCUSDT'


def get_account_balance():
    balance = client.futures_account_balance()[6]['balance']
    return float(balance)


def get_min_quant(symbol):
    info = client.futures_exchange_info()
    for item in info['symbols']:
        if item['symbol'] == symbol:
            for f in item['filters']:
                if f['filterType'] == 'PRICE_FILTER':
                    return f['tickSize']



bet = 13  # the percentage of the balance I am willing to buy with
balance = get_account_balance() * bet / 100

tick_size = float(get_min_quant(symbol))
print(tick_size)

symbol_info = client.get_ticker(symbol=symbol)
symbol_price = float(symbol_info['lastPrice'])
quantity = int(balance / symbol_price)

enter_long_coeff = 0.4
take_profit_percent = 0.2
stop_loss_percent = 0.2

price_long_enter = symbol_price * (1 - enter_long_coeff / 100)  # entry price for a limit order
price_long_enter = round_step_size(price_long_enter, tick_size)

take_profit_price = price_long_enter * (1 + take_profit_percent / 100)
take_profit_price = round_step_size(take_profit_price, tick_size)

stop_loss_price = price_long_enter * (1 + take_profit_percent / 100)
stop_loss_price = round_step_size(stop_loss_price, tick_size)

print(price_long_enter)
print(stop_loss_price)
print(take_profit_price)
print('=======')

limit_order_long = client.futures_create_order(
    symbol=symbol,
    side='BUY',
    positionSide='LONG',
    type='LIMIT',
    quantity=quantity,
    timeInForce='GTC',
    price=price_long_enter
)

sell_gain_market_long = client.futures_create_order(
    symbol=symbol,
    side='SELL',
    type='TAKE_PROFIT_MARKET',
    positionSide='LONG',
    quantity=quantity,
    stopPrice=take_profit_price
)

sell_stop_market_short = client.futures_create_order(
    symbol=symbol,
    side='SELL',
    type='STOP_MARKET',
    positionSide='LONG',
    quantity=quantity,
    stopPrice=stop_loss_price
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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