def ma_mem_error_test(column_names): sma_dates = [1, 2] sma_data = [11, 12] sma_df = DataFrame(data={"Test": sma_data}, index=sma_dates) sma_df.index.name = "Date" try: SMA(sma_df, 3, column_names).result except CalculableError as e: assert True
def combined_ind(df, symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), window=10): mome10 = momentum(df, symbol, window=10)[1] SMA20, div20 = SMA(df, symbol, window=20)[1:] bb_ind20 = bol_bands(df, symbol, window=20)[3] #print mome10 mome10 = mome10 / mome10.iloc[10] div20 = div20 / div20.iloc[19] bb_ind20 = bb_ind20 / bb_ind20.iloc[19] #print momentum1 short_entries = [] short_exits = [] long_entries = [] long_exits = [] signals = [] entry = False for i in range(len(df.index)): if mome10[i - 1] > 3.0 and mome10[i] < 2.8 and div20[ i] < 1.5 and bb_ind20[i] < 1.0 and not entry: short_entries.append(df.index[i]) entry = True signals.append([str(df.index[i]).split()[0], 'BUY']) if mome10[ i - 1] < -2.0 and div20[i] > 0.5 and bb_ind20[i] > -1.0 and entry: short_exits.append(df.index[i]) entry = False signals.append([str(df.index[i]).split()[0], 'SELL']) if mome10[i - 1] < -6.0 and mome10[i] > -6.0 and div20[ i] < 1.5 and bb_ind20[i] < 1.0 and not entry: long_entries.append(df.index[i]) entry = True signals.append([str(df.index[i]).split()[0], 'BUY']) if mome10[i - 1] > 5.0 and mome10[ i - 1] < 5.0 and div20[i] > 0.5 and bb_ind20[i] > -1.0 and entry: long_exits.append(df.index[i]) entry = False signals.append([str(df.index[i]).split()[0], 'SELL']) ordersfile = open( 'combined_indicator_order_{}_{}.csv'.format( str(sd).split()[0], str(ed).split()[0]), 'w') ordersfile.write("Date,Symbol,Order,Shares\n") for signal in signals: ordersfile.write("%s,%s,%s,1000\n" % (signal[0], symbol, signal[1])) ordersfile.close() df[symbol] = df[symbol] / df[symbol].iloc[0] ax = df[symbol].plot(title='Combined_ind', label=symbol) mome10.plot(label='Mome10', ax=ax) div20.plot(label='Div20', ax=ax) bb_ind20.plot(label='bb_ind20', ax=ax) ymin, ymax = ax.get_ylim() plt.vlines(long_entries, ymin, ymax, color='g') plt.vlines(long_exits, ymin, ymax) plt.vlines(short_entries, ymin, ymax, color='r') plt.vlines(short_exits, ymin, ymax) ax.legend(loc='lower right') plt.savefig('combined_ind_{}_{}.png'.format( str(sd).split()[0], str(ed).split()[0])) plt.close('all') #plt.show() return long_entries, long_exits, short_entries, short_exits