def analyze(context, perf): import matplotlib.pyplot as plt print('the stats: {}'.format(get_pretty_stats(perf))) # The base currency of the algo exchange base_currency = context.exchanges.values()[0].base_currency.upper() # Plot the portfolio value over time. ax1 = plt.subplot(611) perf.loc[:, 'portfolio_value'].plot(ax=ax1) ax1.set_ylabel('Portfolio Value ({})'.format(base_currency)) # Plot the price increase or decrease over time. ax2 = plt.subplot(612, sharex=ax1) perf.loc[:, 'price'].plot(ax=ax2, label='Price') ax2.set_ylabel('{asset} ({base})'.format( asset=context.asset.symbol, base=base_currency )) transaction_df = extract_transactions(perf) if not transaction_df.empty: buy_df = transaction_df[transaction_df['amount'] > 0] sell_df = transaction_df[transaction_df['amount'] < 0] ax2.scatter( buy_df.index.to_pydatetime(), perf.loc[buy_df.index, 'price'], marker='^', s=100, c='green', label='' ) ax2.scatter( sell_df.index.to_pydatetime(), perf.loc[sell_df.index, 'price'], marker='v', s=100, c='red', label='' ) ax4 = plt.subplot(613, sharex=ax1) perf.loc[:, 'cash'].plot( ax=ax4, label='Base Currency ({})'.format(base_currency) ) ax4.set_ylabel('Cash ({})'.format(base_currency)) perf['algorithm'] = perf.loc[:, 'algorithm_period_return'] ax5 = plt.subplot(614, sharex=ax1) perf.loc[:, ['algorithm', 'price_change']].plot(ax=ax5) ax5.set_ylabel('Percent Change') plt.legend(loc=3) # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() pass
def analyze(context, perf): # Get the base_currency that was passed as a parameter to the simulation base_currency = context.exchanges.values()[0].base_currency.upper() # First chart: Plot portfolio value using base_currency ax1 = plt.subplot(411) perf.loc[:, ['portfolio_value']].plot(ax=ax1) ax1.legend_.remove() ax1.set_ylabel('Portfolio Value\n({})'.format(base_currency)) start, end = ax1.get_ylim() ax1.yaxis.set_ticks(np.arange(start, end, (end - start) / 5)) # Second chart: Plot asset price, moving averages and buys/sells ax2 = plt.subplot(412, sharex=ax1) perf.loc[:, ['price', 'short_mavg', 'long_mavg']].plot(ax=ax2, label='Price') ax2.legend_.remove() ax2.set_ylabel('{asset}\n({base})'.format(asset=context.asset.symbol, base=base_currency)) start, end = ax2.get_ylim() ax2.yaxis.set_ticks(np.arange(start, end, (end - start) / 5)) transaction_df = extract_transactions(perf) if not transaction_df.empty: buy_df = transaction_df[transaction_df['amount'] > 0] sell_df = transaction_df[transaction_df['amount'] < 0] ax2.scatter(buy_df.index.to_pydatetime(), perf.loc[buy_df.index, 'price'], marker='^', s=100, c='green', label='') ax2.scatter(sell_df.index.to_pydatetime(), perf.loc[sell_df.index, 'price'], marker='v', s=100, c='red', label='') # Third chart: Compare percentage change between our portfolio # and the price of the asset ax3 = plt.subplot(413, sharex=ax1) perf.loc[:, ['algorithm_period_return', 'price_change']].plot(ax=ax3) ax3.legend_.remove() ax3.set_ylabel('Percent Change') start, end = ax3.get_ylim() ax3.yaxis.set_ticks(np.arange(start, end, (end - start) / 5)) # Fourth chart: Plot our cash ax4 = plt.subplot(414, sharex=ax1) perf.cash.plot(ax=ax4) ax4.set_ylabel('Cash\n({})'.format(base_currency)) start, end = ax4.get_ylim() ax4.yaxis.set_ticks(np.arange(0, end, end / 5)) plt.show()
def analyze(context=None, perf=None): end = time.time() log.info('elapsed time: {}'.format(end - context.start_time)) import matplotlib.pyplot as plt # The base currency of the algo exchange base_currency = context.exchanges.values()[0].base_currency.upper() # Plot the portfolio value over time. ax1 = plt.subplot(611) perf.loc[:, 'portfolio_value'].plot(ax=ax1) ax1.set_ylabel('Portfolio\nValue\n({})'.format(base_currency)) # Plot the price increase or decrease over time. ax2 = plt.subplot(612, sharex=ax1) perf.loc[:, 'price'].plot(ax=ax2, label='Price') ax2.set_ylabel('{asset}\n({base})'.format(asset=context.market.symbol, base=base_currency)) transaction_df = extract_transactions(perf) if not transaction_df.empty: buy_df = transaction_df[transaction_df['amount'] > 0] sell_df = transaction_df[transaction_df['amount'] < 0] ax2.scatter(buy_df.index.to_pydatetime(), perf.loc[buy_df.index.floor('1 min'), 'price'], marker='^', s=100, c='green', label='') ax2.scatter(sell_df.index.to_pydatetime(), perf.loc[sell_df.index.floor('1 min'), 'price'], marker='v', s=100, c='red', label='') ax4 = plt.subplot(613, sharex=ax1) perf.loc[:, 'cash'].plot(ax=ax4, label='Base Currency ({})'.format(base_currency)) ax4.set_ylabel('Cash\n({})'.format(base_currency)) perf['algorithm'] = perf.loc[:, 'algorithm_period_return'] ax5 = plt.subplot(614, sharex=ax1) perf.loc[:, ['algorithm', 'price_change']].plot(ax=ax5) ax5.set_ylabel('Percent\nChange') ax6 = plt.subplot(615, sharex=ax1) perf.loc[:, 'rsi'].plot(ax=ax6, label='RSI') ax6.set_ylabel('RSI') ax6.axhline(context.RSI_OVERBOUGHT, color='darkgoldenrod') ax6.axhline(context.RSI_OVERSOLD, color='darkgoldenrod') if not transaction_df.empty: ax6.scatter(buy_df.index.to_pydatetime(), perf.loc[buy_df.index.floor('1 min'), 'rsi'], marker='^', s=100, c='green', label='') ax6.scatter(sell_df.index.to_pydatetime(), perf.loc[sell_df.index.floor('1 min'), 'rsi'], marker='v', s=100, c='red', label='') plt.legend(loc=3) start, end = ax6.get_ylim() ax6.yaxis.set_ticks(np.arange(0, end, end / 5)) # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() pass