def test_run(): in_dates = pd.date_range('2007-12-31', '2009-12-31') #Add in sample and out of sample dates out_dates = pd.date_range('2009-12-31', '2011-12-31') #Add in sample and out of sample dates symbols = list(['IBM']) df = get_data(symbols, in_dates, addSPY=True) # Compute Bollinger Bands # 1. Compute rolling mean rm_IBM = get_rolling_mean(df['IBM'], window=20) # 2. Compute rolling standard deviation rstd_IBM = get_rolling_std(df['IBM'], window=20) # 3. Compute upper and lower bands upper_band, lower_band = get_bollinger_bands(rm_IBM, rstd_IBM) # 4. Add signals for buy and sell combo_df = pd.concat([df['IBM'], rm_IBM, upper_band, lower_band], axis=1) combo_df.columns = ['Price', 'SMA', 'upper_band', 'lower_band'] combo_df.rename(index={0:'Date'}, inplace=True) combo_df.to_csv('IBM.csv') #print combo_df orders = get_bollinger_strategy(combo_df) # Plot raw SPY values, rolling mean and Bollinger Bands ax = df['IBM'].plot(title="Bollinger Bands", label='IBM') rm_IBM.plot(label='SMA', color='yellow', ax=ax) combo_df[['upper_band', 'lower_band']].plot(label='Bollinger Bands', color='cyan', ax=ax) # Plot vertical lines for shorts for i in range(0, len(orders)): if (orders.ix[i]['Type'] == 'Short') and (orders.ix[i]['BB_Strat'] == 'BUY'): ax.axvline(orders.ix[i]['Date'], color='r', linestyle='solid') elif (orders.ix[i]['Type'] == 'Short') and (orders.ix[i]['BB_Strat'] == 'SELL'): ax.axvline(orders.ix[i]['Date'], color='black', linestyle='solid') elif (orders.ix[i]['Type'] == 'Long') and (orders.ix[i]['BB_Strat'] == 'BUY'): ax.axvline(orders.ix[i]['Date'], color='green', linestyle='solid') elif (orders.ix[i]['Type'] == 'Long') and (orders.ix[i]['BB_Strat'] == 'SELL'): ax.axvline(orders.ix[i]['Date'], color='black', linestyle='solid') else: pass # Add axis labels and legend ax.set_xlabel("Date") ax.set_ylabel("Price") ax.legend(loc='lower left', labels=['IBM', 'SMA', 'Bollinger Bands']) #plt.show() fig = ax.get_figure() fig.savefig('output/bb_strategy.png') #prep orders for market simulator orders['Shares'] = 100 orders.set_index('Date', inplace=True) orders[['Symbol', 'Order', 'Shares']].to_csv('output/bb_orders.csv') #send order to marketsims ms.sims_output(sv=10000, of="./output/bb_orders.csv", strat_name='BB' )
def test_run(): #in and out sample dates in_dates = pd.date_range( '2007-12-31', '2009-12-31') #Add in sample and out of sample dates out_dates = pd.date_range( '2009-12-31', '2011-12-31') #Add in sample and out of sample dates symbols = list(['IBM']) df = get_data_more(symbols, in_dates, addSPY=True) df_mfi = df.copy() for col in df_mfi: if 'SPY' in col: try: df_mfi.drop(col, axis=1, inplace=True) except Exception: pass #COMPUTE MY STRATEGY - Money Flow Index(mfi) & RELATIVE STRENGTH (RS) #1. Compute Typical Price df_mfi['tprice'] = (df_mfi['IBM_High'] + df_mfi['IBM_Low'] + df_mfi['IBM']) / 3 # 2. Raw Money Flow df_mfi['raw_mf'] = df_mfi['tprice'] * df_mfi['IBM_Volume'] # 3. Calculate changes df_mfi['mf_ch'] = df_mfi['raw_mf'].shift(1) - df_mfi['raw_mf'] df_mfi['mf_ch_prop'] = df_mfi['raw_mf'].shift(1) / df_mfi['raw_mf'] # 4. Compute Gain and Loss df_mfi['mf_gain'] = df_mfi['mf_ch'].apply(lambda x: abs(x) if x > 0 else 0) df_mfi['mf_loss'] = df_mfi['mf_ch'].apply(lambda x: abs(x) if x < 0 else 0) # 5. Compute rolling mean Average Gain and Loss # lower window to increase the sensitivity df_mfi['rm_mf_gain'] = get_rolling_mean(df_mfi['mf_gain'], window=14) df_mfi['rm_mf_loss'] = get_rolling_mean(df_mfi['mf_loss'], window=14) # 5. Compute Money Flow Ratio df_mfi['mf_ratio'] = df_mfi['rm_mf_gain'] / df_mfi['rm_mf_loss'] # 6. Compute mfi df_mfi['mfi'] = df_mfi['mf_ratio'].apply( lambda x: 100 if x == 0.0 else 100 - (100 / (x + 1))) df_mfi.to_csv('./output/mfi_df.csv') # 7 Add signals for buy and sell df_mfi.rename(columns={'IBM': 'Price'}, inplace=True) orders = get_mfi_strategy(df_mfi) orders.to_csv('./output/mfi_orders.csv') # #df_mfi.reset_index(inplace=True) # # Plot raw IBM values ax = df_mfi['Price'].plot(title="Relative Strength Index", label='IBM') df_mfi['mfi'].plot(label='mfi', color='black', ax=ax) #TODO plot on secondary axis #df_mfi[['rm_gain', 'rm_loss']].plot(label='Rolling Loss and Gain', color='cyan', ax=ax) # Plot vertical lines for shorts for i in range(0, len(orders)): if (orders.ix[i]['Type'] == 'Short') and (orders.ix[i]['mfi_Strat'] == 'BUY'): ax.axvline(orders.ix[i]['Date'], color='r', linestyle='solid') elif (orders.ix[i]['Type'] == 'Short') and (orders.ix[i]['mfi_Strat'] == 'SELL'): ax.axvline(orders.ix[i]['Date'], color='black', linestyle='solid') elif (orders.ix[i]['Type'] == 'Long') and (orders.ix[i]['mfi_Strat'] == 'BUY'): ax.axvline(orders.ix[i]['Date'], color='green', linestyle='solid') elif (orders.ix[i]['Type'] == 'Long') and (orders.ix[i]['mfi_Strat'] == 'SELL'): ax.axvline(orders.ix[i]['Date'], color='black', linestyle='solid') else: pass # Add axis labels and legend ax.set_xlabel("Date") ax.set_ylabel("Price") ax.legend(loc='lower left', labels=['IBM', 'MFI']) # # #plt.show() fig = ax.get_figure() fig.savefig('output/mfi_strategy.png') # #prep orders for market simulator orders['Shares'] = 100 orders.set_index('Date', inplace=True) orders[['Symbol', 'Order', 'Shares']].to_csv('./output/mfi_orders.csv') # #send order to marketsims ms.sims_output(sv=10000, of="./output/orders.csv", strat_name='mfi')
def test_run(): in_dates = pd.date_range( '2007-12-31', '2009-12-31') #Add in sample and out of sample dates out_dates = pd.date_range( '2009-12-31', '2011-12-31') #Add in sample and out of sample dates symbols = list(['IBM']) df = get_data(symbols, out_dates, addSPY=True) df_rsi = df.copy() df_rsi.drop(['SPY'], axis=1, inplace=True) #COMPUTE MY STRATEGY - RELATIVE STRENGTH INDEX (RSI) & RELATIVE STRENGTH (RS) # 1. Compute change df_rsi['change'] = df_rsi['IBM'].shift(1) - df_rsi['IBM'] df_rsi['change_prop'] = df_rsi['IBM'].shift(1) / df_rsi['IBM'] # 2. Compute Gain and Loss df_rsi['gain'] = df_rsi['change'].apply(lambda x: abs(x) if x > 0 else 0) df_rsi['loss'] = df_rsi['change'].apply(lambda x: abs(x) if x < 0 else 0) # 3. Compute rolling mean Average Gain and Loss # #lower window to increase the sensitivity df_rsi['rm_gain'] = get_rolling_mean(df_rsi['gain'], window=20) df_rsi['rm_loss'] = get_rolling_mean(df_rsi['loss'], window=20) # 5. Compute RS df_rsi['rs'] = df_rsi['rm_gain'] / df_rsi['rm_loss'] # 6. Compute RSI df_rsi['rsi'] = df_rsi['rs'].apply(lambda x: 100 if x == 0.0 else 100 - (100 / (x + 1))) # 7 Add signals for buy and sell #df_rsi.columns =['Price', 'change', 'gain', 'loss', 'rm_gain', 'rm_loss', 'rs', 'rsi'] orders = get_rsi_strategy(df_rsi) orders.to_csv('./output/rsi_orders (in sample).csv') #df_rsi.reset_index(inplace=True) # Plot raw IBM values ax = df_rsi['IBM'].plot(title="Relative Strength Index", label='IBM') df_rsi['rsi'].plot(label='RSI', color='black', ax=ax) #TODO plot on secondary axis #df_rsi[['rm_gain', 'rm_loss']].plot(label='Rolling Loss and Gain', color='cyan', ax=ax) # Plot vertical lines for shorts for i in range(0, len(orders)): if (orders.ix[i]['Type'] == 'Short') and (orders.ix[i]['RSI_Strat'] == 'BUY'): ax.axvline(orders.ix[i]['Date'], color='r', linestyle='solid') elif (orders.ix[i]['Type'] == 'Short') and (orders.ix[i]['RSI_Strat'] == 'SELL'): ax.axvline(orders.ix[i]['Date'], color='black', linestyle='solid') elif (orders.ix[i]['Type'] == 'Long') and (orders.ix[i]['RSI_Strat'] == 'BUY'): ax.axvline(orders.ix[i]['Date'], color='green', linestyle='solid') elif (orders.ix[i]['Type'] == 'Long') and (orders.ix[i]['RSI_Strat'] == 'SELL'): ax.axvline(orders.ix[i]['Date'], color='black', linestyle='solid') else: pass # Add axis labels and legend ax.set_xlabel("Date") ax.set_ylabel("Price") ax.legend(loc='lower left', labels=['IBM', 'RSI']) # # #plt.show() fig = ax.get_figure() fig.savefig('output/rsi_strategy (in sample).png') # #prep orders for market simulator orders['Shares'] = 100 orders.set_index('Date', inplace=True) orders[['Symbol', 'Order', 'Shares']].to_csv('./output/orders.csv') # #send order to marketsims ms.sims_output(sv=10000, of="./output/orders.csv", strat_name='RSI')
sv=10000) # testing step #df_trades.reset_index(inplace=True) df_trades.dropna(inplace=True) #print df_trades if verbose: print iteration with open('./Orders/%s_orders.csv' % symbol, 'w+') as csvfile: fieldnames = ['Date', 'Symbol', 'Order', 'Shares'] writer = csv.writer(csvfile, delimiter=',') writer.writerow(fieldnames) for i in range(0, len(df_trades)): trade_dt = df_trades.index[i].strftime('%Y-%m-%d') if df_trades.ix[i, 'action'] == 1: # Date, Symbol, Order, Shares writer.writerow([trade_dt, symbol, 'BUY', 100]) elif df_trades.ix[i, 'action'] == 2: writer.writerow([trade_dt, symbol, 'SELL', 100]) else: pass # 5) Run orders through market simulators and output back testing graph sims_output(sv=10000, of='./Orders/%s_orders.csv' % symbol, gen_plot=True, symbol=symbol, strat_name='Strategy_%s' % (symbol))
# 3) Send test and train data to model pred_train_Y, pred_test_Y = SendtoModel(train_df=train, train_price=train_data['price_norm'], test_df=test, test_price=test_data['price_norm'], model='knn', symbol=sym, verbose=True, k=3) # 4) Create orders from predictions by merging predicted Y-returns with SPY-dates # a) in sample returns_train_df = pd.concat([is_spy_df, pd.DataFrame(pred_train_Y, index = train.index, columns=['predY_returns'])], axis=1) create_5day_orders(returns_train_df, sym=sym, type='insample') #plot_strategy(price=is_df[sym], of='./Orders/%s_knn_orders_5day_insample.csv' % sym[0], name='%s_in_sample' % sym[0]) # b) out of sample returns_test_df = pd.concat([oos_spy_df, pd.DataFrame(pred_test_Y, index = test.index, columns=['predY_returns'])], axis=1) create_5day_orders(returns_test_df, sym=sym, type='outsample') # plot_strategy(price=oos_df[sym], of='./Orders/%s_knn_orders_5day_outsample.csv' % sym[0], name='%s_out_sample' % sym[0]) #create_rolling_orders(pred_return_df, sym=sym) #TODO extra credit # 5) Run orders through market simulators and output back testing graph sims_output(sv=start_val, of='./Orders/%s_knn_orders_5day_insample.csv' % sym[0], gen_plot=True, symbol=sym[0], strat_name='5day_KNN_%s_%s'% (sym[0], 'insample')) # sims_output(sv=start_val, of='./Orders/%s_knn_orders_5day_outsample.csv' % sym[0], symbol=sym[0], # gen_plot=True, strat_name='5day_KNN_%s_%s'% (sym[0], 'outsample'))