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:
dienakdz
2026-04-06 16:47:36 +07:00
parent 3ca291a346
commit 11e2e5aaa6
64 changed files with 2323 additions and 2336 deletions
+37 -37
View File
@@ -1,8 +1,8 @@
"""
期货数据源
支持:
1. 加密货币期货(Binance Futures via CCXT
2. 传统期货(Yahoo Finance
Futures data source
support:
1. Cryptocurrency Futures (Binance Futures via CCXT)
2. Traditional futures (Yahoo Finance)
"""
from typing import Dict, List, Any, Optional
from datetime import datetime, timedelta
@@ -17,11 +17,11 @@ logger = get_logger(__name__)
class FuturesDataSource(BaseDataSource):
"""期货数据源"""
"""Futures data source"""
name = "Futures"
# Yahoo Finance时间周期映射
# Yahoo Finance time period mapping
YF_TIMEFRAME_MAP = {
'1m': '1m',
'5m': '5m',
@@ -33,21 +33,21 @@ class FuturesDataSource(BaseDataSource):
'1W': '1wk'
}
# CCXT时间周期映射
# CCXT time period mapping
CCXT_TIMEFRAME_MAP = CCXTConfig.TIMEFRAME_MAP
# 传统期货合约代码(Yahoo Finance
# Traditional futures contract code (Yahoo Finance)
YF_SYMBOLS = {
'GC': 'GC=F', # 黄金期货
'SI': 'SI=F', # 白银期货
'CL': 'CL=F', # 原油期货
'NG': 'NG=F', # 天然气期货
'ZC': 'ZC=F', # 玉米期货
'ZW': 'ZW=F', # 小麦期货
'GC': 'GC=F', # gold futures
'SI': 'SI=F', # Silver futures
'CL': 'CL=F', # Crude oil futures
'NG': 'NG=F', # Natural gas futures
'ZC': 'ZC=F', # Corn futures
'ZW': 'ZW=F', # Wheat futures
}
def __init__(self):
# 初始化CCXT(用于加密货币期货)
# Initialize CCXT (for cryptocurrency futures)
config = {
'timeout': CCXTConfig.TIMEOUT,
'enableRateLimit': CCXTConfig.ENABLE_RATE_LIMIT,
@@ -103,7 +103,7 @@ class FuturesDataSource(BaseDataSource):
return self.exchange.fetch_ticker(sym)
def _get_timeframe_seconds(self, timeframe: str) -> int:
"""获取时间周期对应的秒数"""
"""Get the number of seconds corresponding to the time period"""
return TIMEFRAME_SECONDS.get(timeframe, 86400)
def get_kline(
@@ -114,15 +114,15 @@ class FuturesDataSource(BaseDataSource):
before_time: Optional[int] = None
) -> List[Dict[str, Any]]:
"""
获取期货K线数据
Get futures K-line data
Args:
symbol: 期货合约代码
timeframe: 时间周期
limit: 数据条数
before_time: 结束时间戳
symbol: futures contract code
timeframe: time period
limit: number of data items
before_time: end timestamp
"""
# 判断是传统期货还是加密货币期货
# Determine whether it is traditional futures or cryptocurrency futures
if symbol in self.YF_SYMBOLS or symbol.endswith('=F'):
return self._get_traditional_futures(symbol, timeframe, limit, before_time)
else:
@@ -135,19 +135,19 @@ class FuturesDataSource(BaseDataSource):
limit: int,
before_time: Optional[int] = None
) -> List[Dict[str, Any]]:
"""使用yfinance获取传统期货数据"""
"""Use yfinance to obtain traditional futures data"""
try:
# 转换symbol格式
# Convert symbol format
yf_symbol = self.YF_SYMBOLS.get(symbol, symbol)
if not yf_symbol.endswith('=F'):
yf_symbol = symbol + '=F'
# 转换时间周期
# conversion time period
yf_interval = self.YF_TIMEFRAME_MAP.get(timeframe, '1d')
# logger.info(f"获取传统期货K线: {yf_symbol}, 周期: {yf_interval}, 条数: {limit}")
# logger.info(f"Get traditional futures K-line: {yf_symbol}, period: {yf_interval}, number of bars: {limit}")
# 计算时间范围
# Calculation time range
if before_time:
end_time = datetime.fromtimestamp(before_time)
else:
@@ -156,10 +156,10 @@ class FuturesDataSource(BaseDataSource):
tf_seconds = self._get_timeframe_seconds(timeframe)
start_time = end_time - timedelta(seconds=tf_seconds * limit * 1.5)
# yfinance 的 end 参数是不包含的(exclusive),需要加一天
# The end parameter of yfinance is not included (exclusive), and one day needs to be added.
end_time_inclusive = end_time + timedelta(days=1)
# 获取数据
# Get data
ticker = yf.Ticker(yf_symbol)
df = ticker.history(
start=start_time,
@@ -171,7 +171,7 @@ class FuturesDataSource(BaseDataSource):
logger.warning(f"No data: {yf_symbol}")
return []
# 转换格式
# Convert format
klines = []
for index, row in df.iterrows():
klines.append({
@@ -187,7 +187,7 @@ class FuturesDataSource(BaseDataSource):
if len(klines) > limit:
klines = klines[-limit:]
# logger.info(f"获取到 {len(klines)} 条传统期货数据")
# logger.info(f"obtained {len(klines)} pieces of traditional futures data")
return klines
except Exception as e:
@@ -201,15 +201,15 @@ class FuturesDataSource(BaseDataSource):
limit: int,
before_time: Optional[int] = None
) -> List[Dict[str, Any]]:
"""使用CCXT获取加密货币期货数据"""
"""Obtain cryptocurrency futures data using CCXT"""
try:
# 确保symbol格式正确
# Make sure the symbol format is correct
ccxt_symbol = symbol if '/' in symbol else f"{symbol}/USDT"
ccxt_timeframe = self.CCXT_TIMEFRAME_MAP.get(timeframe, '1d')
# logger.info(f"获取加密货币期货K线: {ccxt_symbol}, 周期: {ccxt_timeframe}, 条数: {limit}")
# logger.info(f"Get cryptocurrency futures K-line: {ccxt_symbol}, period: {ccxt_timeframe}, number of bars: {limit}")
# 获取数据
# Get data
if before_time:
since_time = before_time - limit * self._get_timeframe_seconds(timeframe)
ohlcv = self.exchange.fetch_ohlcv(
@@ -225,7 +225,7 @@ class FuturesDataSource(BaseDataSource):
limit=limit
)
# 转换格式
# Convert format
klines = []
for candle in ohlcv:
klines.append({
@@ -237,7 +237,7 @@ class FuturesDataSource(BaseDataSource):
'volume': float(candle[5])
})
# logger.info(f"获取到 {len(klines)} 条加密货币期货数据")
# logger.info(f"obtained {len(klines)} pieces of cryptocurrency futures data")
return klines
except Exception as e: