import csv
import os
import time

import numpy as np
import pandas as pd
data_huice=pd.read_csv('202301-03打板盈利首笔大于500w开盘价.csv',encoding='gbk')
data_huice['日期']=pd.to_datetime(data_huice['日期'])
data_huice=data_huice[data_huice['日期']>=pd.to_datetime('20230317')]
data_shipan= pd.read_excel('/Users/yumingwu/Desktop/打板.xlsx')
data_shipan['买入时间']=pd.to_datetime(data_shipan['买入时间'])
data_shipan=data_shipan[data_shipan['买入时间']>=pd.to_datetime('20230317')]
data_shipan=data_shipan[data_shipan['买入时间']<pd.to_datetime('20230401')]

data_shipan_weichengjiao= pd.read_excel('/Users/yumingwu/Desktop/打板未成交.xlsx')
data_shipan_weichengjiao['买入时间']=pd.to_datetime(data_shipan_weichengjiao['买入时间'])
data_shipan_weichengjiao=data_shipan_weichengjiao[data_shipan_weichengjiao['买入时间']>=pd.to_datetime('20230317')]
data_shipan_weichengjiao=data_shipan_weichengjiao[data_shipan_weichengjiao['买入时间']<pd.to_datetime('20230401')]


with open('./'+'回测买入重合'+'.csv','w',encoding='gbk',newline='') as csfile:
    writer =csv.writer(csfile)
    writer.writerow(['代码','日期', '昨日收盘价','成交量','成交额','换手率','买入价格','卖出价格','盈利','涨幅','10日涨幅','流通市值','涨停trainID','涨停BuyOrderID','涨停vol','涨停amount','大于500w','实盘'])

    for index, item in data_huice.iterrows():
        code = item['代码'][2:8]
        if item['开盘价']==item['买入价格']:
            continue
        if item['流通市值']>200:
            continue

        data_shipan0 =data_shipan[data_shipan['代码']==int(code)]
        data_shipan_weichengjiao0 = data_shipan_weichengjiao[data_shipan_weichengjiao['代码'] == int(code)]
        if len(data_shipan0)>0:
            writer.writerow(
                [item['代码'],
                 item['日期'],
                 item['昨日收盘价'],
                 item['成交量'],
                 item['成交额'],
                 item['换手率'],
                 item['买入价格'],
                 item['卖出价格'],
                 item['盈利'],
                 item['涨幅'],
                 item['10日涨幅'],
                 item['流通市值'],
                 item['涨停trainID'],
                 item['涨停BuyOrderID'],
                 item['涨停vol'],
                 item['涨停amount'],
                 1,
                 0

                 ])

        elif len(data_shipan_weichengjiao0)>0:
            data_shipan_weichengjiao0=data_shipan_weichengjiao0.iloc[0]
            if data_shipan_weichengjiao0['未成交原因'].find('撤单')==-1:
                writer.writerow(
                    [item['代码'],
                     item['日期'],
                     item['昨日收盘价'],
                     item['成交量'],
                     item['成交额'],
                     item['换手率'],
                     item['买入价格'],
                     item['卖出价格'],
                     item['盈利'],
                     item['涨幅'],
                     item['10日涨幅'],
                     item['流通市值'],
                     item['涨停trainID'],
                     item['涨停BuyOrderID'],
                     item['涨停vol'],
                     item['涨停amount'],
                     1,
                     1

                     ])
            else:
                writer.writerow(
                    [item['代码'],
                     item['日期'],
                     item['昨日收盘价'],
                     item['成交量'],
                     item['成交额'],
                     item['换手率'],
                     item['买入价格'],
                     item['卖出价格'],
                     item['盈利'],
                     item['涨幅'],
                     item['10日涨幅'],
                     item['流通市值'],
                     item['涨停trainID'],
                     item['涨停BuyOrderID'],
                     item['涨停vol'],
                     item['涨停amount'],
                     1,
                     3

                     ])


        else:
            writer.writerow(
                [item['代码'],
                 item['日期'],
                 item['昨日收盘价'],
                 item['成交量'],
                 item['成交额'],
                 item['换手率'],
                 item['买入价格'],
                 item['卖出价格'],
                 item['盈利'],
                 item['涨幅'],
                 item['10日涨幅'],
                 item['流通市值'],
                 item['涨停trainID'],
                 item['涨停BuyOrderID'],
                 item['涨停vol'],
                 item['涨停amount'],
                 1,
                 2

                 ])


