Advertisement
den4ik2003

Untitled

Apr 16th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. paths = [
  2. '/mnt/ico_data/PRYUSDT-2024-04-03-13-02-00_DEPTH.log',
  3. '/mnt/ico_data/SCOTTYAIUSDT-2024-03-26-22-15-57_DEPTH.log',
  4. '/mnt/ico_data/SPARTAUSDT-2024-03-26-22-14-32_DEPTH.log',
  5. '/mnt/ico_data/MUSKUSDT-2024-04-03-08-56-27_DEPTH.log',
  6. '/mnt/ico_data/HOVUSDT-2024-03-26-22-08-45_DEPTH.log',
  7. '/mnt/ico_data/GGMTUSDT-2024-03-27-09-52-27_DEPTH.log',
  8. '/mnt/ico_data/BXNUSDT-2024-04-02-11-58-18_DEPTH.log'
  9. ]
  10.  
  11. total = []
  12. before_1h = []
  13. after_1h = []
  14.  
  15. for path_to_log in paths:
  16. with open(path_to_log) as file:
  17. orderbook = []
  18. is_ask = 'none'
  19. time = ''
  20.  
  21. row = file.readline()
  22. while row:
  23. if row[0] == 'D':
  24. time = int(row[row.find('time: ') + len('time: '):].strip())
  25. is_ask = 'none'
  26. row = file.readline()
  27.  
  28. while row and row[0] != 'D':
  29. if row[0] == 'a':
  30. is_ask = 'ask'
  31. elif row[0] == 'b':
  32. is_ask = 'bid'
  33. else:
  34. try:
  35. d = json.loads(row)
  36. d['time'] = time
  37. d['type'] = is_ask
  38. orderbook.append(d)
  39. except json.JSONDecodeError:
  40. print("error")
  41.  
  42. row = file.readline()
  43.  
  44. df = pd.DataFrame(orderbook)
  45. df['price'] = df['price'].astype('float')
  46. df['quantity'] = df['quantity'].astype('float')
  47. df['time'] = pd.to_datetime(df['time']*1e6)
  48.  
  49. df['size'] = df['price'] * df['quantity']
  50. df = df.groupby(by=['time', 'type']).sum().reset_index()[['time', 'type', 'size']]
  51. trades_start_time = df[df['type'] == 'bid'].iloc[0]['time']
  52.  
  53. print(path_to_log.split('/')[3].split('-')[0])
  54. print('-------------------------')
  55. print('depths data time (hours) = ', np.round((np.max(df['time']) - trades_start_time).total_seconds() / 3600, 1))
  56.  
  57. end_time = min(trades_start_time + datetime.timedelta(hours=36), np.max(df['time']))
  58. df = df[(df['time'] >= trades_start_time) & (df['time'] <= end_time)].reset_index(drop=True)
  59.  
  60. ob_before_1h = df[df['time'] <= trades_start_time + datetime.timedelta(hours=1)]
  61. ob_after_1h = df[df['time'] > trades_start_time + datetime.timedelta(hours=1)]
  62.  
  63. df_asks = df[df['type'] == 'ask'].reset_index()
  64. df_bids = df[df['type'] == 'bid'].reset_index()
  65.  
  66. df_asks_before_1h = ob_before_1h[ob_before_1h['type'] == 'ask'].reset_index()
  67. df_bids_before_1h = ob_before_1h[ob_before_1h['type'] == 'bid'].reset_index()
  68.  
  69. df_asks_after_1h = ob_after_1h[ob_after_1h['type'] == 'ask'].reset_index()
  70. df_bids_after_1h = ob_after_1h[ob_after_1h['type'] == 'bid'].reset_index()
  71.  
  72. print("len (asks/bids) = ", len(df_asks), len(df_bids))
  73. print('-------------------------')
  74.  
  75. d = df.groupby(by='time').min().reset_index()
  76. d['delta'] = (d['time'].shift(-1) - d['time']).dt.total_seconds()[:-1]
  77.  
  78. size_mean = np.round((df_asks['size'] + df_bids['size']).mean(), 2)
  79. total.append(size_mean)
  80. print('all time orderbook size mean', size_mean)
  81.  
  82. size_mean = np.round((df_asks_before_1h['size'] + df_bids_before_1h['size']).mean(), 2)
  83. time_mean = np.round(d[d['time'] <= trades_start_time + datetime.timedelta(hours=1)]['delta'].mean(), 3)
  84. before_1h.append((size_mean, time_mean))
  85. print('1 hour orderbook size mean', size_mean)
  86. print('1h order book change time mean', time_mean)
  87.  
  88. size_mean = np.round((df_asks_after_1h['size'] + df_bids_after_1h['size']).mean(), 2)
  89. time_mean = np.round(d[d['time'] > trades_start_time + datetime.timedelta(hours=1)]['delta'].mean(), 3)
  90. after_1h.append((size_mean, time_mean))
  91. print('after 1 hour orderbook size mean', size_mean)
  92. print('after 1h order book change time mean', time_mean)
  93.  
  94. print()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement