Daniel 3 лет назад
Родитель
Сommit
daa11d6870
2 измененных файлов с 32 добавлено и 34 удалено
  1. 2 2
      hlfx.py
  2. 30 32
      qbh.py

+ 2 - 2
hlfx.py

@@ -101,8 +101,8 @@ engine = []
 tosql = []
 tosql = []
 for i in range(0, len(table_list), step):
 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'))
     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'))
-    thread = threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i], tosql[i],))
+    tosql.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_backup?charset=utf8'))
+    thread = threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i], tosql[i]))
     thread.start()
     thread.start()
     thread_list.append(thread)
     thread_list.append(thread)
 
 

+ 30 - 32
qbh.py

@@ -29,44 +29,42 @@ import threading
 engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
 engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
 
 
 stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
 stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
-stocks =stocks[0:40]
+# stocks =stocks[0:40]
 
 
 thd = threading.local()
 thd = threading.local()
 
 
 
 
 def qbh(stocks, engine, engine_backup):
 def qbh(stocks, engine, engine_backup):
     for stock in stocks:
     for stock in stocks:
-        print(engine)
-        print(engine_backup)
-        # thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
-        # # print(new_df.head())
-        # thd.df_day = stk['stk' + stock[:6]]
-        # for i in thd.df_day.index:
-        #     if i == 0 or i == 1:
-        #         thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
-        #     # 不包含
-        #     elif (thd.new_df.iloc[-1, 3] > thd.df_day.loc[i, 'high']
-        #           and thd.new_df.iloc[-1, 4] > thd.df_day.loc[i, 'low']) \
-        #             or (thd.new_df.iloc[-1, 3] < thd.df_day.loc[i, 'high']
-        #                 and thd.new_df.iloc[-1, 4] < thd.df_day.loc[i, 'low']):
-        #         thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
-        #     # 包含
-        #     else:
-        #         # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
-        #         # 左高,下降
-        #         if thd.new_df.iloc[-2, 3] > thd.new_df.iloc[-1, 3]:
-        #             thd.new_df.iloc[-1, 3] = min(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
-        #             thd.new_df.iloc[-1, 4] = min(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
-        #         else:
-        #             # 右高,上升
-        #             thd.new_df.iloc[-1, 3] = max(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
-        #             thd.new_df.iloc[-1, 4] = max(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
-        # thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine, index=True, if_exists='replace')
-        # with engine.connect() as con:
-        #     con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
-        # thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine_backup, index=True, if_exists='replace')
-        # with engine_backup.connect() as con_backup:
-        #     con_backup.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
+        thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
+        # print(new_df.head())
+        thd.df_day = stk['stk' + stock[:6]]
+        for i in thd.df_day.index:
+            if i == 0 or i == 1:
+                thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
+            # 不包含
+            elif (thd.new_df.iloc[-1, 3] > thd.df_day.loc[i, 'high']
+                  and thd.new_df.iloc[-1, 4] > thd.df_day.loc[i, 'low']) \
+                    or (thd.new_df.iloc[-1, 3] < thd.df_day.loc[i, 'high']
+                        and thd.new_df.iloc[-1, 4] < thd.df_day.loc[i, 'low']):
+                thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
+            # 包含
+            else:
+                # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
+                # 左高,下降
+                if thd.new_df.iloc[-2, 3] > thd.new_df.iloc[-1, 3]:
+                    thd.new_df.iloc[-1, 3] = min(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
+                    thd.new_df.iloc[-1, 4] = min(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
+                else:
+                    # 右高,上升
+                    thd.new_df.iloc[-1, 3] = max(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
+                    thd.new_df.iloc[-1, 4] = max(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
+        thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine, index=True, if_exists='replace')
+        with engine.connect() as con:
+            con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
+        thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine_backup, index=True, if_exists='replace')
+        with engine_backup.connect() as con_backup:
+            con_backup.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
         # thd.new_df.to_csv(
         # thd.new_df.to_csv(
         #     '/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220211qbh/qbh%s.csv' % stock[:6])
         #     '/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220211qbh/qbh%s.csv' % stock[:6])
         # print(stock)
         # print(stock)