Manual trade review is tedious. Let's automate the analysis part so you can focus on improving your strategy.
The Problem
After a week of trading, you have dozens of trades to review. Manually calculating metrics, identifying patterns, and generating reports takes hours.
Solution: A Python Analysis Pipeline
Step 1: Import Trade Data
Most platforms export to CSV:
import pandas as pd
import numpy as np
def load_trades(filepath):
df = pd.read_csv(filepath)
df['date'] = pd.to_datetime(df['date'])
df['pnl'] = df['exit_price'] - df['entry_price']
df['pnl'] *= np.where(df['direction'] == 'short', -1, 1)
df['pnl'] *= df['size']
return df
Step 2: Core Metrics
def calculate_metrics(df):
metrics = {}
metrics['total_trades'] = len(df)
metrics['win_rate'] = (df['pnl'] > 0).mean() * 100
metrics['avg_win'] = df[df['pnl'] > 0]['pnl'].mean()
metrics['avg_loss'] = df[df['pnl'] < 0]['pnl'].mean()
metrics['profit_factor'] = (
df[df['pnl'] > 0]['pnl'].sum() /
abs(df[df['pnl'] < 0]['pnl'].sum())
)
metrics['max_drawdown'] = calculate_max_drawdown(df)
metrics['sharpe'] = df['pnl'].mean() / df['pnl'].std() * np.sqrt(252)
return metrics
def calculate_max_drawdown(df):
cumulative = df['pnl'].cumsum()
peak = cumulative.cummax()
drawdown = (cumulative - peak)
return drawdown.min()
Step 3: Pattern Detection
def find_patterns(df):
# Best performing hours
df['hour'] = df['date'].dt.hour
hourly = df.groupby('hour')['pnl'].agg(['mean', 'count'])
best_hour = hourly['mean'].idxmax()
# Consecutive losses
df['is_loss'] = df['pnl'] < 0
df['loss_streak'] = (df['is_loss'] != df['is_loss'].shift()).cumsum()
max_streak = df[df['is_loss']].groupby('loss_streak').size().max()
# Performance by instrument
by_instrument = df.groupby('instrument')['pnl'].agg(['sum', 'mean', 'count'])
return {
'best_hour': best_hour,
'max_loss_streak': max_streak,
'by_instrument': by_instrument
}
Step 4: Weekly Report
def generate_report(df, metrics, patterns):
report = f"""
WEEKLY TRADING REPORT
{'='*40}
Total Trades: {metrics['total_trades']}
Win Rate: {metrics['win_rate']:.1f}%
Profit Factor: {metrics['profit_factor']:.2f}
Max Drawdown: ${metrics['max_drawdown']:.2f}
Sharpe Ratio: {metrics['sharpe']:.2f}
Best Trading Hour: {patterns['best_hour']}:00
Max Loss Streak: {patterns['max_loss_streak']}
BY INSTRUMENT:
{patterns['by_instrument'].to_string()}
"""
return report
Going Further
This basic pipeline can be extended with:
- Matplotlib/Plotly charts for equity curves
- Monte Carlo simulations for risk assessment
- Machine learning for setup classification
- Automated email reports via cron
If you're trading with a prop firm, these metrics help you stay within their risk parameters. Understanding your own patterns is key to passing evaluations β I cover what each firm expects at propfirmkey.com.
What's your trade analysis workflow? Share in the comments.
Top comments (0)