123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- from jqdatasdk import *
- import pandas as pd
- import pymysql
- from sqlalchemy import create_engine
- import threading
- from datetime import datetime as dt
- import datetime
- auth('18019403367', 'Qwer4321')
- stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
- start = dt.now()
- for fre in ['30m', '1d']:
- start = dt.now()
- print(fre)
-
- db = pymysql.connect(host='localhost',
- user='root',
- port=3307,
- password='r6kEwqWU9!v3',
- database='hlfx')
- cursor = db.cursor()
- cursor.execute("show tables like '%%%s%%' " % fre)
- table_list = [tuple[0] for tuple in cursor.fetchall()]
- print('取得 table_list %s' % fre)
- stk = locals()
- thd = threading.local()
- def hlfx(stocks, engine, engine2):
- for thd.stock in stocks:
- print(thd.stock)
- if ('stk%s_%s' % (thd.stock, fre)) in table_list:
-
- index_len = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine2).iloc[-1, 0]
-
- startdate = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine2).iloc[-1, 1]
-
- thd.get_price = pd.read_sql_query(
- 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (thd.stock, fre), engine)
- thd.get_price = thd.get_price.loc[thd.get_price['date'] > startdate]
- thd.df_day = pd.read_sql_query(
- 'select date,open,close,high,low,volume,money,HL from `stk%s_%s`' % (thd.stock, fre), engine2)
-
- for i in thd.get_price.index:
-
- if (thd.df_day.iloc[-1, 3] > thd.get_price.loc[i, 'high']
- and thd.df_day.iloc[-1, 4] > thd.get_price.loc[i, 'low']) \
- or (thd.df_day.iloc[-1, 3] < thd.get_price.loc[i, 'high']
- and thd.df_day.iloc[-1, 4] < thd.get_price.loc[i, 'low']):
- thd.df_day = pd.concat([thd.df_day, thd.get_price.loc[[i]]], ignore_index=True)
-
-
- else:
-
-
- if thd.df_day.iloc[-2, 3] > thd.df_day.iloc[-1, 3]:
- thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
- thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
- else:
-
- thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
- thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
-
- if len(thd.df_day.index) > 2:
-
- for x in range(index_len, len(thd.df_day.index)):
- m = x - 1
-
- if ((thd.df_day.loc[x, 'high'] > thd.df_day.loc[x - 1, 'high']) and (
- thd.df_day.loc[x - 2, 'high'] > thd.df_day.loc[x - 1, 'high'])):
-
-
- thd.df_day.loc[x, 'HL'] = 'L*'
- while m:
- if thd.df_day.loc[m, 'HL'] == 'H':
- if (x - m) > 3:
- thd.df_day.loc[x, 'HL'] = 'L'
- if x == len(thd.df_day.index) - 1:
- print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
- break
- elif (thd.df_day.loc[m, 'HL'] == 'L'):
- if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
-
- thd.df_day.loc[x, 'HL'] = 'L'
- if x == len(thd.df_day.index) - 1:
-
- print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
- break
- else:
- break
- m = m - 1
- if m == 0:
- thd.df_day.loc[x, 'HL'] = 'L'
-
- elif ((thd.df_day.loc[x, 'high'] < thd.df_day.loc[x - 1, 'high']) and (
- thd.df_day.loc[x - 2, 'high'] < thd.df_day.loc[x - 1, 'high'])):
-
-
- thd.df_day.loc[x, 'HL'] = 'H*'
- while m:
- if thd.df_day.loc[m, 'HL'] == 'L':
- if x - m > 3:
- thd.df_day.loc[x, 'HL'] = 'H'
- if x == len(thd.df_day.index) - 1:
-
- pass
- thd.df_day.loc[x, 9] = thd.df_day.loc[x, 'close'] - thd.df_day.loc[m, 'close']
- break
- elif (thd.df_day.loc[m, 'HL'] == 'H'):
- if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
-
- thd.df_day.loc[x, 'HL'] = 'H'
- if x == len(thd.df_day.index) - 1:
- pass
-
- break
- break
- m = m - 1
- if m == 0:
- thd.df_day.loc[x, 'HL'] = 'H'
- else:
- thd.df_day.loc[x, 'HL'] = '-'
-
- thd.df_day[index_len + 1:].to_sql('stk%s_%s' % (thd.stock, fre), con=engine2, index=True, if_exists='append')
- else:
-
- thd.df_day = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
- thd.get_price = pd.read_sql_query(
- 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (thd.stock, fre), engine)
-
- for i in thd.get_price.index:
- if i == 0 or i == 1:
- thd.df_day = pd.concat([thd.df_day, thd.get_price.iloc[[i]]], ignore_index=True)
-
- elif (thd.df_day.iloc[-1, 3] > thd.get_price.loc[i, 'high']
- and thd.df_day.iloc[-1, 4] > thd.get_price.loc[i, 'low']) \
- or (thd.df_day.iloc[-1, 3] < thd.get_price.loc[i, 'high']
- and thd.df_day.iloc[-1, 4] < thd.get_price.loc[i, 'low']):
- thd.df_day = pd.concat([thd.df_day, thd.get_price.loc[[i]]], ignore_index=True)
-
- else:
-
- if thd.df_day.iloc[-2, 3] > thd.df_day.iloc[-1, 3]:
- thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
- thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
- else:
-
- thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
- thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
- if len(thd.df_day.index) > 2:
-
- for x in range(2, len(thd.df_day.index)):
- m = x - 1
-
- if ((thd.df_day.loc[x, 'high'] > thd.df_day.loc[x - 1, 'high']) and (
- thd.df_day.loc[x - 2, 'high'] > thd.df_day.loc[x - 1, 'high'])):
-
-
- thd.df_day.loc[x, 'HL'] = 'L*'
- while m:
- if thd.df_day.loc[m, 'HL'] == 'H':
- if (x - m) > 3:
- thd.df_day.loc[x, 'HL'] = 'L'
- if x == len(thd.df_day.index) - 1:
- print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
- break
- elif (thd.df_day.loc[m, 'HL'] == 'L'):
- if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
-
- thd.df_day.loc[x, 'HL'] = 'L'
- if x == len(thd.df_day.index) - 1:
-
- print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
- break
- else:
- break
- m = m - 1
- if m == 0:
- thd.df_day.loc[x, 'HL'] = 'L'
-
- elif ((thd.df_day.loc[x, 'high'] < thd.df_day.loc[x - 1, 'high']) and (
- thd.df_day.loc[x - 2, 'high'] < thd.df_day.loc[x - 1, 'high'])):
-
-
- thd.df_day.loc[x, 'HL'] = 'H*'
- while m:
- if thd.df_day.loc[m, 'HL'] == 'L':
- if x - m > 3:
- thd.df_day.loc[x, 'HL'] = 'H'
- if x == len(thd.df_day.index) - 1:
-
- pass
- thd.df_day.loc[x, 9] = thd.df_day.loc[x, 'close'] - thd.df_day.loc[m, 'close']
- break
- elif (thd.df_day.loc[m, 'HL'] == 'H'):
- if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
-
- thd.df_day.loc[x, 'HL'] = 'H'
- if x == len(thd.df_day.index) - 1:
- pass
-
- break
- break
- m = m - 1
- if m == 0:
- thd.df_day.loc[x, 'HL'] = 'H'
- else:
- thd.df_day.loc[x, 'HL'] = '-'
-
- thd.df_day.to_sql('stk%s_%s' % (thd.stock, fre), con=engine2, index=True, if_exists='append')
- step = 500
- thread_list = []
- engine = []
- engine2 = []
- times_engine = 0
- for i in range(0, len(stocks), step):
- engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8'))
- engine2.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8'))
- thread = threading.Thread(target=hlfx, args=(stocks[i:i + step], engine[times_engine], engine2[times_engine]))
- times_engine = times_engine + 1
- thread.start()
- thread_list.append(thread)
- for thread in thread_list:
- thread.join()
- db.close()
- end= dt.now()
- print('总时长:', (end - start).seconds)
|