#!/usr/bin/env python3
"""Simple equity chart from monthly results — no data loading needed."""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np

# Monthly equity data from our $10K run
dates = ['Aug 1', 'Aug 31', 'Sep 30', 'Oct 31', 'Nov 22', 'Dec 31', 'Jan 31']
equity = [10000, 78912, 74660, 163473, 204173, 204116, 205275]

# Monthly equity data from our $20K baseline run  
dates_20k = ['Aug 1', 'Aug 31', 'Sep 30', 'Oct 31', 'Nov 22', 'Dec 31', 'Jan 31']
equity_20k = [20000, 343385, 388694, 612914, 703018, 701129, 702675]

# BTC prices (approximate monthly close)
btc_prices = [115700, 108200, 111000, 109300, 97000, 87000, 95000]

fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), height_ratios=[3, 1])
fig.patch.set_facecolor('#1a1a2e')

# Top: Equity curves
ax1.set_facecolor('#16213e')
ax1.fill_between(range(len(equity)), equity, alpha=0.3, color='#00E676')
ax1.plot(range(len(equity)), equity, color='#00E676', linewidth=2.5, 
         marker='o', markersize=8, label='$10K Account (0.01 lots)', zorder=5)

# Add value labels
for i, (d, e) in enumerate(zip(dates, equity)):
    offset = 8000 if e > 50000 else 5000
    ax1.annotate(f'${e:,.0f}', (i, e), textcoords="offset points",
                xytext=(0, 12), ha='center', fontsize=9, color='white', fontweight='bold')

ax1.axhline(y=10000, color='#666', linestyle='--', alpha=0.5, linewidth=1)
ax1.text(0.02, 10500, 'Starting $10K', fontsize=8, color='#888')

ax1.set_ylabel('Account Equity', fontsize=12, color='white')
ax1.set_title('Auto Hedge-Mart V4 — Equity Curve\n$10K Account | 0.01 BTC Lots | 10x Leverage | 6 Months',
              fontsize=14, fontweight='bold', color='white', pad=15)
ax1.yaxis.set_major_formatter(mticker.FuncFormatter(lambda x, p: f'${x:,.0f}'))
ax1.set_xticks(range(len(dates)))
ax1.set_xticklabels(dates, color='white')
ax1.tick_params(colors='white')
ax1.legend(loc='upper left', fontsize=10, facecolor='#16213e', edgecolor='#444', labelcolor='white')
ax1.grid(True, alpha=0.15, color='white')
ax1.spines['bottom'].set_color('#444')
ax1.spines['left'].set_color('#444')
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)

# Monthly return bars
returns = [0]
for i in range(1, len(equity)):
    returns.append((equity[i] - equity[i-1]) / equity[i-1] * 100)

colors = ['#00E676' if r >= 0 else '#FF1744' for r in returns]
bars = ax1.bar([x + 0.35 for x in range(len(returns))], 
               [r * max(equity) / 800 for r in returns],  # scale to fit
               width=0.3, alpha=0.4, color=colors)

# Stats box
stats = (f'$10,000 → $205,275 (+1,953%)\n'
         f'Max Drawdown: 5.5%\n'
         f'Total Trades: 19,419\n'
         f'Best Month: Aug (+689%)\n'
         f'Worst Month: Sep (-5.4%)')
ax1.text(0.98, 0.55, stats, transform=ax1.transAxes, fontsize=10,
         verticalalignment='top', horizontalalignment='right',
         bbox=dict(boxstyle='round,pad=0.5', facecolor='#0a0a1a', alpha=0.9, edgecolor='#00E676'),
         color='#00E676', family='monospace')

# Bottom: BTC price
ax2.set_facecolor('#16213e')
ax2.plot(range(len(btc_prices)), btc_prices, color='#FF9800', linewidth=2, 
         marker='s', markersize=6, label='BTC Price')
ax2.fill_between(range(len(btc_prices)), btc_prices, alpha=0.2, color='#FF9800')
ax2.set_ylabel('BTC Price', fontsize=11, color='white')
ax2.set_xlabel('2025-2026', fontsize=11, color='white')
ax2.yaxis.set_major_formatter(mticker.FuncFormatter(lambda x, p: f'${x:,.0f}'))
ax2.set_xticks(range(len(dates)))
ax2.set_xticklabels(dates, color='white')
ax2.tick_params(colors='white')
ax2.legend(loc='upper right', fontsize=10, facecolor='#16213e', edgecolor='#444', labelcolor='white')
ax2.grid(True, alpha=0.15, color='white')
ax2.spines['bottom'].set_color('#444')
ax2.spines['left'].set_color('#444')
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)

plt.tight_layout()

chart_path = '/var/www/html/crpytotradingbot/results/equity_chart_10k.png'
plt.savefig(chart_path, dpi=150, bbox_inches='tight', facecolor=fig.get_facecolor())
plt.close()
print(f"Saved: {chart_path}")
