Advertisement
den4ik2003

Untitled

Apr 4th, 2024
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. def parse_events(path_to_logs):
  2.     list_of_events_dicts = []
  3.     count_of_decode_errors = 0
  4.    
  5.     with open(path_to_logs, 'r') as logs:
  6.         rows = logs.readlines()
  7.         for i in range(len(rows)):
  8.             if re.search('Account order event', rows[i]):
  9.                 if i + 1 < len(rows):
  10.                     try:
  11.                         event = json.loads(re.sub(r'(LIMIT_MAKER")', r'\1,', rows[i+1][1:].strip()))
  12.                         if type(event) is dict:
  13.                             list_of_events_dicts.append(event)
  14.                     except json.JSONDecodeError:
  15.                         count_of_decode_errors += 1
  16.    
  17.     return (list_of_events_dicts, count_of_decode_errors)
  18.  
  19.  
  20. def create_events_csv(path_to_logs, name='mm_events.csv'):
  21.     events, errors_count = parse_events(path_to_logs)
  22.     print(f'events rows = {len(events)}; errors count = {errors_count}')
  23.    
  24.     df = pd.DataFrame(events)
  25.     df.drop(['symbol', 'type', 'is_maker', 'average_price', 'filled_quantity'], axis=1, inplace=True)
  26.    
  27.     for float_column in ['amount', 'price', 'quantity']:
  28.         df[float_column] = df[float_column].astype(float)
  29.        
  30.     for str_column in ['status', 'side']:
  31.         df[str_column] = df[str_column].astype('string')
  32.    
  33.     df['create_time'] = pd.to_datetime(df['create_time'] * 1e6).dt.round('1s')
  34.     df['update_time'] = pd.to_datetime(df['update_time'] * 1e6).dt.round('1s')
  35.    
  36.     df = df.sort_values('update_time')
  37.     df.to_csv(name, index=False)
  38.  
  39.     return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement