get_hisbars_tosql.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from jqdatasdk import *
  2. auth('18616891214','Ea?*7f68nD.dafcW34d!')
  3. from sqlalchemy import create_engine
  4. import pandas as pd
  5. import threading
  6. from datetime import datetime as dt
  7. import pymysql
  8. starttime = dt.now()
  9. stocks = list(get_all_securities(['stock'],date='2021-12-31').index)
  10. print('ready to get bars')
  11. df = get_bars(stocks, count=2000, unit='1d',
  12. fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'], include_now=False, df=True)
  13. # db = pymysql.connect(host='localhost',
  14. # user='root',
  15. # password='VopNWyLA-W8W8EooUmsX',
  16. # database='test-stocks',
  17. # connect_timeout=600)
  18. db_stk_sql = pymysql.connect(host='chenzheshi.myds.me',
  19. port=3456,
  20. user='root',
  21. password='r6kEwqWU9!v3',
  22. database='stocks',
  23. connect_timeout=600)
  24. cursor = db_stk_sql.cursor()
  25. engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@chenzheshi.myds.me:3456/stocks?charset=utf8')
  26. print('ready to write to mysql')
  27. for stock in stocks:
  28. df_stock =df.loc[stock]
  29. df_stock.to_sql('stk%s_%s' % (stock[:6], '1d'), con=engine, index=True, if_exists='replace')
  30. with engine.connect() as con:
  31. con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' %(stock[:6], '1d'))
  32. endtime = dt.now()
  33. db_stk_sql.close()
  34. print((endtime-starttime).seconds)