77
88try :
99 import plotly .graph_objects as go
10+
1011 PLOTLY_AVAILABLE = True
1112except 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+
4346def _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+
7883def _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
0 commit comments