123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- from jqdatasdk import *
- import pandas as pd
- import pymysql
- from sqlalchemy import create_engine
- import threading
- from datetime import datetime as dt
- auth('18019403367', 'Qwer4321')
- starttime = dt.now()
- engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
- stocks = list(get_all_securities(['stock'], date='2022-02-01').index)
- thd = threading.local()
- fre = '30m'
- stk = locals()
- for stock in stocks:
- stk['stk'+stock] = pd.read_sql_query('select date,open,close,high,low,volume,money from `stk%s_%s`'
- % (stock, fre), engine2)
-
- print("###############################################################################################################"
- "###############################################################################################################"
- "###############################################################################################################"
- "###############################################################################################################"
- "###############################################################################################################"
- "###############################################################################################################"
- "###############################################################################################################")
- engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh?charset=utf8')
- 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("**************")
-
-
-
-
- endtime = dt.now()
- print((endtime-starttime).seconds)
|