def test_variance_of_slope_sums(): """ Used to check the variance of the values formt he slopesum """ ticker = 'GOOG' main_df = pd.read_pickle(settings.settings_dict['stock_data_path']) main_df = sample_slopes.create_slope_sum(main_df) slope_sums = main_df[ticker + "slope_sum"] print np.mean(main_df[ticker + "slope_sum"]) print np.std(main_df[ticker + "slope_sum"]) std = pd.rolling_std(slope_sums, window=20) _, ax2 = plt.subplots() ax2.plot(slope_sums) ax2.plot(slope_sums + std) ax2.plot(slope_sums - std) plt.legend(['Slope_Sum ', 'Slope_Sum +1 Std', 'Slope_Sum -1 Std']) plt.title(ticker + ' varrience of slope sum') plt.show()
def test_slope_sum_stock_price(): """ Used to check the variance of the values formt he slopesum makes sure the slope sum is normially distribueted """ ticker = 'FB' main_df = pd.read_pickle(settings.settings_dict['stock_data_path']) main_df = sample_slopes.create_slope_sum(main_df) slope_sums = main_df[ticker + "slope_sum"] print np.mean(main_df[ticker + "slope_sum"]) print np.std(main_df[ticker + "slope_sum"]) # test for normality assert jarque_bera(main_df[ticker + "slope_sum"])[1] < .005 # std = pd.rolling_std(slope_sums, window=20) plt.figure(1) plt.subplot(211) plt.plot(main_df[ticker + "CLS"]) plt.title(str(ticker) + ' Price and Slope Sum') plt.subplot(212) plt.plot(slope_sums) plt.show()
def test_sample_slopes(): data = { 'col1CHG': [3, 3, 4, 5, 7, 8, 7, 6, 5, 4], 'col2CHG': [6, 5, 5, 6, 7, 6, 4, 3, 3, 8], 'col3CHG': [7, 6, 4, 6, 4, 2, 4, 5, 6, 5], 'col4CHG': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 'col5CHG': [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], 'col6CHG': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 'col7CLS': [4, 4, 4, 4, 4, 4, 4, 4, 4, 4], 'col8CLS': [4, 4, 4, 4, 4, 4, 4, 4, 4, 4], 'col9CLS': [4, 4, 4, 4, 4, 4, 4, 4, 4, 4], } stock_data = pd.DataFrame(data=data) print stock_data new_df_columns = sample_slopes.create_slope_sum(stock_data).columns counter = 0 for column in new_df_columns: counter += 1 # print new_df_columns assert counter == 13 assert list(new_df_columns) == [ u'col1CHG', u'col2CHG', u'col3CHG', u'col4CHG', u'col5CHG', u'col6CHG', u'col7CLS', u'col8CLS', u'col9CLS', u'col2slope_sum', u'col3slope_sum', u'col4slope_sum', u'col5slope_sum' ]
def test_check_if_prior_days_have_a_buy_signal(): """ makes sure it can look backwards and pick out the 1 and the index """ close = [ 1.2, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.4, 2.1, 2.6, 2.7, 2.1, 2.0, 1.6, 1.9, 1.2, 1.6, 2, 2.1 ] bid_stream = [1, 1, 0, 1, 0, 0, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ticker = 'FB' main_df = pd.read_pickle(settings.settings_dict['stock_data_path']) main_df = sample_slopes.create_slope_sum(main_df) Back_Test = back_test.BackTest(main_df, settings.settings_dict['model_path']) index = 5 is_there_a_one, close_value = Back_Test.check_if_prior_days_have_a_buy_signal( bid_stream, close, index) while not is_there_a_one: index = index - 1 is_there_a_one, close_value = Back_Test.check_if_prior_days_have_a_buy_signal( bid_stream, close, index) assert [is_there_a_one, close_value] == [True, 1.7]
def test_different_lengths_of_objects(): """ This test is used to try out the returns calculator on the stock market data """ ticker = 'FB' main_df = pd.read_pickle(settings.settings_dict['stock_data_path']) main_df = sample_slopes.create_slope_sum(main_df) Back_Test = back_test.BackTest(main_df, settings.settings_dict['model_path']) y_values = sample_slopes.generate_target_values(main_df, 18, ticker + "CLS", 2) x_values = sample_slopes.create_batch_of_slopes(main_df, ticker + 'CLS', 18, y_values[1]) array_of_batches = Back_Test.create_batch_of_slopes( main_df, ticker + 'slope_sum', 18, y_values[1]) print array_of_batches, ' here is len of array_of_batch' print Back_Test.append_list_of_buy_sells(array_of_batches, ticker + "slope_sum") print "algorithm dfa ", sum( Back_Test.take_bid_stream_calculate_profit(ticker + "bid_stream", 18, 2)), ' alogritmsss' print "percent change", Back_Test.calculate_holding_profit( ticker + "CLS", 18, 2),
def test_on_market_data_single_stock(): """ This test is used to try out the returns calculator on the stock market data """ main_df = pd.read_pickle(settings.settings_dict['stock_data_path']) main_df = sample_slopes.create_slope_sum(main_df) Back_Test = back_test.BackTest(main_df, settings.settings_dict['model_path']) y_values = sample_slopes.generate_target_values(main_df, 18, 'FBCLS', 2) x_values = sample_slopes.create_batch_of_slopes(main_df, 'FBCLS', 18, y_values[1]) array_of_batches = Back_Test.create_batch_of_slopes( main_df, 'FBslope_sum', 18, y_values[1]) print array_of_batches, ' here is lensss' print Back_Test.append_list_of_buy_sells(array_of_batches, "FBslope_sum") print "algorithm ", sum( Back_Test.take_bid_stream_calculate_return("FBbid_stream", 18, 2)) * 100, '%' print "log return ", sum( Back_Test.test_calculate_holding_log_return('FBCLS')) * 100, '%' print "percent change", Back_Test.calculate_holding_percent_change_return( "FBCLS") * 100, '%'
def test_on_array_of_market(): """ This test is used to try out the returns calculator on the stock market data """ tickers = [ "GOOG", "FB", "INTC", 'TSM', "CSCO", "ORCL", "NVDA", "SAP", "IBM", "ADBE", "TXN", "AVGO", "CRM", "QCOM", "MU", "BIDU", "ADP", "VMW", "ATVI", "AMAT", "INTU", "CTSH", "EA", "EA", "NXPI", "INFY" ] # tickers = ["GOOG", "FB", "INTC", 'TSM', "CSCO"] main_df = pd.read_pickle(settings.settings_dict['stock_data_path']) main_df = sample_slopes.create_slope_sum(main_df) Back_Test = back_test.BackTest(main_df, settings.settings_dict['model_path']) with open('files/testing_files/return_output.csv', 'w') as f: for ticker in tickers: y_values = sample_slopes.generate_target_values( main_df, 18, ticker + "CLS", 2) x_values = sample_slopes.create_batch_of_slopes( main_df, ticker + 'CLS', 18, y_values[1]) array_of_batches = Back_Test.create_batch_of_slopes( main_df, ticker + 'slope_sum', 18, y_values[1]) Back_Test.append_list_of_buy_sells(array_of_batches, ticker + "slope_sum") algorithm_return = sum( Back_Test.take_bid_stream_calculate_return( ticker + "bid_stream", 18, 2)) * 100 log_return = sum( Back_Test.test_calculate_holding_log_return(ticker + 'CLS')) * 100 percent_change = Back_Test.calculate_holding_percent_change_return( ticker + "CLS") * 100 print "algorithm ", algorithm_return, '%' print "log return ", log_return, ' %' print "percent change", percent_change, '%' f.write(ticker + ',' + str(algorithm_return) + ',' + str(log_return) + ',' + str(percent_change) + '\n')