需求:股票分析
●使用tushare包获取某股票的历史行情数据。.
●输出该股票所有收盘比开盘上涨3%以上的日期。
●输出该股票所有开盘比前日收盘跌幅超过2%的日期。
●假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易8卖出所有股票,到今天为止,我的收益如
何?
●tushare: 财经数据接口包,可以批量获取相关金融产品的历史数据
pip install tushare
金融分析
基本面分析
■宏观经济面分析:国家的财政政策、货币政策等
■行业分析
■公司分析:财务数据、业绩报告等
技术面分析:各项技术指标
■K线
■MA (均线)
■KDJ(随机指标)
■MACD (指数平滑移动平均线)
…
金融量化投资
量化投资:利用计算机技术井且采用一定的数学模型去实践投资理念,实现投资策略的过程.
量化投资的优势:
■避免主观情绪。人性弱点和认知偏差,选择更加客观
I能网时包括多角度的观察和多层次的模型
■及时跟踪市场变化,不断发现新的统计模型,寻找交易机会
对股票的数据进行基本操作
一、数据的预处理
>>> import tushare as ts
>>> data = ts.get_k_data(code='600519',start='1900-01-01')
本接口即将停止更新,请尽快使用Pro版接口:https://waditu.com/document/2
>>> data
date open close high low volume code
0 2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519
1 2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519
2 2001-08-29 5.777 5.684 5.781 5.640 53252.75 600519
3 2001-08-30 5.668 5.796 5.860 5.624 48013.06 600519
4 2001-08-31 5.804 5.782 5.877 5.749 23231.48 600519
... ... ... ... ... ... ... ...
4718 2021-06-01 2222.000 2240.950 2248.950 2206.000 30616.00 600519
4719 2021-06-02 2250.000 2222.000 2264.000 2208.180 22863.00 600519
4720 2021-06-03 2220.000 2222.220 2260.340 2210.080 23450.00 600519
4721 2021-06-04 2215.000 2251.500 2288.000 2214.040 31422.00 600519
4722 2021-06-07 2265.000 2271.000 2279.000 2240.010 25466.00 600519
[4723 rows x 7 columns]
1.将股票数据进行持久化存储
import tushare as ts
data = ts.get_k_data(code='600519', start='1900-01-01')
data.to_csv('./maotai.csv')
2.将存储到本地的数据加载读取到df中
df = pd.read_csv('./maotai.csv')
print(df)
3.删除df中指定的列数据,在drop系列函数中,axis=0表示行,1表示列,inplace=True直接作用到原表中
df.drop(labels='Unnamed: 0',axis=1,inplace=True)
4.查看df每一列的数据类型
df['date'].dtype
Out[6]: dtype('O')
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4723 entries, 0 to 4722
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 4723 non-null int64
1 date 4723 non-null object
2 open 4723 non-null float64
3 close 4723 non-null float64
4 high 4723 non-null float64
5 low 4723 non-null float64
6 volume 4723 non-null float64
7 code 4723 non-null int64
dtypes: float64(5), int64(2), object(1)
memory usage: 295.3+ KB
5.将某一列作为行索引
——将date列的数据类型由字符串转换成时间序列
pd.to_datetime(df['date'])
Out[11]:
0 2001-08-27
1 2001-08-28
2 2001-08-29
3 2001-08-30
4 2001-08-31
...
4718 2021-06-01
4719 2021-06-02
4720 2021-06-03
4721 2021-06-04
4722 2021-06-07
Name: date, Length: 4723, dtype: datetime64[ns]
df['date']=pd.to_datetime(df['date'])
df.info
Out[15]:
andas.core.frame.DataFrame'>
RangeIndex: 4723 entries, 0 to 4722
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 4723 non-null datetime64[ns]
1 open 4723 non-null float64
2 close 4723 non-null float64
3 high 4723 non-null float64
4 low 4723 non-null float64
5 volume 4723 non-null float64
6 code 4723 non-null int64
dtypes: datetime64[ns](1), float64(5), int64(1)
——将date作为原数据的行索引
df.set_index('date')
Out[17]:
Unnamed: 0 open close ... low volume code
date ...
2001-08-27 0 5.392 5.554 ... 5.132 406318.00 600519
2001-08-28 1 5.467 5.759 ... 5.407 129647.79 600519
2001-08-29 2 5.777 5.684 ... 5.640 53252.75 600519
2001-08-30 3 5.668 5.796 ... 5.624 48013.06 600519
2001-08-31 4 5.804 5.782 ... 5.749 23231.48 600519
... ... ... ... ... ... ...
2021-06-01 4718 2222.000 2240.950 ... 2206.000 30616.00 600519
2021-06-02 4719 2250.000 2222.000 ... 2208.180 22863.00 600519
2021-06-03 4720 2220.000 2222.220 ... 2210.080 23450.00 600519
2021-06-04 4721 2215.000 2251.500 ... 2214.040 31422.00 600519
2021-06-07 4722 2265.000 2271.000 ... 2240.010 25466.00 600519
[4723 rows x 7 columns]
df = df.set_index('date')
二、 需求:股票分析
- 使用tushare包获取某股票的历史行情数据。
- 输出该股票所有收盘比开盘上涨3%以上的日期。
- 输出该股票所有开盘比前日收盘跌幅超过2%的日期。
- 假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易8卖出所有股票,到今天为止,我的收益如何?
#输出该股票所有收盘比开盘上涨3%以上的日期。
#(收盘价-开盘价)/开盘>0.03
df.loc[(df['close']-df['open'])/df['open']>0.03].index
Out[31]:
Index(['2001-08-27', '2001-08-28', '2001-09-10', '2001-12-21', '2002-01-18',
'2002-01-31', '2003-01-14', '2003-10-29', '2004-01-05', '2004-01-14',
...
'2021-01-25', '2021-02-04', '2021-02-09', '2021-02-10', '2021-03-03',
'2021-03-05', '2021-03-11', '2021-04-02', '2021-05-11', '2021-05-25'],
dtype='object', name='date', length=328)
.
#将布尔值作为行索引,True会被保留,False会被舍弃
a =(df['close']-df['open'])/df['open']>0.03
a.dtype
Out[39]: dtype('bool')
.#输出该股票所有开盘比前日收盘跌幅超过2%的日期。
#(开盘-前日收盘)/前日收盘 < -0.02
#先切片得到前日收盘
open_1 = df['close'].shift(1)
open_1
Out[47]:
date
2001-08-27 NaN
2001-08-28 5.554
2001-08-29 5.759
2001-08-30 5.684
2001-08-31 5.796
...
2021-06-01 2218.000
2021-06-02 2240.950
2021-06-03 2222.000
2021-06-04 2222.220
2021-06-07 2251.500
Name: close, Length: 4723, dtype: float64
#其余操作同上
(df['open']-df['close'].shift(1))/df['close'].shift(1)<-0.02
Out[51]:
date
2001-08-27 False
2001-08-28 False
2001-08-29 False
2001-08-30 False
2001-08-31 False
...
2021-06-01 False
2021-06-02 False
2021-06-03 False
2021-06-04 False
2021-06-07 False
Length: 4723, dtype: bool
df.loc[(df['open']-df['close'].shift(1))/df['close'].shift(1)<-0.02].index
Out[53]:
Index(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01', '2004-10-29',
'2006-08-21', '2006-08-23', '2007-01-25', '2007-02-01', '2007-02-06',
'2007-03-19', '2007-05-21', '2007-05-30', '2007-06-05', '2007-07-27',
'2007-09-05', '2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25',
'2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29', '2008-05-13',
'2008-06-10', '2008-06-13', '2008-06-24', '2008-06-27', '2008-08-11',
'2008-08-19', '2008-09-23', '2008-10-10', '2008-10-15', '2008-10-16',
'2008-10-20', '2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12',
'2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27', '2009-03-25',
'2009-08-13', '2010-04-26', '2010-04-30', '2011-08-05', '2012-03-27',
'2012-08-10', '2012-11-22', '2012-12-04', '2012-12-24', '2013-01-16',
'2013-01-25', '2013-09-02', '2014-04-25', '2015-01-19', '2015-05-25',
'2015-07-03', '2015-07-08', '2015-07-13', '2015-08-24', '2015-09-02',
'2015-09-15', '2017-11-17', '2018-02-06', '2018-02-09', '2018-03-23',
'2018-03-28', '2018-07-11', '2018-10-11', '2018-10-24', '2018-10-25',
'2018-10-29', '2018-10-30', '2019-05-06', '2019-05-08', '2019-10-16',
'2020-01-02', '2020-02-03', '2020-03-13', '2020-03-23', '2020-10-26',
'2021-02-26', '2021-03-04', '2021-04-28'],
dtype='object', name='date')
#假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
-1.将数据从2010切到2020年
-2.买股票(一个完整的年,需要买12次、12手股票)
-3.卖股票(一个完整的年,需要卖出1次股票、1200支)
-4.买卖股票的单价(收盘价):
-5.在2021年只能买入5手股票无法卖出,需要将剩余的股票价值计算到总收益中。
#实现买入股票的操作代码
——1.找到每月第一个交易日的收盘价,乘以一百则表示买入了1手股票;
#数据的重新取样(根据指定条件在df中取出指定的数据)
new_df = df['2010':'2021-06-01']
new_df
Out[65]:
open close high low volume code
date
2010-01-04 109.760 108.446 109.760 108.044 44304.88 600519
2010-01-05 109.116 108.127 109.441 107.846 31513.18 600519
2010-01-06 107.840 106.417 108.165 106.129 39889.03 600519
2010-01-07 106.417 104.477 106.691 103.302 48825.55 600519
2010-01-08 104.655 103.379 104.655 102.167 36702.09 600519
... ... ... ... ... ...
2021-05-26 2207.980 2220.000 2241.800 2193.080 38604.00 600519
2021-05-27 2211.020 2245.000 2320.000 2201.100 45373.00 600519
2021-05-28 2240.970 2230.000 2250.000 2205.510 25807.00 600519
2021-05-31 2215.500 2218.000 2218.350 2172.000 31739.00 600519
2021-06-01 2222.000 2240.950 2248.950 2206.000 30616.00 600519
[2766 rows x 6 columns]
#数据的重新取样(根据指定条件在df中取出指定的数据)
new_df.resample('M').first()
Out[111]:
open close high low volume code
date
2010-01-31 109.760 108.446 109.760 108.044 44304.88 600519
2010-02-28 107.769 107.776 108.216 106.576 29655.94 600519
2010-03-31 106.219 106.085 106.857 105.925 21734.74 600519
2010-04-30 101.324 102.141 102.422 101.311 23980.83 600519
2010-05-31 81.676 82.091 82.678 80.974 23975.16 600519
... ... ... ... ... ...
2021-02-28 2130.000 2109.320 2160.000 2095.000 29341.00 600519
2021-03-31 2179.000 2158.000 2179.000 2120.000 44916.00 600519
2021-04-30 2021.000 2044.500 2046.800 2001.220 26588.00 600519
2021-05-31 2000.000 1959.000 2006.840 1953.000 53546.00 600519
2021-06-30 2222.000 2240.950 2248.950 2206.000 30616.00 600519
[138 rows x 6 columns]
df_monthly = new_df.resample('M').first()
#收盘价为单价买入股票
cost_money = df_monthly['close'].sum()*100
cost_money
Out[114]: 6762786.3
#卖出股票
df_yearly = new_df.resample('A').last()[:-1]
resv_money = df_yearly['close'].sum()*1200
#剩余股票价值
last_money = 400 * new_df['close'][-1]
#总收益
resv_money + last_money - cost_money
Out[123]: 922372.9000000004
三、双均线策略制定
import tushare as ts
data = ts.get_k_data('000001',start='1800')
data.to_csv('pingan.csv')
df = pd.read_csv('./pingan.csv')
df = df.drop(labels='Unnamed: 0',axis=1)
df['date']=pd.to_datetime(df['date'])
df = df.set_index('date')
df
Out[143]:
open close high low volume code
date
1991-01-02 0.185 0.188 0.188 0.185 759.00 1
1991-01-03 0.429 0.429 0.429 0.429 212.40 1
1991-01-04 0.428 0.428 0.428 0.428 167.90 1
1991-01-05 0.426 0.426 0.426 0.426 131.50 1
1991-01-07 0.426 0.426 0.426 0.426 161.36 1
... ... ... ... ... ...
2021-06-01 24.200 23.920 24.250 23.500 625018.00 1
2021-06-02 23.890 23.890 23.920 23.380 497527.00 1
2021-06-03 23.720 23.770 24.140 23.680 400901.00 1
2021-06-04 23.990 24.540 24.960 23.800 756739.00 1
2021-06-07 24.480 24.300 24.480 23.930 447914.00
●使用tushare包获取某股票的历史行情数据
●计算该股票历史数据的5日均线和30日均线
■什么是均线?
-
对于每一个交易日,都可以计算出前N天的移动平均值,然后把这些移动平均值连起来,成为-条线,就叫做N日移动平均线。移动平均线常用线有5天、10天、 30天、60天. 120天和240天的指标。
-
5天和10天的是短线操作的参照指标,称做日均线指标;
-
30天和60天的是中期均线指标,称做季均线指标;
-
120天和240天的是长期均线指标,称做年均线指标。
#5日均值 ma5 = df['close'].rolling(5).mean() #10日均值 ma10 = df['close'].rolling(10).mean()
#画不出图,换jupyter环境运行了(真踏马好用!)
cmd>jupyter notebook
■均线计算方法: MA= (C1+C2+3+…+Cn)/N C:某日收盘价N:移动平均周期(天数)
●分析输出所有金叉日期和死叉日期
■股票分析技术中的金叉和死叉,可以简单解释为:
- 分析指标中的两根线,-根为短时间内的指标线,另一根为较长时间的指标线。
- 如果短时间的指标线方向拐头向上,并且穿过了较长时间的指标线,这种状态叫“金叉;
- 如果短时间的指标线方向拐头向下,并且穿过了较长时间的指标线,这种状态叫“死叉"; .
- 一般情况下,出现金叉后,操作趋向买入;死叉则趋向卖出。当然,金叉和死叉只是分析指标之一,要和其他很多指标配合使用,才能增加操作的准确性。
●如果我从假如我从2010年1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益
率如何?
分析:
■买卖股票的单价使用开盘价
■买卖股票的时机
■最终手里会有剩余的股票没有卖出去
- 会有。如果最后一天为金叉,则买入股票。估量剩余股票的价值计算到总收益。
- 剩余股票的单价就是用最后一天的收盘价。
#s1 = ma5<ma30
#s2 = ~s1(取反)
…
【不看了,去学pandas数据清洗】