一、使用tushare包获取某股票的历史行情数据
二、输出该股票全部收盘比开盘上涨3%以上的日期
三、输出该股票全部开盘比前日收盘跌幅超过2%的日期
四、假如我从2010年1月1日开始,每个月第一个交易日买入1受股票,每一年最后一个交易日卖出全部的股票,到今天为止,个人收益如何?python
一、实现代码 spa
import pandas as pd import numpy as np import tushare as ts %matplotlib auto 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
二、输出截图blog
......接口
一、代码 get
df[(df['close']-df['open'])/df['open']>0.03].index
二、输出pandas
一、代码ast
df[(df['open']-df['close'].shift(1))/df['close'].shift(1)<=-0.02].index
二、输出class
一、代码import
df['close'].shift(1)
二、输出date
一、代码
df_monthly = df.resample('M').first() df_yearly = df.resample('A').last()[:-1] cost_money = 0 hold = 0 for year in range(2001, 2019): cost_money += df_monthly[str(year)]['open'].sum()*100 hold += len(df_monthly[str(year)]['open']) * 100 if year != 2018: cost_money -= df_yearly[str(year)]['open'][0] * hold hold = 0 print(cost_money) cost_money -= hold * price_last print(-cost_money)
二、输出
三、打印df_yearly
df_monthly = df.resample('M').first() df_yearly = df.resample('A').last()[:-1] df_yearly['2001']
输出
四、打印df_monthly
df_monthly['2018']
一、代码
price_last = df['open'][-1] df = df['2001-9':'2018-9'] df
二、输出
.......
一、代码
df.resample('m').first()
二、输出
.........
一、代码
df.resample('3D').mean() df.resample('A').last()[:-1]
二、输出
.........
切掉最后一行,是由于今年还没到年末
虽然显示的是一天可是其实表明一年
这是每一年的最后一天,交易日不必定有这一天