init
This commit is contained in:
parent
be5fb603f3
commit
859fb7a19d
11 changed files with 436 additions and 0 deletions
36
app/api/positions.py
Normal file
36
app/api/positions.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from fastapi import APIRouter, Query, HTTPException
|
||||
from typing import Optional
|
||||
from app.services.bybit_service import get_bybit_client
|
||||
import json
|
||||
import os
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
ACCOUNTS_FILE = os.path.join(os.path.dirname(__file__), '../../accounts.json')
|
||||
|
||||
def get_user_api(user_id: str):
|
||||
try:
|
||||
with open(ACCOUNTS_FILE, 'r', encoding='utf-8') as f:
|
||||
accounts = json.load(f)
|
||||
for acc in accounts:
|
||||
if acc['id'] == user_id:
|
||||
return acc['bybit_api_key'], acc['bybit_api_secret']
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Lỗi đọc file accounts: {e}")
|
||||
raise HTTPException(status_code=404, detail="Không tìm thấy userId")
|
||||
|
||||
@router.get("/positions")
|
||||
def get_positions(
|
||||
userId: str = Query(..., description="ID của user để lấy API key/secret"),
|
||||
symbol: Optional[str] = Query(None, description="Mã giao dịch, ví dụ: BTCUSDT"),
|
||||
category: str = Query("linear", description="Loại lệnh: linear (future) hoặc spot")
|
||||
):
|
||||
api_key, api_secret = get_user_api(userId)
|
||||
client = get_bybit_client(api_key, api_secret)
|
||||
params = {
|
||||
"category": category
|
||||
}
|
||||
if symbol:
|
||||
params["symbol"] = symbol
|
||||
res = client.get_positions(**params)
|
||||
return res
|
||||
Loading…
Add table
Add a link
Reference in a new issue