Refactor code for improved readability and consistency
- Cleaned up whitespace and formatting in various files including http.py, language.py, logger.py, safe_exec.py, and SQL migration scripts. - Consolidated import statements and removed unnecessary blank lines. - Updated logging configuration for better clarity. - Enhanced the safe execution code with improved error handling and logging. - Removed commented-out code and unnecessary variables in backfill_zero_trades.py and other scripts. - Added a pyproject.toml for Ruff and Vulture configuration. - Introduced requirements-dev.txt for development dependencies. - Removed commented-out stock entries in init.sql for cleaner migration scripts.
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
Data source base class
|
||||
Define a unified data source interface
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, List, Any, Optional
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from app.utils.logger import get_logger
|
||||
|
||||
@@ -12,40 +13,27 @@ logger = get_logger(__name__)
|
||||
|
||||
|
||||
# K-line cycle mapping (seconds)
|
||||
TIMEFRAME_SECONDS = {
|
||||
'1m': 60,
|
||||
'5m': 300,
|
||||
'15m': 900,
|
||||
'30m': 1800,
|
||||
'1H': 3600,
|
||||
'4H': 14400,
|
||||
'1D': 86400,
|
||||
'1W': 604800
|
||||
}
|
||||
TIMEFRAME_SECONDS = {"1m": 60, "5m": 300, "15m": 900, "30m": 1800, "1H": 3600, "4H": 14400, "1D": 86400, "1W": 604800}
|
||||
|
||||
|
||||
class BaseDataSource(ABC):
|
||||
"""Data source base class."""
|
||||
|
||||
name: str = "base"
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def get_kline(
|
||||
self,
|
||||
symbol: str,
|
||||
timeframe: str,
|
||||
limit: int,
|
||||
before_time: Optional[int] = None
|
||||
self, symbol: str, timeframe: str, limit: int, before_time: Optional[int] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get K-line data
|
||||
|
||||
|
||||
Args:
|
||||
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-line data list, format:
|
||||
[{"time": int, "open": float, "high": float, "low": float, "close": float, "volume": float}, ...]
|
||||
@@ -60,82 +48,63 @@ class BaseDataSource(ABC):
|
||||
Implementations may return a dict compatible with CCXT `fetch_ticker` shape (e.g. {'last': ...}).
|
||||
"""
|
||||
raise NotImplementedError("get_ticker is not implemented for this data source")
|
||||
|
||||
|
||||
def format_kline(
|
||||
self,
|
||||
timestamp: int,
|
||||
open_price: float,
|
||||
high: float,
|
||||
low: float,
|
||||
close: float,
|
||||
volume: float
|
||||
self, timestamp: int, open_price: float, high: float, low: float, close: float, volume: float
|
||||
) -> Dict[str, Any]:
|
||||
"""Format a single K-line record."""
|
||||
return {
|
||||
'time': timestamp,
|
||||
'open': round(float(open_price), 4),
|
||||
'high': round(float(high), 4),
|
||||
'low': round(float(low), 4),
|
||||
'close': round(float(close), 4),
|
||||
'volume': round(float(volume), 2)
|
||||
"time": timestamp,
|
||||
"open": round(float(open_price), 4),
|
||||
"high": round(float(high), 4),
|
||||
"low": round(float(low), 4),
|
||||
"close": round(float(close), 4),
|
||||
"volume": round(float(volume), 2),
|
||||
}
|
||||
|
||||
def calculate_time_range(
|
||||
self,
|
||||
timeframe: str,
|
||||
limit: int,
|
||||
buffer_ratio: float = 1.2
|
||||
) -> int:
|
||||
|
||||
def calculate_time_range(self, timeframe: str, limit: int, buffer_ratio: float = 1.2) -> int:
|
||||
"""
|
||||
Calculate the time range (seconds) required to obtain the specified number of K-lines
|
||||
|
||||
|
||||
Args:
|
||||
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)
|
||||
|
||||
|
||||
def filter_and_limit(
|
||||
self,
|
||||
klines: List[Dict[str, Any]],
|
||||
limit: int,
|
||||
before_time: Optional[int] = None
|
||||
self, klines: List[Dict[str, Any]], limit: int, before_time: Optional[int] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Filter and limit K-line data
|
||||
|
||||
|
||||
Args:
|
||||
klines: K-line data list
|
||||
limit: maximum quantity
|
||||
before_time: Filter data after this time
|
||||
|
||||
|
||||
Returns:
|
||||
Processed K-line data
|
||||
"""
|
||||
# Sort by time
|
||||
klines.sort(key=lambda x: x['time'])
|
||||
|
||||
klines.sort(key=lambda x: x["time"])
|
||||
|
||||
# filter time
|
||||
if before_time:
|
||||
klines = [k for k in klines if k['time'] < 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:]
|
||||
|
||||
|
||||
return klines
|
||||
|
||||
def log_result(
|
||||
self,
|
||||
symbol: str,
|
||||
klines: List[Dict[str, Any]],
|
||||
timeframe: str
|
||||
):
|
||||
|
||||
def log_result(self, symbol: str, klines: List[Dict[str, Any]], timeframe: str):
|
||||
"""Record the result log.
|
||||
|
||||
Delayed judgment:
|
||||
|
||||
Reference in New Issue
Block a user