def cross_validation_gold(df, lower_bound, higher_bound, step_length, leng_of_training, leng_of_test, type): Interval = 10 error = [] for delta in np.linspace(lower_bound, higher_bound, step_length): oneerror = 0 for count in range(1, 9): x1 = df[(Interval * (count - 1)):(Interval * (count - 1) + leng_of_training)] if type == "L1T": filtered = l1tf(x1, (delta)) elif type == "L1C": filtered = l1ctf(x1, (delta)) else: filtered = hp(x1, (delta)) estimate = filtered[(leng_of_training - leng_of_test):leng_of_training] y1 = list( df[(Interval * (count - 1) + leng_of_training):(Interval * (count - 1) + (leng_of_training + leng_of_test))]) newerror = [pow(y1[i] - estimate[i], 2) for i in range(0, len(y1))] oneerror = oneerror + sum(newerror) error.append(oneerror) np.array(error) plt.plot(np.linspace(lower_bound, higher_bound, step_length), error) plt.legend([type], fontsize=20) plt.xlabel("lambda") plt.ylabel('Error') plt.title('CrossValidation') plt.show() return np.linspace(lower_bound, higher_bound, step_length)[error.index(min(error))]
def l1(pocetak, kraj): # uvoz podataka iz datoteke with open('NYSE_prices_daily.csv') as f: lines = list(csv.reader(f)) f.close() # broj_dionica = len(lines[0]) - 1 max_lambda = 1000 br = 1 firstCol = 501 for stupac in range(firstCol, 571): # drugi broj mora biti broj_dionica+1 x = pd.Series(np.asarray([float(e[stupac]) for e in lines[pocetak:kraj]])) for delta in range(1, max_lambda+1): # drugi broj mora biti konacna_delta+1 filter_data = l1tf(x, delta) nagib = filter_data[filter_data.size-1] / filter_data[filter_data.size-2] if nagib > 1: zeros[stupac-firstCol][delta - 1] = 1 # zeros[stupac-1][delta-1] = 1 elif nagib < 1: zeros[stupac-firstCol][delta - 1] = -1 # zeros[stupac - 1][delta - 1] = -1 else: zeros[stupac-firstCol][delta - 1] = 0 # zeros[stupac - 1][delta - 1] = 0 print(lines[0][stupac] + " obraden " + str(br)) br += 1 return zeros
def estimate_expected_returns(data): max_lambda = 1000 stock_num = len(data[0]) days_num = len(data) filtered_data = [[0] * max_lambda for _ in range(stock_num) ] # stock_num-1 ako zanemarujemo prvi stupac for col in range(1, stock_num + 1): # promijeniti u 0? x = pd.Series(np.asarray([float(e[col]) for e in data[1:days_num]])) for delta in range(1, max_lambda + 1): filtered_stock = l1tf(x, delta) slope = filtered_stock[filtered_stock.size - 1] / filtered_stock[filter_stock.size - 2] if slope > 1: filtered_data[col - 1][delta - 1] = 1 elif slope < 1: filtered_data[col - 1][delta - 1] = -1 else: filtered_data[col - 1][delta - 1] = 0 returns = [] for row in filtered_data: returns.append(np.mean(row)) del filtered_data return returns
alltype = ["L1T", "L1C", "L2", "L1TC"] allbestlambda = [best_lamda_L1T, best_lamda_L1C, best_lamda_L2] result1 = calculate_return(df, alltype[0], allbestlambda[0], time) result2 = calculate_return(df, alltype[1], allbestlambda[1], time) result3 = calculate_return(df, alltype[2], allbestlambda[2], time) result4 = calculate_return(df, alltype[3], allbestlambda[0], time, allbestlambda[1]) newfilter1 = np.array(result1[3]) newfilter2 = np.array(result2[3]) newfilter3 = np.array(result3[3]) newfilter4 = np.array(result4[3]) close_15to17 = df['log Close'].loc["2019-01-01":"2019-04-04"] filtered = l1tf(close_15to17, best_lamda_L1T) filtered2 = l1ctf(close_15to17, best_lamda_L1C) gc.collect() filtered3 = hp(close_15to17, best_lamda_L2) filtered4 = l1tccf(close_15to17, best_lamda_L1T, best_lamda_L1C) df["trend_L1T"] = np.hstack((filtered, newfilter1)) df["trend_L1C"] = np.hstack((filtered2, newfilter2)) df["trend_L2"] = np.hstack((filtered3, newfilter3)) df["trend_L1TC"] = np.hstack((filtered4, newfilter4)) plt.subplot(221) plt.plot(df["trend_L1T"], linewidth='2.5') plt.plot(df["log Close"]) #plt.show() plt.subplot(222)
def calculate_return(df, TYPE, best_lambda1, time, best_lambda2=0): close_2015 = time[0] close_2016_Actual = time[1] close_2016 = np.log(close_2016_Actual) close_2017_Actual = time[2] close_2017 = np.log(close_2017_Actual) close_2018_Actual = time[3] close_2018 = np.log(close_2018_Actual) close_2019_Actual = time[4] close_2019 = np.log(close_2019_Actual) start = close_2015 end = close_2016 close_all = np.hstack((start, end)) close_end_Actual = close_2016_Actual inter = 10 if TYPE == "L1T": filtered = l1tf(start, best_lambda1) elif TYPE == "L1C": filtered = l1ctf(start, best_lambda1) elif TYPE == "L1TC": filtered = l1tccf(start, best_lambda1, best_lambda2) elif TYPE == "Wave": filtered = wave(start) else: filtered = hp(start, best_lambda1) filter_derivative = [(filtered[i] - filtered[i - 1]) for i in range(1, len(filtered))] filter_derivative.insert(0, filter_derivative[0]) average_filtered_derivative = [ np.mean(filter_derivative[(i - inter):i]) for i in range(inter, len(filter_derivative)) ] temp_lis = np.array([0] * inter) average_filtered_derivative = np.hstack( (temp_lis, average_filtered_derivative)) size_start = len(start) size_end = len(end) temp_lis2 = np.array([0] * size_end) average_filtered_derivative = np.hstack( (average_filtered_derivative, temp_lis2)) for i in range(0, size_end): if TYPE == "L1T": filtered_updating = l1tf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1C": filtered_updating = l1ctf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1TC": filtered_updating = l1tccf(close_all[0:size_start + i + 1], best_lambda1, best_lambda2) elif TYPE == "Wave": filtered_updating = wave(close_all[0:size_start + i + 1]) else: filtered_updating = hp(close_all[0:size_start + i + 1], best_lambda1) filter_derivative = [(filtered_updating[i] - filtered_updating[i - 1]) for i in range(1, len(filtered_updating))] target = np.mean(filter_derivative[-inter - 1:-1]) try: average_filtered_derivative[size_start + i] = target except: continue # updating zt n = 60 size = len(close_all) st = [0] * size for date in range(n, size): total = 0 for i in range(0, n - 1): for j in range(i + 1, n): value = judge(close_all[date - i], close_all[date - j]) total = total + value st[date] = total # st_normal = list(map(lambda num: num * 2 / (n * n + 1), st)) std = math.sqrt((n * (n - 1) * (2 * n + 5)) / 18) zt = list(map(lambda num: num / std, st)) confidence = [0] * size for i in range(len(zt)): if (zt[i] > 1.96): confidence[i] = 1 elif (zt[i] < -1.96): confidence[i] = -1 average_filtered_derivative_end = average_filtered_derivative[-len(end):] confidence_end = confidence[-len(end):] bp11 = best_period(close_end_Actual, average_filtered_derivative_end, confidence_end) bp31 = best_period3(close_end_Actual, average_filtered_derivative_end) # print(bp11,bp11_r,bp31,bp31_r) # print(100000/close_2018_Actual[0]*close_2018_Actual[-1]) # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Do for 2nd start = close_2016 end = close_2017 close_all = np.hstack((start, end)) close_end_Actual = close_2017_Actual if TYPE == "L1T": filtered = l1tf(start, best_lambda1) elif TYPE == "L1C": filtered = l1ctf(start, best_lambda1) elif TYPE == "L1TC": filtered = l1tccf(start, best_lambda1, best_lambda2) elif TYPE == "Wave": filtered = wave(start) else: filtered = hp(start, best_lambda1) filter_derivative = [(filtered[i] - filtered[i - 1]) for i in range(1, len(filtered))] filter_derivative.insert(0, filter_derivative[0]) average_filtered_derivative = [ np.mean(filter_derivative[(i - inter):i]) for i in range(inter, len(filter_derivative)) ] temp_lis = np.array([0] * inter) average_filtered_derivative = np.hstack( (temp_lis, average_filtered_derivative)) size_start = len(start) size_end = len(end) temp_lis2 = np.array([0] * size_end) average_filtered_derivative = np.hstack( (average_filtered_derivative, temp_lis2)) for i in range(0, size_end): if TYPE == "L1T": filtered_updating = l1tf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1C": filtered_updating = l1ctf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1TC": filtered_updating = l1tccf(close_all[0:size_start + i + 1], best_lambda1, best_lambda2) elif TYPE == "Wave": filtered_updating = wave(close_all[0:size_start + i + 1]) else: filtered_updating = hp(close_all[0:size_start + i + 1], best_lambda1) filter_derivative = [(filtered_updating[i] - filtered_updating[i - 1]) for i in range(1, len(filtered_updating))] target = np.mean(filter_derivative[-inter - 1:-1]) try: average_filtered_derivative[size_start + i] = target except: continue # updating zt n = 60 size = len(close_all) st = [0] * size for date in range(n, size): total = 0 for i in range(0, n - 1): for j in range(i + 1, n): value = judge(close_all[date - i], close_all[date - j]) total = total + value st[date] = total # st_normal = list(map(lambda num: num * 2 / (n * n + 1), st)) std = math.sqrt((n * (n - 1) * (2 * n + 5)) / 18) zt = list(map(lambda num: num / std, st)) confidence = [0] * size for i in range(len(zt)): if (zt[i] > 1.96): confidence[i] = 1 elif (zt[i] < -1.96): confidence[i] = -1 average_filtered_derivative_end = average_filtered_derivative[-len(end):] confidence_end = confidence[-len(end):] bp12 = best_period(close_end_Actual, average_filtered_derivative_end, confidence_end) bp32 = best_period3(close_end_Actual, average_filtered_derivative_end) # print(bp12,bp12_r,bp32,bp32_r) # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Do for 3rd start = close_2017 end = close_2018 close_all = np.hstack((start, end)) close_end_Actual = close_2018_Actual if TYPE == "L1T": filtered = l1tf(start, best_lambda1) elif TYPE == "L1C": filtered = l1ctf(start, best_lambda1) elif TYPE == "L1TC": filtered = l1tccf(start, best_lambda1, best_lambda2) elif TYPE == "Wave": filtered = wave(start) else: filtered = hp(start, best_lambda1) filter_derivative = [(filtered[i] - filtered[i - 1]) for i in range(1, len(filtered))] filter_derivative.insert(0, filter_derivative[0]) average_filtered_derivative = [ np.mean(filter_derivative[(i - inter):i]) for i in range(inter, len(filter_derivative)) ] temp_lis = np.array([0] * inter) average_filtered_derivative = np.hstack( (temp_lis, average_filtered_derivative)) size_start = len(start) size_end = len(end) temp_lis2 = np.array([0] * size_end) average_filtered_derivative = np.hstack( (average_filtered_derivative, temp_lis2)) for i in range(0, size_end): if TYPE == "L1T": filtered_updating = l1tf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1C": filtered_updating = l1ctf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1TC": filtered_updating = l1tccf(close_all[0:size_start + i + 1], best_lambda1, best_lambda2) elif TYPE == "Wave": filtered_updating = wave(close_all[0:size_start + i + 1]) else: filtered_updating = hp(close_all[0:size_start + i + 1], best_lambda1) filter_derivative = [(filtered_updating[i] - filtered_updating[i - 1]) for i in range(1, len(filtered_updating))] target = np.mean(filter_derivative[-inter - 1:-1]) try: average_filtered_derivative[size_start + i] = target except: continue # updating zt n = 60 size = len(close_all) st = [0] * size for date in range(n, size): total = 0 for i in range(0, n - 1): for j in range(i + 1, n): value = judge(close_all[date - i], close_all[date - j]) total = total + value st[date] = total # st_normal = list(map(lambda num: num * 2 / (n * n + 1), st)) std = math.sqrt((n * (n - 1) * (2 * n + 5)) / 18) zt = list(map(lambda num: num / std, st)) confidence = [0] * size for i in range(len(zt)): if (zt[i] > 1.96): confidence[i] = 1 elif (zt[i] < -1.96): confidence[i] = -1 average_filtered_derivative_end = average_filtered_derivative[-len(end):] confidence_end = confidence[-len(end):] bp13 = best_period(close_end_Actual, average_filtered_derivative_end, confidence_end) bp33 = best_period3(close_end_Actual, average_filtered_derivative_end) # print(bp13,bp13_r,bp33,bp33_r) # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Do for 4th start = close_2018 end = close_2019 close_all = np.hstack((start, end)) close_end_Actual = close_2019_Actual if TYPE == "L1T": filtered = l1tf(start, best_lambda1) elif TYPE == "L1C": filtered = l1ctf(start, best_lambda1) elif TYPE == "L1TC": filtered = l1tccf(start, best_lambda1, best_lambda2) elif TYPE == "Wave": filtered = wave(start) else: filtered = hp(start, best_lambda1) filter_derivative = [(filtered[i] - filtered[i - 1]) for i in range(1, len(filtered))] filter_derivative.insert(0, filter_derivative[0]) average_filtered_derivative = [ np.mean(filter_derivative[(i - inter):i]) for i in range(inter, len(filter_derivative)) ] temp_lis = np.array([0] * inter) average_filtered_derivative = np.hstack( (temp_lis, average_filtered_derivative)) size_start = len(start) size_end = len(end) temp_lis2 = np.array([0] * size_end) average_filtered_derivative = np.hstack( (average_filtered_derivative, temp_lis2)) for i in range(0, size_end): if TYPE == "L1T": filtered_updating = l1tf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1C": filtered_updating = l1ctf(close_all[0:size_start + i + 1], best_lambda1) elif TYPE == "L1TC": filtered_updating = l1tccf(close_all[0:size_start + i + 1], best_lambda1, best_lambda2) elif TYPE == "Wave": filtered_updating = wave(close_all[0:size_start + i + 1]) else: filtered_updating = hp(close_all[0:size_start + i + 1], best_lambda1) filter_derivative = [(filtered_updating[i] - filtered_updating[i - 1]) for i in range(1, len(filtered_updating))] target = np.mean(filter_derivative[-inter - 1:-1]) try: average_filtered_derivative[size_start + i] = target except: continue # updating zt n = 60 size = len(close_all) st = [0] * size for date in range(n, size): total = 0 for i in range(0, n - 1): for j in range(i + 1, n): value = judge(close_all[date - i], close_all[date - j]) total = total + value st[date] = total std = math.sqrt((n * (n - 1) * (2 * n + 5)) / 18) zt = list(map(lambda num: num / std, st)) confidence = [0] * size for i in range(len(zt)): if (zt[i] > 1.96): confidence[i] = 1 elif (zt[i] < -1.96): confidence[i] = -1 average_filtered_derivative_end = average_filtered_derivative[-len(end):] confidence_end = confidence[-len(end):] bp1 = int(np.mean([bp11, bp12, bp13])) bp3 = int(np.mean([bp31, bp32, bp33])) Ac_list1 = strategy(average_filtered_derivative_end, 0, bp1, confidence_end) Ac_list3 = strategy3(average_filtered_derivative_end, 0, bp3) print( "######################################################################" ) print(bp11, bp12, bp13) print(bp31, bp32, bp33) print(bp1, bp3) revenue1 = calculate_revenue(Ac_list1, close_2019_Actual, bp1) revenue2 = calculate_revenue(Ac_list3, close_2019_Actual, bp3) benchmark = 100000 / close_2019_Actual[0] * close_2019_Actual[-1] print(calculate_revenue(Ac_list1, close_2019_Actual, bp1)) print(calculate_revenue(Ac_list3, close_2019_Actual, bp3)) print(100000 / close_2019_Actual[0] * close_2019_Actual[-1]) return (revenue1, revenue2, benchmark, filtered_updating)
plt.plot(trend) plt.grid() #Simulate Yt walk = [] for i in range(steps): step = np.random.normal(0, 2) walk.append(trend[i] + step) plt.subplot(322) plt.plot(walk) plt.grid() plt.title("Real Value") walk = np.array(walk) delta = 5 #L1-T filtered = l1tf(walk, delta) plt.subplot(323) plt.title("L1-T filter") plt.plot(filtered) plt.grid() #L1_C filtered = l1ctf(walk, delta) plt.subplot(324) plt.title("L1-C filter") plt.plot(filtered) plt.grid() #L1-tc filtered = l1tccf(walk, 5, 10) plt.subplot(325) plt.title("L1-TC filter") plt.plot(filtered)