量化交易之数字货币篇 - 通过时间戳与方向来合并逐笔成交数据(大单合并)

时间:2023-03-01 19:59:03


import csv
from source_data.tqz_load_source_data import TQZSourceData
import tqdm
from public_module.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator
import datetime

# from public_module.tqz_extern.tools.pandas_operator.pandas_operator import pandas

if __name__ == '__main__':
symbol = 'BTCUSDT'
start_date, end_date = datetime.datetime.strptime("2022-05-03", '%Y-%m-%d').date(), datetime.datetime.strptime("2022-07-24", '%Y-%m-%d').date()

digital_point_counts = 1
qty_digital_point_counts = 3
quoteQty_digital_point_counts = 2

while True:
if start_date > end_date:
break

trades_file = f'F:/tqz_test/{symbol}-trades-{str(start_date)}.csv'
merge_trades_path = f'F:/tqz_test/{symbol}-mergeTrades-{str(start_date)}.csv'

current_timestamp = None
row_data_map = {'buy_qty': 0, 'sell_qty': 0, 'buy_quoteQty': 0, 'sell_quoteQty': 0}
with open(trades_file, 'rt') as update_file:
csv_reader_file = csv.reader(update_file)

for row in tqdm.tqdm(csv_reader_file):

# 1. write row_data & update current_timestamp
if current_timestamp != row[4]:
# 1.1 write to csv
if current_timestamp is not None:
with open(merge_trades_path, 'a', newline="") as _file:
if row_data_map['buy_qty'] != 0:
csv.writer(_file).writerow([
str(round(row_data_map['buy_quoteQty'] / row_data_map['buy_qty'], digital_point_counts)),
str(round(row_data_map['buy_qty'], qty_digital_point_counts)),
str(round(row_data_map['buy_quoteQty'], quoteQty_digital_point_counts)),
current_timestamp,
'true'
])

if row_data_map['sell_qty'] != 0:
csv.writer(_file).writerow([
str(round(row_data_map['sell_quoteQty'] / row_data_map['sell_qty'], digital_point_counts)),
str(round(row_data_map['sell_qty'], qty_digital_point_counts)),
str(round(row_data_map['sell_quoteQty'], quoteQty_digital_point_counts)),
current_timestamp,
'false'
])

row_data_map = {'buy_qty': 0, 'sell_qty': 0, 'buy_quoteQty': 0, 'sell_quoteQty': 0}
else:
with open(merge_trades_path, 'a', newline="") as _file:
csv.writer(_file).writerow(['price', 'qty', 'quoteQty', 'datetime', 'isBuyerMaker'])

# 1.2 update current_timestamp
current_timestamp = row[4]

# 2. update row_data_map
if row[5] == 'true':
row_data_map['buy_qty'] += float(row[2])
row_data_map['buy_quoteQty'] += float(row[3])
else:
row_data_map['sell_qty'] += float(row[2])
row_data_map['sell_quoteQty'] += float(row[3])

start_date += datetime.timedelta(days=1)