Administrator
2025-05-28 d5aa41a4647fe47e72bb21f7dff6962cfe3906be
data_parser/transaction_big_order_parser.py
@@ -194,10 +194,25 @@
    ))
__combined_df_cache = {}
def extract_big_order_of_all(dir_path):
    combined_path = os.path.join(dir_path, 'combined.csv')
    if not os.path.exists(combined_path):
        print("拼接数据不存在")
        return
    codes = extract_big_order_codes(dir_path)
    print("总代码数量:", len(codes))
    for code in codes:
        extract_big_order_of_code(dir_path, code)
def extract_big_order_of_code(dir_path, code):
    """
    提取代码的大单
    @param code:
    @param dir_path: 数据目录
    @param code: 为空表示导出全部
    @return:
    """
@@ -208,21 +223,31 @@
            @return:
            """
        return pd.Series({
            'TotalAmount': group['TotalAmount'].sum(),
            'SecurityID': group['SecurityID'].iloc[0],
            'BuyNo': group['BuyNo'].iloc[0],
            'TotalVolume': group['TotalVolume'].sum(),
            'StartTime': group['StartTime'].iloc[0],
            'StartPrice': group['StartPrice'].iloc[0],
            'TotalAmount': group['TotalAmount'].sum(),
            'EndTime': group['EndTime'].iloc[-1],
            'EndPrice': group['EndPrice'].iloc[-1]
            'EndPrice': group['EndPrice'].iloc[-1],
            'StartTime': group['StartTime'].iloc[0],
            'StartPrice': group['StartPrice'].iloc[0]
        })
    combined_path = os.path.join(dir_path, 'combined.csv')
    if not os.path.exists(combined_path):
        print("拼接数据不存在")
        return
    df = pd.read_csv(combined_path)
    output_path = os.path.join(dir_path, f"big_buy_{code}.csv")
    if os.path.exists(output_path):
        print("路径已存在:", output_path)
        return
    df = __combined_df_cache.get(combined_path)
    if not df:
        df = pd.read_csv(combined_path)
        __combined_df_cache[combined_path] = df
    df_copy = df.copy()
    df_copy = df_copy[df_copy["SecurityID"] == int(code)]
    if code:
        df_copy = df_copy[df_copy["SecurityID"] == int(code)]
    if df_copy.empty:
        print("目标代码对应成交数据为空")
        return
@@ -231,12 +256,37 @@
    grouped = df_copy.groupby(['SecurityID', 'BuyNo'])
    grouped_result = grouped.apply(first_last)
    grouped_result = grouped_result[grouped_result["TotalAmount"] > 500000]
    grouped_result.to_csv(os.path.join(dir_path, f"{code}.csv"))
    print("保存成功")
    # print(grouped_result)
    # 遍历内容
    grouped_result.to_csv(output_path, index=False)
    print(f"保存成功,路径:{output_path}")
def extract_big_order_codes(dir_path):
    """
    导出大单代码
    @param dir_path: 数据目录
    @param code:
    @return:
    """
    combined_path = os.path.join(dir_path, 'combined.csv')
    if not os.path.exists(combined_path):
        print("拼接数据不存在")
        return
    df = pd.read_csv(combined_path)
    df_copy = df.copy()
    if df_copy.empty:
        print("目标代码对应成交数据为空")
        return
    df_copy["SecurityID"] = df_copy["SecurityID"].apply(BigOrderDealParser.code_format)
    # 按SecurityID和BuyNo分组
    grouped = df_copy.groupby(['SecurityID'])
    return set(grouped.groups.keys())
if __name__ == "__main__":
    # pre_process_transactions("E:/测试数据/Transaction_Test.csv")
    # pre_process_ngtsticks("E:/测试数据/NGTSTick_Test.csv")
    # concat_pre_transactions("E:/测试数据/Transaction_Test")
    extract_big_order_of_code("E:/测试数据/Transaction_Test", "000017")
    # extract_big_order_codes("E:/测试数据/Transaction_Test")
    extract_big_order_of_code("E:/测试数据/Transaction_Test")