0

我正在尝试创建一个LONG TRADE包含 aBUY ORDERSTOP-LOSSusing的Python asyncio, ccxt API(调用Binance APIBinance Crypto Exchange 的 )。

首先,我创建了购买订单。之后,我等到订单完成。完成后,我正在尝试创建一个与创建的 LONG 一致的止损订单(原因:如果交易对我不利,我不想损失很多钱)。

根据日志,正在创建 BUY ORDER。我在 STOP-LIMIT 订单上收到“资金不足”。

我不明白为什么当BUY 订单 顺利通过(它甚至具有FILLED状态)时,我会在 STOP-LOSS 订单上获得“资金不足”。

如果我以 9000 美元的价格购买.0015 BTC(并且订单已完成),我应该能够以 8875 美元的价格止损 .0015 BTC(再次,以在交易对我不利的情况下保留资金)

为什么这不起作用?为什么我不能为我的交易创建止损?

ETA:在另一个线程中看到了这个:

TAKE_PROFITSTOP_LOSS相反。一个向下执行,另一个向上执行。

问题:如何构建 API 以便为订单设置TAKE_PROFITSTOP_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(对于 BUY 部分 - 效果很好):

    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."}
4

2 回答 2

0

您的限价单 API 帖子中没有价格

于 2021-01-12T21:45:48.837 回答
-2

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 )

[...剪断...]

        return (results)

    except ccxt.InsufficientFunds as e:
        print ("insufficient funds)
        return

    except Exception as e:
        print (" unexpected error ")
        exit()
于 2020-11-12T09:44:29.960 回答