【发布时间】:2020-06-10 02:30:45
【问题描述】:
我正在尝试使用Python asyncio、ccxt API(为 Binance Crypto Exchange 调用Binance API)创建一个包含BUY ORDER 和STOP-LOSS 的LONG TRADE。
首先我创建了购买订单。之后,我等到订单完成。 成交后,我正在尝试创建一个与创建的 LONG 一致的止损订单(原因:如果交易对我不利,我不想损失很多钱) .
根据日志,BUY ORDER 正在创建中。 我在 STOP-LIMIT 订单上收到“资金不足”。
我不明白为什么当买入订单 顺利通过时,我会在止损订单上获得“资金不足”(它甚至有状态已填充)。
如果我以 9000 美元的价格买入 .0015 BTC(并且订单已FILLED),我应该能够以 8875 美元的价格止损 .0015 BTC(再次,以保留案件中的资金交易对我不利)
为什么这不起作用?为什么我不能为我的交易创建止损?
ETA:在另一个线程中看到了这个:
TAKE_PROFIT 与 STOP_LOSS 相反。一个在 向下,另一个向上执行。
问题:如何构建 API 以便为订单设置 TAKE_PROFIT 和 STOP_LOSS?
async def execute_long_trade(self, trade: LongTrade):
try:
buy_price = trade.start_price
sell_price = trade.exit_price
symbol = trade.exchange_symbol
amount = trade.amount
stop_loss = trade.stop_loss
order = self.exchange.ccxt_create_buy_order(symbol, amount, buy_price, 0 )
[...剪辑...]
logging.info(f'Opened long trade: {amount} of {symbol}. Target buy {stop_loss}, sell price {sell_price}')
await self._wait_order_complete(order["data"][0]["id"], symbol)
# set up a stop loss order
order = self.exchange.ccxt_create_sell_order(symbol, amount, sell_price, stop_loss )
logging.info(f'Completed long trade: {amount} of {symbol}. Bought at {buy_price} and sold at {sell_price}')
except ExchangeError as e:
raise
except Exception as e:
print (" unexpected exception ")
exit()
[...剪辑...]
CALLING BINANCE(用于购买部分 - 效果很好):
def ccxt_create_buy_order( self, symbol: str, amount: float, price: float, stop_price: float ):
try:
results = {}
if ( stop_price > 0 ):
params = { 'stopPrice': stop_price - 10 }
output = self.ccxt_binance.createOrder(symbol, 'STOP_LOSS_LIMIT', amount=amount, side="buy",
price = stop_price, params=params)
else:
output = self.ccxt_binance.create_order(symbol=symbol, type="limit", side="buy",\
amount=amount, price=price )
[ ... snip ...]
return (results)
except ccxt.InsufficientFunds as e:
print ("insufficient funds)
return
except Exception as e:
print (" unexpected error ")
exit()
CALLING BINANCE(对于 SELL 部分 - 出现“资金不足”错误):
def ccxt_create_sell_order( self, symbol: str, amount: float, price: float, stop_price: float ):
try:
results = {}
if ( stop_price > 0 ):
params = { 'stopPrice': stop_price + 10 }
output = self.ccxt_binance.createOrder(symbol, 'STOP_LOSS_LIMIT', amount=amount, \
side="sell", price = stop_price, params=params)
print(output)
else:
output = self.ccxt_binance.create_order(symbol=symbol, \
type="limit", side="sell",amount=amount )
[ ... snip ...]
return (results)
except ccxt.InsufficientFunds as e:
print ("insufficient funds)
return
except Exception as e:
print (" unexpected error ")
exit()
购买部分的日志
2020-06-10 01:01:08 - DEBUG - 16537 - ccxt.base.exchange - DEBUG
MESSAGE : POST https://api.binance.com/api/v3/order, Request: {'X-MBX-APIKEY': 'JXXXXXXXX-XXXXXX', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate'} timestamp=1591750868935&recvWindow=5000&symbol=BTCUSDT&type=LIMIT&side=BUY&quantity=0.0015&newOrderRespType=RESULT&price=9777.99&timeInForce=GTC&signature=XXXXXXXXXXXX-XXXXXXXXXXXXXX
2020-06-10 01:01:09 - DEBUG - 16537 - urllib3.connectionpool - DEBUG
MESSAGE : https://api.binance.com:443 "POST /api/v3/order HTTP/1.1" 200 None
2020-06-10 01:01:09 - DEBUG - 16537 - ccxt.base.exchange - DEBUG
MESSAGE : POST https://api.binance.com/api/v3/order, Response: 200 {'Content-Type': 'application/json;charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Date': 'Wed, 10 Jun 2020 01:01:09 GMT', 'Server': 'nginx', 'X-MBX-UUID': 'XXXXXXXXX-XXXXx', 'X-MBX-USED-WEIGHT': '5', 'X-MBX-USED-WEIGHT-1M': '5', 'X-MBX-ORDER-COUNT-10S': '1', 'X-MBX-ORDER-COUNT-1D': '10', 'Content-Encoding': 'gzip', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains', 'X-Frame-Options': 'SAMEORIGIN', 'X-Xss-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'self'", 'X-Content-Security-Policy': "default-src 'self'", 'X-WebKit-CSP': "default-src 'self'", 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS', 'X-Cache': 'Miss from cloudfront', 'Via': '1.1 e9ccfc64a258a54713XXXXb7b.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ATL56-C3', 'X-Amz-Cf-Id': 'gXXXXXXXXXXX-XXX9TzUqeLltao4UiQ=='} {"symbol":"BTCUSDT","orderId":2434736796,"orderListId":-1,"clientOrderId":"93XXXXXXXECejmtfb","transactTime":1591750869025,"price":"9777.99000000","origQty":"0.00150000","executedQty":"0.00150000","cummulativeQuoteQty":"14.66698500","status":"FILLED","timeInForce":"GTC","type":"LIMIT","side":"BUY"}
出售部分的日志:
2020-06-10 01:01:24 - DEBUG - 16537 - ccxt.base.exchange - DEBUG
MESSAGE : POST https://api.binance.com/api/v3/order, Request: {'X-MBX-APIKEY': 'XXXXXXXXX-XXX', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate'} timestamp=1591750884617&recvWindow=5000&symbol=BTCUSDT&type=STOP_LOSS_LIMIT&side=SELL&quantity=0.0015&newOrderRespType=RESULT&price=9670.21&timeInForce=GTC&stopPrice=9680.21&signature=XXXXXXX-XXXXXXX-XXXXXXXXXXX-XXXXXXXXXXXXX
2020-06-10 01:01:24 - DEBUG - 16537 - urllib3.connectionpool - DEBUG
MESSAGE : https://api.binance.com:443 "POST /api/v3/order HTTP/1.1" 400 None
2020-06-10 01:01:24 - DEBUG - 16537 - ccxt.base.exchange - DEBUG
MESSAGE : POST https://api.binance.com/api/v3/order, Response: 400 {'Content-Type': 'application/json;charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Date': 'Wed, 10 Jun 2020 01:01:24 GMT', 'Server': 'nginx', 'X-MBX-UUID': 'XXXXXX-023741e9e18b', 'X-MBX-USED-WEIGHT': '7', 'X-MBX-USED-WEIGHT-1M': '7', 'X-MBX-ORDER-COUNT-10S': '1', 'X-MBX-ORDER-COUNT-1D': '11', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains', 'X-Frame-Options': 'SAMEORIGIN', 'X-Xss-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'self'", 'X-Content-Security-Policy': "default-src 'self'", 'X-WebKit-CSP': "default-src 'self'", 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 e9XXXXXXXX-09e5b7b.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ATL56-C3', 'X-Amz-Cf-Id': 'XXXXXXX'} {"code":-2010,"msg":"Account has insufficient balance for requested action."}
【问题讨论】:
-
那是很多代码,但从最后几行来看,您的问题似乎不属于技术性质,因为服务器返回了状态为 400 的正确响应,表明您发送的请求这不怎么样。通常这意味着它的格式不正确,或者引用了一些不存在的值。由于余额不足,它可能会向您发送 400,但是您是否尝试在 STOP-LOSS 请求之前请求您的余额?它是否表明你有足够的平衡?是不是太快了?服务器是事务性的,您是否需要在提交 STOP-LOSS 之前提交 BUY?
-
感谢您的回复。 >>>那是很多代码>>由于余额不足,它可能会向您发送 400
标签: python python-asyncio binance ccxt