Untitled

 avatar
unknown
plain_text
20 days ago
2.7 kB
2
Indexable
import matplotlib.pyplot as plt
import numpy as np

# -----------------------------
# Chart 1: Cumulative Returns
# -----------------------------

# Compute strategy cumulative return: (Total AUM / initial AUM) - 1
initial_aum = result_df.iloc[0]['Total AUM']
result_df['strategy_cum_return'] = result_df['Total AUM'] / initial_aum - 1

# Compute benchmark cumulative return from SPY price: (price / initial price) - 1
spy_prices = result_df['price_SPY US Equity']
initial_spy = spy_prices.iloc[0]
result_df['benchmark_cum_return'] = spy_prices / initial_spy - 1

# Plot cumulative returns chart
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(result_df.index, result_df['strategy_cum_return'], label='Strategy', 
        color='#1f77b4', linewidth=2)
ax.plot(result_df.index, result_df['benchmark_cum_return'], label='Benchmark', 
        color='#ff7f0e', linewidth=2)

ax.set_title('Cumulative Returns: Strategy vs Benchmark')
ax.set_xlabel('Date')
ax.set_ylabel('Cumulative Return')
ax.legend()

# Remove all spines except bottom spine
for spine in ['top', 'right', 'left']:
    ax.spines[spine].set_visible(False)
ax.spines['bottom'].set_linewidth(1.5)
ax.tick_params(axis='x', which='both', bottom=True)
ax.grid(True, axis='y', linestyle='--', alpha=0.7)

plt.show()


# -------------------------------------------------
# Chart 2: Grouped Column Chart - Year-on-Year Returns
# -------------------------------------------------

# Resample to get year-end values for the strategy and benchmark (using SPY price as benchmark)
yearly_data = result_df.resample('A').last()

# Compute year-on-year returns (in percentage)
yearly_data['strategy_yoy_return'] = yearly_data['Total AUM'].pct_change() * 100
yearly_data['benchmark_yoy_return'] = yearly_data['price_SPY US Equity'].pct_change() * 100

# Prepare data for the grouped bar chart
years = yearly_data.index.year.astype(str)
x = np.arange(len(years))
width = 0.35  # width of each bar

fig, ax = plt.subplots(figsize=(10, 6))
bars_strategy = ax.bar(x - width/2, yearly_data['strategy_yoy_return'], width, 
                       label='Strategy', color='#1f77b4')
bars_benchmark = ax.bar(x + width/2, yearly_data['benchmark_yoy_return'], width, 
                        label='Benchmark', color='#ff7f0e')

ax.set_xlabel('Year')
ax.set_ylabel('Year-on-Year Return (%)')
ax.set_title('Year-on-Year Returns: Strategy vs Benchmark')
ax.set_xticks(x)
ax.set_xticklabels(years)
ax.legend()

# Remove all spines except bottom spine
for spine in ['top', 'right', 'left']:
    ax.spines[spine].set_visible(False)
ax.spines['bottom'].set_linewidth(1.5)
ax.tick_params(axis='x', which='both', bottom=True)
ax.grid(True, axis='y', linestyle='--', alpha=0.7)

plt.show()
Editor is loading...
Leave a Comment