import csv
import os
import time

import numpy as np
import pandas as pd
root='/Users/yumingwu/Downloads/逐笔/'

num =45
data=pd.read_csv('202303市值触板.csv')
data['日期']=pd.to_datetime(data['日期'])
data = data.groupby('日期')

with open('./'+'202303打板盈利首笔涨停大于500w1'+'.csv','w',encoding='gbk',newline='') as csfile:
    writer =csv.writer(csfile)
    writer.writerow(['代码','日期','成交量','成交额','换手率','买入价格','卖出价格','盈利','涨幅','10日涨幅','流通市值','涨停trainID','涨停BuyOrderID','涨停vol','涨停amount','大于500w'])
    for day,dataR in data:

        if(day>=pd.to_datetime('2023-04-01')):
            continue
        daystr= day.strftime("%Y-%m-%d")

        for index,item in dataR.iterrows():

            zhubiData= pd.read_csv(root+daystr+'/'+item['代码'][2:8]+'.csv')
            zhubiData=zhubiData[zhubiData['Time']>'09:25:00']
            zhubiData['amount']=zhubiData['Volume']*zhubiData['BuyOrderPrice']
            zhangtingjia=zhubiData['BuyOrderPrice'].max()
            maxPrice= zhubiData['Price'].max()
            maxPriceItem = zhubiData[zhubiData['Price']>=maxPrice]
            maxPriceItem=maxPriceItem.iloc[0]

            zhangtingdata = zhubiData[zhubiData['BuyOrderID']==maxPriceItem['BuyOrderID']]
            amount=zhangtingdata['amount'].sum()

            if(amount>=5000000):
                writer.writerow(
                    [item['代码'],
                     item['日期'],
                     item['成交量'],
                     item['成交额'],
                     item[ '换手率'],
                     item[ '买入价格'],
                     item['卖出价格'],
                     item['盈利'],
                     item['涨幅'],
                     item['10日涨幅'],
                     item['流通市值'],
                     maxPriceItem['TranID'],
                     maxPriceItem['BuyOrderID'],
                     zhangtingdata['Volume'].sum(),
                     amount,
                     1
                     ])
            else:
                writer.writerow(
                    [item['代码'],
                     item['日期'],
                     item['成交量'],
                     item['成交额'],
                     item['换手率'],
                     item['买入价格'],
                     item['卖出价格'],
                     item['盈利'],
                     item['涨幅'],
                     item['10日涨幅'],
                     item['流通市值'],
                     maxPriceItem['TranID'],
                     maxPriceItem['BuyOrderID'],
                     zhangtingdata['Volume'].sum(),
                     amount,
                     0
                     ])

