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

 

 

转载于:https://www.cnblogs.com/feifeifeisir/p/10497998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值