Dual Moving Average Crossover algorithm buys stock once its short moving average crosses its long moving average (indicating upwards momentum) and sells it once the averages cross again, indicating downwards momentum (source: zipline)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
fig = plt.figure(figsize=(20, 12), dpi=320, facecolor="#342F2F", edgecolor='w') ax1 = fig.add_subplot(211, axisbg='k') results['short_mavg'].plot(ax=ax1, color='#00FF26', ls=":", lw=2) results['long_mavg'].plot(ax=ax1, color='#FFA500') data[stock].plot(ax=ax1, lw=3, color='#226dc9') ax1.plot(results.ix[results.buy].index, results.short_mavg[results.buy], '^', markersize=10, color='w') ax1.plot(results.ix[results.sell].index, results.short_mavg[results.sell], 'v', markersize=10, color='w') ax1.grid(color='#D9D9D9', alpha=1, lw=0.5) ax2 = fig.add_subplot(212, axisbg='k') results.portfolio_value.plot(ax=ax2, lw=3, color='#179349') ax2.grid(color='#D9D9D9', alpha=1, lw=0.5) x = np.arange(0,len(results.portfolio_value)) ax2.fill_between(results.portfolio_value.index,results.portfolio_value, color="#179349", alpha=0.5) ax1.set_title('Price and Moving Avarage(s), $\n', color="w") ax2.set_title('Portfolio, $\n', color="w") ax1.tick_params(axis="x", colors="#BFBFBF") ax1.tick_params(axis="y", colors="#BFBFBF") ax2.tick_params(axis="x", colors="#BFBFBF") ax2.tick_params(axis="y", colors="#BFBFBF") ax1.set_xlabel("") subplots_adjust(hspace = 0.4) leg = ax1.legend(loc=0, fancybox=True, shadow=True) leg.get_frame().set_alpha(0.01) for text in leg.get_texts(): text.set_color("#BFBFBF") |
Leave Your Response