import pandas as pd
import sys
import matplotlib.pyplot as plt
import math
chicang_file = ''
df_chouma = None
def draw_chouma():
    global df_chouma ,chicang_file
    df_grouped = df_chouma.groupby('avg_b_pri').sum()['chicang'].reset_index()
   
    max_pri = df_chouma['avg_b_pri'].max()
    min_pri = df_chouma['avg_b_pri'].min()

 
    # 自定义价格区间
    price_range = [pri*0.5 for pri in range(math.floor(min_pri),math.ceil(max_pri)*2)]
  
 
    # # 根据价格区间计算每个区间的总持仓量
    df_grouped['区间'] = pd.cut(df_grouped['avg_b_pri'], bins=price_range)
    grouped_by_interval = df_grouped.groupby('区间').sum()

    # 绘制筹码分布图
    fig, ax = plt.subplots(figsize=(10, 6))
    grouped_by_interval['chicang'].plot(kind='bar', ax=ax, color='skyblue')
    # 设置图表标题和标签
    ax.set_title('chouma-fenbu')
    ax.set_xlabel('chichang-pri')
    ax.set_ylabel('chicang-amount')
    plt.xticks(rotation=45)

    plt.ylim(0, 400)
    # plt.yticks(np.arange(0, 401, 50))
 
    # 保存图像到文件
    
    output_image_path = 'drop_neg'+ chicang_file.split('_')[-1].replace('.csv','') + f'.png'  # 替换为你希望保存图像的路径和文件名
    plt.savefig(output_image_path, bbox_inches='tight', dpi=300)  # 使用高分辨率保存图像
    
    

def get_chouma_chicang():
    global chicang_file,df_chouma
    df = pd.read_csv(chicang_file)
    df_neg_hold = df[df['li_lun_hold']<0  ].reset_index(drop=True)
    df_pos_hold = df[df['li_lun_hold']>0].reset_index(drop=True)
    sum_neg_hold = abs(df_neg_hold['li_lun_hold'].sum())
    sum_pos_hold = abs(df_pos_hold['li_lun_hold'].sum())
    df_pos_hold['chicang'] = df_pos_hold['li_lun_hold']*(1-sum_neg_hold/sum_pos_hold) 

    df_chouma = df_pos_hold


# swap_trader_id,li_lun_hold,token_b_M,token_s_M,eth_b,eth_s,b_count,s_count,avg_b_pri,avg_s_pri

if __name__ == "__main__":
 
    if len(sys.argv)<2:
        print("args queshao ")
        sys.exit(1)
    chicang_file = sys.argv[1]
    get_chouma_chicang()
    draw_chouma()
 