fix: stage missing code review fixes (7 files)
Files were edited but not staged in earlier commits: - adaptive_rhythm_manager.py: mt→mt5, pytz→timezone, get_volatility_level, shutdown() - check_market_regime.py: ADX_THRESHOLD, Wilder EWM, try/finally, UTC timestamp, sys import - check_system_status.py: remove duplicate cursor.execute - drawdown_protection.py: float(inf), persist pause state, DB save_setting, Markdown fix - performance_analysis.py: KeyError export fix, profit factor, drawdown positive, SQL filter - performance_analysis_simple.py: fromisoformat, numeric bin sort, profit factor - trading_dashboard.py: st.rerun(), session_state auto-refresh, pathlib DB path, errors=coerce Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+18
-15
@@ -16,7 +16,6 @@ import json
|
||||
def get_connection(db_path="trading_bot.db"):
|
||||
"""Verbindung zur Datenbank"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
# ==========================================
|
||||
@@ -25,7 +24,12 @@ def get_connection(db_path="trading_bot.db"):
|
||||
|
||||
def load_closed_trades(conn, exclude_historical=True):
|
||||
"""Lade geschlossene Trades"""
|
||||
query = """
|
||||
# 'historical' is a distinct status for imported legacy trades.
|
||||
# exclude_historical=True → only live bot trades (status='closed')
|
||||
# exclude_historical=False → live + historical imports
|
||||
status_filter = "status = 'closed'" if exclude_historical else "status IN ('closed', 'historical')"
|
||||
|
||||
query = f"""
|
||||
SELECT
|
||||
ticket, symbol, type, volume,
|
||||
entry_price, exit_price,
|
||||
@@ -38,20 +42,15 @@ def load_closed_trades(conn, exclude_historical=True):
|
||||
profit_pct, rr_ratio,
|
||||
exit_reason, status
|
||||
FROM trades
|
||||
WHERE status = 'closed'
|
||||
WHERE {status_filter}
|
||||
ORDER BY exit_time DESC
|
||||
"""
|
||||
|
||||
if exclude_historical:
|
||||
query += " AND status != 'historical'"
|
||||
|
||||
query += " ORDER BY exit_time DESC"
|
||||
|
||||
df = pd.read_sql_query(query, conn)
|
||||
|
||||
# Convert datetime columns
|
||||
if not df.empty:
|
||||
df['entry_time'] = pd.to_datetime(df['entry_time'], format='mixed')
|
||||
df['exit_time'] = pd.to_datetime(df['exit_time'], format='mixed')
|
||||
df['entry_time'] = pd.to_datetime(df['entry_time'], errors='coerce')
|
||||
df['exit_time'] = pd.to_datetime(df['exit_time'], errors='coerce')
|
||||
df['duration_hours'] = (df['exit_time'] - df['entry_time']).dt.total_seconds() / 3600
|
||||
df['win'] = df['net_profit'] > 0
|
||||
|
||||
@@ -78,7 +77,9 @@ def calculate_overall_metrics(df):
|
||||
avg_win = df[df['win']]['net_profit'].mean() if winning_trades > 0 else 0
|
||||
avg_loss = df[~df['win']]['net_profit'].mean() if losing_trades > 0 else 0
|
||||
|
||||
profit_factor = abs(avg_win / avg_loss) if avg_loss != 0 else 0
|
||||
gross_profit = df[df['win']]['net_profit'].sum()
|
||||
gross_loss = abs(df[~df['win']]['net_profit'].sum())
|
||||
profit_factor = round(gross_profit / gross_loss, 2) if gross_loss > 0 else 0
|
||||
|
||||
avg_duration = df['duration_hours'].mean()
|
||||
|
||||
@@ -87,7 +88,7 @@ def calculate_overall_metrics(df):
|
||||
df_sorted['cumulative'] = df_sorted['net_profit'].cumsum()
|
||||
df_sorted['running_max'] = df_sorted['cumulative'].cummax()
|
||||
df_sorted['drawdown'] = df_sorted['cumulative'] - df_sorted['running_max']
|
||||
max_drawdown = df_sorted['drawdown'].min()
|
||||
max_drawdown = abs(df_sorted['drawdown'].min())
|
||||
|
||||
return {
|
||||
'total_trades': total_trades,
|
||||
@@ -429,7 +430,7 @@ def run_performance_analysis(db_path="trading_bot.db", exclude_historical=True):
|
||||
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
results = {
|
||||
'overall': overall,
|
||||
'by_session': session_df,
|
||||
'by_confidence': conf_df,
|
||||
@@ -439,6 +440,8 @@ def run_performance_analysis(db_path="trading_bot.db", exclude_historical=True):
|
||||
'by_regime': regime_df
|
||||
}
|
||||
|
||||
return results
|
||||
|
||||
# ==========================================
|
||||
# EXPORT TO JSON
|
||||
# ==========================================
|
||||
@@ -457,7 +460,7 @@ def export_analysis_to_json(results, output_file="performance_analysis.json"):
|
||||
'by_regime': results['by_regime'].to_dict('records') if not results['by_regime'].empty else []
|
||||
}
|
||||
|
||||
with open(output_file, 'w') as f:
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(output, f, indent=2)
|
||||
|
||||
print(f"\n✅ Analysis exported to: {output_file}")
|
||||
|
||||
Reference in New Issue
Block a user