无法将订单参数作为变量发送到 Binance API - Python

时间:2021-04-28 17:29:25

标签: python api binance

我试图将止盈和止损不是作为固定价格发送给币安,而是作为一个变量来计算它,例如 sl = price*0.98

如果我将止损和止盈设置为固定价格(“价格”:“650”),订单会通过,但是当我使用变量(见下面的代码)时,我会收到 [{'code': 400, 'msg': None}, {'code': 400, 'msg': None}] 响应。< /p>

这是签名请求的方法(我没有编码)

def send_signed_request(http_method, url_path, payload={}):
    query_string = urlencode(payload)
    # replace single quote to double quote
    query_string = query_string.replace('%27', '%22')
    if query_string:
        query_string = "{}&timestamp={}".format(query_string, get_timestamp())
    else:
        query_string = 'timestamp={}'.format(get_timestamp())

    url = BASE_URL + url_path + '?' + query_string + '&signature=' + hashing(query_string)
    print("{} {}".format(http_method, url))
    params = {'url': url, 'params': {}}
    response = dispatch_request(http_method)(**params)
    return response.json()

# get the price an calculate sl and tp

response = send_public_request('/fapi/v1/ticker/24hr?symbol=BNBUSDT')

price = float(response['weightedAvgPrice'])

TP = float("{:.2f}".format(price*1.20))
SL = float("{:.2f}".format(price*0.90))


#send sl and tp

params = {
    "batchOrders": [
        {
            "symbol":"BNBUSDT",
            "side": "SELL",
            "type": "STOP",
            "quantity": "1",
            "price": SL,
            "timeInForce": "GTC",
            "stopPrice": SL-1
        },
        {
            "symbol":"BNBUSDT",
            "side": "SELL",
            "type": "LIMIT",
            "quantity": "1",
            "price": TP,
            "timeInForce": "GTC"
        },
    ]
}

response = send_signed_request('POST', '/fapi/v1/batchOrders', params)
print(response)

我需要如何更改代码才能使用计算出的 SL 和 TP 变量?

0 个答案:

没有答案