简体   繁体   English

Binance API 开期货交易的正确方法?

[英]Binance API proper way to open a futures trade?

I am currently in the midst of writing myself a little python bot for binance using their API and I find the documentation rather lackluster and wondered whether someone on here might be able to help.我目前正在使用他们的 API 为自己编写一个小 python 机器人来进行币安,我发现文档相当乏味,想知道这里是否有人可以提供帮助。

Let's say I want to open up a futures trade in the BTCUSDT pairing with a leverage of 5x and a margin of 100 USDT at market price, with a take profit at 50% and a stop loss at 10%.假设我想在 BTCUSDT 配对中开立期货交易,杠杆为 5 倍,保证金为 100 USDT,以市价计算,止盈为 50%,止损为 10%。

from binance.client import Client
import cfg
client = Client(cfg.api_key, cfg.api_secret)
client.futures_create_order(symbol='BNBUSDT', side='LONG', type='MARKET',  quantity = 100 USDT * leverage / asset_price)

This is about as far as I get.据我所知,这差不多。 I don't see any leverage attribute, however there is another function called futures_change_leverage() which is able to change your leverage, so I do have to initialize a position and then change the leverage?我没有看到任何杠杆属性,但是还有另一个名为futures_change_leverage()的 function 可以改变你的杠杆,所以我必须初始化一个 position 然后改变杠杆? Wouldn't that also just scale my margin down?那不也只是缩小我的保证金吗?

I am also rather lost as to how the later attributes work and how I would be able to place a take profit and stop loss order.对于后面的属性如何工作以及如何下止盈和止损订单,我也很迷茫。

Thanks for any help.谢谢你的帮助。

From running a few tests it seems like Binance uses the margin type (cross or isolated) and the leverage you last used on that pairing on the web, desktop or mobile app.从运行一些测试来看,Binance 似乎使用了保证金类型(交叉或隔离)以及您上次在 web、桌面或移动应用程序上使用的杠杆。 If you have not changed it, it defaults to 20x Cross.如果您没有更改它,则默认为 20x Cross。

So before opening a futures trade you should change the leverage and margin type and only then open the position.因此,在开始期货交易之前,您应该更改杠杆和保证金类型,然后才打开 position。

For example:例如:

client.futures_change_margin_type(symbol='BNBUSDT', marginType='ISOLATED')

marginType has to be either 'ISOLATED' or 'CROSSED'. marginType 必须是“隔离”或“交叉”。

Since questions came up about leverage and margin types and how to set those:由于出现了有关杠杆和保证金类型以及如何设置的问题:

def adjust_leverage(symbol, client):
    client.futures_change_leverage(symbol=symbol, leverage=10)

def adjust_margintype(symbol, client):
    client.futures_change_margin_type(symbol=symbol, marginType='ISOLATED')

I am still figuring out how to do Stop Losses and Take Profits, potentially even Trailing SL, if I do find them I will keep you posted.我仍在研究如何进行止损和止盈,甚至可能是追踪 SL,如果我找到了它们,我会及时通知你。

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

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