Bläddra i källkod

表名重构
自更新hlfx

daniel 3 år sedan
förälder
incheckning
06504ee6c8
6 ändrade filer med 289 tillägg och 61 borttagningar
  1. 9 12
      get_history_price.py
  2. 7 7
      hlfx.py
  3. 13 13
      qbh.py
  4. 12 12
      real_time_signal_30m.py
  5. 218 0
      updata_qbh_hlfx.py
  6. 30 17
      update_data_tosql.py

+ 9 - 12
get_history_price.py

@@ -1,33 +1,30 @@
 from jqdatasdk import *
-auth('18616891214','Ea?*7f68nD.dafcW34d!')
 from sqlalchemy import create_engine
-
+auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
 
 # 获得代码列表
-stocks = list(get_all_securities(['stock'], date='2022-02-25').index)
+stocks = list(get_all_securities(['stock'], date='2022-02-01').index)
+stocks = stocks[0:50]
 
 # 建立stocks 数据库
-engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/sit?charset=utf8')
 
 # 定义周期级别
-fre = '30m'
-
+fre = '1d'
 print('ready to get history')
-
 # 逐一取数据,写入sql
-for stock in stocks[0:10]:
+for stock in stocks:
     print(stock)
-    df_stock = get_price(stock, start_date='2022-01-01 00:00:00', end_date='2022-01-05 00:00:00',
+    df_stock = get_price(stock, start_date='2020-01-01 00:00:00', end_date='2022-02-01 00:00:00',
                          frequency=fre, fields=['open', 'close', 'high', 'low', 'volume', 'money'], skip_paused=False,
                          fq='pre', count=None, panel=False)
     # 去除无数据日
     df_stock = df_stock.dropna(axis=0)
     # 重置index
     df_stock.reset_index(inplace=True)
-    # df_stock.index = df_stock.index + index_len + 1
     # 保留日期数据
     df_stock.rename(columns={'index': 'date'}, inplace=True)
     # 写入数据库
-    df_stock.to_sql('stk%s_%s' % (stock[:6], fre), con=engine, if_exists='append')
+    df_stock.to_sql('stk%s_%s' % (stock, fre), con=engine, if_exists='append')
     with engine.connect() as con:
-        con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], fre))
+        con.execute("ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);" % (stock, fre))

+ 7 - 7
hlfx.py

@@ -13,9 +13,9 @@ db = pymysql.connect(host='localhost',
                      user='root',
                      port=3307,
                      password='r6kEwqWU9!v3',
-                     database='qbh_hlfx')
+                     database='qbh')
 
-fre = '30m'
+fre = '1d'
 
 cursor = db.cursor()
 # cursor.execute("select table_name from information_schema.tables where table_schema='qbh_hlfx_backup' and table_name like {}".format('\'%{}\''.format(fre)))
@@ -31,7 +31,7 @@ stk = threading.local()
 def hlfx(table_list, engine, tosql):
     for table in table_list:
         # stk.fxdf = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
-        stk.df_day = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from %s' % table, engine)
+        stk.df_day = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from `%s`' % table, engine)
 
         for i in stk.df_day.index:
             m = i - 1
@@ -84,7 +84,7 @@ def hlfx(table_list, engine, tosql):
         # stk.df_day.to_csv('/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220212hlfx2/hlfx%s.csv' % table)
         stk.df_day.to_sql('%s' % table, con=tosql, index=True, if_exists='replace')
         with tosql.connect() as con_backup:
-            con_backup.execute('ALTER TABLE %s ADD PRIMARY KEY (`date`);' % table)
+            con_backup.execute('ALTER TABLE `%s` ADD PRIMARY KEY (`date`);' % table)
         print(table, '\n', '**********************************')
 
 # table_list = ['stk002237_1d','stk000004_1d']
@@ -92,14 +92,14 @@ def hlfx(table_list, engine, tosql):
 # tosql = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/bb22?charset=utf8')
 # hlfx(table_list, engine, tosql)
 
-step = 500
+step = 5
 thread_list = []
 engine = []
 tosql = []
 times_engine = 0
 for i in range(0, len(table_list), step):
-    engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8'))
-    tosql.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_backup?charset=utf8'))
+    engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh?charset=utf8'))
+    tosql.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8'))
     thread = threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[times_engine], tosql[times_engine]))
     times_engine = times_engine + 1
     thread.start()

+ 13 - 13
qbh.py

@@ -1,11 +1,10 @@
 from jqdatasdk import *
-auth('18019403367', 'Qwer4321')
 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()
 
 # 连接数据库
@@ -27,10 +26,10 @@ starttime = dt.now()
 #
 # cursor = db_qbh.cursor()
 # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8')
-engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
+engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/sit?charset=utf8')
 
-stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
-# stocks =stocks[0:40]
+stocks = list(get_all_securities(['stock'], date='2022-02-01').index)
+stocks =stocks[0:70]
 
 thd = threading.local()
 
@@ -39,7 +38,7 @@ def qbh(stocks, engine, engine_backup):
     for stock in stocks:
         thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
         # print(new_df.head())
-        thd.df_day = stk['stk' + stock[:6]]
+        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)
@@ -60,9 +59,9 @@ def qbh(stocks, engine, engine_backup):
                     # 右高,上升
                     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[:6], u), con=engine, index=True, if_exists='replace')
+        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[:6], u))
+            con.execute('ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);' % (stock, fre))
         # thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine_backup, index=True, if_exists='replace')
         # with engine_backup.connect() as con_backup:
         #     con_backup.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
@@ -80,11 +79,12 @@ stk = locals()
 engine = []
 engine_backup = []
 
-u = '30m'
+fre = '1d'
 # 获取数据存入DataFrame
 
 for stock in stocks:
-    stk['stk'+stock[:6]] = pd.read_sql_query('select date,open,close,high,low,volume,money from stk%s_%s' % (stock[:6], u), engine2)
+    stk['stk'+stock] = pd.read_sql_query('select date,open,close,high,low,volume,money from `stk%s_%s`'
+                                         % (stock, fre), engine2)
     # print(stock, stk['stk'+stock[:6]])
 print("###############################################################################################################"
       "###############################################################################################################"
@@ -97,11 +97,11 @@ print("#########################################################################
 # 开始去包含
 # qbh(stocks)
 thread_list = []
-step = 1000
+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_hlfx?charset=utf8', pool_recycle= 3600))
-    engine_backup.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8', pool_recycle= 3600))
+    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()

+ 12 - 12
real_time_signal_30m.py

@@ -49,24 +49,24 @@ def qbh_hlfx(stocks, df):
             thd.get_bars = df.loc[stock]
             stk_len = len(thd.df_day)
             # 先处理去包含
-            for i in thd.get_bars.index:
+            for x in thd.get_bars.index:
                 # 不包含
-                if (thd.df_day.iloc[-1, 3] > thd.get_bars.loc[i, 'high']
-                    and thd.df_day.iloc[-1, 4] > thd.get_bars.loc[i, 'low']) \
-                        or (thd.df_day.iloc[-1, 3] < thd.get_bars.loc[i, 'high']
-                            and thd.df_day.iloc[-1, 4] < thd.get_bars.loc[i, 'low']):
-                    thd.df_day = pd.concat([thd.df_day, thd.get_bars.iloc[[i]]], ignore_index=True)
+                if (thd.df_day.iloc[-1, 3] > thd.get_bars.loc[x, 'high']
+                    and thd.df_day.iloc[-1, 4] > thd.get_bars.loc[x, 'low']) \
+                        or (thd.df_day.iloc[-1, 3] < thd.get_bars.loc[x, 'high']
+                            and thd.df_day.iloc[-1, 4] < thd.get_bars.loc[x, 'low']):
+                    thd.df_day = pd.concat([thd.df_day, thd.get_bars.iloc[[x]]], 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_bars.loc[i, 'high'])
-                        thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_bars.loc[i, 'low'])
+                        thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_bars.loc[x, 'high'])
+                        thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_bars.loc[x, 'low'])
                     else:
                         # 右高,上升
-                        thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_bars.loc[i, 'high'])
-                        thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_bars.loc[i, 'low'])
+                        thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_bars.loc[x, 'high'])
+                        thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_bars.loc[x, 'low'])
         # return thd.df_day
 
             # 寻找顶底分型
@@ -82,7 +82,7 @@ def qbh_hlfx(stocks, df):
                         # print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '考虑————买入!!')
                     while m:
                         if thd.df_day.loc[m, 'HL'] == 'H':
-                            if(i-m) > 3:
+                            if(x-m) > 3:
                                 thd.df_day.loc[x, 'HL'] = 'L'
                                 if x == len(thd.df_day.index) - 1:
                                     print(stock, '$$$$$$$', '\n', '买买买买买!!')
@@ -108,7 +108,7 @@ def qbh_hlfx(stocks, df):
                         pass
                     while m:
                         if thd.df_day.loc[m, 'HL'] == 'L':
-                            if i-m > 3:
+                            if x-m > 3:
                                 thd.df_day.loc[x, 'HL'] = 'H'
                                 if x == len(thd.df_day.index) - 1:
                                     # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')

+ 218 - 0
updata_qbh_hlfx.py

@@ -0,0 +1,218 @@
+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)
+stocks = stocks[0:80]
+
+start = dt.now()
+# 确定级别
+# 注意修改time delta
+fre = '1d'
+# 连接数据库
+db = pymysql.connect(host='localhost',
+                     user='root',
+                     port=3307,
+                     password='r6kEwqWU9!v3',
+                     database='hlfx')
+engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8')
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/sit?charset=utf8')
+cursor = db.cursor()
+cursor.execute('show tables like {}'.format('\'%{}\''.format(fre)))
+table_list = [tuple[0] for tuple in cursor.fetchall()]
+
+print(table_list)
+stk = locals()
+thd = threading.local()
+
+for stock in stocks:
+    print(stock)
+    if ('stk%s_%s' % (stock, fre)) in table_list:
+        # 有历史数据
+        index_len = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine2).iloc[-1, 0]
+        # 注意修改time delta
+        startdate = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine2).iloc[-1, 1] + datetime.timedelta(days=1)
+        thd.get_price = pd.read_sql_query(
+            'select date,open,close,high,low,volume,money from `stk%s_%s`' % (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`' % (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)
+            # 包含
+            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'])
+                    # 寻找顶底分型
+        # hlfx判断
+        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*'
+                if x == len(thd.df_day.index) - 1:
+                    pass
+                    # print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '考虑————买入!!')
+                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(stock, '$$$$$$$', '\n', '买买买买买!!')
+                        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:
+                                print(stock, '$$$$$$$', '\n', '中继后的底————买吗?!')
+                            break
+                        else:
+                            break
+                    m = m - 1
+
+            # 顶
+            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*'
+                if x == len(thd.df_day.index) - 1:
+                    # print(stock, '!!!!!!!', '\n', thd.df_day.loc[x, 'date'], '考虑————卖出!!')
+                    pass
+                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
+                            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']:
+                            # 前一个为顶,且中间存在不包含 or 更高的顶
+                            thd.df_day.loc[x, 'HL'] = 'H'
+                            if x == len(thd.df_day.index) - 1:
+                                pass
+                                # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
+                            break
+                        break
+                    m = m - 1
+            else:
+                thd.df_day.loc[x, 'HL'] = '-'
+        # 更新数据库
+        thd.df_day[index_len + 1:].to_sql('stk%s_%s' % (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`' % (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'])
+        # hlfx判断
+        for x in thd.df_day.index:
+            print(len(thd.df_day.index), x)
+            m = x-1
+            if x < 3:
+                continue
+            # 底
+            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'] = 'L*'
+                if x == len(thd.df_day.index) - 1:
+                    pass
+                    # print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '考虑————买入!!')
+                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(stock, '$$$$$$$', '\n', '买买买买买!!')
+                        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:
+                                print(stock, '$$$$$$$', '\n', '中继后的底————买吗?!')
+                            break
+                        else:
+                            break
+                    m = m - 1
+
+            # 顶
+            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*'
+                if x == len(thd.df_day.index) - 1:
+                    # print(stock, '!!!!!!!', '\n', thd.df_day.loc[x, 'date'], '考虑————卖出!!')
+                    pass
+                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
+                            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']:
+                            # 前一个为顶,且中间存在不包含 or 更高的顶
+                            thd.df_day.loc[x, 'HL'] = 'H'
+                            if x == len(thd.df_day.index) - 1:
+                                pass
+                                # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
+                            break
+                        break
+                    m = m - 1
+            else:
+                thd.df_day.loc[x, 'HL'] = '-'
+        # 更新数据库
+        thd.df_day.to_sql('stk%s_%s' % (stock, fre), con=engine2, index=True, if_exists='append')

+ 30 - 17
update_data_tosql.py

@@ -1,29 +1,42 @@
 from jqdatasdk import *
-auth('18616891214','Ea?*7f68nD.dafcW34d!')
 from sqlalchemy import create_engine
 import pandas as pd
 from datetime import datetime as dt
 import datetime
 
-stocks = list(get_all_securities(['stock'], date='2022-02-25').index)
-engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
-fre = '30m'
+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/sit?charset=utf8')
+fre = '1d'
 
 print('ready to write to mysql')
-for stock in stocks[0:10]:
+for stock in stocks[0:80]:
     print(stock)
-    index_len = pd.read_sql_table('stk%s_%s' % (stock[:6], fre), con=engine).iloc[-1, 0]
-    # 注意修改time delta
-    startdate = pd.read_sql_table('stk%s_%s' % (stock[:6], fre), con=engine).iloc[-1, 1] + datetime.timedelta(minutes=5)
-    print(startdate)
-    df_stock = get_price(stock, start_date=startdate, 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)
-    df_stock = df_stock.dropna(axis=0)
-    df_stock.reset_index(inplace=True)
-    df_stock.rename(columns={'index': 'date'}, inplace=True)
-    df_stock.index = df_stock.index + index_len + 1
-    df_stock.to_sql('stk%s_%s' % (stock[:6], fre), con=engine, index=True, if_exists='append')
+    try:
+        index_len = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine).iloc[-1, 0]
+        # 注意修改time delta
+        startdate = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine).iloc[-1, 1] + datetime.timedelta(days=1)
+        print(startdate)
+        df_stock = get_price(stock, start_date=startdate, 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)
+        df_stock = df_stock.dropna(axis=0)
+        df_stock.reset_index(inplace=True)
+        df_stock.rename(columns={'index': 'date'}, inplace=True)
+        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'),
+                             frequency=fre, fields=['open', 'close', 'high', 'low', 'volume', 'money'],
+                             skip_paused=False,
+                             fq='pre', count=None, panel=False)
+        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, index=True, if_exists='append')
+        with engine.connect() as con:
+            con.execute("ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);" % (stock, fre))