This commit is contained in:
薇薇安 2026-01-14 13:50:00 +08:00
parent f99f508b09
commit 09373b16ac
2 changed files with 36 additions and 20 deletions

View File

@ -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:

View File

@ -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