Skip to content

Commit 3516995

Browse files
style: format code with black
1 parent 843f8bb commit 3516995

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

src/quant_research_starter/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def compute_factors(data_file, factors, output):
131131
"--plotly",
132132
is_flag=True,
133133
default=False,
134-
help="Also generate interactive Plotly HTML chart"
134+
help="Also generate interactive Plotly HTML chart",
135135
)
136136
def backtest(data_file, signals_file, initial_capital, output, plot, plotly):
137137
"""Run backtest with given signals."""
@@ -229,7 +229,7 @@ def backtest(data_file, signals_file, initial_capital, output, plot, plotly):
229229
portfolio_values=results_dict["portfolio_value"],
230230
initial_capital=initial_capital,
231231
output_path=str(html_path),
232-
plot_type="html"
232+
plot_type="html",
233233
)
234234

235235
click.echo(f"Plotly HTML chart saved -> {html_path}")

src/quant_research_starter/metrics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from .plotting import create_equity_curve_plot
44
from .risk import RiskMetrics
55

6-
__all__ = [ "create_equity_curve_plot","RiskMetrics"]
6+
__all__ = ["create_equity_curve_plot", "RiskMetrics"]

src/quant_research_starter/metrics/plotting.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
try:
99
import plotly.graph_objects as go
10+
1011
PLOTLY_AVAILABLE = True
1112
except ImportError:
1213
PLOTLY_AVAILABLE = False
@@ -19,7 +20,7 @@ def create_equity_curve_plot(
1920
portfolio_values: List[float],
2021
initial_capital: float,
2122
output_path: str,
22-
plot_type: str = "png"
23+
plot_type: str = "png",
2324
) -> str:
2425
"""
2526
Create equity curve plot in specified format.
@@ -40,50 +41,54 @@ def create_equity_curve_plot(
4041
return _create_plotly_chart(portfolio_series, initial_capital, output_path)
4142
else:
4243
return _create_matplotlib_chart(portfolio_series, output_path)
44+
45+
4346
def _create_plotly_chart(
44-
portfolio_series: pd.Series,
45-
initial_capital: float,
46-
output_path: str
47+
portfolio_series: pd.Series, initial_capital: float, output_path: str
4748
) -> str:
4849
"""Create interactive Plotly HTML chart."""
4950
fig = go.Figure()
50-
fig.add_trace(go.Scatter(
51-
x=portfolio_series.index,
52-
y=portfolio_series.values,
53-
mode='lines',
54-
name='Portfolio Value',
55-
line={'color': '#2E86AB', 'width': 3},
56-
hovertemplate='<b>Date</b>: %{x|%Y-%m-%d}<br><b>Value</b>: $%{y:,.2f}<extra></extra>'
57-
))
51+
fig.add_trace(
52+
go.Scatter(
53+
x=portfolio_series.index,
54+
y=portfolio_series.values,
55+
mode="lines",
56+
name="Portfolio Value",
57+
line={"color": "#2E86AB", "width": 3},
58+
hovertemplate="<b>Date</b>: %{x|%Y-%m-%d}<br><b>Value</b>: $%{y:,.2f}<extra></extra>",
59+
)
60+
)
5861
# Add initial capital reference line
5962
fig.add_hline(
6063
y=initial_capital,
6164
line_dash="dash",
6265
line_color="red",
6366
annotation_text=f"Initial Capital: ${initial_capital:,.0f}",
64-
annotation_position="bottom right"
67+
annotation_position="bottom right",
6568
)
6669
total_return_pct = ((portfolio_series.iloc[-1] / initial_capital) - 1) * 10
6770
fig.update_layout(
6871
title=f"Backtest Performance (Total Return: {total_return_pct:+.1f}%)",
69-
xaxis_title='Date',
70-
yaxis_title='Portfolio Value ($)',
71-
template='plotly_white',
72-
hovermode='x unified',
73-
height=500
72+
xaxis_title="Date",
73+
yaxis_title="Portfolio Value ($)",
74+
template="plotly_white",
75+
hovermode="x unified",
76+
height=500,
7477
)
75-
fig.update_yaxes(tickprefix='$', tickformat=',.0f')
78+
fig.update_yaxes(tickprefix="$", tickformat=",.0f")
7679
fig.write_html(output_path)
7780
return output_path
81+
82+
7883
def _create_matplotlib_chart(portfolio_series: pd.Series, output_path: str) -> str:
7984
"""Create static matplotlib PNG chart."""
8085
plt.figure(figsize=(10, 6))
8186
plt.plot(portfolio_series.index, portfolio_series.values, linewidth=2)
82-
plt.title('Portfolio Value')
83-
plt.ylabel('USD')
87+
plt.title("Portfolio Value")
88+
plt.ylabel("USD")
8489
plt.grid(True, alpha=0.3)
8590
plt.gcf().autofmt_xdate()
8691
plt.tight_layout()
87-
plt.savefig(output_path, dpi=150, bbox_inches='tight')
92+
plt.savefig(output_path, dpi=150, bbox_inches="tight")
8893
plt.close()
8994
return output_path

tests/test_plotting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def test_plotly_html_creation():
1515
portfolio_values=portfolio_values,
1616
initial_capital=1000000,
1717
output_path=test_html_path,
18-
plot_type="html"
18+
plot_type="html",
1919
)
2020

2121
# Verify file was created
2222
assert os.path.exists(html_path)
23-
assert html_path.endswith('.html')
23+
assert html_path.endswith(".html")
2424
assert os.path.getsize(html_path) > 1000
2525

2626
# Cleanup
@@ -42,11 +42,11 @@ def test_plotly_fallback_to_matplotlib():
4242
portfolio_values=portfolio_values,
4343
initial_capital=1000000,
4444
output_path=test_png_path,
45-
plot_type="png"
45+
plot_type="png",
4646
)
4747

4848
assert os.path.exists(png_path)
49-
assert png_path.endswith('.png')
49+
assert png_path.endswith(".png")
5050

5151
# Cleanup
5252
if os.path.exists(png_path):

0 commit comments

Comments
 (0)