一、使用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]
二、輸出
.........
切掉最後一行,是由於今年還沒到年末
雖然顯示的是一天可是其實表明一年
這是每一年的最後一天,交易日不必定有這一天