|
@@ -4,26 +4,16 @@ import pymysql
|
|
|
from sqlalchemy import create_engine
|
|
|
import threading
|
|
|
from datetime import datetime as dt
|
|
|
-import datetime
|
|
|
+from jqdatasdk.technical_analysis import *
|
|
|
|
|
|
-
|
|
|
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
|
|
|
|
|
|
-engine_stocks_list = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-stocks = pd.read_sql_query(
|
|
|
- 'select securities from stocks_list', engine_stocks_list)
|
|
|
-stocks = stocks.iloc[-1, 0]
|
|
|
-stocks = stocks.split(",")
|
|
|
-print(len(stocks), type(stocks), stocks)
|
|
|
-
|
|
|
+stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
|
|
|
+
|
|
|
|
|
|
start = dt.now()
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-for fre in ['1d', '30m']:
|
|
|
+
|
|
|
+for fre in ['1d']:
|
|
|
start = dt.now()
|
|
|
print(fre)
|
|
|
|
|
@@ -37,111 +27,154 @@ for fre in ['1d', '30m']:
|
|
|
table_list = [tuple[0] for tuple in cursor.fetchall()]
|
|
|
print('取得 table_list %s' % fre)
|
|
|
|
|
|
+ db_pool = pymysql.connect(host='localhost',
|
|
|
+ user='root',
|
|
|
+ port=3307,
|
|
|
+ password='r6kEwqWU9!v3',
|
|
|
+ database='hlfx_pool')
|
|
|
+ cursor_pool = db_pool.cursor()
|
|
|
+
|
|
|
+
|
|
|
stk = locals()
|
|
|
thd = threading.local()
|
|
|
|
|
|
- def hlfx(stocks, engine, engine2):
|
|
|
+ def hlfx(stocks, engine_stock, engine_hlfx):
|
|
|
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]
|
|
|
- if index_len > 2:
|
|
|
-
|
|
|
- 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)
|
|
|
-
|
|
|
-
|
|
|
+ index_len = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine_hlfx).iloc[-1, 0]
|
|
|
+ startdate = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine_hlfx).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_stock)
|
|
|
+ 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), engine_hlfx)
|
|
|
+
|
|
|
+
|
|
|
+ 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:
|
|
|
-
|
|
|
-
|
|
|
- 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.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'
|
|
|
-
|
|
|
- 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
|
|
|
+
|
|
|
+
|
|
|
+ if x == len(thd.df_day.index) - 1 :
|
|
|
+
|
|
|
+ print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '\n', thd.df_day.loc[m, 'date'], '买买买买买!!')
|
|
|
+ results.append(thd.stock)
|
|
|
+ print('222')
|
|
|
+
|
|
|
+ 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'
|
|
|
+ x_macd_dif, x_macd_dea, x_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[x, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ m_macd_dif, m_macd_dea, m_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[m, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ if x == len(thd.df_day.index) - 1 and x_macd_dif[thd.stock] > m_macd_dif[thd.stock]:
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], 'MACD背驰————买吗?!')
|
|
|
+ results.append(thd.stock)
|
|
|
+ print('333')
|
|
|
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
|
|
|
+ else:
|
|
|
break
|
|
|
- m = m - 1
|
|
|
- if m == 0:
|
|
|
+ m = m - 1
|
|
|
+ if m == 0:
|
|
|
+ thd.df_day.loc[x, 'HL'] = 'L'
|
|
|
+ results.append(thd.stock)
|
|
|
+ print('444')
|
|
|
+
|
|
|
+ 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'
|
|
|
- else:
|
|
|
- thd.df_day.loc[x, 'HL'] = '-'
|
|
|
+ if x == len(thd.df_day.index) - 1:
|
|
|
+ print(thd.stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
|
|
|
+
|
|
|
+ results_short.append(thd.stock)
|
|
|
+ if thd.stock in results:
|
|
|
+ results.remove(thd.stock)
|
|
|
+
|
|
|
+ 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'
|
|
|
+ x_macd_dif, x_macd_dea, x_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[x, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ m_macd_dif, m_macd_dea, m_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[m, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ if x == len(thd.df_day.index) - 1 and x_macd_dif[thd.stock] < m_macd_dif[thd.stock]:
|
|
|
+
|
|
|
+ print(thd.stock, '/\/\/\/\/\/\/', '顶背离了!!!!')
|
|
|
+ results_short.append(thd.stock)
|
|
|
+ if thd.stock in results:
|
|
|
+ results.remove(thd.stock)
|
|
|
+ break
|
|
|
+ break
|
|
|
+ m = m - 1
|
|
|
+ if m == 0:
|
|
|
+ thd.df_day.loc[x, 'HL'] = 'H'
|
|
|
+ results_short.append(thd.stock)
|
|
|
+ if thd.stock in results:
|
|
|
+ results.remove(thd.stock)
|
|
|
+ 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)
|
|
|
+ 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (thd.stock, fre), engine_stock)
|
|
|
|
|
|
for i in thd.get_price.index:
|
|
|
if i == 0 or i == 1:
|
|
@@ -176,22 +209,42 @@ for fre in ['1d', '30m']:
|
|
|
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
|
|
|
+ results.append(thd.stock)
|
|
|
+
|
|
|
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:
|
|
|
+ x_macd_dif, x_macd_dea, x_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[x, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ m_macd_dif, m_macd_dea, m_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[m, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ if x == len(thd.df_day.index) - 1 and x_macd_dif[thd.stock] > m_macd_dif[
|
|
|
+ thd.stock]:
|
|
|
|
|
|
- print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'],
|
|
|
+ 'MACD背驰————买吗?!')
|
|
|
+ results.append(thd.stock)
|
|
|
+
|
|
|
break
|
|
|
else:
|
|
|
break
|
|
|
m = m - 1
|
|
|
if m == 0:
|
|
|
thd.df_day.loc[x, 'HL'] = 'L'
|
|
|
+ results.append(thd.stock)
|
|
|
|
|
|
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'])):
|
|
@@ -203,44 +256,76 @@ for fre in ['1d', '30m']:
|
|
|
if x - m > 3:
|
|
|
thd.df_day.loc[x, 'HL'] = 'H'
|
|
|
if x == len(thd.df_day.index) - 1:
|
|
|
-
|
|
|
- pass
|
|
|
- break
|
|
|
+ print(thd.stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
|
|
|
+
|
|
|
+ results.remove(thd.stock)
|
|
|
+
|
|
|
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
|
|
|
-
|
|
|
+ x_macd_dif, x_macd_dea, x_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[x, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ m_macd_dif, m_macd_dea, m_macd_macd = MACD(thd.stock,
|
|
|
+ check_date=thd.df_day.loc[m, 'date'],
|
|
|
+ SHORT=12, LONG=26, MID=9, unit=fre)
|
|
|
+ if x == len(thd.df_day.index) - 1 and x_macd_dif[thd.stock] < m_macd_dif[
|
|
|
+ thd.stock]:
|
|
|
+
|
|
|
+ print(thd.stock, '/\/\/\/\/\/\/', '顶背离了!!!!')
|
|
|
+ results.remove(thd.stock)
|
|
|
break
|
|
|
break
|
|
|
m = m - 1
|
|
|
if m == 0:
|
|
|
thd.df_day.loc[x, 'HL'] = 'H'
|
|
|
+ results.remove(thd.stock)
|
|
|
else:
|
|
|
thd.df_day.loc[x, 'HL'] = '-'
|
|
|
+ print(thd.df_day[-20:])
|
|
|
|
|
|
- thd.df_day.to_sql('stk%s_%s' % (thd.stock, fre), con=engine2, index=True, if_exists='append')
|
|
|
+
|
|
|
|
|
|
- step = 700
|
|
|
+ step = 500
|
|
|
thread_list = []
|
|
|
- engine = []
|
|
|
- engine2 = []
|
|
|
+ engine_stock = []
|
|
|
+ engine_hlfx = []
|
|
|
times_engine = 0
|
|
|
- print(len(stocks))
|
|
|
- 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()
|
|
|
+ engine_hlfx_pool = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
|
|
|
+
|
|
|
+ results= pd.read_sql_query(
|
|
|
+ 'select value from `%s`' % fre, engine_hlfx_pool)
|
|
|
+
|
|
|
+ results = results.iloc[-1, 0]
|
|
|
+ results = results.split(",")
|
|
|
+ results_short=[]
|
|
|
+
|
|
|
+ print('数据库读取', results, type(results))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ time = dt.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
+ results_list =','.join(set(results))
|
|
|
+ print(set(results))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ print('做多', set(results))
|
|
|
+ print('做空', set(results_short))
|
|
|
+ print(len(set(results)))
|
|
|
+ print(len(set(results_short)))
|
|
|
|
|
|
end= dt.now()
|
|
|
print('总时长:', (end - start).seconds)
|