123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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')
- 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()]
- stk = threading.local()
- engine = locals()
- def hlfx(table_list,engine):
- 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'])):
-
-
- 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']):
-
- 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'])):
-
-
- 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']):
-
- stk.df_day.loc[i, 'HL'] = 'H'
- break
- m = m-1
- else:
- stk.df_day.loc[i, 'HL'] = '-'
-
- print(table, '\n', stk.df_day)
- stk.df_day.to_csv('/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220212hlfx/this%s.csv' % table)
- 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)
|