一个免费额、开源的python财经数据接口包
import tushare as ts df = ts.get_k_data(\'60138\')
Signature:
ts.get_k_data(
[\'code=None\', "start=\'\'", "end=\'\'", "ktype=\'D\'", "autype=\'qfq\'", \'index=False\', \'retry_count=3\', \'pause=0.001\'],
)
Docstring:
获取k线数据
---------
Parameters:
code:string
股票代码 e.g. 600848
start:string
开始日期 format:YYYY-MM-DD 为空时取上市首日
end:string
结束日期 format:YYYY-MM-DD 为空时取最近一个交易日
autype:string
复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
ktype:string
数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame
date 交易日期 (index)
open 开盘价
high 最高价
close 收盘价
low 最低价
volume 成交量
amount 成交额
turnoverratio 换手率
code 股票代码
File: d:\python35\lib\site-packages\tushare\stock\trading.py
Type: function
import tushare as ts import pandas as pd import numpy as np import matplotlib.pyplot as plt df = ts.get_k_data(\'600519\',start=\'1988-01-01\') df.to_csv(\'600519.csv\')
df = pd.read_csv(\'600519.csv\',index_col=\'date\',parse_dates=[\'date\'])[[\'open\',\'close\',\'high\',\'low\']]
df = df[(df[\'close\']-df[\'open\'])/df[\'open\']>=0.03]
# df.shift(-1)# 往上移动一位,nan补
df = df[(df[\'open\']-df[\'close\'].shift(1))/df[\'close\'].shift(1)<=-0.02] # 布尔型索引
price_last = df[\'open\'][-1]
df = df[\'2001-09\':\'2017-11\'] # 索引为日期类型才允许这么干
df_monthly = df.resample(\'M\').first()
df_yearly = df.resample(\'A\').last()[:-1]
cost_money=0
hold=0
for year in range(2001,2018):
cost_money += df_monthly[str(year)][\'open\'].sum()*100
hold+=len(df_monthly[str(year)][\'open\'])*1000
if year!=2017:
cost_money -= df_yearly[str(year)][\'open\'][0] * hold
hold = 0
print(cost_money)
cost_money -= hold*price_last
print(-cost_money )
查找历史金叉死叉日期
均线: 对于每一个交易日,都乐意计算出前N天的移动平均值,然后把这些移动平均值连接起来成一条线,就叫做N日移动平均线。移动平均线常用5、10、30、60天。 5天和10天的是短线操作的参照指标,称做日均线指标 30天和60天的是中期均线指标,称做季均线指标 120和240天的是长期均线指标,称做年均线指标 金叉:短期均线上穿长期均线,买入信号 死叉:短期均线下穿长期均线,卖出信号
import matplotlib.pyplot as plt import mpl_finance as fin from matplotlib.dates import date2num import pandas as pd df = pd.read_csv(\'600519.csv\', parse_dates=[\'date\'], index_col=[\'date\'], sep=\'\s+\')[[\'open\', \'close\', \'high\', \'low\']] df[\'time\'] = date2num(df.index.to_pydatetime()) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # 创建子图实例,传入第一参数 arr = df[[\'time\', \'open\', \'close\', \'high\', \'low\']].values # 获取序列化值,传入第二参数 fin.candlestick_ochl(ax, arr) # 把子图和数据传入 plt.show()