您现在的位置是:首页 > 技术教程 正文

python金融数据分析和可视化--03利用Akshare获取股票数据

admin 阅读: 2024-03-24
后台-插件-广告管理-内容页头部广告(手机)

02利用Akshare获取股票数据

1. AKShare 的介绍

AKShare 是基于 Python 的财经数据接口库,目的是实现对股票、期货、期权、基金、外汇、债券、指数、加密货币等金融产品的基本面数据、实时和历史行情数据、衍生数据从数据采集、数据清洗到数据落地的一套工具,主要用于学术研究目的。

AKShare 的特点是获取的是相对权威的财经数据网站公布的原始数据,通过利用原始数据进行各数据源之间的交叉验证,进而再加工,从而得出科学的结论。

2. 安装 AKShare

pip install akshare

3. 获取股票数据

AKShare 股票数据
AKShare github

  1. # 股票市场总貌
  2. import akshare as ak
  3. # 上海证券交易所
  4. # http://www.sse.com.cn/market/stockdata/statistic/
  5. def sh_df():
  6. stock_sse_summary_df = ak.stock_sse_summary()
  7. print(stock_sse_summary_df)
  8. # 深圳证券交易所
  9. # 证券类别统计
  10. # http://www.szse.cn/market/overview/index.html
  11. def sz_df():
  12. stock_szse_summary_df = ak.stock_szse_summary()
  13. print(stock_szse_summary_df)
  14. # 深圳证券交易所
  15. # 地区交易排序
  16. # http://www.szse.cn/market/overview/index.html
  17. def sz_area():
  18. stock_szse_area_summary_df = ak.stock_szse_area_summary(date="202203")
  19. print(stock_szse_area_summary_df)
  20. # 深圳证券交易所
  21. # 股票行业成交
  22. # http://docs.static.szse.cn/www/market/periodical/month/W020220511355248518608.html
  23. def sz_sector():
  24. stock_szse_sector_summary_df = ak.stock_szse_sector_summary(symbol="当年", date="202204")
  25. print(stock_szse_sector_summary_df)
  26. # 上海证券交易所
  27. # 每日概况
  28. # http://www.sse.com.cn/market/stockdata/overview/day/
  29. def sh_day():
  30. stock_sse_deal_daily_df = ak.stock_sse_deal_daily(date="20201111")
  31. print(stock_sse_deal_daily_df)
  32. def get_account_statistics():
  33. # 股票账户统计月度
  34. """
  35. 输出参数
  36. 名称 类型 描述
  37. 数据日期 object -
  38. 新增投资者-数量 float64 注意单位: 万户
  39. 新增投资者-环比 float64 -
  40. 新增投资者-同比 float64 -
  41. 期末投资者-总量 float64 注意单位: 万户
  42. 期末投资者-A股账户 float64 注意单位: 万户
  43. 期末投资者-B股账户 float64 注意单位: 万户
  44. 沪深总市值 float64 -
  45. 沪深户均市值 float64 注意单位: 万
  46. 上证指数-收盘 float64 -
  47. 上证指数-涨跌幅 float64 -
  48. """
  49. account = ak.stock_account_statistics_em()
  50. account.set_index("数据日期", inplace=True) # 设置索引值
  51. account.to_csv("I:\\bianchengxx\\pythonxx\\backtrader_001\\datas\\stock_account_statistics.csv")
  52. print(account)
  53. if __name__ == '__main__':
  54. # sh_df()
  55. # sz_df()
  56. # sz_area()
  57. # sz_sector()
  58. # sh_day()
  59. get_account_statistics()
  1. # 个股信息查询
  2. # http://quote.eastmoney.com/concept/sh603777.html?from=classic
  3. import akshare as ak
  4. df = ak.stock_individual_info_em(symbol="002624")
  5. print(df)
  1. # 实时行情数据-东财
  2. # 沪深京 A 股
  3. # http://quote.eastmoney.com/center/gridlist.html#hs_a_board
  4. import akshare as ak
  5. # 实时行情数据-东财
  6. # 沪深京 A 股
  7. # 单次返回所有沪深京 A 股上市公司的实时行情数据
  8. def em_spot():
  9. stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
  10. print(stock_zh_a_spot_em_df)
  11. # 实时行情数据-东财
  12. # 沪 A 股
  13. # http://quote.eastmoney.com/center/gridlist.html#sh_a_board
  14. def em_sha_spot():
  15. stock_sh_a_spot_em_df = ak.stock_sh_a_spot_em()
  16. print(stock_sh_a_spot_em_df)
  17. # 实时行情数据-东财
  18. # 深 A 股
  19. # http://quote.eastmoney.com/center/gridlist.html#sz_a_board
  20. def em_sza_spot():
  21. stock_sz_a_spot_em_df = ak.stock_sz_a_spot_em()
  22. print(stock_sz_a_spot_em_df)
  23. # 实时行情数据-东财
  24. # 京 A 股
  25. # http://quote.eastmoney.com/center/gridlist.html#bj_a_board
  26. def em_bja_spot():
  27. stock_bj_a_spot_em_df = ak.stock_bj_a_spot_em()
  28. print(stock_bj_a_spot_em_df)
  29. # 实时行情数据-东财
  30. # 新股
  31. # http://quote.eastmoney.com/center/gridlist.html#newshares
  32. def em_new_spot():
  33. stock_new_a_spot_em_df = ak.stock_new_a_spot_em()
  34. print(stock_new_a_spot_em_df)
  35. # 实时行情数据-东财
  36. # 科创板
  37. # http://quote.eastmoney.com/center/gridlist.html#hs_a_board
  38. def em_kc_spot():
  39. stock_kc_a_spot_em_em_df = ak.stock_kc_a_spot_em()
  40. print(stock_kc_a_spot_em_em_df)
  41. # 实时行情数据-新浪
  42. # http://vip.stock.finance.sina.com.cn/mkt/#hs_a
  43. def xl_a_spot():
  44. stock_zh_a_spot_df = ak.stock_zh_a_spot()
  45. print(stock_zh_a_spot_df)
  46. if __name__ == "__main__":
  47. em_spot()
  48. # em_sha_spot()
  49. # em_sza_spot()
  1. # 历史行情数据-东财
  2. # https://quote.eastmoney.com/concept/sh603777.html?from=classic
  3. import akshare as ak
  4. import pandas as pd
  5. pd.set_option('expand_frame_repr', False) # True就是可以换行显示。设置成False的时候不允许换行
  6. pd.set_option('display.max_columns', None) # 显示所有列
  7. # pd.set_option('display.max_rows', None) # 显示所有行
  8. pd.set_option('colheader_justify', 'centre') # 显示居中
  9. def a_hist():
  10. # period
  11. # str
  12. # choice of {'daily', 'weekly', 'monthly'}
  13. # start_date
  14. # str
  15. # 开始查询的日期
  16. # end_date
  17. # str
  18. # 结束查询的日期
  19. # adjust
  20. # str
  21. # 默认返回不复权的数据;
  22. # qfq: 返回前复权后的数据;
  23. # hfq: 返回后复权后的数据
  24. stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol="002624", period="weekly", start_date="20190301", end_date='20230907',
  25. adjust="qfq")
  26. print(stock_zh_a_hist_df)
  27. # 分时数据-东财
  28. # http://quote.eastmoney.com/concept/sh603777.html?from=classic
  29. # period str choice of {'1', '5', '15', '30', '60'};
  30. # adjust str choice of {'', 'qfq', 'hfq'}; '': 不复权, 'qfq': 前复权, 'hfq': 后复权,
  31. # 其中 1 分钟数据返回近 5 个交易日数据且不复权
  32. def a_hist_min():
  33. stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol="002624", start_date="2023-01-01 09:30:00", end_date="2023-02-03 15:00:00", period='30', adjust='')
  34. print(stock_zh_a_hist_min_em_df)
  35. if __name__ == "__main__":
  36. a_hist()
  37. # a_hist_min()

4. 获取股票数据本地存储

  1. import akshare as ak
  2. import pandas as pd
  3. pd.set_option('expand_frame_repr', False) # True就是可以换行显示。设置成False的时候不允许换行
  4. pd.set_option('display.max_columns', None) # 显示所有列
  5. pd.set_option('display.max_rows', None) # 显示所有行
  6. pd.set_option('colheader_justify', 'centre') # 显示居中
  7. def download_hist(symbol="002624", period="daily", start="1990101", end="20230318", adjust="qfq"):
  8. stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=symbol, period=period, start_date=start, end_date=end,
  9. adjust=adjust)
  10. stock_zh_a_hist_df.sort_values("日期", inplace=True)
  11. stock_zh_a_hist_df.set_index("日期", inplace=True)
  12. if period=="daily":
  13. stock_zh_a_hist_df.to_csv("I:\\akshare_stock\\stock_datas\\day\\{}.csv".format(symbol))
  14. elif period=="weekly":
  15. stock_zh_a_hist_df.to_csv("I:\\akshare_stock\\stock_datas\\week\\{}.csv".format(symbol))
  16. elif period=="monthly":
  17. stock_zh_a_hist_df.to_csv("I:\\akshare_stock\\stock_datas\\month\\{}.csv".format(symbol))
  18. else:
  19. print("period错误")
  20. def download_hist_min(symbol="002624", start="1990-01-01 09:30:00", end="2023-02-03 15:00:00", period='30', adjust='qfq'):
  21. stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol=symbol, start_date=start, end_date=end, period=period, adjust=adjust)
  22. stock_zh_a_hist_min_em_df.set_index("时间", inplace=True)
  23. stock_zh_a_hist_min_em_df.to_csv("I:\\akshare_stock\\stock_datas\\minute\\"+symbol+"_{}.csv".format(period))
  24. if __name__ == "__main__":
  25. df = pd.read_csv("I:\\akshare_stock\\stock_datas\\stock_list.csv")
  26. df.sort_values("symbol", inplace=True)
  27. code = list(df["ts_code"])
  28. print(len(code))
  29. for period in ['daily', 'weekly', 'monthly']:
  30. for i in range(0, len(code)):
  31. symbol = code[i].rstrip('.SZHBJ')
  32. download_hist(symbol=symbol, period=period)
  33. print(symbol)
  34. for period in ['1', '5', '15', '30', '60']:
  35. for i in range(0, len(code)):
  36. symbol = code[i].rstrip('.SZHBJ')
  37. download_hist_min(symbol=symbol, period=period)
  38. print(symbol)

5.将股票数据存到mysql数据库中

  1. """
  2. date:20210918
  3. 将CSV文件写入到MySQL中
  4. """
  5. import pandas as pd
  6. from sqlalchemy import create_engine
  7. def connect_db(db):
  8. engine = create_engine('mysql+pymysql://hao:671010@localhost:3306/{}?charset=utf8'.format(db))
  9. return engine
  10. def create_stock(akcode, db, date):
  11. # 读取本地CSV文件
  12. df = pd.read_csv(
  13. 'I:\\akshare_stock\\stock_datas\\' + date + '\\{}.csv'.format(akcode))
  14. engine = connect_db(db)
  15. # name='stocklist'全部小写否则会报错
  16. df.to_sql(name='ak_' + date + '_{}'.format(akcode), con=engine, index=False, if_exists='replace')
  17. def create_min_stock(akcode, db, date):
  18. # 读取本地CSV文件
  19. df = pd.read_csv(
  20. 'I:\\akshare_stock\\stock_datas\\minute\\'+akcode+'_{}.csv'.format(date))
  21. engine = connect_db(db)
  22. # name='stocklist'全部小写否则会报错
  23. df.to_sql(name='ak_' + akcode + '_{}'.format(date), con=engine, index=False, if_exists='replace')
  24. def read_csv(code):
  25. df = pd.read_csv('I:\\akshare_stock\\stock_datas\\{}.csv'.format(code))
  26. return df
  27. stockDB = 'akshare_stock'
  28. stockList = 'stock_list'
  29. create_stock(akcode=stockList, db=stockDB, date="day")
  30. df1 = read_csv(stockList)
  31. df1.sort_values("ts_code", inplace=True)
  32. li1 = list(df1['ts_code'])
  33. for date in ["day", "week", "month"]:
  34. for i in range(0, len(li1)):
  35. codeStock = li1[i].rstrip('.SHZBJ')
  36. create_stock(akcode=codeStock, db=stockDB, date=date)
  37. print(codeStock)
  38. for date in ['1', '5', '15', '30', '60']:
  39. for i in range(0, len(li1)):
  40. codeStock = li1[i].rstrip('.SHZBJ')
  41. create_min_stock(akcode=codeStock, db=stockDB, date=date)
  42. print(codeStock)
标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索