我正在尝试创建一个LONG TRADE
,其中将包含一个BUY ORDER
和一个STOP-LOSS
,使用Python asyncio
,ccxt API
(称为{{1 }}用于币安加密货币交易所。
首先,我创建购买订单。然后,我等到订单完成。 交易完成后,我试图创建一个止损定单来处理所创建的长线(原因:如果交易不利于我,我不想损失很多钱)
根据日志,确定正在创建“购买订单”。 我在STOP-LIMIT订单上收到“资金不足”。
我不明白为什么买入订单 顺利通过时,为什么我会在止损订单上获得“资金不足”的信息(甚至处于状态(已填充)。
如果我以9000美元的价格买进.0015 BTC(且订单已完成 Filled ),那么我应该能够以8875美元的价格止损.0015 BTC(同样,在这种情况下,您可以保留资金)贸易对我不利)
为什么这不起作用?为什么我不能为自己的交易创建止损?
TAKE_PROFIT 与 STOP_LOSS 相反。一个执行 向下执行,另一个执行向上执行。
问题:如何构造API,以便可以为订单设置 TAKE_PROFIT 和 STOP_LOSS ?
Binance API
[...片段...]
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()
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()
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"}
答案 0 :(得分:0)
def ccxt_create_sell_order(self,symbol:str,amount:float,price:float,stop_price:float): 尝试: 结果= {}
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 )
[...片段...]
return (results)
except ccxt.InsufficientFunds as e:
print ("insufficient funds)
return
except Exception as e:
print (" unexpected error ")
exit()
答案 1 :(得分:0)
您的限价单api帖子中没有价格