13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

493

)

label=’Backtest’, ax=ax, **kwargs

ax.axvline(

sharpe.index[self.periods],

linestyle="dashed", c="gray", lw=2

)

ax.set_ylabel(’Rolling Annualised Sharpe’)

..

..

The plot_results code is modified to include the rolling Sharpe graph if the self.rolling_sharpe

flag is set to True. This is carried out using an offset_index. The index is used to let Matplotlib

know if there are five or six vertical sections in the chart, and if so, to adjust the plot

placement:

def plot_results(self, filename=None):

..

..

if self.rolling_sharpe:

offset_index = 1

else:

offset_index = 0

vertical_sections = 5 + offset_index

fig = plt.figure(figsize=(10, vertical_sections * 3.5))

fig.suptitle(self.title, y=0.94, weight=’bold’)

gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.25, hspace=0.5)

stats = self.get_results()

ax_equity = plt.subplot(gs[:2, :])

if self.rolling_sharpe:

ax_sharpe = plt.subplot(gs[2, :])

ax_drawdown = plt.subplot(gs[2 + offset_index, :])

ax_monthly_returns = plt.subplot(gs[3 + offset_index, :2])

ax_yearly_returns = plt.subplot(gs[3 + offset_index, 2])

ax_txt_curve = plt.subplot(gs[4 + offset_index, 0])

ax_txt_trade = plt.subplot(gs[4 + offset_index, 1])

ax_txt_time = plt.subplot(gs[4 + offset_index, 2])

self._plot_equity(stats, ax=ax_equity)

if self.rolling_sharpe:

self._plot_rolling_sharpe(stats, ax=ax_sharpe)

self._plot_drawdown(stats, ax=ax_drawdown)

..

..

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!