@@ -143,6 +143,20 @@ def _calc_drawdown_series(
143143 max_drawdown_df ["drawdown_relative" ] = (
144144 max_drawdown_df ["high_value" ] - max_drawdown_df ["cumulative" ]
145145 ) / max_drawdown_df ["high_value" ]
146+
147+ # Add zero row at start to account for edge-cases with no winning / losing trades - so high/low
148+ # will be 0.0 in such cases.
149+ zero_row = pd .DataFrame (
150+ {
151+ "cumulative" : [0.0 ],
152+ "high_value" : [0.0 ],
153+ "drawdown" : [0.0 ],
154+ "drawdown_relative" : [0.0 ],
155+ "date" : [profit_results .loc [0 , date_col ]],
156+ }
157+ )
158+
159+ max_drawdown_df = pd .concat ([zero_row , max_drawdown_df ], ignore_index = True )
146160 return max_drawdown_df
147161
148162
@@ -215,6 +229,7 @@ def calculate_max_drawdown(
215229 max_drawdown_df = _calc_drawdown_series (
216230 profit_results , date_col = date_col , value_col = value_col , starting_balance = starting_balance
217231 )
232+ # max_drawdown_df has an extra zero row at the start
218233
219234 # Calculate maximum drawdown
220235 idxmin = (
@@ -223,15 +238,15 @@ def calculate_max_drawdown(
223238 else max_drawdown_df ["drawdown" ].idxmin ()
224239 )
225240 high_idx = max_drawdown_df .iloc [: idxmin + 1 ]["high_value" ].idxmax ()
226- high_date = profit_results .loc [ high_idx , date_col ]
227- low_date = profit_results .loc [ idxmin , date_col ]
228- high_val = max_drawdown_df .loc [high_idx , "cumulative" ]
229- low_val = max_drawdown_df .loc [idxmin , "cumulative" ]
230- max_drawdown_rel = max_drawdown_df .loc [idxmin , "drawdown_relative" ]
241+ high_date = profit_results .at [ max ( high_idx - 1 , 0 ) , date_col ]
242+ low_date = profit_results .at [ max ( idxmin - 1 , 0 ) , date_col ]
243+ high_val = max_drawdown_df .at [high_idx , "cumulative" ]
244+ low_val = max_drawdown_df .at [idxmin , "cumulative" ]
245+ max_drawdown_rel = max_drawdown_df .at [idxmin , "drawdown_relative" ]
231246
232247 # Calculate current drawdown
233248 current_high_idx = max_drawdown_df ["high_value" ].iloc [:- 1 ].idxmax ()
234- current_high_date = profit_results .loc [ current_high_idx , date_col ]
249+ current_high_date = profit_results .at [ max ( current_high_idx - 1 , 0 ) , date_col ]
235250 current_high_value = max_drawdown_df .iloc [- 1 ]["high_value" ]
236251 current_cumulative = max_drawdown_df .iloc [- 1 ]["cumulative" ]
237252 current_drawdown_abs = current_high_value - current_cumulative
0 commit comments