8,双均线策略
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 F
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))
s1 = ma5 < ma30 s2 = ma5 >= ma30 df.loc[~(s1 | s2.shift(1))].index
df.loc[s1&s2.shift(1)].index
问题:如果我从假如我从2010年1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益率如何?
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.8999999994
二、基于茅台数据的处理,熟悉DataFrame
import tushare as ts import pandas as pd from pandas import DataFrame,Series
DataFrame
- 索引:
- 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