MCPcopy Index your code
hub / github.com/kernc/backtesting.py / _plot_equity_section

Function _plot_equity_section

backtesting/_plotting.py:324–412  ·  view source on GitHub ↗

Equity section

(is_return=False)

Source from the content-addressed store, hash-verified

322 tooltips=tooltips, mode='vline' if vline else 'mouse'))
323
324 def _plot_equity_section(is_return=False):
325 """Equity section"""
326 # Max DD Dur. line
327 equity = equity_data['Equity'].copy()
328 dd_end = equity_data['DrawdownDuration'].idxmax()
329 if np.isnan(dd_end):
330 dd_start = dd_end = equity.index[0]
331 else:
332 dd_start = equity[:dd_end].idxmax()
333 # If DD not extending into the future, get exact point of intersection with equity
334 if dd_end != equity.index[-1]:
335 dd_end = np.interp(equity[dd_start],
336 (equity[dd_end - 1], equity[dd_end]),
337 (dd_end - 1, dd_end))
338
339 if smooth_equity:
340 interest_points = pd.Index([
341 # Beginning and end
342 equity.index[0], equity.index[-1],
343 # Peak equity and peak DD
344 equity.idxmax(), equity_data['DrawdownPct'].idxmax(),
345 # Include max dd end points. Otherwise the MaxDD line looks amiss.
346 dd_start, int(dd_end), min(int(dd_end + 1), equity.size - 1),
347 ])
348 select = pd.Index(trades['ExitBar']).union(interest_points)
349 select = select.unique().dropna()
350 equity = equity.iloc[select].reindex(equity.index)
351 equity.interpolate(inplace=True)
352
353 assert equity.index.equals(equity_data.index)
354
355 if relative_equity:
356 equity /= equity.iloc[0]
357 if is_return:
358 equity -= equity.iloc[0]
359
360 yaxis_label = 'Return' if is_return else 'Equity'
361 source_key = 'eq_return' if is_return else 'equity'
362 source.add(equity, source_key)
363 fig = new_indicator_figure(
364 y_axis_label=yaxis_label,
365 **(dict(height=80) if plot_drawdown else dict(height=100)))
366
367 # High-watermark drawdown dents
368 fig.patch('index', 'equity_dd',
369 source=ColumnDataSource(dict(
370 index=np.r_[index, index[::-1]],
371 equity_dd=np.r_[equity, equity.cummax()[::-1]]
372 )),
373 fill_color='#ffffea', line_color='#ffcb66')
374
375 # Equity line
376 r = fig.line('index', source_key, source=source, line_width=1.5, line_alpha=1)
377 if relative_equity:
378 tooltip_format = f'@{source_key}{{+0,0.[000]%}}'
379 tick_format = '0,0.[00]%'
380 legend_format = '{:,.0f}%'
381 else:

Callers 1

plotFunction · 0.85

Calls 2

new_indicator_figureFunction · 0.85
set_tooltipsFunction · 0.85

Tested by

no test coverage detected