def main(): df = get_stock(TICKER, START_DATE, END_DATE) bt = Backtest( df, SmaCross, cash=INIT_CASH, trade_on_close=False, exclusive_orders=True ) stats, heatmap = bt.optimize( long_term=range(3, MAX_LONG_TERM + 1, TERM_STEP), short_term=range(2, MAX_LONG_TERM, TERM_STEP), return_heatmap=True, constraint=lambda p: p.short_term < p.long_term) print(stats) bt.plot() plot_heatmaps(heatmap, agg='mean', plot_width=2048, filename='heatmap') sns.set(font='IPAexGothic') d = heatmap.reset_index().pivot('short_term', 'long_term', 'SQN') fig, ax = plt.subplots(figsize=(12, 9)) sns.heatmap(d, square=True, cmap='seismic', center=0, ax=ax) ax.set_title('移動平均期間の組み合わせによるSQN') ax.invert_yaxis() ax.grid() plt.savefig('strategy4.png') plt.show()
def main(): df = get_stock(TICKER, START_DATE, END_DATE) bt = Backtest( df, RsiStrategy, cash=INIT_CASH, trade_on_close=False, exclusive_orders=True ) stats, heatmap = bt.optimize( timeperiod=range(5, 41), rsi_upper=range(60, 91), rsi_lower=range(10, 41), return_heatmap=True) print(stats) bt.plot() plot_heatmaps(heatmap, agg='mean', plot_width=2048, filename='heatmap') # 3Dヒートマップで表示 d = heatmap.reset_index() fig = plt.figure() ax = Axes3D(fig) ax.scatter(d['timeperiod'], d['rsi_upper'], d['rsi_lower'], c=d['SQN']) ax.set_xlabel('RSI期間(日)') ax.set_ylabel('RSI上限') ax.set_zlabel('RSI下限') plt.show()
def test_plot_heatmaps(self): bt = Backtest(GOOG, SmaCross) stats, heatmap = bt.optimize(fast=range(2, 7, 2), slow=range(7, 15, 2), return_heatmap=True) with _tempfile() as f: for agg in ('mean', lambda x: np.percentile(x, 75)): plot_heatmaps(heatmap, agg, filename=f, open_browser=False) # Preview plot_heatmaps(heatmap, filename=f) time.sleep(5)
def main(): df = get_stock('^N225', START_DATE, END_DATE) bt = Backtest(df, SmaCross, cash=INIT_CASH, trade_on_close=False, exclusive_orders=True) stats, heatmap = bt.optimize( long_term=range(3, MAX_LONG_TERM + 1, TERM_STEP), short_term=range(2, MAX_LONG_TERM, TERM_STEP), return_heatmap=True, constraint=lambda p: p.short_term < p.long_term) print(stats) bt.plot() plot_heatmaps(heatmap, agg='mean', plot_width=2048, filename='heatmap')
# But people have this enormous faculty of vision, used to make judgements on much larger data sets much faster. # Let's plot the whole heatmap by projecting it on two chosen dimensions. # Say we're mostly interested in how parameters `n1` and `n2`, on average, affect the outcome. hm = heatmap.groupby(['n1', 'n2']).mean().unstack() hm # Let's plot this table using the excellent [_Seaborn_](https://seaborn.pydata.org) package: # + # %matplotlib inline import seaborn as sns sns.heatmap(hm[::-1], cmap='viridis') # - # We see that, on average, we obtain the highest result using trend-determining parameters `n1=40` and `n2=60`, # and it's not like other nearby combinations work similarly well — in our particular strategy, this combination really stands out. # # Since our strategy contains several parameters, we might be interested in other relationships between their values. # We can use # [`backtesting.lib.plot_heatmaps()`](https://kernc.github.io/backtesting.py/doc/backtesting/lib.html#backtesting.lib.plot_heatmaps) # function to plot interactive heatmaps of all parameter combinations simultaneously. # + from backtesting.lib import plot_heatmaps plot_heatmaps(heatmap, agg='mean')