|
|
@@ -123,13 +123,15 @@ class TradingDashboard:
|
|
|
|
|
|
@self.app.route('/api/trading_events')
|
|
|
def get_trading_events():
|
|
|
- """获取交易事件API"""
|
|
|
+ """获取交易事件API - 支持时间戳范围"""
|
|
|
hours = request.args.get('hours', 24, type=float)
|
|
|
symbol = request.args.get('symbol', '')
|
|
|
db_path = request.args.get('db_path', self.db_path)
|
|
|
-
|
|
|
+ start_time = request.args.get('start_time', type=float) # 开始时间戳
|
|
|
+ end_time = request.args.get('end_time', type=float) # 结束时间戳
|
|
|
+
|
|
|
try:
|
|
|
- events = self.get_trading_events(hours, symbol, db_path)
|
|
|
+ events = self.get_trading_events(hours, symbol, db_path, start_time, end_time)
|
|
|
return jsonify({
|
|
|
'success': True,
|
|
|
'data': events
|
|
|
@@ -308,8 +310,18 @@ class TradingDashboard:
|
|
|
|
|
|
return data
|
|
|
|
|
|
- def get_trading_events(self, hours: float = 24, symbol: str = '', db_path: str = None) -> List[Dict[str, Any]]:
|
|
|
- """获取交易事件"""
|
|
|
+ def get_trading_events(self, hours: float = 24, symbol: str = '', db_path: str = None,
|
|
|
+ start_time: float = None, end_time: float = None) -> List[Dict[str, Any]]:
|
|
|
+ """
|
|
|
+ 获取交易事件
|
|
|
+
|
|
|
+ Args:
|
|
|
+ hours: 时间范围(小时),当start_time和end_time都未指定时使用
|
|
|
+ symbol: 交易对符号
|
|
|
+ db_path: 数据库路径
|
|
|
+ start_time: 开始时间戳(秒)
|
|
|
+ end_time: 结束时间戳(秒)
|
|
|
+ """
|
|
|
if db_path is None:
|
|
|
# 使用最新的可用数据库
|
|
|
available_dbs = self.get_available_databases()
|
|
|
@@ -320,30 +332,42 @@ class TradingDashboard:
|
|
|
|
|
|
if not os.path.exists(db_path):
|
|
|
return []
|
|
|
-
|
|
|
+
|
|
|
conn = sqlite3.connect(db_path)
|
|
|
cursor = conn.cursor()
|
|
|
-
|
|
|
+
|
|
|
# 计算时间范围(转换为Unix时间戳)
|
|
|
- end_time = datetime.now()
|
|
|
- start_time = end_time - timedelta(hours=hours)
|
|
|
-
|
|
|
- # 构建查询 - 更新字段名
|
|
|
+ if start_time is not None and end_time is not None:
|
|
|
+ # 使用指定的时间戳范围
|
|
|
+ start_ts = start_time
|
|
|
+ end_ts = end_time
|
|
|
+ elif start_time is not None:
|
|
|
+ # 只指定了开始时间,使用hours计算结束时间
|
|
|
+ start_ts = start_time
|
|
|
+ end_ts = start_time + (hours * 3600)
|
|
|
+ else:
|
|
|
+ # 使用hours从当前时间往前推
|
|
|
+ end_dt = datetime.now()
|
|
|
+ start_dt = end_dt - timedelta(hours=hours)
|
|
|
+ start_ts = start_dt.timestamp()
|
|
|
+ end_ts = end_dt.timestamp()
|
|
|
+
|
|
|
+ # 构建查询
|
|
|
query = """
|
|
|
- SELECT timestamp, symbol, event_type, price, quantity, side,
|
|
|
- strategy_state, ask_bps, bid_bps, binance_price,
|
|
|
+ SELECT timestamp, symbol, event_type, price, quantity, side,
|
|
|
+ strategy_state, ask_bps, bid_bps, binance_price,
|
|
|
tx_hash, error_message, metadata
|
|
|
- FROM trading_events
|
|
|
+ FROM trading_events
|
|
|
WHERE timestamp >= ? AND timestamp <= ?
|
|
|
"""
|
|
|
- params = [start_time.timestamp(), end_time.timestamp()]
|
|
|
-
|
|
|
+ params = [start_ts, end_ts]
|
|
|
+
|
|
|
if symbol:
|
|
|
query += " AND symbol = ?"
|
|
|
params.append(symbol)
|
|
|
-
|
|
|
+
|
|
|
query += " ORDER BY timestamp DESC"
|
|
|
-
|
|
|
+
|
|
|
cursor.execute(query, params)
|
|
|
rows = cursor.fetchall()
|
|
|
conn.close()
|