Binance_interface APP U本位合约交易-市单价平仓
- Github地址
- PyTed量化交易研究院
量化交易研究群(VX) = py_ted
目录
- Binance_interface APP U本位合约交易-市单价平仓
- 1. APP U本位合约交易-市单价平仓函数总览
- 2. 模型实例化
- 3. 同步 市价平仓
- 4. 同步 市价平仓 回调函数
- 5. 异步 市价平仓 回调函数
 
1. APP U本位合约交易-市单价平仓函数总览
| 方法 | 解释 | 
|---|---|
| close_market | 市价单平仓 | 
2. 模型实例化
from binance_interface.app import BinanceUM
from pprint import pprint
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'binanceUM = BinanceUM(key=key, secret=secret,proxy_host=proxy_host
)
trade = binanceUM.trade
3. 同步 市价平仓
close_market3 = trade.close_market(symbol='MANAUSDT',  # 产品marginType='ISOLATED',  # 保证金模式 ISOLATED: 逐仓 CROSSED: 全仓positionSide='LONG',  # 持仓方向 LONG: 多单 SHORT: 空单quantity=12,  # 平仓数量(交易货币) timeout=5,
)pprint(close_market3)
输出:
>>> {'cancel_result': None,
>>>  'error_result': None,
>>>  'func_param': {'callback': None,
>>>                 'cancel': True,
>>>                 'delay': 0.2,
>>>                 'errorback': None,
>>>                 'marginType': 'ISOLATED',
>>>                 'meta': {},
>>>                 'newClientOrderId': '',
>>>                 'newThread': False,
>>>                 'positionSide': 'LONG',
>>>                 'quantity': 12,
>>>                 'symbol': 'MANAUSDT',
>>>                 'timeout': 5},
>>>  'get_order_result': {'code': 200,
>>>                       'data': {'avgPrice': '0.4358',
>>>                                'clientOrderId': '3aC5CtbL9IfbiJBurdJdlS',
>>>                                'closePosition': False,
>>>                                'cumQuote': '5.2296',
>>>                                'executedQty': '12',
>>>                                'goodTillDate': 0,
>>>                                'orderId': 10508478790,
>>>                                'origQty': '12',
>>>                                'origType': 'MARKET',
>>>                                'positionSide': 'LONG',
>>>                                'price': '0.0000',
>>>                                'priceMatch': 'NONE',
>>>                                'priceProtect': False,
>>>                                'reduceOnly': True,
>>>                                'selfTradePreventionMode': 'NONE',
>>>                                'side': 'SELL',
>>>                                'status': 'FILLED',
>>>                                'stopPrice': '0.0000',
>>>                                'symbol': 'MANAUSDT',
>>>                                'time': 1706119940870,
>>>                                'timeInForce': 'GTC',
>>>                                'type': 'MARKET',
>>>                                'updateTime': 1706119940870,
>>>                                'workingType': 'CONTRACT_PRICE'},
>>>                       'msg': ''},
>>>  'meta': {},
>>>  'request_param': {'newClientOrderId': '',
>>>                    'positionSide': 'LONG',
>>>                    'quantity': '12',
>>>                    'side': 'SELL',
>>>                    'symbol': 'MANAUSDT',
>>>                    'type': 'MARKET'},
>>>  'set_order_result': {'code': 200,
>>>                       'data': {'avgPrice': '0.00',
>>>                                'clientOrderId': '3aC5CtbL9IfbiJBurdJdlS',
>>>                                'closePosition': False,
>>>                                'cumQty': '0',
>>>                                'cumQuote': '0.0000',
>>>                                'executedQty': '0',
>>>                                'goodTillDate': 0,
>>>                                'orderId': 10508478790,
>>>                                'origQty': '12',
>>>                                'origType': 'MARKET',
>>>                                'positionSide': 'LONG',
>>>                                'price': '0.0000',
>>>                                'priceMatch': 'NONE',
>>>                                'priceProtect': False,
>>>                                'reduceOnly': True,
>>>                                'selfTradePreventionMode': 'NONE',
>>>                                'side': 'SELL',
>>>                                'status': 'NEW',
>>>                                'stopPrice': '0.0000',
>>>                                'symbol': 'MANAUSDT',
>>>                                'timeInForce': 'GTC',
>>>                                'type': 'MARKET',
>>>                                'updateTime': 1706119940870,
>>>                                'workingType': 'CONTRACT_PRICE'},
>>>                       'msg': ''},
>>>  'status': 'FILLED',
>>>  'symbol': 'MANAUSDT'}
4. 同步 市价平仓 回调函数
# 执行成功回调
def callback4(information):print('callback')pprint(information)# 执行错误回调
def errorback4(information):print('errorback')pprint(information)
# 设置callback和errorback
close_market4 = trade.close_market(symbol='MANAUSDT',  # 产品marginType='ISOLATED',  # 保证金模式 ISOLATED: 逐仓 CROSSED: 全仓positionSide='LONG',  # 持仓方向 LONG: 多单 SHORT: 空单quantity=12,  # 平仓数量 (交易货币) 'all' 表示全部可用数量meta={},  # 向回调函数中传递的参数字典callback=callback4,  # 开仓成功触发的回调函数errorback=errorback4,  # 开仓失败触发的回调函数
)
输出:
>>> callback
>>> {'cancel_result': None,
>>>  'error_result': None,
>>>  'func_param': {'callback': <function callback4 at 0x7fafb90c85e0>,
>>>                 'cancel': True,
>>>                 'delay': 0.2,
>>>                 'errorback': <function errorback4 at 0x7fafb90c8700>,
>>>                 'marginType': 'ISOLATED',
>>>                 'meta': {},
>>>                 'newClientOrderId': '',
>>>                 'newThread': False,
>>>                 'positionSide': 'LONG',
>>>                 'quantity': 12,
>>>                 'symbol': 'MANAUSDT',
>>>                 'timeout': 60},
>>>  'get_order_result': {'code': 200,
>>>                       'data': {'avgPrice': '0.4360',
>>>                                'clientOrderId': 'Lx2chF8OXZjTqltcK8I6pR',
>>>                                'closePosition': False,
>>>                                'cumQuote': '5.2320',
>>>                                'executedQty': '12',
>>>                                'goodTillDate': 0,
>>>                                'orderId': 10508480841,
>>>                                'origQty': '12',
>>>                                'origType': 'MARKET',
>>>                                'positionSide': 'LONG',
>>>                                'price': '0.0000',
>>>                                'priceMatch': 'NONE',
>>>                                'priceProtect': False,
>>>                                'reduceOnly': True,
>>>                                'selfTradePreventionMode': 'NONE',
>>>                                'side': 'SELL',
>>>                                'status': 'FILLED',
>>>                                'stopPrice': '0.0000',
>>>                                'symbol': 'MANAUSDT',
>>>                                'time': 1706119956820,
>>>                                'timeInForce': 'GTC',
>>>                                'type': 'MARKET',
>>>                                'updateTime': 1706119956820,
>>>                                'workingType': 'CONTRACT_PRICE'},
>>>                       'msg': ''},
>>>  'meta': {},
>>>  'request_param': {'newClientOrderId': '',
>>>                    'positionSide': 'LONG',
>>>                    'quantity': '12',
>>>                    'side': 'SELL',
>>>                    'symbol': 'MANAUSDT',
>>>                    'type': 'MARKET'},
>>>  'set_order_result': {'code': 200,
>>>                       'data': {'avgPrice': '0.00',
>>>                                'clientOrderId': 'Lx2chF8OXZjTqltcK8I6pR',
>>>                                'closePosition': False,
>>>                                'cumQty': '0',
>>>                                'cumQuote': '0.0000',
>>>                                'executedQty': '0',
>>>                                'goodTillDate': 0,
>>>                                'orderId': 10508480841,
>>>                                'origQty': '12',
>>>                                'origType': 'MARKET',
>>>                                'positionSide': 'LONG',
>>>                                'price': '0.0000',
>>>                                'priceMatch': 'NONE',
>>>                                'priceProtect': False,
>>>                                'reduceOnly': True,
>>>                                'selfTradePreventionMode': 'NONE',
>>>                                'side': 'SELL',
>>>                                'status': 'NEW',
>>>                                'stopPrice': '0.0000',
>>>                                'symbol': 'MANAUSDT',
>>>                                'timeInForce': 'GTC',
>>>                                'type': 'MARKET',
>>>                                'updateTime': 1706119956820,
>>>                                'workingType': 'CONTRACT_PRICE'},
>>>                       'msg': ''},
>>>  'status': 'FILLED',
>>>  'symbol': 'MANAUSDT'}
5. 异步 市价平仓 回调函数
# 执行成功回调
def callback5(information):print('thread callback')pprint(information)# 执行错误回调
def errorback5(information):print('thread errorback')pprint(information)
# newThread=True
close_market5 = trade.close_market(symbol='MANAUSDT',  # 产品marginType='ISOLATED',  # 保证金模式 ISOLATED: 逐仓 CROSSED: 全仓positionSide='LONG',  # 持仓方向 LONG: 多单 SHORT: 空单quantity=12,  # 平仓数量 (交易货币) 'all' 表示全部可用数量callback=callback5,  # 开仓成功触发的回调函数errorback=errorback5,  # 开仓失败触发的回调函数meta={},  # 向回调函数中传递的参数字典newThread=True,  # 是否开启一个新的线程维护这个订单
)
print(close_market5)
print('-' * 30)
输出:
>>> <Thread(Thread-5, started 123145507303424)>
>>> ------------------------------
>>> thread callback
>>> {'cancel_result': None,
>>>  'error_result': None,
>>>  'func_param': {'callback': <function callback5 at 0x7f91e0fe58b0>,
>>>                 'cancel': True,
>>>                 'delay': 0.2,
>>>                 'errorback': <function errorback5 at 0x7f91e0fe5820>,
>>>                 'marginType': 'ISOLATED',
>>>                 'meta': {},
>>>                 'newClientOrderId': '',
>>>                 'newThread': True,
>>>                 'positionSide': 'LONG',
>>>                 'quantity': 12,
>>>                 'symbol': 'MANAUSDT',
>>>                 'timeout': 60},
>>>  'get_order_result': {'code': 200,
>>>                       'data': {'avgPrice': '0.4351',
>>>                                'clientOrderId': 'k4VRMhvmrSYL4JOsEr90bZ',
>>>                                'closePosition': False,
>>>                                'cumQuote': '5.2212',
>>>                                'executedQty': '12',
>>>                                'goodTillDate': 0,
>>>                                'orderId': 10508487895,
>>>                                'origQty': '12',
>>>                                'origType': 'MARKET',
>>>                                'positionSide': 'LONG',
>>>                                'price': '0.0000',
>>>                                'priceMatch': 'NONE',
>>>                                'priceProtect': False,
>>>                                'reduceOnly': True,
>>>                                'selfTradePreventionMode': 'NONE',
>>>                                'side': 'SELL',
>>>                                'status': 'FILLED',
>>>                                'stopPrice': '0.0000',
>>>                                'symbol': 'MANAUSDT',
>>>                                'time': 1706120019876,
>>>                                'timeInForce': 'GTC',
>>>                                'type': 'MARKET',
>>>                                'updateTime': 1706120019879,
>>>                                'workingType': 'CONTRACT_PRICE'},
>>>                       'msg': ''},
>>>  'meta': {},
>>>  'request_param': {'newClientOrderId': '',
>>>                    'positionSide': 'LONG',
>>>                    'quantity': '12',
>>>                    'side': 'SELL',
>>>                    'symbol': 'MANAUSDT',
>>>                    'type': 'MARKET'},
>>>  'set_order_result': {'code': 200,
>>>                       'data': {'avgPrice': '0.00',
>>>                                'clientOrderId': 'k4VRMhvmrSYL4JOsEr90bZ',
>>>                                'closePosition': False,
>>>                                'cumQty': '0',
>>>                                'cumQuote': '0.0000',
>>>                                'executedQty': '0',
>>>                                'goodTillDate': 0,
>>>                                'orderId': 10508487895,
>>>                                'origQty': '12',
>>>                                'origType': 'MARKET',
>>>                                'positionSide': 'LONG',
>>>                                'price': '0.0000',
>>>                                'priceMatch': 'NONE',
>>>                                'priceProtect': False,
>>>                                'reduceOnly': True,
>>>                                'selfTradePreventionMode': 'NONE',
>>>                                'side': 'SELL',
>>>                                'status': 'NEW',
>>>                                'stopPrice': '0.0000',
>>>                                'symbol': 'MANAUSDT',
>>>                                'timeInForce': 'GTC',
>>>                                'type': 'MARKET',
>>>                                'updateTime': 1706120019879,
>>>                                'workingType': 'CONTRACT_PRICE'},
>>>                       'msg': ''},
>>>  'status': 'FILLED',
>>>  'symbol': 'MANAUSDT'}