123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import threading
- import pymysql
- import pandas as pd
- from sqlalchemy import create_engine
- from datetime import datetime as dt
- starttime = dt.now()
- db = pymysql.connect(host='localhost',
- user='root',
- port=3307,
- password='r6kEwqWU9!v3',
- database='qbh')
- fre = '30m'
- cursor = db.cursor()
- cursor.execute("show tables like '%%%s%%' "% fre)
- table_list = [tuple[0] for tuple in cursor.fetchall()]
- stk = threading.local()
- def hlfx(table_list, engine, tosql):
- for table in table_list:
-
- 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.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'])):
-
-
- stk.df_day.loc[i, 'HL'] = 'L*'
- while m:
- if m == 1:
- stk.df_day.loc[i, 'HL'] = 'l'
- elif stk.df_day.loc[m, 'HL'] == 'H' or stk.df_day.loc[m, 'HL'] == 'h':
- if(i-m) > 3:
- stk.df_day.loc[i, 'HL'] = 'L'
- break
- elif (stk.df_day.loc[m, 'HL'] == 'L' or stk.df_day.loc[m, 'HL'] == 'l'):
- if stk.df_day.loc[i-1, 'low'] < stk.df_day.loc[m-1, 'low']:
-
- stk.df_day.loc[i, 'HL'] = 'L'
- break
- else:
- 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'])):
-
-
- stk.df_day.loc[i, 'HL'] = 'H*'
- while m:
- if m == 1:
- stk.df_day.loc[i, 'HL'] = 'h'
- elif stk.df_day.loc[m, 'HL'] == 'L' or stk.df_day.loc[m, 'HL'] == 'l':
- if i-m > 3:
- stk.df_day.loc[i, 'HL'] = 'H'
- stk.df_day.loc[i, 9] = stk.df_day.loc[i, 'close'] - stk.df_day.loc[m, 'close']
- break
- elif (stk.df_day.loc[m, 'HL'] == 'H' or stk.df_day.loc[m, 'HL'] == 'h'):
- if stk.df_day.loc[i-1, 'high'] > stk.df_day.loc[m-1, 'high']:
-
- stk.df_day.loc[i, 'HL'] = 'H'
- break
- break
- m = m-1
- else:
- stk.df_day.loc[i, 'HL'] = '-'
-
- stk.df_day.to_sql('%s' % table, con=tosql, index=True, if_exists='replace')
- with tosql.connect() as con_backup:
- con_backup.execute('ALTER TABLE `%s` ADD PRIMARY KEY (`date`);' % table)
- print(table, '\n', '**********************************')
- step = 100
- thread_list = []
- engine = []
- tosql = []
- times_engine = 0
- for i in range(0, len(table_list), step):
- engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh?charset=utf8'))
- tosql.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8'))
- thread = threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[times_engine], tosql[times_engine]))
- times_engine = times_engine + 1
- thread.start()
- thread_list.append(thread)
- for thread in thread_list:
- thread.join()
- endtime = dt.now()
- print('总时长:', (endtime - starttime).seconds)
|