import csv
import os
import time

import numpy as np
import pandas as pd
root='/Users/yumingwu/Downloads/Stk_Day_QFQ/'
path =os.listdir(root)
path.sort()
num =30


with open('./'+'202303-30日未触板1-3月'+'.csv','w',encoding='gbk',newline='') as csfile:
    writer =csv.writer(csfile)
    writer.writerow(['代码','日期','昨日收盘价','开盘价','最低价','最高价涨幅','成交量','成交额','换手率','买入价格','卖出价格','盈利','涨幅','10日涨幅','流通市值'])
    guben = pd.read_excel('../全部A股.xlsx')
    for spath in path:
        # 过滤000
        if spath.find("SZ0") == -1:
            continue

        print(spath)
        dataS = pd.read_csv(root + spath, encoding='gbk')
        dataS['时间'] = pd.to_datetime(dataS['时间'])
        data1=dataS[dataS['时间']>=pd.to_datetime('20230101')]
        for indexitemn in np.arange(len(data1)):
            if indexitemn==len(data1)-1:
                continue
            itemn=data1.iloc[indexitemn]

            itemn1 = data1.iloc[indexitemn+1]
            data=dataS[dataS['时间']<itemn['时间']]

            data = data.iloc[len(data) - num - 1:len(data)]

            if (len(data) < num + 1):
                continue
            zhangting = 0
            for i in np.arange(len(data)):
                if i > 0:
                    item0 = data.iloc[i - 1]
                    item1 = data.iloc[i]
                    if item1['最高价'] / item0['收盘价'] > 1.098:
                        zhangting = 1
                        break

            if zhangting == 0:

                item = data.iloc[-1]
                if itemn['最高价'] / item['收盘价'] <= 1.05:
                    continue
                data10 = data.iloc[len(data) - 10 - 1:len(data)]
                minprice = data10['开盘价'].min()
                zhangfu = item['收盘价'] / minprice
                # if zhangfu >= 1.15:
                #     continue

                code = item['代码'][2:8] + "." + item['代码'][0:2]
                gubenitem = guben[guben['证券代码'] == code]
                if len(gubenitem) == 0:
                    continue
                gubenitem = gubenitem.iloc[0]

                shizhi = gubenitem['流通A股'] * item['收盘价'] / 100000000.0
                # if shizhi > 200:
                #     continue
                writer.writerow([itemn['代码'],
                                 itemn['时间'],
                                 item['收盘价'],
                                 itemn['开盘价'],
                                 itemn['最低价'],
                                 itemn['最高价']/item['收盘价']-1,
                                 item['成交量(股)'],
                                 item['成交额(元)'],
                                 item['成交量(股)'] / gubenitem['流通A股'],
                                 itemn['最高价'],
                                 itemn1['开盘价'],
                                 50000*(itemn1['开盘价']/ itemn['最高价']-1-0.0014),
                                 itemn1['开盘价'] / itemn['最高价']-1,
                                 zhangfu,
                                 shizhi
                                 ])
















