def corr_heatmap(returns, name, save=False): ''' Helper for correlation heatmap.''' ax = heatmap(returns.corr()) name = name[0].upper() + name[1:].lower() title = '{} Heatmap'.format(name) plt.title(title) if save: save_plot(plt=plt, folder='index', name=name) else: plt.show()
def drawdown(cumulative, folder, save=False): dd = drawdowns(cumulative=cumulative) plt.plot(dd*100.0, label='Drawdown', lw=3) plt.xlabel('Time, t') plt.ylabel('Value, %') plt.legend() if save: save_plot(plt=plt, folder=folder, name='drawdowns.png') else: plt.show()
def simple_plot(returns, folder): plt.figure(figsize=(12,8)) ax = plt.gca() returns = returns * 100.0 returns.cumsum().plot() ax.set_ylabel('%') ax.set_xlabel('Time') ax.set_title('Cumulative Returns, %', fontweight='bold') ax.axhline(0.0) save_plot(plt=plt, folder=folder, name='returns_cumsum')
def rolling_sharpe_plot(returns, folder): rs = rolling_sharpe(returns=returns, rf=0.0, per=63) plt.figure(figsize=(12,8)) ax = plt.gca() rs.plot() ax.axhline(0.0) ax.set_ylabel('Sharpe Ratio') ax.set_xlabel('Time') ax.set_title('Rolling Sharpe', fontweight='bold') save_plot(plt=plt, folder=folder, name='rolling_sharpe_ratio')
def rolling_yearly_returns(returns, folder): yr = returns.rolling(window=252).mean() * 252 * 100.0 plt.figure(figsize=(12,8)) ax = plt.gca() yr.plot() ax.axhline(0.0) ax.set_ylabel('%') ax.set_xlabel('Time') ax.set_title('Rolling Yearly Returns, %', fontweight='bold') save_plot(plt=plt, folder=folder, name='rolling_yearly_returns')
def drawdown_to_percentile(cumulative, folder, save=False): d = drawdowns(cumulative=cumulative).dropna() dd = d.loc[d != 0] if len(dd) > 100: x = percentiles(dd) y = array([i for i in range(100)]) plt.plot(x, y/100) plt.xlabel('Drawdown, *100%') plt.ylabel('Probability') if save: save_plot(plt=plt, folder=folder, name='drawdown_percentile.png') else: plt.show()
def monthly_heatmap(returns, folder): returns = returns.groupby([lambda x: x.year, lambda x: x.month]).apply(cumulate_returns) returns = returns.to_frame().unstack() returns = round(returns, 3) returns.rename(columns={ 1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}, inplace=True) plt.figure(figsize=(12,8)) ax = plt.gca() heatmap(returns*100.0, xticklabels=[i[1] for i in list(returns.columns)], annot=True, fmt='0.2f', annot_kws={'size': 8}, alpha=1.0, center=0.0, cbar=False, cmap=cm.RdYlGn, ax=ax) ax.set_title('Monthly Returns, %', fontweight='bold') save_plot(plt=plt, folder=folder, name='monthly_returns')
def save_yearly_returns(returns, folder): plt.figure(figsize=(12,8)) ax = plt.gca() despine() ax.yaxis.grid(linestyle=':') returns.index = returns.index.strftime('%Y') plt.bar(x=returns.index, height=returns.values) ax.set_title('Yearly Returns, %', fontweight='bold') ax.set_ylabel('%') plt.xticks(range(len(returns.index)), tuple(returns.index)) ax.set_xlabel('Year') ax.set_xticklabels(ax.get_xticklabels(), rotation=45) ax.xaxis.grid(False) save_plot(plt=plt, folder=folder, name='yearly_returns')