Refactor and translate comments and docstrings in utility modules to English for better clarity and maintainability. Update Gunicorn and application startup messages for consistency in language. Enhance documentation with English translations for better accessibility.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
数据源基类
|
||||
定义统一的数据源接口
|
||||
Data source base class
|
||||
Define a unified data source interface
|
||||
"""
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, List, Any, Optional
|
||||
@@ -11,7 +11,7 @@ from app.utils.logger import get_logger
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# K线周期映射(秒数)
|
||||
# K-line cycle mapping (seconds)
|
||||
TIMEFRAME_SECONDS = {
|
||||
'1m': 60,
|
||||
'5m': 300,
|
||||
@@ -25,8 +25,8 @@ TIMEFRAME_SECONDS = {
|
||||
|
||||
|
||||
class BaseDataSource(ABC):
|
||||
"""数据源基类"""
|
||||
|
||||
"""Data source base class."""
|
||||
|
||||
name: str = "base"
|
||||
|
||||
@abstractmethod
|
||||
@@ -38,16 +38,16 @@ class BaseDataSource(ABC):
|
||||
before_time: Optional[int] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取K线数据
|
||||
Get K-line data
|
||||
|
||||
Args:
|
||||
symbol: 交易对/股票代码
|
||||
timeframe: 时间周期 (1m, 5m, 15m, 30m, 1H, 4H, 1D, 1W)
|
||||
limit: 数据条数
|
||||
before_time: 获取此时间之前的数据(Unix时间戳,秒)
|
||||
symbol: trading pair/stock code
|
||||
timeframe: time period (1m, 5m, 15m, 30m, 1H, 4H, 1D, 1W)
|
||||
limit: number of data items
|
||||
before_time: Get data before this time (Unix timestamp, seconds)
|
||||
|
||||
Returns:
|
||||
K线数据列表,格式:
|
||||
K-line data list, format:
|
||||
[{"time": int, "open": float, "high": float, "low": float, "close": float, "volume": float}, ...]
|
||||
"""
|
||||
pass
|
||||
@@ -70,7 +70,7 @@ class BaseDataSource(ABC):
|
||||
close: float,
|
||||
volume: float
|
||||
) -> Dict[str, Any]:
|
||||
"""格式化单条K线数据"""
|
||||
"""Format a single K-line record."""
|
||||
return {
|
||||
'time': timestamp,
|
||||
'open': round(float(open_price), 4),
|
||||
@@ -87,15 +87,15 @@ class BaseDataSource(ABC):
|
||||
buffer_ratio: float = 1.2
|
||||
) -> int:
|
||||
"""
|
||||
计算获取指定数量K线所需的时间范围(秒)
|
||||
Calculate the time range (seconds) required to obtain the specified number of K-lines
|
||||
|
||||
Args:
|
||||
timeframe: 时间周期
|
||||
limit: K线数量
|
||||
buffer_ratio: 缓冲系数
|
||||
timeframe: time period
|
||||
limit: number of K-lines
|
||||
buffer_ratio: buffer coefficient
|
||||
|
||||
Returns:
|
||||
时间范围(秒)
|
||||
Time range (seconds)
|
||||
"""
|
||||
seconds_per_candle = TIMEFRAME_SECONDS.get(timeframe, 86400)
|
||||
return int(seconds_per_candle * limit * buffer_ratio)
|
||||
@@ -107,24 +107,24 @@ class BaseDataSource(ABC):
|
||||
before_time: Optional[int] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
过滤和限制K线数据
|
||||
Filter and limit K-line data
|
||||
|
||||
Args:
|
||||
klines: K线数据列表
|
||||
limit: 最大数量
|
||||
before_time: 过滤此时间之后的数据
|
||||
klines: K-line data list
|
||||
limit: maximum quantity
|
||||
before_time: Filter data after this time
|
||||
|
||||
Returns:
|
||||
处理后的K线数据
|
||||
Processed K-line data
|
||||
"""
|
||||
# 按时间排序
|
||||
# Sort by time
|
||||
klines.sort(key=lambda x: x['time'])
|
||||
|
||||
# 过滤时间
|
||||
# filter time
|
||||
if before_time:
|
||||
klines = [k for k in klines if k['time'] < before_time]
|
||||
|
||||
# 限制数量(取最新的)
|
||||
# Limit quantity (take the latest)
|
||||
if len(klines) > limit:
|
||||
klines = klines[-limit:]
|
||||
|
||||
@@ -136,12 +136,12 @@ class BaseDataSource(ABC):
|
||||
klines: List[Dict[str, Any]],
|
||||
timeframe: str
|
||||
):
|
||||
"""记录获取结果日志。
|
||||
"""Record the result log.
|
||||
|
||||
延迟判断:
|
||||
- K 线 time 为 Unix 秒(UTC),与 datetime.now(UTC) 比较,避免本地时区误差。
|
||||
- 日线/周线:最后一根通常是「上一交易日收盘」,周末/节假日可达 3~4 天,
|
||||
原先用 2×86400s(48h)会在周一早盘误报;改为日线最多容忍约 5 个自然日,周线更宽。
|
||||
Delayed judgment:
|
||||
- K-line time is Unix seconds (UTC), compared with datetime.now(UTC) to avoid local time zone errors.
|
||||
- Daily/weekly line: The last line is usually the "close of the previous trading day", and it can last 3 to 4 days on weekends/holidays.
|
||||
Originally, using 2×86400s (48h) would cause false alarms in Monday morning trading; instead, the daily line tolerates up to about 5 natural days, and the weekly line is wider.
|
||||
"""
|
||||
if klines:
|
||||
latest_ts = int(klines[-1]["time"])
|
||||
@@ -151,13 +151,13 @@ class BaseDataSource(ABC):
|
||||
|
||||
tf_sec = TIMEFRAME_SECONDS.get(timeframe, 3600)
|
||||
if tf_sec < 86400:
|
||||
# 分钟/小时级:超过约 2 根 K 未更新则告警
|
||||
# Minute/hour level: If it exceeds about 2 K, an alarm will be issued if it is not updated.
|
||||
max_diff = tf_sec * 2
|
||||
elif tf_sec == 86400:
|
||||
# 日线:覆盖周末 + 短假期(约 5 个自然日)
|
||||
# Daily line: covering weekends + short holidays (about 5 calendar days)
|
||||
max_diff = 5 * 86400
|
||||
else:
|
||||
# 周线:允许跨多周数据滞后
|
||||
# Weekly: Allows data lags across multiple weeks
|
||||
max_diff = max(tf_sec * 2, 21 * 86400)
|
||||
|
||||
if time_diff > max_diff:
|
||||
@@ -167,4 +167,3 @@ class BaseDataSource(ABC):
|
||||
)
|
||||
else:
|
||||
logger.warning(f"{self.name}: no data for {symbol}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user