a
This commit is contained in:
parent
f99f508b09
commit
09373b16ac
|
|
@ -68,6 +68,10 @@ class Database:
|
||||||
cursorclass=pymysql.cursors.DictCursor,
|
cursorclass=pymysql.cursors.DictCursor,
|
||||||
autocommit=False
|
autocommit=False
|
||||||
)
|
)
|
||||||
|
# 设置时区为北京时间(UTC+8)
|
||||||
|
with conn.cursor() as cursor:
|
||||||
|
cursor.execute("SET time_zone = '+08:00'")
|
||||||
|
conn.commit()
|
||||||
yield conn
|
yield conn
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if conn:
|
if conn:
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,17 @@
|
||||||
数据库模型定义
|
数据库模型定义
|
||||||
"""
|
"""
|
||||||
from database.connection import db
|
from database.connection import db
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone, timedelta
|
||||||
import json
|
import json
|
||||||
import logging
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -82,24 +89,26 @@ class Trade:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(symbol, side, quantity, entry_price, leverage=10, entry_reason=None):
|
def create(symbol, side, quantity, entry_price, leverage=10, entry_reason=None):
|
||||||
"""创建交易记录"""
|
"""创建交易记录(使用北京时间)"""
|
||||||
|
entry_time = get_beijing_time()
|
||||||
db.execute_update(
|
db.execute_update(
|
||||||
"""INSERT INTO trades
|
"""INSERT INTO trades
|
||||||
(symbol, side, quantity, entry_price, leverage, entry_reason, status)
|
(symbol, side, quantity, entry_price, leverage, entry_reason, status, entry_time)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, 'open')""",
|
VALUES (%s, %s, %s, %s, %s, %s, 'open', %s)""",
|
||||||
(symbol, side, quantity, entry_price, leverage, entry_reason)
|
(symbol, side, quantity, entry_price, leverage, entry_reason, entry_time)
|
||||||
)
|
)
|
||||||
return db.execute_one("SELECT LAST_INSERT_ID() as id")['id']
|
return db.execute_one("SELECT LAST_INSERT_ID() as id")['id']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_exit(trade_id, exit_price, exit_reason, pnl, pnl_percent):
|
def update_exit(trade_id, exit_price, exit_reason, pnl, pnl_percent):
|
||||||
"""更新平仓信息"""
|
"""更新平仓信息(使用北京时间)"""
|
||||||
|
exit_time = get_beijing_time()
|
||||||
db.execute_update(
|
db.execute_update(
|
||||||
"""UPDATE trades
|
"""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'
|
exit_reason = %s, pnl = %s, pnl_percent = %s, status = 'closed'
|
||||||
WHERE id = %s""",
|
WHERE id = %s""",
|
||||||
(exit_price, exit_reason, pnl, pnl_percent, trade_id)
|
(exit_price, exit_time, exit_reason, pnl, pnl_percent, trade_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -138,12 +147,13 @@ class AccountSnapshot:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(total_balance, available_balance, total_position_value, total_pnl, open_positions):
|
def create(total_balance, available_balance, total_position_value, total_pnl, open_positions):
|
||||||
"""创建账户快照"""
|
"""创建账户快照(使用北京时间)"""
|
||||||
|
snapshot_time = get_beijing_time()
|
||||||
db.execute_update(
|
db.execute_update(
|
||||||
"""INSERT INTO account_snapshots
|
"""INSERT INTO account_snapshots
|
||||||
(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)""",
|
VALUES (%s, %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)
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -162,12 +172,13 @@ class MarketScan:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(symbols_scanned, symbols_found, top_symbols, scan_duration):
|
def create(symbols_scanned, symbols_found, top_symbols, scan_duration):
|
||||||
"""创建扫描记录"""
|
"""创建扫描记录(使用北京时间)"""
|
||||||
|
scan_time = get_beijing_time()
|
||||||
db.execute_update(
|
db.execute_update(
|
||||||
"""INSERT INTO market_scans
|
"""INSERT INTO market_scans
|
||||||
(symbols_scanned, symbols_found, top_symbols, scan_duration)
|
(symbols_scanned, symbols_found, top_symbols, scan_duration, scan_time)
|
||||||
VALUES (%s, %s, %s, %s)""",
|
VALUES (%s, %s, %s, %s, %s)""",
|
||||||
(symbols_scanned, symbols_found, json.dumps(top_symbols), scan_duration)
|
(symbols_scanned, symbols_found, json.dumps(top_symbols), scan_duration, scan_time)
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -185,14 +196,15 @@ class TradingSignal:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(symbol, signal_direction, signal_strength, signal_reason,
|
def create(symbol, signal_direction, signal_strength, signal_reason,
|
||||||
rsi=None, macd_histogram=None, market_regime=None):
|
rsi=None, macd_histogram=None, market_regime=None):
|
||||||
"""创建交易信号"""
|
"""创建交易信号(使用北京时间)"""
|
||||||
|
signal_time = get_beijing_time()
|
||||||
db.execute_update(
|
db.execute_update(
|
||||||
"""INSERT INTO trading_signals
|
"""INSERT INTO trading_signals
|
||||||
(symbol, signal_direction, signal_strength, signal_reason,
|
(symbol, signal_direction, signal_strength, signal_reason,
|
||||||
rsi, macd_histogram, market_regime)
|
rsi, macd_histogram, market_regime, signal_time)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s)""",
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""",
|
||||||
(symbol, signal_direction, signal_strength, signal_reason,
|
(symbol, signal_direction, signal_strength, signal_reason,
|
||||||
rsi, macd_histogram, market_regime)
|
rsi, macd_histogram, market_regime, signal_time)
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user