efinance获取基金、股票、债券、期货K线数据
·
efinance github地址:https://github.com/Micro-sheep/efinance
安装方法:
pip install efinance
使用示例
github页面的示例已经足够详细了,这里提供一个把股票数据封装成pandas.DataFrame的方法:
import pandas as pd
import efinance
def get_k_data(stock_code, begin="20200101", end="20210101") -> pd.DataFrame:
"""
根据efinance工具包获取股票数据
:param stock_code:股票代码
:param begin: 开始日期
:param end: 结束日期
:return:
"""
# stock_code = '600519' # 股票代码,茅台
k_dataframe: pd.DataFrame = efinance.stock.get_quote_history(stock_code, beg=begin, end=end, )
k_dataframe = k_dataframe.iloc[:, :9]
k_dataframe.columns = ['name', 'code', 'date', 'open', 'close', 'high', 'low', 'volume', 'turnover']
k_dataframe.index = pd.to_datetime(k_dataframe.date)
k_dataframe.drop(['name', 'code', "date"], axis=1, inplace=True)
return k_dataframe
if __name__ == '__main__':
df = get_k_data(stock_code='600519') # 茅台股价
print(df.head())
得到结果:
open close high low volume turnover
date
2020-01-02 1091.68 1093.68 1108.74 1079.68 148099 1.669684e+10
2020-01-03 1080.68 1042.24 1080.68 1040.58 130319 1.426638e+10
2020-01-06 1034.54 1041.67 1056.58 1030.98 63415 6.853918e+09
2020-01-07 1041.18 1058.21 1062.68 1040.08 47854 5.220697e+09
2020-01-08 1048.73 1051.82 1059.18 1046.26 25008 2.720372e+09
更多推荐




所有评论(0)