-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
56 lines (46 loc) · 1.96 KB
/
plotting.py
File metadata and controls
56 lines (46 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='whitegrid')
def plot_results(results: dict, metrics: dict):
equity = results['equity']
fills = results['fills']
ob = results.get('data')
fig, axes = plt.subplots(4, 1, figsize=(12, 14), sharex=False)
# 1) Equity curve and drawdown
eq = equity.copy()
eq['cummax'] = eq['equity'].cummax()
eq['drawdown'] = (eq['equity'] - eq['cummax']) / (eq['cummax'] + 1e-12)
axes[0].plot(eq.index, eq['equity'], label='Equity')
ax2 = axes[0].twinx()
ax2.fill_between(eq.index, eq['drawdown'], 0, color='red', alpha=0.2, label='Drawdown')
axes[0].set_title('Equity Curve & Drawdown')
axes[0].legend(loc='upper left')
# 2) Position exposure
axes[1].plot(equity.index, equity['position'], color='orange', label='Position')
axes[1].set_title('Position Exposure')
axes[1].legend()
# 3) Trade P&L distribution
if len(fills) > 0 and 'pnl' in fills:
axes[2].hist(fills['pnl'], bins=40, alpha=0.8)
axes[2].set_title('Per-Fill Realized P&L Distribution')
else:
axes[2].text(0.5, 0.5, 'No trades', transform=axes[2].transAxes, ha='center')
# 4) Basic order book statistics
if ob is not None and len(ob) > 0 and {'bid','ask'}.issubset(ob.columns):
spread = (ob['ask'] - ob['bid']).dropna()
axes[3].plot(spread.index, spread, color='green')
axes[3].set_title('Bid-Ask Spread over Time')
else:
axes[3].text(0.5, 0.5, 'No order book data', transform=axes[3].transAxes, ha='center')
# Overall title with key metrics
suptitle = (
f"Strategy: {results['params']['strategy']} | "
f"Sharpe: {metrics.get('sharpe_ratio',0):.2f} | "
f"Max DD: {metrics.get('max_drawdown',0):.2%} | "
f"Trades: {metrics.get('num_trades',0)}"
)
fig.suptitle(suptitle, fontsize=12, y=0.98)
plt.tight_layout(rect=[0, 0, 1, 0.97])
plt.show()