Binance_interface APP 币本位合约交易-基础订单
- Github地址
- PyTed量化交易研究院
量化交易研究群(VX) = py_ted
目录
- 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 BinanceCM
from binance_interface.app.utils import eprint
import paux.date
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'binanceCM = BinanceCM(key=key,secret=secret,proxy_host=proxy_host,timezone='Asia/Shanghai',
)
trade = binanceCM.trade
3. 下单(API原始接口) set_order
set_order_result = trade.set_order(symbol='DOTUSD_PERP',side='BUY',type='LIMIT',price='6.1',quantity=1,positionSide='LONG',timeInForce='GTC',
)
eprint(set_order_result)
输出:
>>> {'code': 200,
>>>  'data': {'orderId': 7108734736,
>>>           'symbol': 'DOTUSD_PERP',
>>>           'pair': 'DOTUSD',
>>>           'status': 'NEW',
>>>           'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',
>>>           'price': '6.100',
>>>           'avgPrice': '0.000',
>>>           'origQty': '1',
>>>           'executedQty': '0',
>>>           'cumQty': '0',
>>>           'cumBase': '0',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'updateTime': 1706193909383},
>>>  'msg': ''}
4. 查询订单(API原始接口) get_order
orderId = set_order_result['data']['orderId']
order_result = trade.get_order(symbol='DOTUSD_PERP',orderId=orderId,
)
eprint(order_result)
输出:
>>> {'code': 200,
>>>  'data': {'orderId': 7108734736,
>>>           'symbol': 'DOTUSD_PERP',
>>>           'pair': 'DOTUSD',
>>>           'status': 'NEW',
>>>           'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',
>>>           'price': '6.100',
>>>           'avgPrice': '0.000',
>>>           'origQty': '1',
>>>           'executedQty': '0',
>>>           'cumBase': '0',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'time': 1706193909383,
>>>           'updateTime': 1706193909383},
>>>  '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': 7108734736,
>>>            'symbol': 'DOTUSD_PERP',
>>>            'pair': 'DOTUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',
>>>            'price': '6.100',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193909383,
>>>            'updateTime': 1706193909383},
>>>           {'orderId': 70547715976,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193177536,
>>>            'updateTime': 1706193177536},
>>>           {'orderId': 70548864413,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',
>>>            'price': '2100',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193876902,
>>>            'updateTime': 1706193876902},
>>>           {'orderId': 70547694568,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_Qf3K1XXNtOayzl3M05Ek',
>>>            'price': '2000',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193160778,
>>>            'updateTime': 1706193160778},
>>>           {'orderId': 70548673979,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_NsHOBwSq1znyGgs0TDBz',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193739701,
>>>            'updateTime': 1706193739701}],
>>>  'msg': ''}
# 查询symbol 从start到end全部未成交订单
orders_pending_result = trade.get_orders_pending(symbol='ETHUSD_PERP',  # 默认为空,表示全部产品start='2024-01-01 10:00:00',  # 默认为空,表示不限定订单的起始时间end='2024-12-26',  # 默认为空,表示不限定订单的终止时间
)
eprint(orders_pending_result, length=30)
输出:
>>> {'code': 200,
>>>  'data': [{'orderId': 70547715976,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193177536,
>>>            'updateTime': 1706193177536},
>>>           {'orderId': 70548673979,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_NsHOBwSq1znyGgs0TDBz',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193739701,
>>>            'updateTime': 1706193739701},
>>>           {'orderId': 70547694568,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_Qf3K1XXNtOayzl3M05Ek',
>>>            'price': '2000',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193160778,
>>>            'updateTime': 1706193160778},
>>>           {'orderId': 70548864413,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',
>>>            'price': '2100',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193876902,
>>>            'updateTime': 1706193876902}],
>>>  'msg': ''}
6. 查看当前开仓挂单 get_orders_pending_open
# 参数positionSide默认值为'',表示全部多单和空单
orders_pending_open_result = trade.get_orders_pending_open(symbol='ETHUSD_PERP',
)
eprint(orders_pending_open_result, length=30)
输出:
>>> {'code': 200,
>>>  'data': [{'orderId': 70547715976,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193177536,
>>>            'updateTime': 1706193177536},
>>>           {'orderId': 70547694568,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_Qf3K1XXNtOayzl3M05Ek',
>>>            'price': '2000',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193160778,
>>>            'updateTime': 1706193160778}],
>>>  'msg': ''}
# positionSide='SHORT' 限制结果为空单
orders_pending_open_result = trade.get_orders_pending_open(symbol='ETHUSD_PERP',positionSide='SHORT',
)
eprint(orders_pending_open_result, length=30)
输出:
>>> {'code': 200,
>>>  'data': [{'orderId': 70547715976,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': False,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193177536,
>>>            'updateTime': 1706193177536}],
>>>  'msg': ''}
7. 查看当前平仓挂单 get_orders_pending_close
# 参数positionSide默认值为'',表示全部多单和空单
orders_pending_close_result = trade.get_orders_pending_close(symbol='ETHUSD_PERP',
)
eprint(orders_pending_close_result, length=30)
输出:
>>> {'code': 200,
>>>  'data': [{'orderId': 70548673979,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_NsHOBwSq1znyGgs0TDBz',
>>>            'price': '2400',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'SELL',
>>>            'positionSide': 'LONG',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193739701,
>>>            'updateTime': 1706193739701},
>>>           {'orderId': 70548864413,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',
>>>            'price': '2100',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193876902,
>>>            'updateTime': 1706193876902}],
>>>  'msg': ''}
# positionSide='SHORT' 限制结果为空单
orders_pending_close_result = trade.get_orders_pending_close(symbol='ETHUSD_PERP',positionSide='SHORT',
)
eprint(orders_pending_close_result, length=30)
输出:
>>> {'code': 200,
>>>  'data': [{'orderId': 70548864413,
>>>            'symbol': 'ETHUSD_PERP',
>>>            'pair': 'ETHUSD',
>>>            'status': 'NEW',
>>>            'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',
>>>            'price': '2100',
>>>            'avgPrice': '0',
>>>            'origQty': '1',
>>>            'executedQty': '0',
>>>            'cumBase': '0',
>>>            'timeInForce': 'GTC',
>>>            'type': 'LIMIT',
>>>            'reduceOnly': True,
>>>            'closePosition': False,
>>>            'side': 'BUY',
>>>            'positionSide': 'SHORT',
>>>            'stopPrice': '0',
>>>            'workingType': 'CONTRACT_PRICE',
>>>            'priceProtect': False,
>>>            'origType': 'LIMIT',
>>>            'time': 1706193876902,
>>>            'updateTime': 1706193876902}],
>>>  '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': 7108734736,
>>>           'symbol': 'DOTUSD_PERP',
>>>           'pair': 'DOTUSD',
>>>           'status': 'NEW',
>>>           'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',
>>>           'price': '6.100',
>>>           'avgPrice': '0.000',
>>>           'origQty': '1',
>>>           'executedQty': '0',
>>>           'cumBase': '0',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'time': 1706193909383,
>>>           'updateTime': 1706193909383},
>>>  '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': 7108734736,
>>>           'symbol': 'DOTUSD_PERP',
>>>           'pair': 'DOTUSD',
>>>           'status': 'CANCELED',
>>>           'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',
>>>           'price': '6.100',
>>>           'avgPrice': '0.000',
>>>           'origQty': '1',
>>>           'executedQty': '0',
>>>           'cumQty': '0',
>>>           'cumBase': '0',
>>>           'timeInForce': 'GTC',
>>>           'type': 'LIMIT',
>>>           'reduceOnly': False,
>>>           'closePosition': False,
>>>           'side': 'BUY',
>>>           'positionSide': 'LONG',
>>>           'stopPrice': '0',
>>>           'workingType': 'CONTRACT_PRICE',
>>>           'priceProtect': False,
>>>           'origType': 'LIMIT',
>>>           'updateTime': 1706193919012},
>>>  'msg': ''}