commit 1cc68a70c3a5cfed8e4855f27b6cede406ec2696 Author: laixingyu Date: Mon Jan 15 16:56:42 2024 +0800 fix: 添加涨停回测;及优化 diff --git a/calculateTrends1.py b/calculateTrends1.py new file mode 100644 index 0000000..de57e3b --- /dev/null +++ b/calculateTrends1.py @@ -0,0 +1,853 @@ +# 涨停后,各个场景买入后,第二天开盘卖出的成功率 +import pandas as pd +import pymysql +from sqlalchemy import create_engine +import matplotlib.pyplot as plt +import os + +# 计算盈亏, -6以上,-6,-3,0,3,6,6以上 +def count_profit(diff, loss_over_6, loss_6_to_3, loss_3_to_0,profit_0_to_3,profit_3_to_6,profit_over_6): + if diff <= -6: + loss_over_6 += 1 + elif diff > -6 and diff <= -3: + loss_6_to_3 += 1 + elif diff > -3 and diff <= 0: + loss_3_to_0 += 1 + elif diff > 0 and diff <= 3: + profit_0_to_3 += 1 + elif diff > 3 and diff <= 6: + profit_3_to_6 += 1 + else: + profit_over_6 += 1 + return loss_over_6, loss_6_to_3, loss_3_to_0,profit_0_to_3,profit_3_to_6,profit_over_6 + +# 创建数据库连接 +engine = create_engine('mysql+pymysql://root:1qazse42W3@192.168.0.222:3306/ry') +# 执行SQL查询来获取日期列表 +# 假设你的数据表中有一个date字段表示日期 +date_df = pd.read_sql_query('select date from trade_dates where trade = "trading" and date between "2023-01-01" and "2023-06-01" order by date', engine) + +# 将date字段转换为日期类型 +date_df['date'] = pd.to_datetime(date_df['date']) + +# 获取日期列表 +date_list = date_df['date'].tolist() + +counters = { + 'total': 0, + # 买入日的开盘价分布 + 'open': {'low_over_6': 0,'low_6_to_3': 0,'low_3_to_0': 0, 'high_0_to_3': 0, 'high_3_to_6': 0, 'high_over_6': 0}, + # 买入日的收盘价分布 + 'close': {'low_over_6': 0,'low_6_to_3': 0,'low_3_to_0': 0, 'high_0_to_3': 0, 'high_3_to_6': 0, 'high_over_6': 0}, + # 清仓日的开盘价分布 + 'sale_open': {'low_over_6': 0,'low_6_to_3': 0,'low_3_to_0': 0, 'high_0_to_3': 0, 'high_3_to_6': 0, 'high_over_6': 0}, + # 清仓日的收盘价分布 + 'sale_close': {'low_over_6': 0,'low_6_to_3': 0,'low_3_to_0': 0, 'high_0_to_3': 0, 'high_3_to_6': 0, 'high_over_6': 0}, + 'scenarios': { + 'low_open_low_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'low_open_high_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'low_open_normal_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'normal_open_low_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'normal_open_high_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'normal_open_normal_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'high_open_low_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'high_open_high_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + 'high_open_normal_sale': {'high_profit': 0, 'low_profit': 0, 'low_loss': 0, 'high_loss': 0}, + } +} + +total_count = 0 +# 低于-3%的开盘价计数 +low_open_count = 0 +# 正常开盘,介于-3%和3%之间的计数 +normal_open_count = 0 +# 高于3%的开盘价计数 +high_open_count = 0 +# 低于-3%的收盘价计数 +low_close_count = 0 +# 正常收盘,介于-3%和3%之间的计数 +normal_close_count = 0 +# 高于3%的收盘价计数 +high_close_count = 0 + +# 开盘清仓,低于3%的清仓计数 +low_sale_open_count = 0 +# 开盘清仓,高于3%的清仓计数 +high_sale_open_count = 0 +# 开盘清仓,正常清仓计数 +normal_sale_open_count = 0 + +# 收盘清仓,低于3%的清仓计数 +low_sale_close_count = 0 +# 收盘清仓,高于3%的清仓计数 +high_sale_close_count = 0 +# 收盘清仓,正常清仓计数 +normal_sale_close_count = 0 + +##########基于开盘、收盘买入的整体盈亏分布############ +# 盈利高于5%的清仓开盘价计数 +high_profit_sale_open_count = 0 +# 盈利低于5%的清仓开盘价计数 +low_profit_sale_open_count = 0 +# 亏损低于5%的清仓开盘价计数 +low_loss_sale_open_count = 0 +# 亏损高于5%的清仓开盘价计数 +high_loss_sale_open_count = 0 + +# 盈利高于5%的清仓收盘价计数 +high_profit_sale_close_count = 0 +# 盈利低于5%的清仓收盘价计数 +low_profit_sale_close_count = 0 +# 亏损低于5%的清仓收盘价计数 +low_loss_sale_close_count = 0 +# 亏损高于5%的清仓收盘价计数 +high_loss_sale_close_count = 0 + +##################################### +# 组合场景 低开盘-低清仓 低开盘-高清仓 低开盘-正常清仓 正常开盘-低清仓 正常开盘-高清仓 正常开盘-正常清仓 高开盘-低清仓 高开盘-高清仓 高开盘-正常清仓 +low_open_low_sale_count = 0 +low_open_high_sale_count = 0 +low_open_normal_sale_count = 0 +normal_open_low_sale_count = 0 +normal_open_high_sale_count = 0 +normal_open_normal_sale_count = 0 +high_open_low_sale_count = 0 +high_open_high_sale_count = 0 +high_open_normal_sale_count = 0 + +# 各组合场景的收益情况 如 低开盘-高清仓:盈利高于5%的清仓收盘价计数 盈利低于5%的清仓收盘价计数 亏损低于5%的清仓收盘价计数 亏损高于5%的清仓收盘价计数 +low_open_low_sale_high_profit_count = 0 +low_open_low_sale_low_profit_count = 0 +low_open_low_sale_low_loss_count = 0 +low_open_low_sale_high_loss_count = 0 + +low_open_high_sale_high_profit_count = 0 +low_open_high_sale_low_profit_count = 0 +low_open_high_sale_low_loss_count = 0 +low_open_high_sale_high_loss_count = 0 + +low_open_normal_sale_high_profit_count = 0 +low_open_normal_sale_low_profit_count = 0 +low_open_normal_sale_low_loss_count = 0 +low_open_normal_sale_high_loss_count = 0 + +normal_open_low_sale_high_profit_count = 0 +normal_open_low_sale_low_profit_count = 0 +normal_open_low_sale_low_loss_count = 0 +normal_open_low_sale_high_loss_count = 0 + +normal_open_high_sale_high_profit_count = 0 +normal_open_high_sale_low_profit_count = 0 +normal_open_high_sale_low_loss_count = 0 +normal_open_high_sale_high_loss_count = 0 + +normal_open_normal_sale_high_profit_count = 0 +normal_open_normal_sale_low_profit_count = 0 +normal_open_normal_sale_low_loss_count = 0 +normal_open_normal_sale_high_loss_count = 0 + +high_open_low_sale_high_profit_count = 0 +high_open_low_sale_low_profit_count = 0 +high_open_low_sale_low_loss_count = 0 +high_open_low_sale_high_loss_count = 0 + +high_open_high_sale_high_profit_count = 0 +high_open_high_sale_low_profit_count = 0 +high_open_high_sale_low_loss_count = 0 +high_open_high_sale_high_loss_count = 0 + +high_open_normal_sale_high_profit_count = 0 +high_open_normal_sale_low_profit_count = 0 +high_open_normal_sale_low_loss_count = 0 +high_open_normal_sale_high_loss_count = 0 + +# 组合场景 低收盘-低清仓 低收盘-高清仓 低收盘-正常清仓 正常收盘-低清仓 正常收盘-高清仓 正常收盘-正常清仓 高收盘-低清仓 高收盘-高清仓 高收盘-正常清仓 +low_close_low_sale_count = 0 +low_close_high_sale_count = 0 +low_close_normal_sale_count = 0 +normal_close_low_sale_count = 0 +normal_close_high_sale_count = 0 +normal_close_normal_sale_count = 0 +high_close_low_sale_count = 0 +high_close_high_sale_count = 0 +high_close_normal_sale_count = 0 + +# 各组合场景的收益情况 如 低收盘-高清仓:盈利高于5%的清仓收盘价计数 盈利低于5%的清仓收盘价计数 亏损低于5%的清仓收盘价计数 亏损高于5%的清仓收盘价计数 +low_close_low_sale_high_profit_count = 0 +low_close_low_sale_low_profit_count = 0 +low_close_low_sale_low_loss_count = 0 +low_close_low_sale_high_loss_count = 0 + +low_close_high_sale_high_profit_count = 0 +low_close_high_sale_low_profit_count = 0 +low_close_high_sale_low_loss_count = 0 +low_close_high_sale_high_loss_count = 0 + +low_close_normal_sale_high_profit_count = 0 +low_close_normal_sale_low_profit_count = 0 +low_close_normal_sale_low_loss_count = 0 +low_close_normal_sale_high_loss_count = 0 + +normal_close_low_sale_high_profit_count = 0 +normal_close_low_sale_low_profit_count = 0 +normal_close_low_sale_low_loss_count = 0 +normal_close_low_sale_high_loss_count = 0 + +normal_close_high_sale_high_profit_count = 0 +normal_close_high_sale_low_profit_count = 0 +normal_close_high_sale_low_loss_count = 0 +normal_close_high_sale_high_loss_count = 0 + +normal_close_normal_sale_high_profit_count = 0 +normal_close_normal_sale_low_profit_count = 0 +normal_close_normal_sale_low_loss_count = 0 +normal_close_normal_sale_high_loss_count = 0 + +high_close_low_sale_high_profit_count = 0 +high_close_low_sale_low_profit_count = 0 +high_close_low_sale_low_loss_count = 0 +high_close_low_sale_high_loss_count = 0 + +high_close_high_sale_high_profit_count = 0 +high_close_high_sale_low_profit_count = 0 +high_close_high_sale_low_loss_count = 0 +high_close_high_sale_high_loss_count = 0 + +high_close_normal_sale_high_profit_count = 0 +high_close_normal_sale_low_profit_count = 0 +high_close_normal_sale_low_loss_count = 0 +high_close_normal_sale_high_loss_count = 0 + + + + + +# 遍历日期列表 +for i, date in enumerate(date_list): + if i+2 < len(date_list): + # 将日期转换为字符串格式,以便在SQL查询中使用 + date_str = date.strftime('%Y-%m-%d') + if os.path.exists(f'D:/calculate_data/{date_str}_stock.csv'): + df = pd.read_csv(f'D:/calculate_data/{date_str}_stock.csv') + limit_list = df['code'].tolist() + for code in limit_list: + # 总统计数 + total_count += 1 + print(f'正在处理{date_str} - {code}的数据-从文件获取') + saleopen = df.loc[df['code'] == code, 'saleprice_open'] + saleclose = df.loc[df['code'] == code, 'saleprice_close'] + nextclose = df.loc[df['code'] == code, 'next_close'] + # 计算清仓日开盘收盘价 + saleprice_open_differrange = 100 * (saleopen - df.loc[df['code'] == code, 'next_open'])/df.loc[df['code'] == code, 'next_open'] + saleprice_close_differrange = 100 * (saleclose - nextclose)/nextclose + + # 第二日开盘价分布 + next_open_diff = df.loc[df['code'] == code, 'next_open_diff'].values[0] + if next_open_diff < -3: + low_open_count += 1 + elif next_open_diff > 3: + high_open_count += 1 + else: + normal_open_count += 1 + + # 第二日收盘价分布 + next_close_differrange = df.loc[df['code'] == code, 'next_close_differrange'].values[0] + if next_close_differrange < -3: + low_close_count += 1 + elif next_close_differrange > 3: + high_close_count += 1 + else: + normal_close_count += 1 + + # 清仓盈亏分布-开盘清仓 + saleprice_open_differrange = df.loc[df['code'] == code, 'saleprice_open_differrange'].values[0] + if saleprice_open_differrange < -5: + high_loss_sale_open_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + low_loss_sale_open_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + low_profit_sale_open_count += 1 + else: + high_profit_sale_open_count += 1 + + # 清仓盈亏分布-收盘清仓 + saleprice_close_differrange = df.loc[df['code'] == code, 'saleprice_close_differrange'].values[0] + if saleprice_close_differrange < -5: + high_loss_sale_close_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + low_loss_sale_close_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + low_profit_sale_close_count += 1 + else: + high_profit_sale_close_count += 1 + + # 组合场景 低开盘-低清仓 低开盘-高清仓 低开盘-正常清仓 正常开盘-低清仓 正常开盘-高清仓 正常开盘-正常清仓 高开盘-低清仓 高开盘-高清仓 高开盘-正常清仓 + # 组合之类再进行计算盈亏分布 + if next_open_diff < -3 : + if saleprice_open_differrange < -3: + low_open_low_sale_count += 1 + elif saleprice_open_differrange > 3: + low_open_high_sale_count += 1 + else: + low_open_normal_sale_count += 1 + + if saleprice_open_differrange < -3: + if saleprice_open_differrange < -5: + low_open_low_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + low_open_low_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + low_open_low_sale_low_profit_count += 1 + else: + low_open_low_sale_high_profit_count += 1 + elif saleprice_open_differrange > 3: + if saleprice_open_differrange < -5: + low_open_high_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + low_open_high_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + low_open_high_sale_low_profit_count += 1 + else: + low_open_high_sale_high_profit_count += 1 + else: + if saleprice_open_differrange < -5: + low_open_normal_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + low_open_normal_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + low_open_normal_sale_low_profit_count += 1 + else: + low_open_normal_sale_high_profit_count += 1 + + + elif next_open_diff > 3: + if saleprice_open_differrange < -3: + high_open_low_sale_count += 1 + elif saleprice_open_differrange > 3: + high_open_high_sale_count += 1 + else: + high_open_normal_sale_count += 1 + + if saleprice_open_differrange < -3: + if saleprice_open_differrange < -5: + high_open_low_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + high_open_low_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + high_open_low_sale_low_profit_count += 1 + else: + high_open_low_sale_high_profit_count += 1 + elif saleprice_open_differrange > 3: + if saleprice_open_differrange < -5: + high_open_high_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + high_open_high_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + high_open_high_sale_low_profit_count += 1 + else: + high_open_high_sale_high_profit_count += 1 + else: + if saleprice_open_differrange < -5: + high_open_normal_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + high_open_normal_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + high_open_normal_sale_low_profit_count += 1 + else: + high_open_normal_sale_high_profit_count += 1 + + else: + if saleprice_open_differrange < -3: + normal_open_low_sale_count += 1 + elif saleprice_open_differrange > 3: + normal_open_high_sale_count += 1 + else: + normal_open_normal_sale_count += 1 + + if saleprice_open_differrange < -3: + if saleprice_open_differrange < -5: + normal_open_low_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + normal_open_low_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + normal_open_low_sale_low_profit_count += 1 + else: + normal_open_low_sale_high_profit_count += 1 + elif saleprice_open_differrange > 3: + if saleprice_open_differrange < -5: + normal_open_high_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + normal_open_high_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + normal_open_high_sale_low_profit_count += 1 + else: + normal_open_high_sale_high_profit_count += 1 + else: + if saleprice_open_differrange < -5: + normal_open_normal_sale_high_loss_count += 1 + elif saleprice_open_differrange > -5 and saleprice_open_differrange < 0: + normal_open_normal_sale_low_loss_count += 1 + elif saleprice_open_differrange > 0 and saleprice_open_differrange < 5: + normal_open_normal_sale_low_profit_count += 1 + else: + normal_open_normal_sale_high_profit_count += 1 + + + # 组合场景 低收盘-低清仓 低收盘-高清仓 低收盘-正常清仓 正常收盘-低清仓 正常收盘-高清仓 正常收盘-正常清仓 高收盘-低清仓 高收盘-高清仓 高收盘-正常清仓 + if next_close_differrange < -3 : + if saleprice_close_differrange < -3: + low_close_low_sale_count += 1 + elif saleprice_close_differrange > 3: + low_close_high_sale_count += 1 + else: + low_close_normal_sale_count += 1 + + if saleprice_close_differrange < -3: + if saleprice_close_differrange < -5: + low_close_low_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + low_close_low_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + low_close_low_sale_low_profit_count += 1 + else: + low_close_low_sale_high_profit_count += 1 + elif saleprice_close_differrange > 3: + if saleprice_close_differrange < -5: + low_close_high_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + low_close_high_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + low_close_high_sale_low_profit_count += 1 + else: + low_close_high_sale_high_profit_count += 1 + else: + if saleprice_close_differrange < -5: + low_close_normal_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + low_close_normal_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + low_close_normal_sale_low_profit_count += 1 + else: + low_close_normal_sale_high_profit_count += 1 + + elif next_close_differrange > 3: + if saleprice_close_differrange < -3: + high_close_low_sale_count += 1 + elif saleprice_close_differrange > 3: + high_close_high_sale_count += 1 + else: + high_close_normal_sale_count += 1 + + if saleprice_close_differrange < -3: + if saleprice_close_differrange < -5: + high_close_low_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + high_close_low_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + high_close_low_sale_low_profit_count += 1 + else: + high_close_low_sale_high_profit_count += 1 + elif saleprice_close_differrange > 3: + if saleprice_close_differrange < -5: + high_close_high_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + high_close_high_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + high_close_high_sale_low_profit_count += 1 + else: + high_close_high_sale_high_profit_count += 1 + else: + if saleprice_close_differrange < -5: + high_close_normal_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + high_close_normal_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + high_close_normal_sale_low_profit_count += 1 + else: + high_close_normal_sale_high_profit_count += 1 + else: + if saleprice_close_differrange < -3: + normal_close_low_sale_count += 1 + elif saleprice_close_differrange > 3: + normal_close_high_sale_count += 1 + else: + normal_close_normal_sale_count += 1 + + if saleprice_close_differrange < -3: + if saleprice_close_differrange < -5: + normal_close_low_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + normal_close_low_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + normal_close_low_sale_low_profit_count += 1 + else: + normal_close_low_sale_high_profit_count += 1 + elif saleprice_close_differrange > 3: + if saleprice_close_differrange < -5: + normal_close_high_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + normal_close_high_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + normal_close_high_sale_low_profit_count += 1 + else: + normal_close_high_sale_high_profit_count += 1 + else: + if saleprice_close_differrange < -5: + normal_close_normal_sale_high_loss_count += 1 + elif saleprice_close_differrange > -5 and saleprice_close_differrange < 0: + normal_close_normal_sale_low_loss_count += 1 + elif saleprice_close_differrange > 0 and saleprice_close_differrange < 5: + normal_close_normal_sale_low_profit_count += 1 + else: + normal_close_normal_sale_high_profit_count += 1 + else: + nextdate_str = date_list[i+1].strftime('%Y-%m-%d')#买入日 + saledate_str = date_list[i+2].strftime('%Y-%m-%d')#卖出日 + + print(f'正在处理{date_str}的数据') + + # 执行SQL查询(这只是一个示例,你需要根据你的实际需求和数据库结构进行修改) + query = f'select code,open,close,high,low, volumn,amount,differrange10,differrange20,differrange60 from stocks where islimit = "是" and trade_day = "{date_str}"' + df = pd.read_sql_query(query, engine) + limit_list = df['code'].tolist() + for code in limit_list: + # 总统计数 + total_count += 1 + print(f'正在处理{date_str} - {code}的数据') + query = f'select code,open,close,differrange from stocks where trade_day = "{nextdate_str}" and code = "{code}"' + dfnext = pd.read_sql_query(query,engine) + + if dfnext.size <= 0: + total_count -= 1 + continue + # 处理数据... + open = df.loc[df['code'] == code, 'open'].values[0] + nextopen = dfnext.loc[dfnext['code'] == code, 'open'].values[0] + nextclose = dfnext.loc[dfnext['code'] == code, 'close'].values[0] + nextdifferrange = dfnext.loc[dfnext['code'] == code, 'differrange'].values[0] + + df.loc[df['code'] == code, 'next_open'] = nextopen + df.loc[df['code'] == code, 'next_open_diff'] = 100 * (nextopen - open)/open + df.loc[df['code'] == code, 'next_close'] = nextclose + df.loc[df['code'] == code, 'next_close_differrange'] = nextdifferrange + + query = f'select code,open,close,differrange from stocks where trade_day = "{saledate_str}" and code = "{code}"' + dfsale = pd.read_sql_query(query,engine) + + if dfsale.size <= 0: + total_count -= 1 + continue + + saleopen = dfsale.loc[dfsale['code'] == code, 'open'].values[0] + saleclose = dfsale.loc[dfsale['code'] == code, 'close'].values[0] + + df.loc[df['code'] == code, 'saleprice_open'] = saleopen + df.loc[df['code'] == code, 'saleprice_close'] = saleclose + # 清仓盈亏 + df.loc[df['code'] == code, 'saleprice_open_differrange'] = 100 * (saleopen - nextopen)/nextopen + df.loc[df['code'] == code, 'saleprice_close_differrange'] = 100 * (saleclose - nextclose)/nextclose + + # 第二日开盘价分布 + next_open_diff = df.loc[df['code'] == code, 'next_open_diff'].values[0] + if next_open_diff < -3: + low_open_count += 1 + elif next_open_diff > 3: + high_open_count += 1 + else: + normal_open_count += 1 + + # 第二日收盘价分布 + next_close_differrange = df.loc[df['code'] == code, 'next_close_differrange'].values[0] + if next_close_differrange < -3: + low_close_count += 1 + elif next_close_differrange > 3: + high_close_count += 1 + else: + normal_close_count += 1 + + # 清仓盈亏分布-开盘清仓 + saleprice_open_differrange = df.loc[df['code'] == code, 'saleprice_open_differrange'].values[0] + if saleprice_open_differrange < -3: # 亏损大于-3 + low_sale_open_count += 1 + elif saleprice_open_differrange > 0: # 盈利大于0 + high_sale_open_count += 1 + else: + normal_sale_open_count += 1 # 亏损小于-3 + + # 清仓盈亏分布-收盘清仓 + saleprice_close_differrange = df.loc[df['code'] == code, 'saleprice_close_differrange'].values[0] + if saleprice_close_differrange < -3: + low_sale_close_count += 1 + elif saleprice_close_differrange > 3: + high_sale_close_count += 1 + else: + normal_sale_close_count += 1 + + # 组合场景 低开盘-低清仓 低开盘-高清仓 低开盘-正常清仓 正常开盘-低清仓 正常开盘-高清仓 正常开盘-正常清仓 高开盘-低清仓 高开盘-高清仓 高开盘-正常清仓 + if next_open_diff < -3 : + if saleprice_open_differrange < -3: + low_open_low_sale_count += 1 + elif saleprice_open_differrange > 3: + low_open_high_sale_count += 1 + else: + low_open_normal_sale_count += 1 + elif next_open_diff > 3: + if saleprice_open_differrange < -3: + high_open_low_sale_count += 1 + elif saleprice_open_differrange > 3: + high_open_high_sale_count += 1 + else: + high_open_normal_sale_count += 1 + else: + if saleprice_open_differrange < -3: + normal_open_low_sale_count += 1 + elif saleprice_open_differrange > 3: + normal_open_high_sale_count += 1 + else: + normal_open_normal_sale_count += 1 + + # 组合场景 低收盘-低清仓 低收盘-高清仓 低收盘-正常清仓 正常收盘-低清仓 正常收盘-高清仓 正常收盘-正常清仓 高收盘-低清仓 高收盘-高清仓 高收盘-正常清仓 + if next_close_differrange < -3 : + if saleprice_close_differrange < -3: + low_close_low_sale_count += 1 + elif saleprice_close_differrange > 3: + low_close_high_sale_count += 1 + else: + low_close_normal_sale_count += 1 + elif next_close_differrange > 3: + if saleprice_close_differrange < -3: + high_close_low_sale_count += 1 + elif saleprice_close_differrange > 3: + high_close_high_sale_count += 1 + else: + high_close_normal_sale_count += 1 + else: + if saleprice_close_differrange < -3: + normal_close_low_sale_count += 1 + elif saleprice_close_differrange > 3: + normal_close_high_sale_count += 1 + else: + normal_close_normal_sale_count += 1 + + # 将数据框保存到磁盘 + df.to_csv(f'D:/calculate_data/{date_str}_stock.csv', index=False) + # # 从磁盘加载数据框 + # df = pd.read_csv('df_distribution.csv') + +print(f'总统计数:{total_count}') +print(f'低于-3%的开盘价计数:{low_open_count}') +print(f'正常开盘,介于-3%和3%之间的计数:{normal_open_count}') +print(f'高于3%的开盘价计数:{high_open_count}') +print(f'低于-3%的收盘价计数:{low_close_count}') +print(f'正常收盘,介于-3%和3%之间的计数:{normal_close_count}') +print(f'高于3%的收盘价计数:{high_close_count}') +print(f'低开盘-低清仓:{low_open_low_sale_count}') +print(f'低开盘-高清仓:{low_open_high_sale_count}') +print(f'低开盘-正常清仓:{low_open_normal_sale_count}') +print(f'正常开盘-低清仓:{normal_open_low_sale_count}') +print(f'正常开盘-高清仓:{normal_open_high_sale_count}') +print(f'正常开盘-正常清仓:{normal_open_normal_sale_count}') +print(f'高开盘-低清仓:{high_open_low_sale_count}') +print(f'高开盘-高清仓:{high_open_high_sale_count}') +print(f'高开盘-正常清仓:{high_open_normal_sale_count}') +print(f'低收盘-低清仓:{low_close_low_sale_count}') +print(f'低收盘-高清仓:{low_close_high_sale_count}') +print(f'低收盘-正常清仓:{low_close_normal_sale_count}') +print(f'正常收盘-低清仓:{normal_close_low_sale_count}') +print(f'正常收盘-高清仓:{normal_close_high_sale_count}') +print(f'正常收盘-正常清仓:{normal_close_normal_sale_count}') +print(f'高收盘-低清仓:{high_close_low_sale_count}') +print(f'高收盘-高清仓:{high_close_high_sale_count}') +print(f'高收盘-正常清仓:{high_close_normal_sale_count}') +print(f'低开盘-低清仓-盈利高于5%:{low_open_low_sale_high_profit_count}') +print(f'低开盘-低清仓-盈利低于5%:{low_open_low_sale_low_profit_count}') +print(f'低开盘-低清仓-亏损低于5%:{low_open_low_sale_low_loss_count}') +print(f'低开盘-低清仓-亏损高于5%:{low_open_low_sale_high_loss_count}') +print(f'低开盘-高清仓-盈利高于5%:{low_open_high_sale_high_profit_count}') +print(f'低开盘-高清仓-盈利低于5%:{low_open_high_sale_low_profit_count}') +print(f'低开盘-高清仓-亏损低于5%:{low_open_high_sale_low_loss_count}') +print(f'低开盘-高清仓-亏损高于5%:{low_open_high_sale_high_loss_count}') +print(f'低开盘-正常清仓-盈利高于5%:{low_open_normal_sale_high_profit_count}') +print(f'低开盘-正常清仓-盈利低于5%:{low_open_normal_sale_low_profit_count}') +print(f'低开盘-正常清仓-亏损低于5%:{low_open_normal_sale_low_loss_count}') +print(f'低开盘-正常清仓-亏损高于5%:{low_open_normal_sale_high_loss_count}') +print(f'正常开盘-低清仓-盈利高于5%:{normal_open_low_sale_high_profit_count}') +print(f'正常开盘-低清仓-盈利低于5%:{normal_open_low_sale_low_profit_count}') +print(f'正常开盘-低清仓-亏损低于5%:{normal_open_low_sale_low_loss_count}') +print(f'正常开盘-低清仓-亏损高于5%:{normal_open_low_sale_high_loss_count}') +print(f'正常开盘-高清仓-盈利高于5%:{normal_open_high_sale_high_profit_count}') +print(f'正常开盘-高清仓-盈利低于5%:{normal_open_high_sale_low_profit_count}') +print(f'正常开盘-高清仓-亏损低于5%:{normal_open_high_sale_low_loss_count}') +print(f'正常开盘-高清仓-亏损高于5%:{normal_open_high_sale_high_loss_count}') +print(f'正常开盘-正常清仓-盈利高于5%:{normal_open_normal_sale_high_profit_count}') +print(f'正常开盘-正常清仓-盈利低于5%:{normal_open_normal_sale_low_profit_count}') +print(f'正常开盘-正常清仓-亏损低于5%:{normal_open_normal_sale_low_loss_count}') +print(f'正常开盘-正常清仓-亏损高于5%:{normal_open_normal_sale_high_loss_count}') +print(f'高开盘-低清仓-盈利高于5%:{high_open_low_sale_high_profit_count}') +print(f'高开盘-低清仓-盈利低于5%:{high_open_low_sale_low_profit_count}') +print(f'高开盘-低清仓-亏损低于5%:{high_open_low_sale_low_loss_count}') +print(f'高开盘-低清仓-亏损高于5%:{high_open_low_sale_high_loss_count}') +print(f'高开盘-高清仓-盈利高于5%:{high_open_high_sale_high_profit_count}') +print(f'高开盘-高清仓-盈利低于5%:{high_open_high_sale_low_profit_count}') +print(f'高开盘-高清仓-亏损低于5%:{high_open_high_sale_low_loss_count}') +print(f'高开盘-高清仓-亏损高于5%:{high_open_high_sale_high_loss_count}') +print(f'高开盘-正常清仓-盈利高于5%:{high_open_normal_sale_high_profit_count}') +print(f'高开盘-正常清仓-盈利低于5%:{high_open_normal_sale_low_profit_count}') +print(f'高开盘-正常清仓-亏损低于5%:{high_open_normal_sale_low_loss_count}') +print(f'高开盘-正常清仓-亏损高于5%:{high_open_normal_sale_high_loss_count}') +print(f'低收盘-低清仓-盈利高于5%:{low_close_low_sale_high_profit_count}') +print(f'低收盘-低清仓-盈利低于5%:{low_close_low_sale_low_profit_count}') +print(f'低收盘-低清仓-亏损低于5%:{low_close_low_sale_low_loss_count}') +print(f'低收盘-低清仓-亏损高于5%:{low_close_low_sale_high_loss_count}') +print(f'低收盘-高清仓-盈利高于5%:{low_close_high_sale_high_profit_count}') +print(f'低收盘-高清仓-盈利低于5%:{low_close_high_sale_low_profit_count}') +print(f'低收盘-高清仓-亏损低于5%:{low_close_high_sale_low_loss_count}') +print(f'低收盘-高清仓-亏损高于5%:{low_close_high_sale_high_loss_count}') +print(f'低收盘-正常清仓-盈利高于5%:{low_close_normal_sale_high_profit_count}') +print(f'低收盘-正常清仓-盈利低于5%:{low_close_normal_sale_low_profit_count}') +print(f'低收盘-正常清仓-亏损低于5%:{low_close_normal_sale_low_loss_count}') +print(f'低收盘-正常清仓-亏损高于5%:{low_close_normal_sale_high_loss_count}') +print(f'正常收盘-低清仓-盈利高于5%:{normal_close_low_sale_high_profit_count}') +print(f'正常收盘-低清仓-盈利低于5%:{normal_close_low_sale_low_profit_count}') +print(f'正常收盘-低清仓-亏损低于5%:{normal_close_low_sale_low_loss_count}') +print(f'正常收盘-低清仓-亏损高于5%:{normal_close_low_sale_high_loss_count}') +print(f'正常收盘-高清仓-盈利高于5%:{normal_close_high_sale_high_profit_count}') +print(f'正常收盘-高清仓-盈利低于5%:{normal_close_high_sale_low_profit_count}') +print(f'正常收盘-高清仓-亏损低于5%:{normal_close_high_sale_low_loss_count}') +print(f'正常收盘-高清仓-亏损高于5%:{normal_close_high_sale_high_loss_count}') +print(f'正常收盘-正常清仓-盈利高于5%:{normal_close_normal_sale_high_profit_count}') +print(f'正常收盘-正常清仓-盈利低于5%:{normal_close_normal_sale_low_profit_count}') +print(f'正常收盘-正常清仓-亏损低于5%:{normal_close_normal_sale_low_loss_count}') +print(f'正常收盘-正常清仓-亏损高于5%:{normal_close_normal_sale_high_loss_count}') +print(f'高收盘-低清仓-盈利高于5%:{high_close_low_sale_high_profit_count}') +print(f'高收盘-低清仓-盈利低于5%:{high_close_low_sale_low_profit_count}') +print(f'高收盘-低清仓-亏损低于5%:{high_close_low_sale_low_loss_count}') +print(f'高收盘-低清仓-亏损高于5%:{high_close_low_sale_high_loss_count}') +print(f'高收盘-高清仓-盈利高于5%:{high_close_high_sale_high_profit_count}') +print(f'高收盘-高清仓-盈利低于5%:{high_close_high_sale_low_profit_count}') +print(f'高收盘-高清仓-亏损低于5%:{high_close_high_sale_low_loss_count}') +print(f'高收盘-高清仓-亏损高于5%:{high_close_high_sale_high_loss_count}') +print(f'高收盘-正常清仓-盈利高于5%:{high_close_normal_sale_high_profit_count}') +print(f'高收盘-正常清仓-盈利低于5%:{high_close_normal_sale_low_profit_count}') +print(f'高收盘-正常清仓-亏损低于5%:{high_close_normal_sale_low_loss_count}') +print(f'高收盘-正常清仓-亏损高于5%:{high_close_normal_sale_high_loss_count}') + + + +df_distribution = pd.DataFrame({'test':[1]}) +df_distribution['total_count'] = total_count +df_distribution['low_open_count'] = low_open_count +df_distribution['normal_open_count'] = normal_open_count +df_distribution['high_open_count'] = high_open_count +df_distribution['low_close_count'] = low_close_count +df_distribution['normal_close_count'] = normal_close_count +df_distribution['high_close_count'] = high_close_count +df_distribution['low_open_low_sale_count'] = low_open_low_sale_count +df_distribution['low_open_high_sale_count'] = low_open_high_sale_count +df_distribution['low_open_normal_sale_count'] = low_open_normal_sale_count +df_distribution['normal_open_low_sale_count'] = normal_open_low_sale_count +df_distribution['normal_open_high_sale_count'] = normal_open_high_sale_count +df_distribution['normal_open_normal_sale_count'] = normal_open_normal_sale_count +df_distribution['high_open_low_sale_count'] = high_open_low_sale_count +df_distribution['high_open_high_sale_count'] = high_open_high_sale_count +df_distribution['high_open_normal_sale_count'] = high_open_normal_sale_count +df_distribution['low_close_low_sale_count'] = low_close_low_sale_count +df_distribution['low_close_high_sale_count'] = low_close_high_sale_count +df_distribution['low_close_normal_sale_count'] = low_close_normal_sale_count +df_distribution['normal_close_low_sale_count'] = normal_close_low_sale_count +df_distribution['normal_close_high_sale_count'] = normal_close_high_sale_count +df_distribution['normal_close_normal_sale_count'] = normal_close_normal_sale_count +df_distribution['high_close_low_sale_count'] = high_close_low_sale_count +df_distribution['high_close_high_sale_count'] = high_close_high_sale_count +df_distribution['high_close_normal_sale_count'] = high_close_normal_sale_count +df_distribution['low_open_low_sale_high_profit_count'] = low_open_low_sale_high_profit_count +df_distribution['low_open_low_sale_low_profit_count'] = low_open_low_sale_low_profit_count +df_distribution['low_open_low_sale_low_loss_count'] = low_open_low_sale_low_loss_count +df_distribution['low_open_low_sale_high_loss_count'] = low_open_low_sale_high_loss_count +df_distribution['low_open_high_sale_high_profit_count'] = low_open_high_sale_high_profit_count +df_distribution['low_open_high_sale_low_profit_count'] = low_open_high_sale_low_profit_count +df_distribution['low_open_high_sale_low_loss_count'] = low_open_high_sale_low_loss_count +df_distribution['low_open_high_sale_high_loss_count'] = low_open_high_sale_high_loss_count +df_distribution['low_open_normal_sale_high_profit_count'] = low_open_normal_sale_high_profit_count +df_distribution['low_open_normal_sale_low_profit_count'] = low_open_normal_sale_low_profit_count +df_distribution['low_open_normal_sale_low_loss_count'] = low_open_normal_sale_low_loss_count +df_distribution['low_open_normal_sale_high_loss_count'] = low_open_normal_sale_high_loss_count +df_distribution['normal_open_low_sale_high_profit_count'] = normal_open_low_sale_high_profit_count +df_distribution['normal_open_low_sale_low_profit_count'] = normal_open_low_sale_low_profit_count +df_distribution['normal_open_low_sale_low_loss_count'] = normal_open_low_sale_low_loss_count +df_distribution['normal_open_low_sale_high_loss_count'] = normal_open_low_sale_high_loss_count +df_distribution['normal_open_high_sale_high_profit_count'] = normal_open_high_sale_high_profit_count +df_distribution['normal_open_high_sale_low_profit_count'] = normal_open_high_sale_low_profit_count +df_distribution['normal_open_high_sale_low_loss_count'] = normal_open_high_sale_low_loss_count +df_distribution['normal_open_high_sale_high_loss_count'] = normal_open_high_sale_high_loss_count +df_distribution['normal_open_normal_sale_high_profit_count'] = normal_open_normal_sale_high_profit_count +df_distribution['normal_open_normal_sale_low_profit_count'] = normal_open_normal_sale_low_profit_count +df_distribution['normal_open_normal_sale_low_loss_count'] = normal_open_normal_sale_low_loss_count +df_distribution['normal_open_normal_sale_high_loss_count'] = normal_open_normal_sale_high_loss_count +df_distribution['high_open_low_sale_high_profit_count'] = high_open_low_sale_high_profit_count +df_distribution['high_open_low_sale_low_profit_count'] = high_open_low_sale_low_profit_count +df_distribution['high_open_low_sale_low_loss_count'] = high_open_low_sale_low_loss_count +df_distribution['high_open_low_sale_high_loss_count'] = high_open_low_sale_high_loss_count +df_distribution['high_open_high_sale_high_profit_count'] = high_open_high_sale_high_profit_count +df_distribution['high_open_high_sale_low_profit_count'] = high_open_high_sale_low_profit_count +df_distribution['high_open_high_sale_low_loss_count'] = high_open_high_sale_low_loss_count +df_distribution['high_open_high_sale_high_loss_count'] = high_open_high_sale_high_loss_count +df_distribution['high_open_normal_sale_high_profit_count'] = high_open_normal_sale_high_profit_count +df_distribution['high_open_normal_sale_low_profit_count'] = high_open_normal_sale_low_profit_count +df_distribution['high_open_normal_sale_low_loss_count'] = high_open_normal_sale_low_loss_count +df_distribution['high_open_normal_sale_high_loss_count'] = high_open_normal_sale_high_loss_count +df_distribution['low_close_low_sale_high_profit_count'] = low_close_low_sale_high_profit_count +df_distribution['low_close_low_sale_low_profit_count'] = low_close_low_sale_low_profit_count +df_distribution['low_close_low_sale_low_loss_count'] = low_close_low_sale_low_loss_count +df_distribution['low_close_low_sale_high_loss_count'] = low_close_low_sale_high_loss_count +df_distribution['low_close_high_sale_high_profit_count'] = low_close_high_sale_high_profit_count +df_distribution['low_close_high_sale_low_profit_count'] = low_close_high_sale_low_profit_count +df_distribution['low_close_high_sale_low_loss_count'] = low_close_high_sale_low_loss_count +df_distribution['low_close_high_sale_high_loss_count'] = low_close_high_sale_high_loss_count +df_distribution['low_close_normal_sale_high_profit_count'] = low_close_normal_sale_high_profit_count +df_distribution['low_close_normal_sale_low_profit_count'] = low_close_normal_sale_low_profit_count +df_distribution['low_close_normal_sale_low_loss_count'] = low_close_normal_sale_low_loss_count +df_distribution['low_close_normal_sale_high_loss_count'] = low_close_normal_sale_high_loss_count +df_distribution['normal_close_low_sale_high_profit_count'] = normal_close_low_sale_high_profit_count +df_distribution['normal_close_low_sale_low_profit_count'] = normal_close_low_sale_low_profit_count +df_distribution['normal_close_low_sale_low_loss_count'] = normal_close_low_sale_low_loss_count +df_distribution['normal_close_low_sale_high_loss_count'] = normal_close_low_sale_high_loss_count +df_distribution['normal_close_high_sale_high_profit_count'] = normal_close_high_sale_high_profit_count +df_distribution['normal_close_high_sale_low_profit_count'] = normal_close_high_sale_low_profit_count +df_distribution['normal_close_high_sale_low_loss_count'] = normal_close_high_sale_low_loss_count +df_distribution['normal_close_high_sale_high_loss_count'] = normal_close_high_sale_high_loss_count +df_distribution['normal_close_normal_sale_high_profit_count'] = normal_close_normal_sale_high_profit_count +df_distribution['normal_close_normal_sale_low_profit_count'] = normal_close_normal_sale_low_profit_count +df_distribution['normal_close_normal_sale_low_loss_count'] = normal_close_normal_sale_low_loss_count +df_distribution['normal_close_normal_sale_high_loss_count'] = normal_close_normal_sale_high_loss_count +df_distribution['high_close_low_sale_high_profit_count'] = high_close_low_sale_high_profit_count +df_distribution['high_close_low_sale_low_profit_count'] = high_close_low_sale_low_profit_count +df_distribution['high_close_low_sale_low_loss_count'] = high_close_low_sale_low_loss_count +df_distribution['high_close_low_sale_high_loss_count'] = high_close_low_sale_high_loss_count +df_distribution['high_close_high_sale_high_profit_count'] = high_close_high_sale_high_profit_count +df_distribution['high_close_high_sale_low_profit_count'] = high_close_high_sale_low_profit_count +df_distribution['high_close_high_sale_low_loss_count'] = high_close_high_sale_low_loss_count +df_distribution['high_close_high_sale_high_loss_count'] = high_close_high_sale_high_loss_count +df_distribution['high_close_normal_sale_high_profit_count'] = high_close_normal_sale_high_profit_count +df_distribution['high_close_normal_sale_low_profit_count'] = high_close_normal_sale_low_profit_count +df_distribution['high_close_normal_sale_low_loss_count'] = high_close_normal_sale_low_loss_count +df_distribution['high_close_normal_sale_high_loss_count'] = high_close_normal_sale_high_loss_count + + + +# 删除'test'列 +df_distribution = df_distribution.drop(columns=['test']) + +# 创建一个新的图形 +fig, ax = plt.subplots() + +# 创建一个条形图 +df_distribution.plot(kind='bar', ax=ax) + +# 设置图形的标题和轴标签 +ax.set_title('Distribution') +ax.set_xlabel('Category') +ax.set_ylabel('Count') + +# 显示图形 +plt.show() \ No newline at end of file diff --git a/calculteLimit.py b/calculteLimit.py new file mode 100644 index 0000000..ae96f75 --- /dev/null +++ b/calculteLimit.py @@ -0,0 +1,206 @@ +import pandas as pd +import pymysql +from sqlalchemy import create_engine +import matplotlib.pyplot as plt + +# # 创建数据库连接 +# engine = create_engine('mysql+pymysql://root:1qazse42W3@192.168.0.222:3306/ry') + +# # 执行SQL查询并将结果存储在pandas DataFrame中 +# df = pd.read_sql_query('select code,trade_day as date ,open as open_price,close as close_price from stocks where trade_day between "2023-06-01" and "2023-06-30" ORDER BY trade_day', engine) + +# # 将date字段转换为日期类型,并设置为索引 +# df['date'] = pd.to_datetime(df['date']) +# df.set_index('date', inplace=True) + +# # 对每个股票计算涨跌幅 +# df['change'] = df.groupby('code')['open_price'].shift(-1) - df['close_price'] + +# # 计算涨跌幅分布 +# distribution = df['change'].value_counts(bins=10, normalize=True) + +# # 显示涨跌幅分布的图表 +# distribution.plot(kind='bar') +# plt.title('Distribution of Price Changes') +# plt.xlabel('Price Change') +# plt.ylabel('Frequency') +# plt.show() + +# 创建数据库连接 +engine = create_engine('mysql+pymysql://root:1qazse42W3@192.168.0.222:3306/ry') +# 执行SQL查询来获取日期列表 +# 假设你的数据表中有一个date字段表示日期 +date_df = pd.read_sql_query('select date from trade_dates where trade = "trading" and date between "2023-06-01" and "2023-06-30" order by date', engine) + +# 将date字段转换为日期类型 +date_df['date'] = pd.to_datetime(date_df['date']) + +# 获取日期列表 +date_list = date_df['date'].tolist() + +total_count = 0 +# 低于-3%的开盘价计数 +low_open_count = 0 +# 正常开盘,介于-3%和3%之间的计数 +normal_open_count = 0 +# 高于3%的开盘价计数 +high_open_count = 0 +# 低于-3%的收盘价计数 +low_close_count = 0 +# 正常收盘,介于-3%和3%之间的计数 +normal_close_count = 0 +# 高于3%的收盘价计数 +high_close_count = 0 + +# 收盘低于-3% 且开盘低于-3%的计数 +low_open_low_close_count = 0 +# 收盘价低于-3% 且开盘价介于-3%和3%之间的计数 +low_open_normal_close_count = 0 +# 收盘低于-3% 且开盘高于3%的计数 +low_open_high_close_count = 0 +# 收盘价介于-3%和3%之间 且开盘低于-3%的计数 +normal_open_low_close_count = 0 +# 收盘价介于-3%和3%之间 且开盘介于-3%和3%之间的计数 +normal_open_normal_close_count = 0 +# 收盘价介于-3%和3%之间 且开盘高于3%的计数 +normal_open_high_close_count = 0 +# 收盘高于3% 且开盘低于-3%的计数 +high_open_low_close_count = 0 +# 收盘高于3% 且开盘介于-3%和3%之间的计数 +high_open_normal_close_count = 0 +# 收盘高于3% 且开盘高于3%的计数 +high_open_high_close_count = 0 + + + +# 遍历日期列表 +for i, date in enumerate(date_list): + if i+1 < len(date_list): + # 将日期转换为字符串格式,以便在SQL查询中使用 + date_str = date.strftime('%Y-%m-%d') + nextdate_str = date_list[i+1].strftime('%Y-%m-%d') + + print(f'正在处理{date_str}的数据') + + # 执行SQL查询(这只是一个示例,你需要根据你的实际需求和数据库结构进行修改) + query = f'select code,open,close,differrange from stocks where islimit = "是" and trade_day = "{date_str}"' + df = pd.read_sql_query(query, engine) + limit_list = df['code'].tolist() + for code in limit_list: + # 总统计数 + total_count += 1 + print(f'正在处理{date_str} - {code}的数据') + query = f'select code,open,close,differrange from stocks where trade_day = "{nextdate_str}" and code = "{code}"' + dfnext = pd.read_sql_query(query,engine) + # 处理数据... + df['next_open'] = dfnext['open'] + df['next_open_diff'] = 100 * (dfnext['open'] - df['open'])/df['open'] + print(df['next_open_diff'][0]) + df['next_close'] = dfnext['close'] + df['next_close_differrange'] = dfnext['differrange'] + print(df['next_close_differrange'][0]) + + if df['next_open_diff'][0] < -3: + low_open_count += 1 + elif df['next_open_diff'][0] > 3: + high_open_count += 1 + else: + normal_open_count += 1 + + if df['next_close_differrange'][0] < -3: + low_close_count += 1 + elif df['next_close_differrange'][0] > 3: + high_close_count += 1 + else: + normal_close_count += 1 + + if df['next_open_diff'][0] < -3 and df['next_close_differrange'][0] < -3: + low_open_low_close_count += 1 + elif df['next_open_diff'][0] < -3 and df['next_close_differrange'][0] > -3 and df['next_close_differrange'][0] < 3: + low_open_normal_close_count += 1 + elif df['next_open_diff'][0] < -3 and df['next_close_differrange'][0] > 3: + low_open_high_close_count += 1 + elif df['next_open_diff'][0] > -3 and df['next_open_diff'][0] < 3 and df['next_close_differrange'][0] < -3: + normal_open_low_close_count += 1 + elif df['next_open_diff'][0] > -3 and df['next_open_diff'][0] < 3 and df['next_close_differrange'][0] > -3 and df['next_close_differrange'][0] < 3: + normal_open_normal_close_count += 1 + elif df['next_open_diff'][0] > -3 and df['next_open_diff'][0] < 3 and df['next_close_differrange'][0] > 3: + normal_open_high_close_count += 1 + elif df['next_open_diff'][0] > 3 and df['next_close_differrange'][0] < -3: + high_open_low_close_count += 1 + elif df['next_open_diff'][0] > 3 and df['next_close_differrange'][0] > -3 and df['next_close_differrange'][0] < 3: + high_open_normal_close_count += 1 + elif df['next_open_diff'][0] > 3 and df['next_close_differrange'][0] > 3: + high_open_high_close_count += 1 + # 将数据框保存到磁盘 + df.to_csv(f'./data/{date_str}_stock.csv', index=False) + # # 从磁盘加载数据框 + # df = pd.read_csv('df_distribution.csv') + +print(f'总统计数:{total_count}') +print(f'低于-3%的开盘价计数:{low_open_count}') +print(f'正常开盘,介于-3%和3%之间的计数:{normal_open_count}') +print(f'高于3%的开盘价计数:{high_open_count}') +print(f'低于-3%的收盘价计数:{low_close_count}') +print(f'正常收盘,介于-3%和3%之间的计数:{normal_close_count}') +print(f'高于3%的收盘价计数:{high_close_count}') +print(f'收盘低于-3% 且开盘低于-3%的计数:{low_open_low_close_count}') +print(f'收盘价低于-3% 且开盘价介于-3%和3%之间的计数:{low_open_normal_close_count}') +print(f'收盘低于-3% 且开盘高于3%的计数:{low_open_high_close_count}') +print(f'收盘价介于-3%和3%之间 且开盘低于-3%的计数:{normal_open_low_close_count}') +print(f'收盘价介于-3%和3%之间 且开盘介于-3%和3%之间的计数:{normal_open_normal_close_count}') +print(f'收盘价介于-3%和3%之间 且开盘高于3%的计数:{normal_open_high_close_count}') +print(f'收盘高于3% 且开盘低于-3%的计数:{high_open_low_close_count}') +print(f'收盘高于3% 且开盘介于-3%和3%之间的计数:{high_open_normal_close_count}') +print(f'收盘高于3% 且开盘高于3%的计数:{high_open_high_close_count}') + +df_distribution = pd.DataFrame({'test':[1]}) +df_distribution['total_count'] = total_count +df_distribution['low_open_count'] = low_open_count +df_distribution['normal_open_count'] = normal_open_count +df_distribution['high_open_count'] = high_open_count +df_distribution['low_close_count'] = low_close_count +df_distribution['normal_close_count'] = normal_close_count +df_distribution['high_close_count'] = high_close_count +df_distribution['low_open_low_close_count'] = low_open_low_close_count +df_distribution['low_open_normal_close_count'] = low_open_normal_close_count +df_distribution['low_open_high_close_count'] = low_open_high_close_count +df_distribution['normal_open_low_close_count'] = normal_open_low_close_count +df_distribution['normal_open_normal_close_count'] = normal_open_normal_close_count +df_distribution['normal_open_high_close_count'] = normal_open_high_close_count +df_distribution['high_open_low_close_count'] = high_open_low_close_count +df_distribution['high_open_normal_close_count'] = high_open_normal_close_count +df_distribution['high_open_high_close_count'] = high_open_high_close_count +# df_distribution["low_open_low_close_rate"] = low_open_low_close_count / total_count +# df_distribution["low_open_normal_close_rate"] = low_open_normal_close_count / total_count +# df_distribution["low_open_high_close_rate"] = low_open_high_close_count / total_count +# df_distribution["normal_open_low_close_rate"] = normal_open_low_close_count / total_count +# df_distribution["normal_open_normal_close_rate"] = normal_open_normal_close_count / total_count +# df_distribution["normal_open_high_close_rate"] = normal_open_high_close_count / total_count +# df_distribution["high_open_low_close_rate"] = high_open_low_close_count / total_count +# df_distribution["high_open_normal_close_rate"] = high_open_normal_close_count / total_count +# df_distribution["high_open_high_close_rate"] = high_open_high_close_count / total_count +# df_distribution["low_open_rate"] = low_open_count / total_count +# df_distribution["normal_open_rate"] = normal_open_count / total_count +# df_distribution["high_open_rate"] = high_open_count / total_count +# df_distribution["low_close_rate"] = low_close_count / total_count +# df_distribution["normal_close_rate"] = normal_close_count / total_count +# df_distribution["high_close_rate"] = high_close_count / total_count + + +# 删除'test'列 +df_distribution = df_distribution.drop(columns=['test']) + +# 创建一个新的图形 +fig, ax = plt.subplots() + +# 创建一个条形图 +df_distribution.plot(kind='bar', ax=ax) + +# 设置图形的标题和轴标签 +ax.set_title('Distribution') +ax.set_xlabel('Category') +ax.set_ylabel('Count') + +# 显示图形 +plt.show() \ No newline at end of file