Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- paths = [
- '/mnt/ico_data/PRYUSDT-2024-04-03-13-02-00_DEPTH.log',
- '/mnt/ico_data/SCOTTYAIUSDT-2024-03-26-22-15-57_DEPTH.log',
- '/mnt/ico_data/SPARTAUSDT-2024-03-26-22-14-32_DEPTH.log',
- '/mnt/ico_data/MUSKUSDT-2024-04-03-08-56-27_DEPTH.log',
- '/mnt/ico_data/HOVUSDT-2024-03-26-22-08-45_DEPTH.log',
- '/mnt/ico_data/GGMTUSDT-2024-03-27-09-52-27_DEPTH.log',
- '/mnt/ico_data/BXNUSDT-2024-04-02-11-58-18_DEPTH.log'
- ]
- total = []
- before_1h = []
- after_1h = []
- for path_to_log in paths:
- with open(path_to_log) as file:
- orderbook = []
- is_ask = 'none'
- time = ''
- row = file.readline()
- while row:
- if row[0] == 'D':
- time = int(row[row.find('time: ') + len('time: '):].strip())
- is_ask = 'none'
- row = file.readline()
- while row and row[0] != 'D':
- if row[0] == 'a':
- is_ask = 'ask'
- elif row[0] == 'b':
- is_ask = 'bid'
- else:
- try:
- d = json.loads(row)
- d['time'] = time
- d['type'] = is_ask
- orderbook.append(d)
- except json.JSONDecodeError:
- print("error")
- row = file.readline()
- df = pd.DataFrame(orderbook)
- df['price'] = df['price'].astype('float')
- df['quantity'] = df['quantity'].astype('float')
- df['time'] = pd.to_datetime(df['time']*1e6)
- df['size'] = df['price'] * df['quantity']
- df = df.groupby(by=['time', 'type']).sum().reset_index()[['time', 'type', 'size']]
- trades_start_time = df[df['type'] == 'bid'].iloc[0]['time']
- print(path_to_log.split('/')[3].split('-')[0])
- print('-------------------------')
- print('depths data time (hours) = ', np.round((np.max(df['time']) - trades_start_time).total_seconds() / 3600, 1))
- end_time = min(trades_start_time + datetime.timedelta(hours=36), np.max(df['time']))
- df = df[(df['time'] >= trades_start_time) & (df['time'] <= end_time)].reset_index(drop=True)
- ob_before_1h = df[df['time'] <= trades_start_time + datetime.timedelta(hours=1)]
- ob_after_1h = df[df['time'] > trades_start_time + datetime.timedelta(hours=1)]
- df_asks = df[df['type'] == 'ask'].reset_index()
- df_bids = df[df['type'] == 'bid'].reset_index()
- df_asks_before_1h = ob_before_1h[ob_before_1h['type'] == 'ask'].reset_index()
- df_bids_before_1h = ob_before_1h[ob_before_1h['type'] == 'bid'].reset_index()
- df_asks_after_1h = ob_after_1h[ob_after_1h['type'] == 'ask'].reset_index()
- df_bids_after_1h = ob_after_1h[ob_after_1h['type'] == 'bid'].reset_index()
- print("len (asks/bids) = ", len(df_asks), len(df_bids))
- print('-------------------------')
- d = df.groupby(by='time').min().reset_index()
- d['delta'] = (d['time'].shift(-1) - d['time']).dt.total_seconds()[:-1]
- size_mean = np.round((df_asks['size'] + df_bids['size']).mean(), 2)
- total.append(size_mean)
- print('all time orderbook size mean', size_mean)
- size_mean = np.round((df_asks_before_1h['size'] + df_bids_before_1h['size']).mean(), 2)
- time_mean = np.round(d[d['time'] <= trades_start_time + datetime.timedelta(hours=1)]['delta'].mean(), 3)
- before_1h.append((size_mean, time_mean))
- print('1 hour orderbook size mean', size_mean)
- print('1h order book change time mean', time_mean)
- size_mean = np.round((df_asks_after_1h['size'] + df_bids_after_1h['size']).mean(), 2)
- time_mean = np.round(d[d['time'] > trades_start_time + datetime.timedelta(hours=1)]['delta'].mean(), 3)
- after_1h.append((size_mean, time_mean))
- print('after 1 hour orderbook size mean', size_mean)
- print('after 1h order book change time mean', time_mean)
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement