Binance_interface APP 现货交易-限价单开仓
- Github地址
- PyTed量化交易研究院
目录
- Binance_interface APP 现货交易-限价单开仓
- 1. APP 现货交易-限价单开仓函数总览
- 2. 模型实例化
- 3. 同步 非堵塞 限价开仓(购买)
- 4. 同步 堵塞 限价开仓(购买)
- 5. 同步 堵塞 限价开仓(购买) 回调函数
- 6. 异步 堵塞 限价开仓(购买)回调函数
1. APP 现货交易-限价单开仓函数总览
方法 | 解释 |
---|---|
open_limit | 限价单购买 |
2. 模型实例化
from binance_interface.app import BinanceSPOT
from pprint import pprint
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'binanceSPOT = BinanceSPOT(key=key, secret=secret,proxy_host=proxy_host
)
trade = binanceSPOT.trade
3. 同步 非堵塞 限价开仓(购买)
open_limit3 = trade.open_limit(symbol='MANAUSDT', # 产品openPrice=0.3, # 开仓价格openMoney=10, # 开仓金额 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级:quantity > openMoney# quantity=10, # 开仓数量block=False, # 是否以堵塞的模式newClientOrderId='', # 客户自定义订单ID
)
pprint(open_limit3)
输出:
>>> {'cancel_result': None,
>>> 'error_result': None,
>>> 'func_param': {'block': False,
>>> 'callback': None,
>>> 'cancel': True,
>>> 'delay': 0.2,
>>> 'errorback': None,
>>> 'meta': {},
>>> 'newClientOrderId': '',
>>> 'newThread': False,
>>> 'openMoney': 10,
>>> 'openPrice': 0.3,
>>> 'quantity': None,
>>> 'symbol': 'MANAUSDT',
>>> 'timeout': 60},
>>> 'get_order_result': None,
>>> 'meta': {},
>>> 'request_param': {'newClientOrderId': '',
>>> 'price': '0.2999',
>>> 'quantity': '33',
>>> 'side': 'BUY',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT'},
>>> 'set_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'syzM1d1gxDo0VVRUFL84h6',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'fills': [],
>>> 'orderId': 2207173337,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'NEW',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105063022,
>>> 'type': 'LIMIT',
>>> 'workingTime': 1706105063022},
>>> 'msg': ''},
>>> 'status': None,
>>> 'symbol': 'MANAUSDT'}
4. 同步 堵塞 限价开仓(购买)
# 超时,可选择是否取消订单
# 设置block、timeout、delay与cancel
open_limit4 = trade.open_limit(symbol='MANAUSDT', # 产品openPrice=0.3, # 开仓价格quantity=20, # 开仓数量 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级:quantity > openMoney# quantity=10, # 开仓数量block=True, # 是否以堵塞的模式timeout=5, # 等待订单成功的超时时间(秒)delay=0.2, # 检测订单状态的间隔 (秒)cancel=True, # 订单超时后是否取消
)pprint(open_limit4)
输出:
>>> {'cancel_result': {'code': 200,
>>> 'data': {'clientOrderId': 'GsocGcpMvmc9FdmSQy9p5V',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'orderId': 2207173936,
>>> 'orderListId': -1,
>>> 'origClientOrderId': 'F0CyjIrocR6qTWnNuEhdeE',
>>> 'origQty': '20.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105095143,
>>> 'type': 'LIMIT'},
>>> 'msg': ''},
>>> 'error_result': None,
>>> 'func_param': {'block': True,
>>> 'callback': None,
>>> 'cancel': True,
>>> 'delay': 0.2,
>>> 'errorback': None,
>>> 'meta': {},
>>> 'newClientOrderId': '',
>>> 'newThread': False,
>>> 'openMoney': None,
>>> 'openPrice': 0.3,
>>> 'quantity': 20,
>>> 'symbol': 'MANAUSDT',
>>> 'timeout': 5},
>>> 'get_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'F0CyjIrocR6qTWnNuEhdeE',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'isWorking': True,
>>> 'orderId': 2207173936,
>>> 'orderListId': -1,
>>> 'origQty': '20.00000000',
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'stopPrice': '0.00000000',
>>> 'symbol': 'MANAUSDT',
>>> 'time': 1706105089770,
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'updateTime': 1706105095143,
>>> 'workingTime': 1706105089770},
>>> 'msg': ''},
>>> 'meta': {},
>>> 'request_param': {'newClientOrderId': '',
>>> 'price': '0.2999',
>>> 'quantity': '20',
>>> 'side': 'BUY',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT'},
>>> 'set_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'F0CyjIrocR6qTWnNuEhdeE',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'fills': [],
>>> 'orderId': 2207173936,
>>> 'orderListId': -1,
>>> 'origQty': '20.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'NEW',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105089770,
>>> 'type': 'LIMIT',
>>> 'workingTime': 1706105089770},
>>> 'msg': ''},
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT'}
5. 同步 堵塞 限价开仓(购买) 回调函数
# 执行成功回调
def callback5(information):print('callback')pprint(information)# 执行错误回调
def errorback5(information):print('errorback')pprint(information)
# 设置callback与errorback
open_limit5 = trade.open_limit(symbol='MANAUSDT', # 产品openPrice=0.3, # 开仓价格openMoney=10, # 开仓金额 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级:quantity > openMoney# quantity=10, # 开仓数量block=True, # 是否以堵塞的模式timeout=5, # 等待订单成功的超时时间(秒)delay=0.2, # 检测订单状态的间隔 (秒)cancel=True, # 订单超时后是否取消callback=callback5, # 开仓成功触发的回调函数errorback=errorback5, # 开仓失败触发的回调函数
)
pprint(open_limit5)
输出:
>>> callback
>>> {'cancel_result': {'code': 200,
>>> 'data': {'clientOrderId': 'REaVqfB7ZyFoxTSoHPyPSO',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'orderId': 2207174395,
>>> 'orderListId': -1,
>>> 'origClientOrderId': 'wwuv94rG5xn8xkGtT93QT9',
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105117970,
>>> 'type': 'LIMIT'},
>>> 'msg': ''},
>>> 'error_result': None,
>>> 'func_param': {'block': True,
>>> 'callback': <function callback5 at 0x7ffbf98ce790>,
>>> 'cancel': True,
>>> 'delay': 0.2,
>>> 'errorback': <function errorback5 at 0x7ffbf98e95e0>,
>>> 'meta': {},
>>> 'newClientOrderId': '',
>>> 'newThread': False,
>>> 'openMoney': 10,
>>> 'openPrice': 0.3,
>>> 'quantity': None,
>>> 'symbol': 'MANAUSDT',
>>> 'timeout': 5},
>>> 'get_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'wwuv94rG5xn8xkGtT93QT9',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'isWorking': True,
>>> 'orderId': 2207174395,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'stopPrice': '0.00000000',
>>> 'symbol': 'MANAUSDT',
>>> 'time': 1706105112126,
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'updateTime': 1706105117970,
>>> 'workingTime': 1706105112126},
>>> 'msg': ''},
>>> 'meta': {},
>>> 'request_param': {'newClientOrderId': '',
>>> 'price': '0.2999',
>>> 'quantity': '33',
>>> 'side': 'BUY',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT'},
>>> 'set_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'wwuv94rG5xn8xkGtT93QT9',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'fills': [],
>>> 'orderId': 2207174395,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'NEW',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105112126,
>>> 'type': 'LIMIT',
>>> 'workingTime': 1706105112126},
>>> 'msg': ''},
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT'}
>>> {'cancel_result': {'code': 200,
>>> 'data': {'clientOrderId': 'REaVqfB7ZyFoxTSoHPyPSO',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'orderId': 2207174395,
>>> 'orderListId': -1,
>>> 'origClientOrderId': 'wwuv94rG5xn8xkGtT93QT9',
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105117970,
>>> 'type': 'LIMIT'},
>>> 'msg': ''},
>>> 'error_result': None,
>>> 'func_param': {'block': True,
>>> 'callback': <function callback5 at 0x7ffbf98ce790>,
>>> 'cancel': True,
>>> 'delay': 0.2,
>>> 'errorback': <function errorback5 at 0x7ffbf98e95e0>,
>>> 'meta': {},
>>> 'newClientOrderId': '',
>>> 'newThread': False,
>>> 'openMoney': 10,
>>> 'openPrice': 0.3,
>>> 'quantity': None,
>>> 'symbol': 'MANAUSDT',
>>> 'timeout': 5},
>>> 'get_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'wwuv94rG5xn8xkGtT93QT9',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'isWorking': True,
>>> 'orderId': 2207174395,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'stopPrice': '0.00000000',
>>> 'symbol': 'MANAUSDT',
>>> 'time': 1706105112126,
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'updateTime': 1706105117970,
>>> 'workingTime': 1706105112126},
>>> 'msg': ''},
>>> 'meta': {},
>>> 'request_param': {'newClientOrderId': '',
>>> 'price': '0.2999',
>>> 'quantity': '33',
>>> 'side': 'BUY',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT'},
>>> 'set_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'wwuv94rG5xn8xkGtT93QT9',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'fills': [],
>>> 'orderId': 2207174395,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'NEW',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105112126,
>>> 'type': 'LIMIT',
>>> 'workingTime': 1706105112126},
>>> 'msg': ''},
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT'}
6. 异步 堵塞 限价开仓(购买)回调函数
# 执行成功回调
def callback6(information):print('thread callback')pprint(information)# 执行错误回调
def errorback6(information):print('thread errorback')pprint(information)
# 设置newThread=True
open_limit6 = trade.open_limit(symbol='MANAUSDT', # 产品openPrice=0.3, # 开仓价格openMoney=10, # 开仓金额 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级:quantity > openMoney# quantity=10, # 开仓数量block=True, # 是否以堵塞的模式timeout=5, # 等待订单成功的超时时间(秒)delay=0.2, # 检测订单状态的间隔 (秒)cancel=True, # 订单超时后是否取消callback=callback6, # 开仓成功触发的回调函数errorback=errorback6, # 开仓失败触发的回调函数newThread=True, # 是否开启一个新的线程维护这个订单
)
pprint(open_limit6)
print('-' * 30)
输出:
>>> <Thread(Thread-5, started 123145546551296)>
>>> ------------------------------
>>> thread callback
>>> {'cancel_result': {'code': 200,
>>> 'data': {'clientOrderId': 'TzdxSgJmvmOCY814wW2Lh0',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'orderId': 2207175195,
>>> 'orderListId': -1,
>>> 'origClientOrderId': 'EhG213m2dtXEkGRTNJ7G5J',
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105173358,
>>> 'type': 'LIMIT'},
>>> 'msg': ''},
>>> 'error_result': None,
>>> 'func_param': {'block': True,
>>> 'callback': <function callback6 at 0x7ffbf98e98b0>,
>>> 'cancel': True,
>>> 'delay': 0.2,
>>> 'errorback': <function errorback6 at 0x7ffbf98e9820>,
>>> 'meta': {},
>>> 'newClientOrderId': '',
>>> 'newThread': True,
>>> 'openMoney': 10,
>>> 'openPrice': 0.3,
>>> 'quantity': None,
>>> 'symbol': 'MANAUSDT',
>>> 'timeout': 5},
>>> 'get_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'EhG213m2dtXEkGRTNJ7G5J',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'icebergQty': '0.00000000',
>>> 'isWorking': True,
>>> 'orderId': 2207175195,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'origQuoteOrderQty': '0.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'CANCELED',
>>> 'stopPrice': '0.00000000',
>>> 'symbol': 'MANAUSDT',
>>> 'time': 1706105167767,
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT',
>>> 'updateTime': 1706105173358,
>>> 'workingTime': 1706105167767},
>>> 'msg': ''},
>>> 'meta': {},
>>> 'request_param': {'newClientOrderId': '',
>>> 'price': '0.2999',
>>> 'quantity': '33',
>>> 'side': 'BUY',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'type': 'LIMIT'},
>>> 'set_order_result': {'code': 200,
>>> 'data': {'clientOrderId': 'EhG213m2dtXEkGRTNJ7G5J',
>>> 'cummulativeQuoteQty': '0.00000000',
>>> 'executedQty': '0.00000000',
>>> 'fills': [],
>>> 'orderId': 2207175195,
>>> 'orderListId': -1,
>>> 'origQty': '33.00000000',
>>> 'price': '0.29990000',
>>> 'selfTradePreventionMode': 'EXPIRE_MAKER',
>>> 'side': 'BUY',
>>> 'status': 'NEW',
>>> 'symbol': 'MANAUSDT',
>>> 'timeInForce': 'GTC',
>>> 'transactTime': 1706105167767,
>>> 'type': 'LIMIT',
>>> 'workingTime': 1706105167767},
>>> 'msg': ''},
>>> 'status': 'CANCELED',
>>> 'symbol': 'MANAUSDT'}