This commit is contained in:
KienVT9 2025-05-22 19:04:57 +07:00
parent ac32800ba7
commit 7cc42ee6a1
7 changed files with 82 additions and 82 deletions

View file

@ -45,17 +45,17 @@ def rsi(series, period=14):
@router.get("/candles", response_model=CandleResponseSchema, response_model_exclude_none=True, tags=["Candle"])
def get_candles(
userId: str = Query(..., description="ID của user để lấy API key/secret"),
symbol: str = Query(..., description="Mã giao dịch, ví dụ: BTCUSDT"),
interval: str = Query("1", description="Khung thời gian nến, ví dụ: 1, 3, 5, 15, 30, 60, 240, D, W, M"),
limit: Optional[int] = Query(200, description="Số lượng nến trả về (tối đa 1000)"),
start: Optional[int] = Query(None, description="Timestamp bắt đầu (miliseconds)"),
end: Optional[int] = Query(None, description="Timestamp kết thúc (miliseconds)")
userId: str = Query(..., description="User ID to get API key/secret"),
symbol: str = Query(..., description="Trading symbol, e.g. BTCUSDT"),
interval: str = Query("1", description="Candle interval, e.g. 1, 3, 5, 15, 30, 60, 240, D, W, M"),
limit: Optional[int] = Query(200, description="Number of candles to return (max 1000)"),
start: Optional[int] = Query(None, description="Start timestamp (milliseconds)"),
end: Optional[int] = Query(None, description="End timestamp (milliseconds)")
):
api_key, api_secret = get_user_api(userId)
client = get_bybit_client(api_key, api_secret)
params = {
"category": "linear", # hoặc "spot" nếu lấy spot
"category": "linear", # or "spot" for spot
"symbol": symbol,
"interval": interval,
"limit": limit
@ -70,7 +70,7 @@ def get_candles(
candles = res.get('result', {}).get('list', [])
if not candles:
return {"data": [], "indicators": {}}
# Chuyển đổi sang DataFrame
# Convert to DataFrame
df = pd.DataFrame(candles, columns=[
"timestamp", "open", "high", "low", "close", "volume", "turnover"
])
@ -84,7 +84,7 @@ def get_candles(
"turnover": np.float64
})
df = df.sort_values("timestamp", ascending=False)
# Tính chỉ báo
# Calculate indicators
close = df["close"]
macd_line, macd_signal, macd_hist = macd(close, fast=45, slow=90, signal=9)
df["macd"] = macd_line
@ -95,7 +95,7 @@ def get_candles(
df["ema100"] = ema(close, 100)
df["ema200"] = ema(close, 200)
df["rsi"] = rsi(close)
# Trả về kết quả
# Return result
data = df.replace({np.nan: None}).to_dict(orient="records")
return {
"data": data,