|
@@ -26,60 +26,18 @@ starttime = dt.now()
|
|
|
|
|
|
|
|
|
|
|
|
-engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/sit?charset=utf8')
|
|
|
+engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
|
|
|
|
|
|
stocks = list(get_all_securities(['stock'], date='2022-02-01').index)
|
|
|
-stocks =stocks[0:70]
|
|
|
+
|
|
|
|
|
|
thd = threading.local()
|
|
|
|
|
|
|
|
|
-def qbh(stocks, engine, engine_backup):
|
|
|
- for stock in stocks:
|
|
|
- thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
|
|
|
-
|
|
|
- thd.df_day = stk['stk' + stock]
|
|
|
- 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:
|
|
|
-
|
|
|
-
|
|
|
- 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, fre), con=engine, index=True, if_exists='append')
|
|
|
- with engine.connect() as con:
|
|
|
- con.execute('ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);' % (stock, fre))
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- print(stock)
|
|
|
- print("**************")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
+fre = '30m'
|
|
|
stk = locals()
|
|
|
-engine = []
|
|
|
-engine_backup = []
|
|
|
-
|
|
|
-fre = '1d'
|
|
|
|
|
|
|
|
|
for stock in stocks:
|
|
@@ -93,22 +51,69 @@ print("#########################################################################
|
|
|
"###############################################################################################################"
|
|
|
"###############################################################################################################"
|
|
|
"###############################################################################################################")
|
|
|
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh?charset=utf8')
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-thread_list = []
|
|
|
-step = 5
|
|
|
-times_engine = 0
|
|
|
-for m in range(0, len(stocks), step):
|
|
|
- engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh?charset=utf8'))
|
|
|
- engine_backup.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8'))
|
|
|
- thread = threading.Thread(target=qbh, args=(stocks[m:m + step], engine[times_engine], engine_backup[times_engine]))
|
|
|
- times_engine =times_engine + 1
|
|
|
- thread.start()
|
|
|
- thread_list.append(thread)
|
|
|
+for stock in stocks:
|
|
|
+ thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
|
|
|
+
|
|
|
+ thd.df_day = stk['stk' + stock]
|
|
|
+ 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:
|
|
|
+
|
|
|
+
|
|
|
+ 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, fre), con=engine, index=True, if_exists='append')
|
|
|
+ with engine.connect() as con:
|
|
|
+ con.execute('ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);' % (stock, fre))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ print(stock)
|
|
|
+ print("**************")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-for thread in thread_list:
|
|
|
- thread.join()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
endtime = dt.now()
|
|
|
print((endtime-starttime).seconds)
|