聚宽
聚宽平台需要申请账号,可试用14天(限制访问数据50w条/每天)
import jqdatasdk as jq
from datetime import datetime, timedelta
import time
# https://www.joinquant.com/help/api/help#api:API%E6%96%87%E6%A1%A3
# https://www.joinquant.com/help/api/help#JQData:JQData
# aa 为你自己的帐号, bb 为你自己的密码
jq.auth('aa','bb')
# 根据股票名,获取股票 code
def get_stock_code(stock_name):
securities = jq.get_all_securities()
stock_code = securities[securities['display_name'] == stock_name].index[0]
return stock_code
def get_stock_data(stock_code,days):
current_dt = time.strftime("%Y-%m-%d", time.localtime())
current_dt = datetime.strptime(current_dt, '%Y-%m-%d')
day = 1
previous_date = current_dt - timedelta(days = day)
data = jq.get_price(stock_code, end_date = previous_date, count = days, frequency='daily', fields=['close'])
return data
if __name__ == "__main__":
stock_name = "大北农"
stock_code = get_stock_code(stock_name)
data = get_stock_data(stock_code,20)
print(data)
qstock
是免费的的数据获取接口,不需要注册。
import qstock as qs
from datetime import datetime, timedelta
import time
def get_stock_data(stock_code,days):
current_dt = time.strftime("%Y-%m-%d", time.localtime())
current_dt = datetime.strptime(current_dt, '%Y-%m-%d')
start_date = current_dt - timedelta(days=int(days / 5 *7))
start_date = start_date.strftime("%Y-%m-%d")
df = qs.get_data(stock_code, start=start_date, end=None, freq='d', fqt=1)
# print(df.tail(days))
# print(df.columns)
return df.tail(days)
if __name__ == "__main__":
get_stock_data("牧原股份",3)
实时数据获取如下:
def get_current_price(stock_code):
df = qs.realtime_data(code = stock_code)
current_price = df.loc[0,"最新"]
return current_price
基金数据获取
import akshare as ak
fund_em_open_fund_daily_df = ak.fund_money_fund_daily_em()
print(fund_em_open_fund_daily_df.tail(10))
df = ak.fund_money_fund_info_em("320007")
print(df.head(10))
数据预处理
参考 https://zhuanlan.zhihu.com/p/408134295