13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

492

rolling_sharpe_s = np.sqrt(self.periods) * (

)

rolling.mean() / rolling.std()

..

..

statistics["rolling_sharpe"] = rolling_sharpe_s

# Benchmark statistics if benchmark ticker specified

if self.benchmark is not None:

equity_b = pd.Series(self.equity_benchmark).sort_index()

returns_b = equity_b.pct_change().fillna(0.0)

rolling_b = returns_b.rolling(window=self.periods)

rolling_sharpe_b = np.sqrt(self.periods) * (

rolling_b.mean() / rolling_b.std()

)

..

..

statistics["rolling_sharpe_b"] = rolling_sharpe_b

..

..

return statistics

To plot the annualised rolling Sharpe a new method _plot_rolling_sharpe has been created.

It is very similar to the _plot_equity method. It produces a similar plot to the equity

curve using identical colours to help distinguish the strategy performance from the benchmark

performance. The only minor addition is a dashed vertical line placed at k periods into the

strategy simulation to represent the first point at which the trailing ratio is calculated:

def _plot_rolling_sharpe(self, stats, ax=None, **kwargs):

..

..

sharpe = stats[’rolling_sharpe’]

..

..

if self.benchmark is not None:

benchmark = stats[’rolling_sharpe_b’]

benchmark.plot(

lw=2, color=’gray’, label=self.benchmark,

alpha=0.60, ax=ax, **kwargs

)

sharpe.plot(

lw=2, color=’green’, alpha=0.6, x_compat=False,

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

Saved successfully!

Ooh no, something went wrong!