Browse Source

时间戳与价格缩略图

skyfffire 3 ngày trước cách đây
mục cha
commit
3c43c60c2f
2 tập tin đã thay đổi với 89 bổ sung13 xóa
  1. 74 2
      src/leadlag/templates/dashboard.html
  2. 15 11
      src/leadlag/web_dashboard.py

+ 74 - 2
src/leadlag/templates/dashboard.html

@@ -530,6 +530,32 @@
                     trigger: 'axis',
                     axisPointer: {
                         type: 'cross'
+                    },
+                    formatter: function(params) {
+                        if (!params || params.length === 0) return '';
+
+                        // 获取第一个数据点的时间戳(毫秒)
+                        const timestamp = params[0].value[0];
+                        const date = new Date(timestamp);
+
+                        // 格式化时间为 YYYY-MM-DD HH:mm:ss.SSS
+                        const year = date.getFullYear();
+                        const month = String(date.getMonth() + 1).padStart(2, '0');
+                        const day = String(date.getDate()).padStart(2, '0');
+                        const hours = String(date.getHours()).padStart(2, '0');
+                        const minutes = String(date.getMinutes()).padStart(2, '0');
+                        const seconds = String(date.getSeconds()).padStart(2, '0');
+                        const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
+
+                        let result = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}<br/>`;
+
+                        // 添加各个系列的数据
+                        params.forEach(param => {
+                            const value = param.value[1];
+                            result += `${param.marker} ${param.name}: ${value.toFixed(6)}<br/>`;
+                        });
+
+                        return result;
                     }
                 },
                 legend: {
@@ -641,6 +667,32 @@
                     trigger: 'axis',
                     axisPointer: {
                         type: 'cross'
+                    },
+                    formatter: function(params) {
+                        if (!params || params.length === 0) return '';
+
+                        // 获取第一个数据点的时间戳(毫秒)
+                        const timestamp = params[0].value[0];
+                        const date = new Date(timestamp);
+
+                        // 格式化时间为 YYYY-MM-DD HH:mm:ss.SSS
+                        const year = date.getFullYear();
+                        const month = String(date.getMonth() + 1).padStart(2, '0');
+                        const day = String(date.getDate()).padStart(2, '0');
+                        const hours = String(date.getHours()).padStart(2, '0');
+                        const minutes = String(date.getMinutes()).padStart(2, '0');
+                        const seconds = String(date.getSeconds()).padStart(2, '0');
+                        const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
+
+                        let result = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}<br/>`;
+
+                        // 添加各个系列的数据
+                        params.forEach(param => {
+                            const value = param.value[1];
+                            result += `${param.marker} ${param.name}: ${value.toFixed(2)}<br/>`;
+                        });
+
+                        return result;
                     }
                 },
                 legend: {
@@ -750,7 +802,9 @@
 
             // 准备数据
             // 对于时间类型的 X 轴,数据格式应该是 [[timestamp, value], ...]
-            const seriesData = data.map(d => [d.timestamp * 1000, d.binance_price]);
+            const binancePriceData = data.map(d => [d.timestamp * 1000, d.binance_price]);
+            const lighterAskData = data.map(d => [d.timestamp * 1000, d.lighter_ask]);
+            const lighterBidData = data.map(d => [d.timestamp * 1000, d.lighter_bid]);
 
             const option = {
                 tooltip: {
@@ -782,12 +836,30 @@
                 series: [
                     {
                         name: 'Binance价格',
-                        data: seriesData,
+                        data: binancePriceData,
                         type: 'line',
                         smooth: true,
                         itemStyle: { color: '#ff6b6b' },
                         lineStyle: { width: 1.5 },
                         showSymbol: false
+                    },
+                    {
+                        name: 'Lighter卖价',
+                        data: lighterAskData,
+                        type: 'line',
+                        smooth: true,
+                        itemStyle: { color: '#f9ca24' },
+                        lineStyle: { width: 1, type: 'dashed' },
+                        showSymbol: false
+                    },
+                    {
+                        name: 'Lighter买价',
+                        data: lighterBidData,
+                        type: 'line',
+                        smooth: true,
+                        itemStyle: { color: '#45b7d1' },
+                        lineStyle: { width: 1, type: 'dashed' },
+                        showSymbol: false
                     }
                 ]
             };

+ 15 - 11
src/leadlag/web_dashboard.py

@@ -247,7 +247,7 @@ class TradingDashboard:
         return data
     
     def get_thumbnail_data(self, hours: float = 24, symbol: str = '', db_path: str = None) -> List[Dict[str, Any]]:
-        """获取缩略图数据 - 返回币安价格,每秒最多一条数据"""
+        """获取缩略图数据 - 返回币安价格、Lighter买价、Lighter卖价,每秒最多一条数据"""
         if db_path is None:
             # 使用最新的可用数据库
             available_dbs = self.get_available_databases()
@@ -258,32 +258,34 @@ 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)
-        
+
         # 构建查询 - 使用GROUP BY按秒分组,只取每秒的第一条记录
         query = """
-        SELECT 
+        SELECT
             MIN(timestamp) as timestamp,
             symbol,
-            binance_price
-        FROM price_data 
+            binance_price,
+            lighter_ask,
+            lighter_bid
+        FROM price_data
         WHERE timestamp >= ? AND timestamp <= ? AND binance_price IS NOT NULL
         """
         params = [start_time.timestamp(), end_time.timestamp()]
-        
+
         if symbol:
             query += " AND symbol = ?"
             params.append(symbol)
-            
+
         # 按秒分组,每秒只取一条数据
         query += " GROUP BY CAST(timestamp AS INTEGER), symbol ORDER BY timestamp ASC"
-        
+
         cursor.execute(query, params)
         rows = cursor.fetchall()
         conn.close()
@@ -299,7 +301,9 @@ class TradingDashboard:
             data.append({
                 'timestamp': row[0],
                 'symbol': row[1],
-                'binance_price': float(row[2]) if row[2] else None
+                'binance_price': float(row[2]) if row[2] else None,
+                'lighter_ask': float(row[3]) if row[3] else None,
+                'lighter_bid': float(row[4]) if row[4] else None
             })
 
         return data