Ver Fonte

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	futures_backtrader.py
#	real_time_signal.py
#	updata_qbh_hlfx.py
#	update_data_tosql.py
Daniel há 2 anos atrás
pai
commit
8eeed5f23a
9 ficheiros alterados com 569 adições e 159 exclusões
  1. 63 0
      MA_Close_futures.py
  2. 81 0
      MA_HLFX_order.py
  3. 93 0
      bk_test.py
  4. 68 0
      factor_values.py
  5. 34 0
      get_history_futures.py
  6. 3 3
      his_money_flow.py
  7. 9 7
      real_time_signal.py
  8. 215 130
      updata_qbh_hlfx.py
  9. 3 19
      update_data_tosql.py

+ 63 - 0
MA_Close_futures.py

@@ -0,0 +1,63 @@
+from jqdatasdk import *
+from datetime import datetime as dt
+import pandas as pd
+from jqdatasdk.technical_analysis import *
+from sqlalchemy import create_engine
+import threading
+
+
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
+# futures = ['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX', 'FU9999.XSGE', 'RB9999.XSGE', 'MA9999.XZCE', 'TA9999.XZCE', 'SA9999.XZCE', 'M9999.XDCE', 'LH9999.XDCE']
+# futures = ['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX']
+futures = ['FU9999.XSGE', 'RB9999.XSGE', 'MA9999.XZCE', 'TA9999.XZCE', 'SA9999.XZCE', 'M9999.XDCE', 'LH9999.XDCE']
+# futures = ['FU9999.XSGE']
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/futures?charset=utf8')
+# 获取各stock的去包含dataframe
+fut = locals()
+print(dt.now(), '开始赋值!')
+for fre in ['30m']:
+    for future in futures:
+        try:
+            fut[future] = pd.read_sql_query('select date,open,close,high,low,volume,money from `%s_%s`' % (future, fre),
+                                            engine)
+        except BaseException:
+            continue
+
+print(dt.now(), '数据库数据已赋值!')
+
+# print(fut['IF9999.CCFX'])
+
+start = dt.now()
+print(start)
+for f in futures:
+    fut[f] = pd.concat([fut[f], pd.DataFrame(columns=['MA5', 'derta'])],axis=1)
+    print(fut[f])
+
+    for i in range(len(fut[f])):
+        MA1 = MA(f, check_date=fut[f].loc[i, 'date'], timeperiod=5)
+        MA1 = MA1[f]
+        fut[f].loc[i, 'MA5'] = MA1
+        # print(fut[f].loc[i, ['close','high','low']])
+        if fut[f].loc[i, 'close'] > MA1:
+            derta = fut[f].loc[i, 'high']/MA1 - 1
+        else:
+            derta = (fut[f].loc[i, 'low']/MA1 -1)
+        fut[f].loc[i, 'derta'] = derta
+    print('___________________________________')
+    print(fut[f])
+    fut[f].to_excel('/Users/daniel/Downloads/MA_derta/30m_%s.xlsx'% f)
+
+end= dt.now()
+print('总时长:', (end - start).seconds)
+
+
+
+
+
+
+
+
+
+# MA1 = MA('MA9999.XZCE', check_date='2022-09-25', timeperiod=5)
+# print(MA1['MA9999.XZCE'])
+

+ 81 - 0
MA_HLFX_order.py

@@ -0,0 +1,81 @@
+from jqdatasdk import *
+from datetime import datetime as dt
+import pandas as pd
+
+from sqlalchemy import create_engine
+
+
+
+
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
+
+fre = ['30m', '1d']
+
+engine_hlfx_pool = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
+engine_stock = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
+
+
+
+fut = locals()
+print(dt.now(), '开始赋值!')
+for fre in ['1d']:
+
+        try:
+            stock_pool = pd.read_sql_query(
+                'select value from `%s`' % fre, engine_hlfx_pool)
+            print(stock_pool)
+            stock_pool = stock_pool.iloc[-1, 0].split(",")
+            print(stock_pool)
+        except BaseException:
+            continue
+        for stock in stock_pool[0:1]:
+            print(stock)
+
+            df_stock=df = get_bars(stock, count=10, unit=fre, fields=['date', 'open', 'close','high','low' ], include_now=True, df=True)
+            price = df_stock.iloc[-1].at['close']
+            price_open = df_stock.iloc[-1].at['open']
+            MA5_1 = df_stock['close'][-7:-2].mean()
+            MA5 = df_stock['close'][-6:-1].mean()
+            MA10 = df_stock['close'].mean()
+            print(price,price_open, 'ma5_1:',MA5_1, 'ma5:', MA5,MA10)
+            if (price > price_open) & (price > MA5) & (MA5 > MA5_1) & (price < MA5 * 1.07):
+                print('BUY')
+
+print(dt.now(), '数据库数据已赋值!')
+
+# print(fut['IF9999.CCFX'])
+
+# start = dt.now()
+# print(start)
+# for f in futures:
+#     fut[f] = pd.concat([fut[f], pd.DataFrame(columns=['MA5', 'derta'])],axis=1)
+#     print(fut[f])
+#
+#     for i in range(len(fut[f])):
+#         MA1 = MA(f, check_date=fut[f].loc[i, 'date'], timeperiod=5)
+#         MA1 = MA1[f]
+#         fut[f].loc[i, 'MA5'] = MA1
+#         # print(fut[f].loc[i, ['close','high','low']])
+#         if fut[f].loc[i, 'close'] > MA1:
+#             derta = fut[f].loc[i, 'high']/MA1 - 1
+#         else:
+#             derta = (fut[f].loc[i, 'low']/MA1 -1)
+#         fut[f].loc[i, 'derta'] = derta
+#     print('___________________________________')
+#     print(fut[f])
+#     fut[f].to_excel('/Users/daniel/Downloads/MA_derta/30m_%s.xlsx'% f)
+#
+# end= dt.now()
+# print('总时长:', (end - start).seconds)
+
+
+
+
+
+
+
+
+
+# MA1 = MA('MA9999.XZCE', check_date='2022-09-25', timeperiod=5)
+# print(MA1['MA9999.XZCE'])
+

+ 93 - 0
bk_test.py

@@ -0,0 +1,93 @@
+from jqdatasdk import *
+from datetime import datetime as dt
+import pandas as pd
+import pymysql
+from sqlalchemy import create_engine
+import time
+
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
+
+# fre = ['30m', '1d']
+
+engine_hlfx_pool = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
+stock_pool = pd.read_sql_query(
+                    'select value from MA5_1d', engine_hlfx_pool)
+stock_pool = stock_pool.iloc[-2, 0].split(",")
+print(type(stock_pool), len(stock_pool),stock_pool)
+num_industry = get_industry(stock_pool)
+print(num_industry)
+results = []
+a = []
+for key in num_industry.values():
+    for key2 in key.values():
+        results.append(key2['industry_name'])
+results = pd.value_counts(results)
+print(results)
+
+results = results[0:3]
+results = list(results.index)
+print(results)
+
+for key,value in num_industry.items():
+    for key2 in value.values():
+        if key2['industry_name'] in results:
+            a.append(key)
+
+
+
+print(set(a))
+
+
+
+
+from jqdatasdk import *
+from datetime import datetime as dt
+import pandas as pd
+import pymysql
+from sqlalchemy import create_engine
+import numpy as np
+from jqdatasdk.technical_analysis import *
+
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
+
+
+
+def calculateEMA(period, closeArray, emaArray=[]):
+    """计算指数移动平均"""
+    length = len(closeArray)
+    nanCounter = np.count_nonzero(np.isnan(closeArray))
+    if not emaArray:
+        emaArray.extend(np.tile([np.nan], (nanCounter + period - 1)))
+        firstema = np.mean(closeArray[nanCounter:nanCounter + period - 1])
+        emaArray.append(firstema)
+        for i in range(nanCounter + period, length):
+            ema = (2 * closeArray[i] + (period - 1) * emaArray[-1]) / (period + 1)
+            emaArray.append(ema)
+    return np.array(emaArray)
+
+
+def calculateMACD(closeArray, shortPeriod=12, longPeriod=26, signalPeriod=9):
+    ema12 = calculateEMA(shortPeriod, closeArray, [])
+    print(ema12)
+    ema26 = calculateEMA(longPeriod, closeArray, [])
+    print(ema26)
+    diff = ema12 - ema26
+
+    dea = calculateEMA(signalPeriod, diff, [])
+    macd = 2 * (diff - dea)
+    return macd, diff, dea
+
+stock = '000010.XSHE'
+fre = '1d'
+engine_stock = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
+df = pd.read_sql_query('select date,open,close,high,low,volume,money from `stk%s_%s`' % (stock, fre), engine_stock)
+df_close = df['close']
+df2 = calculateMACD(df_close)
+print(df2)
+print(len(df), len(df2[0]))
+df3 = pd.concat([df, pd.Series(df2[0]).rename('macd'), pd.Series(df2[1]).rename('diff'), pd.Series(df2[2]).rename('dea')], axis=1)
+
+print(df3.loc[df.date== '2010-02-25',:])
+
+x_macd_dif, x_macd_dea, x_macd_macd = MACD(stock, check_date='2010-02-26 00:00:00', SHORT=12, LONG=26, MID=9, unit=fre)
+print(x_macd_macd, x_macd_dif, x_macd_dea)

+ 68 - 0
factor_values.py

@@ -0,0 +1,68 @@
+from jqdatasdk import *
+from jqdatasdk.technical_analysis import *
+from sqlalchemy import create_engine
+import pandas as pd
+import pymysql
+import datetime
+
+
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
+strattime = datetime.datetime.now()
+print(get_query_count())
+
+stocks = list(get_all_securities(['stock']).index)
+qihuo = get_price(['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX'], start_date='2015-01-01', end_date='2015-12-31', frequency='daily', fields=None, skip_paused=False, fq='pre' ,panel=True)
+
+print(qihuo)
+for fre in ['30m', '1d']:
+    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)
+
+time = datetime.datetime(2010, 1, 4)
+print(time)
+
+for stock in table_list:
+    print(stock)
+    sql = ("select date_format(date, '%%Y-%%m-%%d') from `%s` where HL='L'" % stock)
+    cheak_time = cursor.execute(sql)
+    print(cheak_time)
+    time_list = [''.join(time) for time in cursor.fetchall()]
+    print(time_list)
+
+    break
+    #stock = normalize_code(stock[3:9])
+
+
+# 定义股票池列表
+security_list1 = '000001.XSHE'
+# security_list2 = ['000001.XSHE','000002.XSHE','601211.XSHG','603177.XSHG']
+# # 计算并输出 security_list1 的 MACD 值
+macd_dif, macd_dea, macd_macd = MACD(security_list1, check_date=time_list[1], SHORT = 12, LONG = 26, MID = 9)
+print(macd_dif[security_list1])
+print(macd_dea[security_list1])
+print(macd_macd[security_list1])
+
+# 输出 security_list2 的 MACD 值
+macd_dif, macd_dea, macd_macd = MACD(security_list2,check_date=datetime.datetime.today(), SHORT = 12, LONG = 26, MID = 9)
+for stock in security_list2:
+    print(macd_dif[stock])
+    print(macd_dea[stock])
+    print(macd_macd[stock])
+
+df = get_bars(stocks, count=10, unit='30m',
+              fields=['date','open','close','high','low','volume','money'],include_now=False,end_dt=datetime.date.today())
+print(df)
+endtime = datetime.datetime.now()
+print('单次时长为:', (endtime - strattime).seconds)
+
+
+#get_ticks("000001.XSHE", "2022-01-01", datetime.datetime.today())

+ 34 - 0
get_history_futures.py

@@ -0,0 +1,34 @@
+from jqdatasdk import *
+auth('18616891214','Ea?*7f68nD.dafcW34d!')
+from sqlalchemy import create_engine
+import pandas as pd
+import datetime
+
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
+fre = '1d'
+pool =pd.read_sql_table('%s' % fre, con=engine).iloc[-1,2]
+stock_pool = pool.split(",")
+print(type(pool),pool)
+print(item for item in pool)
+print(type(stock_pool),stock_pool)
+print(item for item in stock_pool)
+
+
+# futures = ['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX', 'FU9999.XSGE', 'RB9999.XSGE', 'MA9999.XZCE', 'TA9999.XZCE', 'SA9999.XZCE', 'M9999.XDCE', 'LH9999.XDCE']
+#
+# for fre in ['30m','1d','5m']:
+#     for future in futures:
+#         df_future = get_price(future, start_date='2015-01-01', end_date='2022-09-25', frequency=fre, fields=None, skip_paused=False, fq='pre' ,panel=True)
+#
+#         # 去除无数据日
+#         df_future = df_future.dropna(axis=0)
+#         # 重置index
+#         df_future.reset_index(inplace=True)
+#         # 保留日期数据
+#         df_future.rename(columns={'index': 'date'}, inplace=True)
+#         print(df_future)
+#         # 写入数据库
+#         df_future.to_sql('%s_%s' % (future, fre), con=engine, if_exists='append')
+#         with engine.connect() as con:
+#             con.execute("ALTER TABLE `%s_%s` ADD PRIMARY KEY (`date`);" % (future, fre))
+

+ 3 - 3
his_money_flow.py

@@ -23,7 +23,7 @@ engine_data = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/st
 
 fre = '1d'
 print('ready to write to mysql %s' % fre)
-for stock in stocks:
+for stock in stocks[2500:]:
     print(stock, fre)
     starttime ='2010-01-04'
     # endtime = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine).iloc[-1, 1]
@@ -43,11 +43,11 @@ for stock in stocks:
     # print(df_money)
 
     df_stock = pd.merge(df_stock, df_money, how='outer', left_index=False , on='date')
-    # df_stock.to_csv('D:\001_QuantTrade\Result.csv')
+    df_stock.to_csv('/Users/daniel/Downloads/Result.csv')
     df_stock = df_stock.dropna(axis=0)
     df_stock.reset_index(inplace=True)
     df_stock.rename(columns={'index': 'date'}, inplace=True)
-    df_stock.to_sql('stk%s_%s' % (stock, fre), con=engine_data, index=True, if_exists='replace')
+    df_stock.to_sql('stk%s_%s' % (stock, fre), con=engine_data, index=True, if_exists='append')
     # with engine.connect() as con:
     #     con.execute("ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);" % (stock, fre))
     print(df_stock)

+ 9 - 7
real_time_signal.py

@@ -9,7 +9,7 @@ from datetime import datetime as dt
 
 start = dt.now()
 # 确定级别
-fre = '30m'
+fre = '1d'
 # 连接数据库
 db = pymysql.connect(host='localhost',
                      user='root',
@@ -24,6 +24,7 @@ cursor = db.cursor()
 cursor.execute("show tables like '%%%s%%' "% fre)
 # stocks = [tuple[0] for tuple in cursor.fetchall()]
 stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
+# stocks = ['301058.XSHE']
 # stocks = stocks[0:500]
 print(dt.now(), 'stocks范围已获取!')
 
@@ -123,18 +124,19 @@ def qbh_hlfx(stocks, df):
                             m = m-1
                     else:
                         thd.df_day.loc[x, 'HL'] = '-'
+                    print(thd.df_day)
             else:
                 pass
         except BaseException:
             continue
 
 
-while True:
-    df = get_bars(stocks, count=2, unit=fre,
-                  fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'], include_now=True, df=True)
-    print(dt.now(), 'get_bars 成功')
-    # strattime = dt.now()
-    qbh_hlfx(stocks, df)
+# while True:
+df = get_bars(stocks, count=2, unit=fre,
+              fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'], include_now=True, df=True)
+print(dt.now(), 'get_bars 成功')
+# strattime = dt.now()
+qbh_hlfx(stocks, df)
 # endtime = dt.now()
 
 

+ 215 - 130
updata_qbh_hlfx.py

@@ -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!')
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
 
-engine_stocks_list = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
-# stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
-
-
-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 = stocks[0:1]
+stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
+# stocks = stocks[0:200]
 
 start = dt.now()
-# 确定级别
-# 注意修改time delta
-# fre = '30m'
-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:
-                    # 注意修改time delta
-                    startdate = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine2).iloc[-1, 1]
-                    # startdate = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine2).iloc[-1, 1] + datetime.timedelta(minutes= 5)
-                    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)
-                            # print(thd.df_day)
-                        # 包含
+                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:
+                        # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
+                        # 左高,下降
+                        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:
-                            # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
-                            # 左高,下降
-                            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'])):
-                                # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
-                                # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
-                                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']:
-                                            # 前一个为底,且中间存在不包含 or 更低的底
-                                            thd.df_day.loc[x, 'HL'] = 'L'
-                                            if x == len(thd.df_day.index) - 1:
-                                                # pass
-                                                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'])):
+                            # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
+                            # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
+                            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'])):
-                                # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
-                                #     stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
-                                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:
-                                                # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
-                                                pass
+                                        # 此处可以获得MACD指标
+                                        # pre-macd_dif, pre-macd_dea, pre-macd_macd = MACD(thd.stock,check_date=thd.df_day.loc[m, 'datetime'], SHORT = 12, LONG = 26, MID = 9)
+                                        if x == len(thd.df_day.index) - 1 :
+                                            # pass
+                                            print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '\n', thd.df_day.loc[m, 'date'], '买买买买买!!')
+                                            results.append(thd.stock)
+                                            print('222')
+                                    # break
+                                elif (thd.df_day.loc[m, 'HL'] == 'L'):
+                                    if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
+                                        # 前一个为底,且中间存在不包含 or 更低的底
+                                        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]:
+                                            # pass
+                                            # print(thd.df_day.loc[m, 'date'], thd.df_day.loc[m, 'low'],
+                                            #       m_macd_dif[thd.stock])
+                                            # print(thd.df_day.loc[x, 'date'], thd.df_day.loc[x, 'low'],
+                                            #       x_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']:
-                                            # 前一个为顶,且中间存在不包含 or 更高的顶
-                                            thd.df_day.loc[x, 'HL'] = 'H'
-                                            if x == len(thd.df_day.index) - 1:
-                                                pass
-                                                # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
-                                            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'])):
+                            # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
+                            #     stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
+                            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', '卖卖卖卖卖卖卖!')
+                                            # pass
+                                            results_short.append(thd.stock)
+                                            if thd.stock in results:
+                                                results.remove(thd.stock)
+                                    # break
+                                elif (thd.df_day.loc[m, 'HL'] == 'H'):
+                                    if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
+                                        # 前一个为顶,且中间存在不包含 or 更高的顶
+                                        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]:
+                                            # pass
+                                            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')
+                # 更新数据库
+                # 可以使用normalize_code(code) 方法 改变代码格式
+                # thd.df_day[index_len + 1:].to_sql('stk%s_%s' % (thd.stock, fre), con=engine_hlfx, 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'
+                                        # 此处可以获得MACD指标
+                                        # pre-macd_dif, pre-macd_dea, pre-macd_macd = MACD(thd.stock,check_date=thd.df_day.loc[m, 'datetime'], SHORT = 12, LONG = 26, MID = 9)
+
                                         if x == len(thd.df_day.index) - 1:
+                                            # pass
                                             print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
-                                    break
+                                            results.append(thd.stock)
+                                    # break
                                 elif (thd.df_day.loc[m, 'HL'] == 'L'):
                                     if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
                                         # 前一个为底,且中间存在不包含 or 更低的底
                                         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]:
                                             # pass
-                                            print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
+                                            # print(thd.df_day.loc[m, 'date'], thd.df_day.loc[m, 'low'],
+                                            #       m_macd_dif[thd.stock])
+                                            # print(thd.df_day.loc[x, 'date'], thd.df_day.loc[x, 'low'],
+                                            #       x_macd_dif[thd.stock])
+                                            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:
-                                            # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
-                                            pass
-                                    break
+                                            print(thd.stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
+                                            # pass
+                                            results.remove(thd.stock)
+                                    # break
                                 elif (thd.df_day.loc[m, 'HL'] == 'H'):
                                     if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
                                         # 前一个为顶,且中间存在不包含 or 更高的顶
                                         thd.df_day.loc[x, 'HL'] = 'H'
-                                        if x == len(thd.df_day.index) - 1:
-                                            pass
-                                            # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
+                                        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]:
+                                            # pass
+                                            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')
+                # thd.df_day.to_sql('stk%s_%s' % (thd.stock, fre), con=engine_hlfx, 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))
+    # for i in range(0, len(stocks), step):
+    #     engine_stock.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8'))
+    #     engine_hlfx.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8'))
+    #     thread = threading.Thread(target=hlfx, args=(stocks[i:i + step], engine_stock[times_engine], engine_hlfx[times_engine]))
+    #     times_engine = times_engine + 1
+    #     thread.start()
+    #     thread_list.append(thread)
+    #
+    # for thread in thread_list:
+    #     thread.join()
+    # db.close()
+
+
+
+    time = dt.now().strftime('%Y-%m-%d %H:%M:%S')
+    results_list =','.join(set(results))
+    print(set(results))
+    # sql = "INSERT INTO %s (date,value) VALUES('%s','%s')" % (fre, dt.now().strftime('%Y-%m-%d %H:%M:%S'), results_list)
+    # cursor_pool.execute(sql)
+    # db_pool.commit()
+    print('做多', set(results))
+    print('做空', set(results_short))
+    print(len(set(results)))
+    print(len(set(results_short)))
 
     end= dt.now()
     print('总时长:', (end - start).seconds)

+ 3 - 19
update_data_tosql.py

@@ -3,32 +3,17 @@ from sqlalchemy import create_engine
 import pandas as pd
 from datetime import datetime as dt
 import datetime
-import pymysql
 
 auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
 stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
 engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
-stocks_List = ','.join(set(stocks))
 
-db_stocks_list = pymysql.connect(host='localhost',
-                          user='root',
-                          port=3307,
-                          password='r6kEwqWU9!v3',
-                          database='hlfx_pool')
-cursor_stock_list = db_stocks_list.cursor()
-sql = "INSERT INTO stocks_list (date,securities) VALUES('%s','%s')" % (dt.today().strftime('%Y-%m-%d'),  stocks_List)
-cursor_stock_list.execute(sql)
-db_stocks_list.commit()
-db_stocks_list.close()
-print('stocks_list已入库')
-
-for fre in ['30m', '1d']:
+for fre in ['1d', '30m']:
     print('ready to write to mysql %s' % fre)
     for stock in stocks:
-        print(stock, fre)
+        print(stock)
         try:
             index_len = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine).iloc[-1, 0]
-            # 注意修改time delta
             if fre == '1d':
                 startdate = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine).iloc[-1, 1] + datetime.timedelta(
                     days=1)
@@ -46,8 +31,7 @@ for fre in ['30m', '1d']:
             df_stock.index = df_stock.index + index_len + 1
             df_stock.to_sql('stk%s_%s' % (stock, fre), con=engine, index=True, if_exists='append')
         except BaseException:
-            df_stock = get_price(stock, start_date='2022-01-01 00:00:00',
-                                 end_date=dt.today().strftime('%Y-%m-%d %H:%M:%S'),
+            df_stock = get_price(stock, start_date='2008-01-01 00:00:00', end_date=dt.today().strftime('%Y-%m-%d %H:%M:%S'),
                                  frequency=fre, fields=['open', 'close', 'high', 'low', 'volume', 'money'],
                                  skip_paused=False,
                                  fq='pre', count=None, panel=False)