auto_trade_sys/update_risk_config_db.py
薇薇安 46d31fde59 1
2026-02-13 17:56:27 +08:00

78 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
import logging
from sqlalchemy import create_engine, text
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Database connection
DB_USER = os.getenv('DB_USER', 'root')
DB_PASSWORD = os.getenv('DB_PASSWORD', '12345678')
DB_HOST = os.getenv('DB_HOST', 'localhost')
DB_PORT = os.getenv('DB_PORT', '3306')
DB_NAME = 'auto_trade_sys_new'
DATABASE_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
def update_config():
try:
engine = create_engine(DATABASE_URL)
with engine.connect() as conn:
# 1. Update MIN_STOP_LOSS_PRICE_PCT (Crucial Fix)
# Default was 0.025 (2.5%), which forces 25% margin loss @ 10x.
# Changing to 0.005 (0.5%), which allows 5% margin loss @ 10x.
conn.execute(text("""
INSERT INTO trading_config (account_id, config_key, config_value, config_type, category, description, updated_at)
VALUES (1, 'MIN_STOP_LOSS_PRICE_PCT', '0.005', 'number', 'risk', '最小止损价格变动0.5%(允许更紧的止损)', NOW())
ON DUPLICATE KEY UPDATE config_value='0.005', updated_at=NOW();
"""))
logger.info("Updated MIN_STOP_LOSS_PRICE_PCT to 0.005")
# 2. Update MIN_TAKE_PROFIT_PRICE_PCT
# Default was 0.02 (2%), forcing 20% margin gain @ 10x.
# Changing to 0.006 (0.6%), allowing smaller TPs if needed.
conn.execute(text("""
INSERT INTO trading_config (account_id, config_key, config_value, config_type, category, description, updated_at)
VALUES (1, 'MIN_TAKE_PROFIT_PRICE_PCT', '0.006', 'number', 'risk', '最小止盈价格变动0.6%(允许更紧的止盈)', NOW())
ON DUPLICATE KEY UPDATE config_value='0.006', updated_at=NOW();
"""))
logger.info("Updated MIN_TAKE_PROFIT_PRICE_PCT to 0.006")
# 3. Ensure STOP_LOSS_PERCENT is 0.1 (10% Margin)
conn.execute(text("""
INSERT INTO trading_config (account_id, config_key, config_value, config_type, category, description, updated_at)
VALUES (1, 'STOP_LOSS_PERCENT', '0.1', 'number', 'risk', '止损10%(相对于保证金)', NOW())
ON DUPLICATE KEY UPDATE config_value='0.1', updated_at=NOW();
"""))
logger.info("Ensured STOP_LOSS_PERCENT is 0.1")
# 4. Ensure TAKE_PROFIT_PERCENT is 0.2 (20% Margin)
conn.execute(text("""
INSERT INTO trading_config (account_id, config_key, config_value, config_type, category, description, updated_at)
VALUES (1, 'TAKE_PROFIT_PERCENT', '0.2', 'number', 'risk', '止盈20%(相对于保证金)', NOW())
ON DUPLICATE KEY UPDATE config_value='0.2', updated_at=NOW();
"""))
logger.info("Ensured TAKE_PROFIT_PERCENT is 0.2")
# 5. Ensure ATR_STOP_LOSS_MULTIPLIER is reasonable (2.0)
conn.execute(text("""
INSERT INTO trading_config (account_id, config_key, config_value, config_type, category, description, updated_at)
VALUES (1, 'ATR_STOP_LOSS_MULTIPLIER', '2.0', 'number', 'risk', 'ATR止损倍数2.0', NOW())
ON DUPLICATE KEY UPDATE config_value='2.0', updated_at=NOW();
"""))
logger.info("Ensured ATR_STOP_LOSS_MULTIPLIER is 2.0")
conn.commit()
logger.info("Configuration update complete.")
except Exception as e:
logger.error(f"Error updating config: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
update_config()