|  | @@ -0,0 +1,78 @@
 | 
	
		
			
				|  |  | +import threading
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import pymysql
 | 
	
		
			
				|  |  | +import pandas as pd
 | 
	
		
			
				|  |  | +from threading import Thread, current_thread, local
 | 
	
		
			
				|  |  | +from sqlalchemy import create_engine
 | 
	
		
			
				|  |  | +from datetime import datetime as dt
 | 
	
		
			
				|  |  | +starttime = dt.now()
 | 
	
		
			
				|  |  | +time = dt.strptime(dt.strftime(dt.now(),'%H:%M:%S'),'%H:%M:%S')
 | 
	
		
			
				|  |  | +# 数据库引擎
 | 
	
		
			
				|  |  | +# engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# 连接数据库
 | 
	
		
			
				|  |  | +db = pymysql.connect(host='localhost',
 | 
	
		
			
				|  |  | +                     user='root',
 | 
	
		
			
				|  |  | +                     port=3307,
 | 
	
		
			
				|  |  | +                     password='r6kEwqWU9!v3',
 | 
	
		
			
				|  |  | +                     database='qbh_hlfx')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +fre = '1d'
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +cursor = db.cursor()
 | 
	
		
			
				|  |  | +cursor.execute("select table_name from information_schema.tables where table_schema='qbh_hlfx' and table_name like {}".format('\'%{}\''.format(fre)))
 | 
	
		
			
				|  |  | +table_list = [tuple[0] for tuple in cursor.fetchall()]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# print(table_list)
 | 
	
		
			
				|  |  | +stk = threading.local()
 | 
	
		
			
				|  |  | +engine = locals()
 | 
	
		
			
				|  |  | +# 主程序
 | 
	
		
			
				|  |  | +# 找顶底(hdx lfx)分型
 | 
	
		
			
				|  |  | +def hlfx(table_list,engine):
 | 
	
		
			
				|  |  | +    for table in table_list:
 | 
	
		
			
				|  |  | +        # stk.fxdf = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
 | 
	
		
			
				|  |  | +        stk.df_day = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from %s' % table, engine)
 | 
	
		
			
				|  |  | +        for i in stk.df_day.index:
 | 
	
		
			
				|  |  | +            m = i-1
 | 
	
		
			
				|  |  | +            if i <= 3:
 | 
	
		
			
				|  |  | +                # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
 | 
	
		
			
				|  |  | +                stk.df_day.loc[i, 'HL'] = '-'
 | 
	
		
			
				|  |  | +            # 底
 | 
	
		
			
				|  |  | +            elif ((stk.df_day.loc[i,'high']>stk.df_day.loc[i-1,'high']) and (stk.df_day.loc[i-2,'high']>stk.df_day.loc[i-1,'high'])):
 | 
	
		
			
				|  |  | +                # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
 | 
	
		
			
				|  |  | +                    # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
 | 
	
		
			
				|  |  | +                while m:
 | 
	
		
			
				|  |  | +                    if (stk.df_day.loc[m, 'HL'] == 'H' and (i-m) > 3) \
 | 
	
		
			
				|  |  | +                            or (stk.df_day.loc[m, 'HL'] == 'L' and stk.df_day.loc[i-1, 'low'] < stk.df_day.loc[m-1, 'low']):
 | 
	
		
			
				|  |  | +                        # 前一个为顶,且中间存在不包含 or 更低的底
 | 
	
		
			
				|  |  | +                        stk.df_day.loc[i, 'HL'] = 'L'
 | 
	
		
			
				|  |  | +                        break
 | 
	
		
			
				|  |  | +                    m = m-1
 | 
	
		
			
				|  |  | +            # 顶
 | 
	
		
			
				|  |  | +            elif ((stk.df_day.loc[i,'high']<stk.df_day.loc[i-1,'high']) and (stk.df_day.loc[i-2,'high']<stk.df_day.loc[i-1,'high'])):
 | 
	
		
			
				|  |  | +                # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
 | 
	
		
			
				|  |  | +                #     stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
 | 
	
		
			
				|  |  | +                while m:
 | 
	
		
			
				|  |  | +                    if (stk.df_day.loc[m, 'HL'] == 'L' and (i-m) > 3) \
 | 
	
		
			
				|  |  | +                            or (stk.df_day.loc[m, 'HL'] == 'H' and stk.df_day.loc[i-1, 'high'] > stk.df_day.loc[m-1, 'high']):
 | 
	
		
			
				|  |  | +                        # 前一个为底,且中间存在不包含 or 更高的顶
 | 
	
		
			
				|  |  | +                        stk.df_day.loc[i, 'HL'] = 'H'
 | 
	
		
			
				|  |  | +                        break
 | 
	
		
			
				|  |  | +                    m = m-1
 | 
	
		
			
				|  |  | +            else:
 | 
	
		
			
				|  |  | +                stk.df_day.loc[i, 'HL'] = '-'
 | 
	
		
			
				|  |  | +        # stk.df_day.to_sql('%s' % table, con=engine, index=True, if_exists='replace', chunksize=20000)
 | 
	
		
			
				|  |  | +        print(table, '\n', stk.df_day)
 | 
	
		
			
				|  |  | +        stk.df_day.to_csv('/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220212hlfx/this%s.csv' % table)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# table_list = ['stk002237_1d','stk000002_1d']
 | 
	
		
			
				|  |  | +# hlfx(table_list)
 | 
	
		
			
				|  |  | +step = 50
 | 
	
		
			
				|  |  | +for i in range(0, len(table_list), step):
 | 
	
		
			
				|  |  | +    engine[i] = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8')
 | 
	
		
			
				|  |  | +    threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i])).start()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +endtime=dt.now()
 | 
	
		
			
				|  |  | +print((endtime - starttime).seconds)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 |