金融量化分析【day112】:股票数据分析Tushare2
目录
1、使用tushare包获取某股票的历史行情数据
2、使用pandas包计算该股票历史数据的5日局限和60日均线
3、matplotlib包可视化历史数据的收盘价和历史均线
4、分析输出所有金叉日志和死叉日期
5、如果我从2010年1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益如何?
一、使用tushare包获取某股票的历史行情数据
1、代码
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
2、输出截图
..........
二、使用pandas包计算该股票历史数据的5日均线和60日均线
1、方法一
1、5日均线代码
df['ma5']=df['open'].rolling(5).mean() df
2、5日均线截图
..............................
3、30日均线代码
df['ma30']=df['open'].rolling(30).mean() df
4、30日均线截图
..................................第30日如下.....................................................
..................................
2、方法二
1、5日均线代码
pd.DataFrame({'c1':df['close'].cumsum(),'c2':df['close'].cumsum().shift(5)})
2、5日均线截图
.........................
1、30日均线代码
pd.DataFrame({'c1':df['close'].cumsum(),'c2':df['close'].cumsum().shift(30)})
2、30日均线截图
..................................................
3、使用知识点
方法一
方法二
三、matplotlib包可视化历史数据的收盘价和历史均线
1、代码(2010-2019)
import matplotlib.pyplot as plt df[['close','ma5','ma30']].plot() plt.show()
2、输出截图(数据比较多所以不是很清晰)
3、代码(2012年)
df.loc["2012",['close','ma5','ma30']].plot() plt.show()
4、输出(线条区分比较明显)
四、分析输出所有金叉日志和死叉日期
1、金叉日期
方法一
1、代码
golden_cross = [] death_cross = [] for i in range(1,len(df)): if df['ma5'][i] >= df['ma30'][i] and df['ma5'][i-1] < df['ma30'][i-1]: golden_cross.append(df.index[i].to_pydatetime()) if df['ma5'][i] <= df['ma30'][i] and df['ma5'][i-1] > df['ma30'][i-1]: death_cross.append(df.index[i].to_pydatetime()) golden_cross
2、输出日志
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[datetime.datetime(2001, 11, 21, 0, 0), datetime.datetime(2002, 1, 25, 0, 0), datetime.datetime(2002, 2, 5, 0, 0), datetime.datetime(2002, 6, 24, 0, 0), datetime.datetime(2002, 12, 6, 0, 0), datetime.datetime(2003, 1, 17, 0, 0), datetime.datetime(2003, 4, 16, 0, 0), datetime.datetime(2003, 6, 2, 0, 0), datetime.datetime(2003, 6, 25, 0, 0), datetime.datetime(2003, 7, 14, 0, 0), datetime.datetime(2003, 10, 15, 0, 0), datetime.datetime(2004, 3, 3, 0, 0), datetime.datetime(2004, 7, 6, 0, 0), datetime.datetime(2004, 9, 14, 0, 0), datetime.datetime(2005, 1, 5, 0, 0), datetime.datetime(2005, 4, 26, 0, 0), datetime.datetime(2005, 6, 15, 0, 0), datetime.datetime(2005, 6, 17, 0, 0), datetime.datetime(2005, 12, 29, 0, 0), datetime.datetime(2006, 7, 31, 0, 0), datetime.datetime(2006, 8, 30, 0, 0), datetime.datetime(2007, 2, 26, 0, 0), datetime.datetime(2007, 4, 16, 0, 0), datetime.datetime(2007, 4, 27, 0, 0), datetime.datetime(2007, 5, 9, 0, 0), datetime.datetime(2007, 5, 24, 0, 0), datetime.datetime(2007, 7, 25, 0, 0), datetime.datetime(2007, 9, 19, 0, 0), datetime.datetime(2007, 10, 10, 0, 0), datetime.datetime(2007, 11, 19, 0, 0), datetime.datetime(2007, 12, 11, 0, 0), datetime.datetime(2008, 3, 6, 0, 0), datetime.datetime(2008, 5, 8, 0, 0), datetime.datetime(2008, 5, 15, 0, 0), datetime.datetime(2008, 7, 17, 0, 0), datetime.datetime(2008, 11, 20, 0, 0), datetime.datetime(2009, 2, 10, 0, 0), datetime.datetime(2009, 4, 9, 0, 0), datetime.datetime(2009, 6, 5, 0, 0), datetime.datetime(2009, 8, 27, 0, 0), datetime.datetime(2009, 9, 9, 0, 0), datetime.datetime(2009, 11, 13, 0, 0), datetime.datetime(2010, 1, 21, 0, 0), datetime.datetime(2010, 2, 4, 0, 0), datetime.datetime(2010, 2, 8, 0, 0), datetime.datetime(2010, 6, 7, 0, 0), datetime.datetime(2010, 7, 20, 0, 0), datetime.datetime(2010, 10, 26, 0, 0), datetime.datetime(2010, 11, 11, 0, 0), datetime.datetime(2011, 2, 14, 0, 0), datetime.datetime(2011, 3, 15, 0, 0), datetime.datetime(2011, 4, 28, 0, 0), datetime.datetime(2011, 10, 26, 0, 0), datetime.datetime(2012, 2, 13, 0, 0), datetime.datetime(2012, 4, 9, 0, 0), datetime.datetime(2012, 7, 26, 0, 0), datetime.datetime(2012, 8, 10, 0, 0), datetime.datetime(2012, 9, 13, 0, 0), datetime.datetime(2012, 9, 28, 0, 0), datetime.datetime(2012, 12, 24, 0, 0), datetime.datetime(2013, 1, 9, 0, 0), datetime.datetime(2013, 3, 12, 0, 0), datetime.datetime(2013, 4, 18, 0, 0), datetime.datetime(2013, 7, 4, 0, 0), datetime.datetime(2013, 10, 22, 0, 0), datetime.datetime(2013, 11, 12, 0, 0), datetime.datetime(2013, 11, 29, 0, 0), datetime.datetime(2014, 1, 24, 0, 0), datetime.datetime(2014, 4, 3, 0, 0), datetime.datetime(2014, 6, 24, 0, 0), datetime.datetime(2014, 9, 5, 0, 0), datetime.datetime(2014, 9, 30, 0, 0), datetime.datetime(2014, 11, 18, 0, 0), datetime.datetime(2014, 12, 1, 0, 0), datetime.datetime(2015, 2, 16, 0, 0), datetime.datetime(2015, 6, 12, 0, 0), datetime.datetime(2015, 7, 16, 0, 0), datetime.datetime(2015, 9, 18, 0, 0), datetime.datetime(2015, 10, 8, 0, 0), datetime.datetime(2015, 12, 7, 0, 0), datetime.datetime(2015, 12, 21, 0, 0), datetime.datetime(2016, 2, 22, 0, 0), datetime.datetime(2016, 8, 11, 0, 0), datetime.datetime(2016, 10, 14, 0, 0), datetime.datetime(2016, 11, 28, 0, 0), datetime.datetime(2017, 7, 25, 0, 0), datetime.datetime(2017, 9, 19, 0, 0), datetime.datetime(2017, 12, 15, 0, 0), datetime.datetime(2018, 3, 19, 0, 0), datetime.datetime(2018, 5, 10, 0, 0), datetime.datetime(2018, 7, 19, 0, 0), datetime.datetime(2018, 7, 26, 0, 0), datetime.datetime(2018, 9, 21, 0, 0)]
方法二
1、代码
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 death_cross
2、输出截图
2、死叉日期
方法一
1、实现代码
golden_cross = [] death_cross = [] for i in range(1,len(df)): if df['ma5'][i] >= df['ma30'][i] and df['ma5'][i-1] < df['ma30'][i-1]: golden_cross.append(df.index[i].to_pydatetime()) if df['ma5'][i] <= df['ma30'][i] and df['ma5'][i-1] > df['ma30'][i-1]: death_cross.append(df.index[i].to_pydatetime()) death_cross
2、输出日期
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [datetime.datetime(2001, 10, 15, 0, 0), 2 datetime.datetime(2002, 1, 17, 0, 0), 3 datetime.datetime(2002, 1, 31, 0, 0), 4 datetime.datetime(2002, 4, 1, 0, 0), 5 datetime.datetime(2002, 7, 30, 0, 0), 6 datetime.datetime(2002, 12, 30, 0, 0), 7 datetime.datetime(2003, 3, 18, 0, 0), 8 datetime.datetime(2003, 4, 23, 0, 0), 9 datetime.datetime(2003, 6, 23, 0, 0), 10 datetime.datetime(2003, 7, 1, 0, 0), 11 datetime.datetime(2003, 8, 5, 0, 0), 12 datetime.datetime(2004, 3, 1, 0, 0), 13 datetime.datetime(2004, 5, 12, 0, 0), 14 datetime.datetime(2004, 8, 23, 0, 0), 15 datetime.datetime(2004, 11, 24, 0, 0), 16 datetime.datetime(2005, 4, 21, 0, 0), 17 datetime.datetime(2005, 5, 17, 0, 0), 18 datetime.datetime(2005, 6, 16, 0, 0), 19 datetime.datetime(2005, 9, 27, 0, 0), 20 datetime.datetime(2006, 7, 11, 0, 0), 21 datetime.datetime(2006, 8, 1, 0, 0), 22 datetime.datetime(2007, 2, 9, 0, 0), 23 datetime.datetime(2007, 2, 27, 0, 0), 24 datetime.datetime(2007, 4, 24, 0, 0), 25 datetime.datetime(2007, 4, 30, 0, 0), 26 datetime.datetime(2007, 5, 14, 0, 0), 27 datetime.datetime(2007, 7, 12, 0, 0), 28 datetime.datetime(2007, 9, 13, 0, 0), 29 datetime.datetime(2007, 9, 20, 0, 0), 30 datetime.datetime(2007, 11, 14, 0, 0), 31 datetime.datetime(2007, 11, 23, 0, 0), 32 datetime.datetime(2008, 1, 31, 0, 0), 33 datetime.datetime(2008, 3, 17, 0, 0), 34 datetime.datetime(2008, 5, 13, 0, 0), 35 datetime.datetime(2008, 5, 23, 0, 0), 36 datetime.datetime(2008, 8, 13, 0, 0), 37 datetime.datetime(2008, 12, 31, 0, 0), 38 datetime.datetime(2009, 3, 13, 0, 0), 39 datetime.datetime(2009, 4, 30, 0, 0), 40 datetime.datetime(2009, 8, 20, 0, 0), 41 datetime.datetime(2009, 9, 3, 0, 0), 42 datetime.datetime(2009, 10, 21, 0, 0), 43 datetime.datetime(2009, 12, 21, 0, 0), 44 datetime.datetime(2010, 1, 22, 0, 0), 45 datetime.datetime(2010, 2, 5, 0, 0), 46 datetime.datetime(2010, 3, 1, 0, 0), 47 datetime.datetime(2010, 6, 24, 0, 0), 48 datetime.datetime(2010, 10, 18, 0, 0), 49 datetime.datetime(2010, 11, 2, 0, 0), 50 datetime.datetime(2010, 12, 27, 0, 0), 51 datetime.datetime(2011, 3, 3, 0, 0), 52 datetime.datetime(2011, 3, 31, 0, 0), 53 datetime.datetime(2011, 9, 9, 0, 0), 54 datetime.datetime(2011, 12, 12, 0, 0), 55 datetime.datetime(2012, 4, 6, 0, 0), 56 datetime.datetime(2012, 7, 25, 0, 0), 57 datetime.datetime(2012, 8, 3, 0, 0), 58 datetime.datetime(2012, 8, 14, 0, 0), 59 datetime.datetime(2012, 9, 24, 0, 0), 60 datetime.datetime(2012, 11, 8, 0, 0), 61 datetime.datetime(2012, 12, 26, 0, 0), 62 datetime.datetime(2013, 1, 18, 0, 0), 63 datetime.datetime(2013, 3, 19, 0, 0), 64 datetime.datetime(2013, 6, 21, 0, 0), 65 datetime.datetime(2013, 7, 16, 0, 0), 66 datetime.datetime(2013, 10, 28, 0, 0), 67 datetime.datetime(2013, 11, 27, 0, 0), 68 datetime.datetime(2013, 12, 4, 0, 0), 69 datetime.datetime(2014, 4, 1, 0, 0), 70 datetime.datetime(2014, 4, 30, 0, 0), 71 datetime.datetime(2014, 8, 25, 0, 0), 72 datetime.datetime(2014, 9, 17, 0, 0), 73 datetime.datetime(2014, 10, 13, 0, 0), 74 datetime.datetime(2014, 11, 24, 0, 0), 75 datetime.datetime(2015, 1, 20, 0, 0), 76 datetime.datetime(2015, 6, 11, 0, 0), 77 datetime.datetime(2015, 6, 18, 0, 0), 78 datetime.datetime(2015, 7, 20, 0, 0), 79 datetime.datetime(2015, 9, 30, 0, 0), 80 datetime.datetime(2015, 11, 27, 0, 0), 81 datetime.datetime(2015, 12, 11, 0, 0), 82 datetime.datetime(2016, 1, 6, 0, 0), 83 datetime.datetime(2016, 8, 9, 0, 0), 84 datetime.datetime(2016, 8, 19, 0, 0), 85 datetime.datetime(2016, 11, 21, 0, 0), 86 datetime.datetime(2017, 7, 7, 0, 0), 87 datetime.datetime(2017, 9, 11, 0, 0), 88 datetime.datetime(2017, 11, 30, 0, 0), 89 datetime.datetime(2018, 2, 5, 0, 0), 90 datetime.datetime(2018, 3, 28, 0, 0), 91 datetime.datetime(2018, 6, 29, 0, 0), 92 datetime.datetime(2018, 7, 24, 0, 0), 93 datetime.datetime(2018, 8, 2, 0, 0), 94 datetime.datetime(2018, 10, 15, 0, 0)]
方法二
1、代码
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 golden_cross
2、输出截图
五、如果我从2010年1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益如何?
1、实现代码
first_money = 100000 money = first_money hold = 0 #持有多少股 sr1 = pd.Series(1, index=golden_cross) sr2 = pd.Series(1, index=death_cross) sr = sr1.append(sr2).sort_index() for i in range(0, len(sr)): p = df['open'][sr.index[i]] if sr.iloc[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)
2、运行结果
10058413.0
3、使用知识点
作者:罗阿红
出处:http://www.cnblogs.com/luoahong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。