Browse Source

get history price 1.0

Daniel 3 years ago
parent
commit
aab537a43c
5 changed files with 75 additions and 70 deletions
  1. 2 2
      get_history_price.py
  2. 1 1
      hlfx.py
  3. 65 60
      qbh.py
  4. 4 4
      real_time_signal.py
  5. 3 3
      real_time_signal_30m.py

+ 2 - 2
get_history_price.py

@@ -10,12 +10,12 @@ stocks = list(get_all_securities(['stock'], date='2022-03-08').index)
 engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
 
 # 定义周期级别
-fre = '1d'
+fre = '30m'
 print('ready to get history')
 # 逐一取数据,写入sql
 for stock in stocks:
     print(stock)
-    df_stock = get_price(stock, start_date='2010-01-01 00:00:00', end_date='2022-02-01 00:00:00',
+    df_stock = get_price(stock, start_date='2020-01-01 00:00:00', end_date='2022-03-08 00:00:00',
                          frequency=fre, fields=['open', 'close', 'high', 'low', 'volume', 'money'], skip_paused=False,
                          fq='pre', count=None, panel=False)
     # 去除无数据日

+ 1 - 1
hlfx.py

@@ -92,7 +92,7 @@ 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 = 5
+step = 100
 thread_list = []
 engine = []
 tosql = []

+ 65 - 60
qbh.py

@@ -26,60 +26,18 @@ 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/sit?charset=utf8')
+engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
 
 stocks = list(get_all_securities(['stock'], date='2022-02-01').index)
-stocks =stocks[0:70]
+# stocks =stocks[0:70]
 
 thd = threading.local()
 
 # docker run --name mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD='r6kEwqWU9!v3' -v /Users/daniel/mysqldata:/var/lib/mysql -d mysql:8.0-oracle
-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]
-        for i in thd.df_day.index:
-            if i == 0 or i == 1:
-                thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
-            # 不包含
-            elif (thd.new_df.iloc[-1, 3] > thd.df_day.loc[i, 'high']
-                  and thd.new_df.iloc[-1, 4] > thd.df_day.loc[i, 'low']) \
-                    or (thd.new_df.iloc[-1, 3] < thd.df_day.loc[i, 'high']
-                        and thd.new_df.iloc[-1, 4] < thd.df_day.loc[i, 'low']):
-                thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
-            # 包含
-            else:
-                # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
-                # 左高,下降
-                if thd.new_df.iloc[-2, 3] > thd.new_df.iloc[-1, 3]:
-                    thd.new_df.iloc[-1, 3] = min(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
-                    thd.new_df.iloc[-1, 4] = min(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
-                else:
-                    # 右高,上升
-                    thd.new_df.iloc[-1, 3] = max(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
-                    thd.new_df.iloc[-1, 4] = max(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
-        thd.new_df.to_sql('stk%s_%s' % (stock, fre), con=engine, index=True, if_exists='append')
-        with engine.connect() as con:
-            con.execute('ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);' % (stock, fre))
-        # 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))
-        # thd.new_df.to_csv(
-        #     '/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220211qbh/qbh%s.csv' % stock[:6])
-        print(stock)
-        print("**************")
-        #
-        # # new_df.to_csv('new_df.csv')
-        #
-        # #return new_df
-
+# def qbh(stocks, engine, engine_backup):
 
+fre = '30m'
 stk = locals()
-engine = []
-engine_backup = []
-
-fre = '1d'
 # 获取数据存入DataFrame
 
 for stock in stocks:
@@ -93,22 +51,69 @@ print("#########################################################################
       "###############################################################################################################"
       "###############################################################################################################"
       "###############################################################################################################")
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh?charset=utf8')
 
-# 开始去包含
-# qbh(stocks)
-thread_list = []
-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?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()
-    thread_list.append(thread)
+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]
+    for i in thd.df_day.index:
+        if i == 0 or i == 1:
+            thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
+        # 不包含
+        elif (thd.new_df.iloc[-1, 3] > thd.df_day.loc[i, 'high']
+              and thd.new_df.iloc[-1, 4] > thd.df_day.loc[i, 'low']) \
+                or (thd.new_df.iloc[-1, 3] < thd.df_day.loc[i, 'high']
+                    and thd.new_df.iloc[-1, 4] < thd.df_day.loc[i, 'low']):
+            thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
+        # 包含
+        else:
+            # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
+            # 左高,下降
+            if thd.new_df.iloc[-2, 3] > thd.new_df.iloc[-1, 3]:
+                thd.new_df.iloc[-1, 3] = min(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
+                thd.new_df.iloc[-1, 4] = min(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
+            else:
+                # 右高,上升
+                thd.new_df.iloc[-1, 3] = max(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
+                thd.new_df.iloc[-1, 4] = max(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
+    thd.new_df.to_sql('stk%s_%s' % (stock, fre), con=engine, index=True, if_exists='append')
+    with engine.connect() as con:
+        con.execute('ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);' % (stock, fre))
+    # 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))
+    # thd.new_df.to_csv(
+    #     '/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220211qbh/qbh%s.csv' % stock[:6])
+    print(stock)
+    print("**************")
+        #
+        # # new_df.to_csv('new_df.csv')
+        #
+        # #return new_df
 
-for thread in thread_list:
-    thread.join()
+
+# engine = []
+# engine_backup = []
+#
+#
+#
+# # 开始去包含
+# # qbh(stocks)
+# thread_list = []
+# 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?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()
+#     thread_list.append(thread)
+#
+# for thread in thread_list:
+#     thread.join()
+#
 
 endtime = dt.now()
 print((endtime-starttime).seconds)

+ 4 - 4
real_time_signal.py

@@ -15,9 +15,9 @@ db = pymysql.connect(host='localhost',
                      user='root',
                      port=3307,
                      password='r6kEwqWU9!v3',
-                     database='qbh_hlfx')
+                     database='hlfx')
 # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
-engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_backup?charset=utf8')
+engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8')
 
 # 获取所有表名——确定计算范围
 cursor = db.cursor()
@@ -31,7 +31,7 @@ print(dt.now(), 'stocks范围已获取!')
 stk = locals()
 for stock in stocks:
     try:
-        stk[stock[:6]] = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from stk%s_%s' % (stock[:6], fre),
+        stk[stock] = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from `stk%s_%s`' % (stock, fre),
                                         engine2)
     except BaseException:
         continue
@@ -45,7 +45,7 @@ def qbh_hlfx(stocks, df):
     for stock in stocks:
         try:
             # thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
-            thd.df_day = stk[stock[:6]]
+            thd.df_day = stk[stock]
             thd.get_bars = df.loc[stock]
             stk_len = len(thd.df_day)
             # 先处理去包含

+ 3 - 3
real_time_signal_30m.py

@@ -17,7 +17,7 @@ db = pymysql.connect(host='localhost',
                      password='r6kEwqWU9!v3',
                      database='qbh_hlfx')
 # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
-engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_backup?charset=utf8')
+engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8')
 
 # 获取所有表名——确定计算范围
 cursor = db.cursor()
@@ -31,7 +31,7 @@ print(dt.now(), 'stocks范围已获取!')
 stk = locals()
 for stock in stocks:
     try:
-        stk[stock[:6]] = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from stk%s_%s' % (stock[:6], fre),
+        stk[stock] = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from `stk%s_%s`' % (stock, fre),
                                         engine2)
     except BaseException:
         continue
@@ -45,7 +45,7 @@ def qbh_hlfx(stocks, df):
     for stock in stocks:
         try:
             # thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
-            thd.df_day = stk[stock[:6]]
+            thd.df_day = stk[stock]
             thd.get_bars = df.loc[stock]
             stk_len = len(thd.df_day)
             # 先处理去包含