import tushare as ts import pandas as pd from pandas import DataFrame,Series df = pd.read_csv('maotai.csv',index_col='date',parse_dates=['date']) df.drop(labels='Unnamed: 0',axis=1,inplace=True) df
ma5 = df['close'].rolling(5).mean() ma30 = df['close'].rolling(30).mean() df['ma5'] = ma5 df['ma30'] = ma30
s1 = ma5 < ma30 T->F金叉 F->T死叉 s2 = ma5 >= ma30 s1 T T F F T T F Fapp
s2 F F T T F F T T T F T T T F T F T F F F T F函數
~(s1 | s2.shift(1))spa
s1 = ma5 < ma30 s2 = ma5 >= ma30 df.loc[~(s1 | s2.shift(1))].index
df.loc[s1&s2.shift(1)].index
問題:若是我從假如我從2010年1月1日開始,初始資金爲100000元,金叉儘可能買入,死叉所有賣出,則到今天爲止,個人炒股收益率如何? code
df = df['2010':'2019'] df
df['ma5']=df['close'].rolling(5).mean() df['ma30']=df['close'].rolling(30).mean()
sr1 = df['ma5'] < df['ma30'] sr2 = df['ma5'] >= df['ma30'] death_cross = df[sr1 & sr2.shift(1)].index golden_cross = df[~(sr1 | sr2.shift(1))].index
first_money = 100000 money = first_money hold = 0#持有多少股 sr1 = pd.Series(1, index=golden_cross) sr2 = pd.Series(0, index=death_cross) #根據時間排序 sr = sr1.append(sr2).sort_index() for i in range(0, len(sr)): p = df['open'][sr.index[i]] if sr[i] == 1: #金叉 buy = (money // (100 * p)) hold += buy*100 money -= buy*100*p else: money += hold * p hold = 0 p = df['open'][-1] now_money = hold * p + money print(now_money - first_money)
結果:1086009.8999999994blog
import tushare as ts import pandas as pd from pandas import DataFrame,Series
- 索引: - df[col] df[[c1,c2]]:取列 - df.loc[index] : 取行 - df.loc[index,col] : 取元素 - 切片: - df[a:b]:切行 - df.loc[:,a:b]:切列 - df運算:Series運算一致 - df級聯:拼接
df = pd.read_csv('maotai.csv',index_col='date',parse_dates=['date']) df.drop(labels='Unnamed: 0',axis=1,inplace=True) df
#假如我從2010年1月1日開始,每個月第一個交易日買入1手股票,每一年最後一個交易日賣出全部股票,到今天爲止,個人收益如何? price_last = df['open'][-1] df = df['2010':'2019'] #剔除首尾無用的數據 #Pandas提供了resample函數用便捷的方式對時間序列進行重採樣,根據時間粒度的變大或者變小分爲降採樣和升採樣: df_monthly = df.resample("M").first() df_yearly = df.resample("Y").last()[:-1] #去除最後一年 cost_money = 0 hold = 0 #每一年持有的股票 for year in range(2010, 2020): cost_money -= df_monthly.loc[str(year)]['open'].sum()*100 hold += len(df_monthly[str(year)]['open']) * 100 if year != 2019: cost_money += df_yearly[str(year)]['open'][0] * hold hold = 0 #每一年持有的股票 cost_money += hold * price_last print(cost_money)
結果:310250.69999999984