Can not set stop loss / take profit, error ‘Order does not exist

未關閉
#18 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
18/100
Issue 類型
缺陷
描述清晰度
需要釐清
活躍度
停滯
技術堆疊
python
領域
api

研究方向

從 create_cross_order 函式及其向 /fapi/v1/order 發出的 POST 請求開始,然後將提供的參數與 Binance Futures API 文件進行比較。Issue 未指出任何 repository 檔案或測試,因此要視為完成,就需要重現回報的錯誤,並確認該訂單及其 TP/SL 行為如預期般被接受。

由索引模型根據 Issue 內容生成。

描述

I have an issue is the TP/SL function not reading the Binance side
could you please help me to resolve this issues

import requests
import hashlib
import hmac
import time



def get_current_price(symbol):
    base_url = 'https://fapi.binance.com'
    endpoint = '/fapi/v1/ticker/price'
    
    params = {
        'symbol': symbol
    }
    
    response = requests.get(f'{base_url}{endpoint}', params=params)
    data = response.json()
    
    if 'price' in data:
        return float(data['price'])
    else:
        return None

def calculate_required_margin(quantity, price, leverage):
    return (quantity * price) / leverage

def get_account_balance():
    base_url = 'https://fapi.binance.com'
    endpoint = '/fapi/v2/account'
    
    params = {
        'timestamp': int(time.time() * 1000)
    }
    
    signature = hmac.new(API_SECRET.encode('utf-8'), '&'.join([f'{k}={v}' for k, v in params.items()]).encode('utf-8'), hashlib.sha256).hexdigest()
    
    headers = {
        'X-MBX-APIKEY': API_KEY
    }
    
    url = f'{base_url}{endpoint}?timestamp={params["timestamp"]}&signature={signature}'
    
    response = requests.get(url, headers=headers)
    return response.json()

def create_cross_order(symbol, side, quantity, leverage, position_side, take_profit_percent, stop_loss_percent):
    base_url = 'https://fapi.binance.com'
    endpoint = '/fapi/v1/order'
    
    current_price = get_current_price(symbol)
    if current_price is None:
        print('Failed to fetch current price.')
        return
    
    required_margin = calculate_required_margin(quantity, current_price, leverage)
    
    # Fetch account information to check available balance
    account_info = get_account_balance()
    if 'totalWalletBalance' in account_info:
        total_wallet_balance = float(account_info['totalWalletBalance'])
        if total_wallet_balance < required_margin:
            print("Insufficient balance for required margin.")
            return
    else:
        print("Failed to fetch account balance.")
        return
    
    # Calculate take profit and stop loss prices
    take_profit_price = current_price * (1 + take_profit_percent / 100)
    stop_loss_price = current_price * (1 - stop_loss_percent / 100)
    
    params = {
        'symbol': symbol,
        'side': side,
        'positionSide': position_side,
        'quantity': quantity,
        'price': current_price,
        'type': 'LIMIT',
        'timeInForce': 'GTC',
        'leverage': leverage,
        'timestamp': int(time.time() * 1000),
        'takeProfit': take_profit_price,
        'stopLoss': stop_loss_price
    }
    
    query_string = '&'.join([f'{k}={v}' for k, v in params.items()])
    signature = hmac.new(API_SECRET.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
    
    headers = {
        'X-MBX-APIKEY': API_KEY
    }
    
    url = f'{base_url}{endpoint}?{query_string}&signature={signature}'
    
    response = requests.post(url, headers=headers)
    return response.json()

# Show account balance
balance_response = get_account_balance()
if 'totalWalletBalance' in balance_response:
    print(f"Total Wallet Balance: {balance_response['totalWalletBalance']} USDT")
else:
    print("Failed to fetch account balance.")

# Create order with take profit of 1.5% and stop loss of 2%
symbol = 'BTCUSDT'
side = 'BUY'  # Change this to 'SELL' if you're opening a short position
quantity = 0.01
leverage = 10  # Leverage x10
position_side = 'LONG'  # Change this to 'SHORT' if opening a short position
take_profit_percent = 1.5
stop_loss_percent = 2.0
response = create_cross_order(symbol, side, quantity, leverage, position_side, take_profit_percent, stop_loss_percent)
if response is not None:
    print(response)

image
image
image

@2pd @aisling11 @chairz @bnbot

主要語言
Python
星號
110
分支
21
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

binance/binance-toolbox-python 的其他 Issue

查看 binance/binance-toolbox-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。