From 09373b16ac4f83770ad60f3efde83146e76f5da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Wed, 14 Jan 2026 13:50:00 +0800 Subject: [PATCH] a --- backend/database/connection.py | 4 +++ backend/database/models.py | 52 +++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/backend/database/connection.py b/backend/database/connection.py index 2e7abbe..f575b3b 100644 --- a/backend/database/connection.py +++ b/backend/database/connection.py @@ -68,6 +68,10 @@ class Database: cursorclass=pymysql.cursors.DictCursor, autocommit=False ) + # 设置时区为北京时间(UTC+8) + with conn.cursor() as cursor: + cursor.execute("SET time_zone = '+08:00'") + conn.commit() yield conn except Exception as e: if conn: diff --git a/backend/database/models.py b/backend/database/models.py index 8227e05..c042f57 100644 --- a/backend/database/models.py +++ b/backend/database/models.py @@ -2,10 +2,17 @@ 数据库模型定义 """ from database.connection import db -from datetime import datetime +from datetime import datetime, timezone, timedelta import json import logging +# 北京时间时区(UTC+8) +BEIJING_TZ = timezone(timedelta(hours=8)) + +def get_beijing_time(): + """获取当前北京时间(UTC+8)""" + return datetime.now(BEIJING_TZ).replace(tzinfo=None) + logger = logging.getLogger(__name__) @@ -82,24 +89,26 @@ class Trade: @staticmethod def create(symbol, side, quantity, entry_price, leverage=10, entry_reason=None): - """创建交易记录""" + """创建交易记录(使用北京时间)""" + entry_time = get_beijing_time() db.execute_update( """INSERT INTO trades - (symbol, side, quantity, entry_price, leverage, entry_reason, status) - VALUES (%s, %s, %s, %s, %s, %s, 'open')""", - (symbol, side, quantity, entry_price, leverage, entry_reason) + (symbol, side, quantity, entry_price, leverage, entry_reason, status, entry_time) + VALUES (%s, %s, %s, %s, %s, %s, 'open', %s)""", + (symbol, side, quantity, entry_price, leverage, entry_reason, entry_time) ) return db.execute_one("SELECT LAST_INSERT_ID() as id")['id'] @staticmethod def update_exit(trade_id, exit_price, exit_reason, pnl, pnl_percent): - """更新平仓信息""" + """更新平仓信息(使用北京时间)""" + exit_time = get_beijing_time() db.execute_update( """UPDATE trades - SET exit_price = %s, exit_time = CURRENT_TIMESTAMP, + SET exit_price = %s, exit_time = %s, exit_reason = %s, pnl = %s, pnl_percent = %s, status = 'closed' WHERE id = %s""", - (exit_price, exit_reason, pnl, pnl_percent, trade_id) + (exit_price, exit_time, exit_reason, pnl, pnl_percent, trade_id) ) @staticmethod @@ -138,12 +147,13 @@ class AccountSnapshot: @staticmethod def create(total_balance, available_balance, total_position_value, total_pnl, open_positions): - """创建账户快照""" + """创建账户快照(使用北京时间)""" + snapshot_time = get_beijing_time() db.execute_update( """INSERT INTO account_snapshots - (total_balance, available_balance, total_position_value, total_pnl, open_positions) - VALUES (%s, %s, %s, %s, %s)""", - (total_balance, available_balance, total_position_value, total_pnl, open_positions) + (total_balance, available_balance, total_position_value, total_pnl, open_positions, snapshot_time) + VALUES (%s, %s, %s, %s, %s, %s)""", + (total_balance, available_balance, total_position_value, total_pnl, open_positions, snapshot_time) ) @staticmethod @@ -162,12 +172,13 @@ class MarketScan: @staticmethod def create(symbols_scanned, symbols_found, top_symbols, scan_duration): - """创建扫描记录""" + """创建扫描记录(使用北京时间)""" + scan_time = get_beijing_time() db.execute_update( """INSERT INTO market_scans - (symbols_scanned, symbols_found, top_symbols, scan_duration) - VALUES (%s, %s, %s, %s)""", - (symbols_scanned, symbols_found, json.dumps(top_symbols), scan_duration) + (symbols_scanned, symbols_found, top_symbols, scan_duration, scan_time) + VALUES (%s, %s, %s, %s, %s)""", + (symbols_scanned, symbols_found, json.dumps(top_symbols), scan_duration, scan_time) ) @staticmethod @@ -185,14 +196,15 @@ class TradingSignal: @staticmethod def create(symbol, signal_direction, signal_strength, signal_reason, rsi=None, macd_histogram=None, market_regime=None): - """创建交易信号""" + """创建交易信号(使用北京时间)""" + signal_time = get_beijing_time() db.execute_update( """INSERT INTO trading_signals (symbol, signal_direction, signal_strength, signal_reason, - rsi, macd_histogram, market_regime) - VALUES (%s, %s, %s, %s, %s, %s, %s)""", + rsi, macd_histogram, market_regime, signal_time) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""", (symbol, signal_direction, signal_strength, signal_reason, - rsi, macd_histogram, market_regime) + rsi, macd_histogram, market_regime, signal_time) ) @staticmethod