def strategy(price_data, signal_a, signal_b, window_1=24 * 7, window_2=24 * 7, buy_sell_fee=0.0075, pnl_0=1.0): sent_score = ah.nb_calc_sentiment_score_b(signal_a, signal_b, window_1, window_2) pnl = ah.nb_backtest_a(price_data, sent_score, 1.0, buy_sell_fee) return pnl
#%% # define the window size for the sentiment score calculation n_days = 7 window_size = 24 * n_days # in hours # generate the sentiment score sent_score = ah.nb_calc_sentiment_score_a(aug_signal_bullish, aug_signal_bearish, window_size, window_size) # define some parameters for the backtest start_pnl = 1.0 buy_sell_fee = 0.0075 # run the backtest pnl = ah.nb_backtest_a(price_data, sent_score, start_pnl, buy_sell_fee) #%% [markdown] # # Compare various windows sizes #%% sent_score = ah.nb_calc_sentiment_score_a(aug_signal_bullish, aug_signal_bearish, 1, 2) pnl = ah.nb_backtest_a(price_data, sent_score, 1.0, 0.0075) #%% sent_score = ah.nb_calc_sentiment_score_a(aug_signal_bullish, aug_signal_bearish, 7 * 24, 7 * 24) pnl = ah.nb_backtest_a(price_data, sent_score, 1.0, 0.0075) #%%
# get the signals we're interested in aug_signal_a = aug_data[:, aug_topics_inv["Positive"]].astype(np.float64) aug_signal_b = aug_data[:, aug_topics_inv["Bearish"]].astype(np.float64) sent_score = ah.nb_calc_sentiment_score_c(aug_signal_a, aug_signal_b, 28 * 24, 14 * 24) date_time = np.array( [datetime.datetime.utcfromtimestamp(t).isoformat() for t in t_price_data]) # define some parameters for the backtest start_pnl = 1.0 buy_sell_fee = 0.0075 # run the backtest pnl = ah.nb_backtest_a(price_data, sent_score, start_pnl, buy_sell_fee) # set up the figure fig, ax = plt.subplots(3, 1, sharex=True, sharey=False) # initialise some labels for the plot datenum_aug_data = [ md.date2num(datetime.datetime.fromtimestamp(el)) for el in t_aug_data ] datenum_price_data = [ md.date2num(datetime.datetime.fromtimestamp(el)) for el in t_price_data ] # plot stuff ax[0].grid(linewidth=0.4) ax[1].grid(linewidth=0.4) ax[2].grid(linewidth=0.4) ax[0].plot(datenum_price_data, price_data, linewidth=0.5) ax[1].plot(datenum_aug_data, sent_score, linewidth=0.5)
# It will give 8649 NLP values calculated from 2017 until the beginning of 2019 #%% #%% # for each combination of signals, generate PNL for the last period in data total = np.zeros(shape=(93, 93)) print("calculating... might take a minute or two...") for i in range(0, len(all_topics)): for j in range(0, len(all_topics)): sent_score = ah.nb_calc_sentiment_score_b(all_topics[i], all_topics[j], ra_win_size_short=24 * 7, ra_win_size_long=24 * 7) pnl = ah.nb_backtest_a(price_data, sent_score, 1.0, buy_sell_fee=0.0075) total[i][j] = pnl[-1] #print("Row " + str(i+1) + " out of 93...") print("done") #%% #%% [markdown] # ### Impossible to see all 8649 values # Chose top 30 #%% # get all PNL in a dataframe data = pd.DataFrame(total).rename(columns=aug_topics, index=aug_topics) # given all combinations of signals, show the combinations that yield the highest PNL
# PNL of various moving window size for a given combination of topics #%% [markdown] # # Test strategies with adding noise to moving windows #%% sensit = 0.01 aug_signal_a = aug_data[:, aug_topics_inv["Positive"]].astype(np.float64) aug_signal_b = aug_data[:, aug_topics_inv["Bearish"]].astype(np.float64) short_win = 26 * 24 long_win = 7 * 24 sent_score = ah.nb_calc_sentiment_score_rand_a(aug_signal_a, aug_signal_b, short_win, long_win, sensit) a = ah.nb_backtest_a(price_data, sent_score, start_pnl=1, buy_sell_fee=0.0075) for i in range(1, 10): short_win_ran = np.int64( np.round(np.random.normal(short_win, np.float64(short_win) * 0.01))) long_win_ran = np.int64( np.round(np.random.normal(long_win, np.float64(long_win) * 0.01))) #short_win_ran, long_win_ran = short_win, long_win sent_score = ah.nb_calc_sentiment_score_rand_a(aug_signal_a, aug_signal_b, short_win_ran, long_win_ran, sensit) cum_ret = ah.nb_backtest_a(price_data, sent_score, start_pnl=1,