[英]With the Python-Binance API, my limit order is only partially filled
我正在使用 Python 3.9 和 Python - Binance API,版本 python-binance==1.0.15。 在他們的測試環境中,我像這樣下訂單
order=self._get_auth_client(account).order_limit_buy(symbol=formatted_name,
quantity=amount,
price=fiat_price)
這將返回以下 JSON
{'symbol': 'ETHUSDT', 'orderId': 2603582, 'orderListId': -1, 'clientOrderId': 'Ru4Vv2jmxHIfGI21vIMtjD', 'transactTime': 1650828003836, 'price': '2915.16000000', 'origQty': '0.34303000', 'executedQty': '0.00000000', 'cummulativeQuoteQty': '0.00000000', 'status': 'NEW', 'timeInForce': 'GTC', 'type': 'LIMIT', 'side': 'BUY', 'fills': []}
使用“orderId”字段,我檢查訂單狀態,然后取回結果
{'symbol': 'ETHUSDT', 'orderId': 2603582, 'orderListId': -1, 'clientOrderId': 'Ru4Vv2jmxHIfGI21vIMtjD', 'price': '2915.16000000', 'origQty': '0.34303000', 'executedQty': '0.08067000', 'cummulativeQuoteQty': '235.16595720', 'status': 'PARTIALLY_FILLED', 'timeInForce': 'GTC', 'type': 'LIMIT', 'side': 'BUY', 'stopPrice': '0.00000000', 'icebergQty': '0.00000000', 'time': 1650828003836, 'updateTime': 1650828050722, 'isWorking': True, 'origQuoteOrderQty': '0.00000000'}
狀態表示部分填充。 我想知道是否有一種方法可以指定我的買單,使其完全成交或根本不成交。 雖然我沒有在他們的文檔中看到任何指定的內容,但它們有點稀疏。
部分執行訂單似乎是Reddit上討論過的常見問題。
以下內容來自與您正在執行的order_limit_buy
相關的API 文檔。
order_limit_buy(timeInForce='GTC', **params)[source]
發送新的限價買單
任何帶有 icebergQty 的訂單必須將 timeInForce 設置為 GTC。
參數:
返回: 。 API 回復
查看訂單端點以獲得完整的響應選項
提高:
下面是order_limit_buy function的源代碼
def order_limit_buy(self, timeInForce=BaseClient.TIME_IN_FORCE_GTC, **params):
"""Send in a new limit buy order
Any order with an icebergQty MUST have timeInForce set to GTC.
:param symbol: required
:type symbol: str
:param quantity: required
:type quantity: decimal
:param price: required
:type price: str
:param timeInForce: default Good till cancelled
:type timeInForce: str
:param newClientOrderId: A unique id for the order. Automatically generated if not sent.
:type newClientOrderId: str
:param stopPrice: Used with stop orders
:type stopPrice: decimal
:param icebergQty: Used with iceberg orders
:type icebergQty: decimal
:param newOrderRespType: Set the response JSON. ACK, RESULT, or FULL; default: RESULT.
:type newOrderRespType: str
:param recvWindow: the number of milliseconds the request is valid for
:type recvWindow: int
:returns: API response
See order endpoint for full response options
:raises: BinanceRequestException, BinanceAPIException, BinanceOrderException, BinanceOrderMinAmountException, BinanceOrderMinPriceException, BinanceOrderMinTotalException, BinanceOrderUnknownSymbolException, BinanceOrderInactiveSymbolException
"""
params.update({
'side': self.SIDE_BUY,
})
return self.order_limit(timeInForce=timeInForce, **params)
API 參數或 Python order_limit_buy
function 參數都沒有明確說明如何防止部分填充訂單問題。
這是您的購買訂單:
order=self._get_auth_client(account).order_limit_buy(symbol=formatted_name,
quantity=amount,
price=fiat_price)
您的訂單包含 API 文檔中所述的 3 個必需參數:
我找到了文章什么是止損限價單? 在幣安學院網站上。 文章有這樣的說法:
如果您擔心您的訂單只能部分成交,請考慮使用 fill or kill。
基於此聲明,我開始查看 API 文檔和源代碼,了解如何設置FILL
或KILL
訂單。
我注意到 Python order_limit_buy
function 有這個參數:
:param timeInForce: default Good till cancelled
:type timeInForce: str
默認值為Good till cancelled
或GTC
。
查看 API 源代碼我發現timeInForce
參數有 3 個可能的值:
TIME_IN_FORCE_GTC = 'GTC' # Good till cancelled
TIME_IN_FORCE_IOC = 'IOC' # Immediate or cancel
TIME_IN_FORCE_FOK = 'FOK' # Fill or kill
請注意值TIME_IN_FORCE_FOK
或FOK
。
以下內容來自GitHub 上的 Binance API 文檔:
生效時間(timeInForce):
這設置了訂單在到期前的有效時間。
地位 | 描述 |
---|---|
技術中心 | 取消前有效 訂單將記錄在冊,除非訂單被取消。 |
國際奧委會 | 立即或取消 訂單將嘗試在訂單到期之前盡可能多地填寫訂單。 |
霍克 | 填充或殺死 如果在執行時無法填寫完整訂單,則訂單將過期 |
使用值為FOK
的timeInForce
參數時,您的購買請求應如下所示:
order=self._get_auth_client(account).order_limit_buy(symbol=formatted_name,
quantity=amount,
price=fiat_price,
timeInForce='FOK')
我創建了一個幣安測試網賬戶並開發了下面的代碼作為測試。 我將目標價定在 2687.00 買入 ETHUSDT。 我用一個循環來放置我的有限購買並檢查它是否被填滿。
from binance.client import Client
api_key = 'my key'
api_secret = 'my secret'
client = Client(api_key, api_secret, testnet=True)
order_status = True
while True:
limit_order = client.order_limit_buy(symbol="ETHUSDT", quantity=0.01, price='2687.00', timeInForce='FOK')
ticker = client.get_ticker(symbol="ETHUSDT")
print(f"Current Price: {ticker.get('askPrice')}")
print(limit_order)
_status = limit_order.get('status')
if _status == 'FILLED':
order_status = False
print(order_status)
break
elif _status == 'EXPIRED':
order_status = True
上面代碼中的 output 如下:
注意:這是 output 的一個片段,因為循環將一直運行到買單觸發。
Current Price: 2687.33000000
{'symbol': 'ETHUSDT', 'orderId': 962373, 'orderListId': -1, 'clientOrderId': 'nW7bI2tkTwQEvrb8sSqgM6', 'transactTime': 1651927994444, 'price': '2687.00000000', 'origQty': '0.01000000', 'executedQty': '0.00000000', 'cummulativeQuoteQty': '0.00000000', 'status': 'EXPIRED', 'timeInForce': 'FOK', 'type': 'LIMIT', 'side': 'BUY', 'fills': []}
Current Price: 2687.33000000
{'symbol': 'ETHUSDT', 'orderId': 962378, 'orderListId': -1, 'clientOrderId': '7MPoHZDykxsK3Oqo7uOB78', 'transactTime': 1651927995310, 'price': '2687.00000000', 'origQty': '0.01000000', 'executedQty': '0.00000000', 'cummulativeQuoteQty': '0.00000000', 'status': 'EXPIRED', 'timeInForce': 'FOK', 'type': 'LIMIT', 'side': 'BUY', 'fills': []}
Current Price: 2687.33000000
{'symbol': 'ETHUSDT', 'orderId': 962387, 'orderListId': -1, 'clientOrderId': '3mRS9GK6pfGpqaqU9SK1vK', 'transactTime': 1651927996177, 'price': '2687.00000000', 'origQty': '0.01000000', 'executedQty': '0.00000000', 'cummulativeQuoteQty': '0.00000000', 'status': 'EXPIRED', 'timeInForce': 'FOK', 'type': 'LIMIT', 'side': 'BUY', 'fills': []}
Current Price: 2687.33000000
{'symbol': 'ETHUSDT', 'orderId': 962395, 'orderListId': -1, 'clientOrderId': '8yTAtrsjNH2PELtg93SdH3', 'transactTime': 1651927997041, 'price': '2687.00000000', 'origQty': '0.01000000', 'executedQty': '0.00000000', 'cummulativeQuoteQty': '0.00000000', 'status': 'EXPIRED', 'timeInForce': 'FOK', 'type': 'LIMIT', 'side': 'BUY', 'fills': []}
Current Price: 2687.06000000
{'symbol': 'ETHUSDT', 'orderId': 962403, 'orderListId': -1, 'clientOrderId': '5q8GPEg5bYgzoW7PUnR2VN', 'transactTime': 1651927997903, 'price': '2687.00000000', 'origQty': '0.01000000', 'executedQty': '0.00000000', 'cummulativeQuoteQty': '0.00000000', 'status': 'EXPIRED', 'timeInForce': 'FOK', 'type': 'LIMIT', 'side': 'BUY', 'fills': []}
Current Price: 2686.87000000
{'symbol': 'ETHUSDT', 'orderId': 962420, 'orderListId': -1, 'clientOrderId': 'ENxCN1JAW4OcxLiAkSmdIH', 'transactTime': 1651927999639, 'price': '2687.00000000', 'origQty': '0.01000000', 'executedQty': '0.01000000', 'cummulativeQuoteQty': '26.86870000', 'status': 'FILLED', 'timeInForce': 'FOK', 'type': 'LIMIT', 'side': 'BUY', 'fills': [{'price': '2686.87000000', 'qty': '0.01000000', 'commission': '0.00000000', 'commissionAsset': 'ETH', 'tradeId': 225595}]}
False # loop closed
我不認為這是可能的。 這是由於交易所訂單匹配系統的性質。 當您發送購買0.34303ETH @2915.16 的訂單時,交易所會尋找想要出售ETH @2915.16 的人,也就是。 對方。 然而,他們想要出售的數量很少能正好是 0.34303ETH。 它可以大於或小於此數量。 這就是為什么當市場在指定的價格水平附近大幅波動時,您可能會部分成交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.