Daniel 3 anni fa
parent
commit
3e1adcbe2b
1 ha cambiato i file con 68 aggiunte e 64 eliminazioni
  1. 68 64
      hlfx.py

+ 68 - 64
hlfx.py

@@ -19,7 +19,7 @@ fre = '1d'
 cursor = db.cursor()
 # 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()]
+table_list = [tuple[0] for tuple in cursor.fetchall()]
 
 # print(table_list)
 stk = threading.local()
@@ -31,68 +31,66 @@ stk = threading.local()
 def hlfx(table_list, 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)
-        print(engine)
-        print(tosql)
-        # stk.df_day.to_sql(name='%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)
-        # 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)
-        #         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']:
-        #                     # 前一个为顶,且中间存在不包含 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 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/20220212hlfx2/hlfx%s.csv' % table)
-        # stk.df_day.to_sql(name='%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)
-
-table_list = ['stk002237_1d','stk000004_1d']
+        stk.df_day = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from %s' % table, engine)
+        stk.df_day.to_sql(name='%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)
+        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)
+                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']:
+                            # 前一个为顶,且中间存在不包含 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 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/20220212hlfx2/hlfx%s.csv' % table)
+        stk.df_day.to_sql(name='%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)
+
+# table_list = ['stk002237_1d','stk000004_1d']
 # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8')
 # tosql = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/bb22?charset=utf8')
 # hlfx(table_list, engine, tosql)
@@ -104,7 +102,13 @@ tosql = []
 for i in range(0, len(table_list), step):
     engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8'))
     tosql.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/bb22?charset=utf8'))
-    threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i], tosql[i],)).start()
+    thread = threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i], tosql[i],))
+    thread.start()
+    thread_list.append(thread)
+
+for thread in thread_list:
+    thread.join()
+