get_history_price.py 1.2 KB

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