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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值