Daniel 3 роки тому
батько
коміт
2b7c1ce74f
1 змінених файлів з 48 додано та 23 видалено
  1. 48 23
      hlfx.py

+ 48 - 23
hlfx.py

@@ -1,12 +1,9 @@
 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')
 
@@ -15,25 +12,29 @@ db = pymysql.connect(host='localhost',
                      user='root',
                      port=3307,
                      password='r6kEwqWU9!v3',
-                     database='qbh_hlfx')
+                     database='qbh_hlfx_backup')
 
 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)))
+# cursor.execute("select table_name from information_schema.tables where table_schema='qbh_hlfx_backup' and table_name like {}".format('\'%{}\''.format(fre)))
+cursor.execute('show tables like {}'.format('\'%{}\''.format(fre)))
 table_list = [tuple[0] for tuple in cursor.fetchall()]
 
 # print(table_list)
 stk = threading.local()
 engine = locals()
+engine_tosql = locals()
+
+
 # 主程序
 # 找顶底(hdx lfx)分型
-def hlfx(table_list,engine):
+def hlfx(table_list, engine, engine_tosql):
     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
+            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'] = '-'
@@ -41,38 +42,62 @@ def hlfx(table_list,engine):
             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)
+                stk.df_day.loc[i, 'HL'] = 'L*'
                 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'
+                    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']:
+                            # 前一个为顶,且中间存在不包含 or 更低的底
+                            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'])):
                 # 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)
+                stk.df_day.loc[i, 'HL'] = 'H*'
                 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'
+                    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']:
+                            # 前一个为底,且中间存在不包含 or 更高的顶
+                            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=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)
+        # print(table, '\n', stk.df_day)
+        stk.df_day.to_csv('/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220212hlfx2/hlfx%s.csv' % table)
+        stk.df_day.to_sql('%s' % table, con=engine_tosql, index=True, if_exists='replace')
+        with engine_tosql.connect() as con_backup:
+            con_backup.execute('ALTER TABLE %s ADD PRIMARY KEY (`date`);' % table)
 
 # table_list = ['stk002237_1d','stk000002_1d']
-# hlfx(table_list)
+# engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8')
+# hlfx(table_list, engine)
+
 step = 50
+thread_list = []
 for i in range(0, len(table_list), step):
+    engine_tosql[i] = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_backup?charset=utf8')
     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()
+    threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i], engine_tosql[i])).start()
 
-endtime=dt.now()
-print((endtime - starttime).seconds)