import csv
import os
import time

import numpy as np
import pandas as pd
root='/Users/yumingwu/Desktop/prelog_300_600/'
path =os.listdir(root)
path.sort()

root1='/Users/yumingwu/Downloads/Stk_Day_QFQ/'

with open('./300-600.csv','w',encoding='gbk',newline='') as csfile:
    writer =csv.writer(csfile)
    writer.writerow(['代码','买入日期','买入价格','卖出日期','卖出价格','涨幅','盈利'])
    for spath in path:
        if spath.find('pre')==-1:
            continue

        day=spath[len(spath)-4-8:len(spath)-4]
        print(day)
        data= pd.read_table(root+spath)
        rows=[]
        for item in data.iterrows():
            s=item[1][0]
            if s.find('[INF]buy')!=-1:
                s= s.split(',')
                code=s[0]
                price = s[2]
                price=float(price)
                code=code[len(code)-6:len(code)]
                if code in rows:
                    continue
                rows.append(code)
                if int(code)>600000:
                    code='SH'+code
                else:
                    code = 'SZ' + code

                codedata= pd.read_csv(root1+code+'.csv',encoding='gbk')
                codedata['时间']=pd.to_datetime(codedata['时间'])
                codedata=codedata[codedata['时间']>pd.to_datetime(day)]
                if len(codedata)==0:
                    continue
                codeitem = codedata.iloc[0]
                writer.writerow([codeitem['代码'], day, price, codeitem['时间'], codeitem['开盘价'], codeitem['开盘价']/price-1, (codeitem['开盘价']/price-1-0.0014)*50000])










