def mean_variance(day, dt): df = create_df('data/1707' + day + '#ES.F.GLOB.1709#I#310#SBE##AB#t0.chi#top5.h5') time = np.array(df["time_from_open"]) ask_price = np.array(df["a1"]) bid_price = np.array(df["b1"]) Sa_sq_sum = 0 #reference price squared Sa_sum = 0 #sum of refernce price Sb_sq_sum = 0 Sb_sum = 0 i = 0 while dt * i < time[-1]: S_a = ask_price[np.searchsorted(time, dt * i)] S_b = bid_price[np.searchsorted(time, dt * i)] #sum of sq and sum for asks Sa_sq_sum += S_a**2 Sa_sum += S_a #sum of sq and sum for bids Sb_sq_sum += S_b**2 Sb_sum += S_b i += 1 sigma_a_sq = Sa_sq_sum / i - (Sa_sum / i)**2 sigma_b_sq = Sb_sq_sum / i - (Sb_sum / i)**2 mu_a = (ask_price[-1] - ask_price[0]) / i mu_b = (bid_price[-1] - bid_price[0]) / i return i, mu_a, sigma_a_sq, mu_b, sigma_b_sq
def Empirical_Inentsity(tick_size, depth, dt, day_string): """ tick_size: this si the tick size in the data depth: This is asking for how many levels we want an intensity for dt: Time interval size day_string:the day in form 'dd' we want from out data set of SP500 in july2017 """ #******************Import the data needed for the below. df = create_df('data/1707' + day_string + '#ES.F.GLOB.1709#I#310#SBE##AB#t0.chi#top5.h5') time = np.array(df["time_from_open"]) #Time from market open in nano seconds ask = np.array(df["a1"]) #best ask price avol = np.array(df["av1"]) #volume at best ask avol2 = np.array(df["av2"]) #volume at level 2 bid = np.array(df["b1"]) bvol = np.array(df["bv1"]) bvol2 = np.array(df["bv2"]) #Creat vectors to stor the intensity at each distance from reference price. Optimistic_ask_intensity = [0]*depth Pessimistic_ask_intensity = [0]*depth Optimistic_bid_intensity = [0]*depth Pessimistic_bid_intensity = [0]*depth for i in range(0,depth): Optimistic_ask_intensity[i] = cb.Ask_Opti(time, ask,avol,avol2,i*tick_size,dt) Pessimistic_ask_intensity[i] = cb.Ask_Pessi(time,ask,avol,avol2,i*tick_size,dt) Optimistic_bid_intensity[i] = cb.Bid_Opti(time, bid,bvol,bvol2,i*tick_size,dt) Pessimistic_bid_intensity[i] = cb.Bid_Pessi(time,bid,bvol,bvol2,i*tick_size,dt) #Convert the Above lists into numoy array for math calculations Opti_a = np.array(Optimistic_ask_intensity) Pessi_a = np.array(Pessimistic_ask_intensity) Opti_b = np.array(Optimistic_bid_intensity) Pessi_b = np.array(Pessimistic_bid_intensity) return (Opti_a, Pessi_a, Opti_b, Pessi_b)
]]))) order_hit = True break #THe case if the ref price becomes equal to the quote, again wait for volume if order_hit == False: waiting_time_sum += dt i += 1 return count / waiting_time_sum * 60e9 #this makes the intensity per minute. #******************************Run the Functions here********************* # df = create_df('data/170707#ES.F.GLOB.1709#I#310#SBE##AB#t0.chi#top5.h5') time = np.array(df["time_from_open"]) ask = np.array(df["a1"]) avol = np.array(df["av1"]) avol2 = np.array(df["av2"]) bid = np.array(df["b1"]) bvol = np.array(df["bv1"]) bvol2 = np.array(df["bv2"]) dt = 60e9 A = Ask_Opti(time, ask, avol, avol2, 50, dt) B = Bid_Opti(time, bid, bvol, bvol2, 50, dt) Optimistic_ask_intensity = [0] * 5 Pessimistic_ask_intensity = [0] * 5
# Load the PAst data to back test on data_file_list = [ '17', '18', '19', '20', '21', '24', '25', '26', '27', '28', '31' ] Max_inv = 0 #P0aramteer to check max_inv PnL = np.array([['No_Trades', 'Cash', "maxinv", "inv_at_T"]]) nu_max = 20000 q_max = 10 dt = 60e9 #Loop through each backtest for file in data_file_list: #**************************Process of the raw data************************** df = create_df('data/1707' + str(file) + '#ES.F.GLOB.1709#I#310#SBE##AB#t0.chi#top5.h5') #df = df[:100000] time = np.array(df["time_from_open"]) ask_price = np.array(df["a1"]) ask_vol = np.array(df["av1"]) ask_vol2 = np.array(df["av2"]) bid_price = np.array(df["b1"]) bid_vol = np.array(df["bv1"]) bid_vol2 = np.array(df["bv2"]) #Getting the price per minute, and then calcing rolling vriance S_a = ask_price[0] S_b = bid_price[0] k = 0
from data_load import create_df #enter the days of the files to get results for. data_file_list = ['03','04','05','06','07','10','11','12','13','14','17',\ '18', '19','20','21','24','25','26','27','28','31'] Max_inv=0 #P0aramteer to check max_inv PnL = np.array([['No_Trades','Cash',"maxinv"]]) for j in range(len(data_file_list)): #**************************Process of the raw data************************** df = create_df('data/1707'+str(data_file_list[j])+'#ES.F.GLOB.1709#I#310#SBE##AB#t0.chi#top5.h5') df_small = df[:1000] time = np.array(df["time_from_open"]) ask_price = np.array(df["a1"]) ask_vol = np.array(df["av1"]) ask_vol2 = np.array(df["av2"]) bid_price = np.array(df["b1"]) bid_vol = np.array(df["bv1"]) bid_vol2 = np.array(df["bv2"]) #***************************** PDE Results ************************************* #This does not really need to be inside the loop!!!