3.5 Binance_interface APP U本位合约交易-基础订单

Binance_interface U本位合约交易-基础订单

量化交易研究群(VX) = py_ted

1. APP U本位合约交易-基础订单函数总览

方法解释
set_order下单(API原始接口)
get_order查询订单(API原始接口)
get_orders_pending查看当前挂单
get_orders_pending_open查看当前开仓挂单
get_orders_pending_close查看当前平仓挂单
wait_order_FILLED等待订单成交
cancel_order撤销订单(API原始接口)

2. 模型实例化

from binance_interface.app import BinanceUM
from binance_interface.app.utils import eprint
import paux.date
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'

binanceUM = BinanceUM(
    key=key,
    secret=secret,
    proxy_host=proxy_host,
    timezone='Asia/Shanghai',
)
trade = binanceUM.trade

3. 下单(API原始接口) set_order

set_order_result = trade.set_order(
    symbol='MANAUSDT',
    side='BUY',
    type='LIMIT',
    price='0.4',
    quantity=15,
    positionSide='LONG',
    timeInForce='GTC',
)
eprint(set_order_result)

输出:

>>> {'code': 200,
>>>  'data': {'orderId': 10508381477,
>>>           'symbol': 'MANAUSDT',
>>>           'status': 'NEW',
>>>           'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>           'price': '0.4000',
>>>           'avgPrice': '0.00',
>>>           'origQty': '15',
>>>           'executedQty': '0',
>>>           'cumQty': '0',
>>>           'cumQuote': '0.0000',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0.0000',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'priceMatch': 'NONE',
>>>           'selfTradePreventionMode': 'NONE',
>>>           'goodTillDate': 0,
>>>           'updateTime': 1706118925588},
>>>  'msg': ''}

4. 查询订单(API原始接口) get_order

orderId = set_order_result['data']['orderId']
order_result = trade.get_order(
    symbol='MANAUSDT',
    orderId=orderId,
)
eprint(order_result)

输出:

>>> {'code': 200,
>>>  'data': {'orderId': 10508381477,
>>>           'symbol': 'MANAUSDT',
>>>           'status': 'NEW',
>>>           'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>           'price': '0.4000',
>>>           'avgPrice': '0.00',
>>>           'origQty': '15',
>>>           'executedQty': '0',
>>>           'cumQuote': '0.0000',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0.0000',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'priceMatch': 'NONE',
>>>           'selfTradePreventionMode': 'NONE',
>>>           'goodTillDate': 0,
>>>           'time': 1706118925588,
>>>           'updateTime': 1706118925588},
>>>  'msg': ''}

5. 查看当前挂单 get_orders_pending

# 查询symbol 从start到end全部未成交订单
orders_pending_result = trade.get_orders_pending(
    symbol='',  # 默认为空,表示全部产品
    start='2024-01-01 10:00:00',  # 默认为空,表示不限定订单的起始时间
    end='2024-12-26',  # 默认为空,表示不限定订单的终止时间
)
eprint(orders_pending_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': [{'orderId': 38469242721,
>>>            'symbol': 'ADAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_8Caon6uUaXDwTaiUu7dY',
>>>            'price': '0.50000',
>>>            'avgPrice': '0',
>>>            'origQty': '20',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.00000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118620980,
>>>            'updateTime': 1706118620980},
>>>           {'orderId': 38469210055,
>>>            'symbol': 'ADAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_Odv0cISfWne64Tslg79N',
>>>            'price': '0.43000',
>>>            'avgPrice': '0',
>>>            'origQty': '23',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.00000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118533703,
>>>            'updateTime': 1706118533703},
>>>           {'orderId': 10485110992,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'CqSdY5QH4x1UWdUypMvoAl',
>>>            'price': '0.5348',
>>>            'avgPrice': '0',
>>>            'origQty': '10',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1705832967589,
>>>            'updateTime': 1705832967589},
>>>           {'orderId': 10508354973,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_pZwB8GcFYVAvxxpyCDaC',
>>>            'price': '0.5000',
>>>            'avgPrice': '0',
>>>            'origQty': '20',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118636567,
>>>            'updateTime': 1706118636567},
>>>           {'orderId': 10508381477,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>            'price': '0.4000',
>>>            'avgPrice': '0',
>>>            'origQty': '15',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118925588,
>>>            'updateTime': 1706118925588}],
>>>  'msg': ''}
# 查询symbol 从start到end全部未成交订单
orders_pending_result = trade.get_orders_pending(
    symbol='MANAUSDT',  # 默认为空,表示全部产品
    start='2024-01-01 10:00:00',  # 默认为空,表示不限定订单的起始时间
    end='2024-12-26',  # 默认为空,表示不限定订单的终止时间
)
eprint(orders_pending_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': [{'orderId': 10485110992,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'CqSdY5QH4x1UWdUypMvoAl',
>>>            'price': '0.5348',
>>>            'avgPrice': '0',
>>>            'origQty': '10',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1705832967589,
>>>            'updateTime': 1705832967589},
>>>           {'orderId': 10508354973,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_pZwB8GcFYVAvxxpyCDaC',
>>>            'price': '0.5000',
>>>            'avgPrice': '0',
>>>            'origQty': '20',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118636567,
>>>            'updateTime': 1706118636567},
>>>           {'orderId': 10508381477,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>            'price': '0.4000',
>>>            'avgPrice': '0',
>>>            'origQty': '15',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118925588,
>>>            'updateTime': 1706118925588}],
>>>  'msg': ''}

6. 查看当前开仓挂单 get_orders_pending_open

# 参数positionSide默认值为'',表示全部多单和空单
orders_pending_open_result = trade.get_orders_pending_open(
    symbol='MANAUSDT',
)
eprint(orders_pending_open_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': [{'orderId': 10508354973,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_pZwB8GcFYVAvxxpyCDaC',
>>>            'price': '0.5000',
>>>            'avgPrice': '0',
>>>            'origQty': '20',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118636567,
>>>            'updateTime': 1706118636567},
>>>           {'orderId': 10508381477,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>            'price': '0.4000',
>>>            'avgPrice': '0',
>>>            'origQty': '15',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118925588,
>>>            'updateTime': 1706118925588}],
>>>  'msg': ''}
# positionSide='SHORT' 限制结果为空单
orders_pending_open_result = trade.get_orders_pending_open(
    symbol='MANAUSDT',
    positionSide='SHORT',
)
eprint(orders_pending_open_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': [{'orderId': 10508354973,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_pZwB8GcFYVAvxxpyCDaC',
>>>            'price': '0.5000',
>>>            'avgPrice': '0',
>>>            'origQty': '20',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1706118636567,
>>>            'updateTime': 1706118636567}],
>>>  'msg': ''}

7. 查看当前平仓挂单 get_orders_pending_close

# 参数positionSide默认值为'',表示全部多单和空单
orders_pending_close_result = trade.get_orders_pending_close(
    symbol='MANAUSDT',
)
eprint(orders_pending_close_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': [{'orderId': 10485110992,
>>>            'symbol': 'MANAUSDT',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'CqSdY5QH4x1UWdUypMvoAl',
>>>            'price': '0.5348',
>>>            'avgPrice': '0',
>>>            'origQty': '10',
>>>            'executedQty': '0',
>>>            'cumQuote': '0.0000',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'priceMatch': 'NONE',
>>>            'selfTradePreventionMode': 'NONE',
>>>            'goodTillDate': 0,
>>>            'time': 1705832967589,
>>>            'updateTime': 1705832967589}],
>>>  'msg': ''}
# positionSide='SHORT' 限制结果为空单
orders_pending_close_result = trade.get_orders_pending_close(
    symbol='MANAUSDT',
    positionSide='SHORT',
)
eprint(orders_pending_close_result, length=30)

输出:

>>> {'code': 200, 'data': [], 'msg': ''}

8. 等待订单成交 wait_order_FILLED

orderId = set_order_result['data']['orderId']
symbol = set_order_result['data']['symbol']
# 堵塞,等待订单完全成交,如果超时后仍未成交,返回订单数据
wait_order_filled_result = trade.wait_order_FILLED(
    symbol=symbol,
    orderId=orderId,
    timeout=5,
)
eprint(wait_order_filled_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': {'orderId': 10508381477,
>>>           'symbol': 'MANAUSDT',
>>>           'status': 'NEW',
>>>           'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>           'price': '0.4000',
>>>           'avgPrice': '0.00',
>>>           'origQty': '15',
>>>           'executedQty': '0',
>>>           'cumQuote': '0.0000',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0.0000',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'priceMatch': 'NONE',
>>>           'selfTradePreventionMode': 'NONE',
>>>           'goodTillDate': 0,
>>>           'time': 1706118925588,
>>>           'updateTime': 1706118925588},
>>>  'msg': ''}

9. 撤销订单(API原始接口) cancel_order

orderId = set_order_result['data']['orderId']
symbol = set_order_result['data']['symbol']

cancel_order_result = trade.cancel_order(
    symbol=symbol,
    orderId=orderId,
)
eprint(cancel_order_result, length=30)

输出:

>>> {'code': 200,
>>>  'data': {'orderId': 10508381477,
>>>           'symbol': 'MANAUSDT',
>>>           'status': 'CANCELED',
>>>           'clientOrderId': '3ilBoDldzzHd16kvCqt99x',
>>>           'price': '0.4000',
>>>           'avgPrice': '0.00',
>>>           'origQty': '15',
>>>           'executedQty': '0',
>>>           'cumQty': '0',
>>>           'cumQuote': '0.0000',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0.0000',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'priceMatch': 'NONE',
>>>           'selfTradePreventionMode': 'NONE',
>>>           'goodTillDate': 0,
>>>           'updateTime': 1706118936170},
>>>  'msg': ''}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PYTED量化交易研究所

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值