Browse Source

get_prices

Daniel 3 years ago
parent
commit
b4cabbb351
3 changed files with 27 additions and 19 deletions
  1. 1 1
      .idea/misc.xml
  2. 1 1
      .idea/stock.iml
  3. 25 17
      get_hisbars_tosql.py

+ 1 - 1
.idea/misc.xml

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (jqdata)" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
 </project>

+ 1 - 1
.idea/stock.iml

@@ -2,7 +2,7 @@
 <module type="PYTHON_MODULE" version="4">
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
-    <orderEntry type="jdk" jdkName="Python 3.8 (jqdata)" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="module" module-name="stocks_to_sql" />
     <orderEntry type="module" module-name="quantify01" />

+ 25 - 17
get_hisbars_tosql.py

@@ -9,36 +9,44 @@ from datetime import datetime as dt
 import pymysql
 
 starttime = dt.now()
-stocks = list(get_all_securities(['stock'],date='2021-12-31').index)
+stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
 
 print('ready to get bars')
-df = get_bars(stocks, count=2000, unit='1d',
-                      fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'], include_now=False, df=True)
+# df = get_bars(stocks, count=2000, unit='1d',
+#                       fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'], include_now=False, df=True)
 
 
 # db = pymysql.connect(host='localhost',
 #                      user='root',
-#                      password='VopNWyLA-W8W8EooUmsX',
-#                      database='test-stocks',
+#                      password='r6kEwqWU9!v3',
+#                      database='stocks',
 #                      connect_timeout=600)
-db_stk_sql = pymysql.connect(host='chenzheshi.myds.me',
-                             port=3456,
-                             user='root',
-                             password='r6kEwqWU9!v3',
-                             database='stocks',
-                             connect_timeout=600)
-cursor = db_stk_sql.cursor()
-engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@chenzheshi.myds.me:3456/stocks?charset=utf8')
+# # db_stk_sql = pymysql.connect(host='chenzheshi.myds.me',
+#                              port=3456,
+#                              user='root',
+#                              password='r6kEwqWU9!v3',
+#                              database='stocks',
+#                              connect_timeout=600)
+# cursor = db.cursor()
+engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
 
 print('ready to write to mysql')
 
+fre = '1d'
+
 for stock in stocks:
-    df_stock =df.loc[stock]
-    df_stock.to_sql('stk%s_%s' % (stock[:6], '1d'), con=engine, index=True, if_exists='replace')
+    # df_stock =df.loc[stock]
+    print(stock)
+    df_stock = get_price(stock, start_date='2010-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)
+    df_stock.reset_index(inplace=True)
+    df_stock.rename(columns={'index': 'date'}, inplace=True)
+    df_stock.to_sql('stk%s_%s' % (stock[:6], fre), con=engine, index=True, if_exists='replace')
     with engine.connect() as con:
-        con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' %(stock[:6], '1d'))
+        con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], fre))
 endtime = dt.now()
-db_stk_sql.close()
+# db_stk_sql.close()
 print((endtime-starttime).seconds)