2.5 Binance_interface APP 现货交易-基础订单
Binance_interface APP 现货交易-基础订单
- Github地址
- PyTed量化交易研究院
目录
- Binance_interface APP 现货交易-基础订单
- 1. APP 现货交易-基础订单函数总览
- 2. 模型实例化
- 3. 下单(API原始接口) set_order
- 4. 查询订单(API原始接口) get_order
- 5. 查看当前挂单 get_orders_pending
- 6 查看当前购买挂单 get_orders_pending_open
- 7. 查看当前卖出挂单 get_orders_pending_close
- 8. 等待订单成交 wait_order_FILLED
- 9. 撤销订单(API原始接口) cancel_order
1. APP 现货交易-基础订单函数总览
方法 | 解释 |
---|---|
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 BinanceSPOT
from binance_interface.app.utils import eprint
import paux.date
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'
binanceSPOT = BinanceSPOT(
key=key,
secret=secret,
proxy_host=proxy_host,
timezone='Asia/Shanghai',
)
trade = binanceSPOT.trade
3. 下单(API原始接口) set_order
set_order_result = trade.set_order(
symbol='MANAUSDT',
price='0.4',
quantity='15',
side='BUY',
type='LIMIT',
timeInForce='GTC',
)
eprint(set_order_result)
输出:
>>> {'code': 200,
>>> 'data': {'symbol': 'MANAUSDT',
>>> 'orderId': 2207382482,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',
>>> 'transactTime': 1706119355892,
>>> 'price': '0.40000000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'BUY',
>>> 'workingTime': 1706119355892,
>>> 'fills': [],
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'},
>>> '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': {'symbol': 'MANAUSDT',
>>> 'orderId': 2207382482,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',
>>> 'price': '0.40000000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'BUY',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706119355892,
>>> 'updateTime': 1706119355892,
>>> 'isWorking': True,
>>> 'workingTime': 1706119355892,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'},
>>> 'msg': ''}
5. 查看当前挂单 get_orders_pending
# 查询symbol 从start到end全部未成交订单
orders_pending_result = trade.get_orders_pending(
symbol='', # 默认为空,表示全部产品
start='2024-01-23 10:00:00', # 默认为空,表示不限定订单的起始时间
end='2024-01-25', # 默认为空,表示不限定订单的终止时间
)
eprint(orders_pending_result, length=30)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'MANAUSDT',
>>> 'orderId': 2207181640,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'm7UdoWyzjkl80q1rgzJ19U',
>>> 'price': '1.00000000',
>>> 'origQty': '5.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'SELL',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706105539772,
>>> 'updateTime': 1706105539772,
>>> 'isWorking': True,
>>> 'workingTime': 1706105539772,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'},
>>> {'symbol': 'MANAUSDT',
>>> 'orderId': 2207182516,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'G03pZe5KataOJ7C1DS3gmn',
>>> 'price': '0.47940000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'SELL',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706105588896,
>>> 'updateTime': 1706105588896,
>>> 'isWorking': True,
>>> 'workingTime': 1706105588896,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'}],
>>> 'msg': ''}
# 查询symbol 从start到end全部未成交订单
orders_pending_result = trade.get_orders_pending(
symbol='BTCUSDT', # 默认为空,表示全部产品
start='2024-01-23 10:00:00', # 默认为空,表示不限定订单的起始时间
end='2024-01-26', # 默认为空,表示不限定订单的终止时间
)
eprint(orders_pending_result, length=30)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'BTCUSDT',
>>> 'orderId': 24501202294,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'ios_7952d852422d460e9c82405c628af712',
>>> 'price': '38000.00000000',
>>> 'origQty': '0.00026000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'BUY',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706119340397,
>>> 'updateTime': 1706119340397,
>>> 'isWorking': True,
>>> 'workingTime': 1706119340397,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'}],
>>> 'msg': ''}
6 查看当前购买挂单 get_orders_pending_open
orders_pending_open_result = trade.get_orders_pending_open(
symbol='MANAUSDT',
)
eprint(orders_pending_open_result, length=30)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'MANAUSDT',
>>> 'orderId': 2207382482,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',
>>> 'price': '0.40000000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'BUY',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706119355892,
>>> 'updateTime': 1706119355892,
>>> 'isWorking': True,
>>> 'workingTime': 1706119355892,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'}],
>>> 'msg': ''}
7. 查看当前卖出挂单 get_orders_pending_close
orders_pending_close_result = trade.get_orders_pending_close(
symbol='MANAUSDT',
)
eprint(orders_pending_close_result, length=30)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'MANAUSDT',
>>> 'orderId': 2207181640,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'm7UdoWyzjkl80q1rgzJ19U',
>>> 'price': '1.00000000',
>>> 'origQty': '5.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'SELL',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706105539772,
>>> 'updateTime': 1706105539772,
>>> 'isWorking': True,
>>> 'workingTime': 1706105539772,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'},
>>> {'symbol': 'MANAUSDT',
>>> 'orderId': 2207182516,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'G03pZe5KataOJ7C1DS3gmn',
>>> 'price': '0.47940000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'SELL',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706105588896,
>>> 'updateTime': 1706105588896,
>>> 'isWorking': True,
>>> 'workingTime': 1706105588896,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'}],
>>> 'msg': ''}
8. 等待订单成交 wait_order_FILLED
# 堵塞,等待订单完全成交,如果超时后仍未成交,返回订单数据
orderId = set_order_result['data']['orderId']
wait_order_filled_result = trade.wait_order_FILLED(
symbol='MANAUSDT',
orderId=orderId,
timeout=5,
)
eprint(wait_order_filled_result)
输出:
>>> {'code': 200,
>>> 'data': {'symbol': 'MANAUSDT',
>>> 'orderId': 2207382482,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',
>>> 'price': '0.40000000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'NEW',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'BUY',
>>> 'stopPrice': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'time': 1706119355892,
>>> 'updateTime': 1706119355892,
>>> 'isWorking': True,
>>> 'workingTime': 1706119355892,
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'},
>>> 'msg': ''}
9. 撤销订单(API原始接口) cancel_order
orderId = set_order_result['data']['orderId']
cancel_order_result = trade.cancel_order(
symbol='MANAUSDT',
orderId=orderId,
)
eprint(cancel_order_result)
输出:
>>> {'code': 200,
>>> 'data': {'symbol': 'MANAUSDT',
>>> 'origClientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',
>>> 'orderId': 2207382482,
>>> 'orderListId': -1,
>>> 'clientOrderId': 'Gll4JiBPwxkI3FYhIB0CHi',
>>> 'transactTime': 1706119384094,
>>> 'price': '0.40000000',
>>> 'origQty': '15.00000000',
>>> 'executedQty': '0.00000000',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'status': 'CANCELED',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'side': 'BUY',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER'},
>>> 'msg': ''}