3 Pandas实战-股票分析
作者:互联网
需求:股票分析
-
使用tushare包获取某股票的历史行情数据。
-
输出该股票所有收盘比开盘上涨3%以上的日期。
-
输出该股票所有开盘比前日收盘跌幅超过2%的日期。
-
假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
-
tushare财经数据接口包
- pip install tushare
import tushare as ts
import pandas as pd
from pandas import DataFrame,Series
import numpy as np
#获取某只股票的历史行情数据
#code:字符串形式的股票代码
df = ts.get_k_data(code='600519',start='2000-01-01')
df
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 |
5 | 2001-09-03 | 5.812 | 5.779 | 5.870 | 5.757 | 22112.09 | 600519 |
6 | 2001-09-04 | 5.782 | 5.852 | 5.949 | 5.762 | 37006.77 | 600519 |
7 | 2001-09-05 | 5.876 | 5.849 | 5.924 | 5.813 | 26066.95 | 600519 |
8 | 2001-09-06 | 5.835 | 5.734 | 5.854 | 5.704 | 28997.03 | 600519 |
9 | 2001-09-07 | 5.702 | 5.574 | 5.773 | 5.570 | 31552.25 | 600519 |
10 | 2001-09-10 | 5.531 | 5.734 | 5.757 | 5.470 | 18878.89 | 600519 |
11 | 2001-09-11 | 5.749 | 5.670 | 5.773 | 5.656 | 11390.81 | 600519 |
12 | 2001-09-12 | 5.520 | 5.621 | 5.656 | 5.515 | 25045.19 | 600519 |
13 | 2001-09-13 | 5.626 | 5.607 | 5.671 | 5.577 | 10986.68 | 600519 |
14 | 2001-09-14 | 5.626 | 5.671 | 5.702 | 5.593 | 7672.83 | 600519 |
15 | 2001-09-17 | 5.637 | 5.599 | 5.670 | 5.546 | 8983.97 | 600519 |
16 | 2001-09-18 | 5.606 | 5.663 | 5.710 | 5.601 | 10773.26 | 600519 |
17 | 2001-09-19 | 5.671 | 5.768 | 5.768 | 5.634 | 8650.53 | 600519 |
18 | 2001-09-20 | 5.765 | 5.720 | 5.788 | 5.702 | 11173.35 | 600519 |
19 | 2001-09-21 | 5.668 | 5.634 | 5.718 | 5.624 | 7879.72 | 600519 |
20 | 2001-09-24 | 5.634 | 5.632 | 5.685 | 5.624 | 4068.60 | 600519 |
21 | 2001-09-25 | 5.668 | 5.660 | 5.709 | 5.632 | 3488.45 | 600519 |
22 | 2001-09-26 | 5.642 | 5.637 | 5.699 | 5.624 | 4956.26 | 600519 |
23 | 2001-09-27 | 5.637 | 5.734 | 5.777 | 5.624 | 8778.04 | 600519 |
24 | 2001-09-28 | 5.765 | 5.795 | 5.812 | 5.702 | 17088.47 | 600519 |
25 | 2001-10-08 | 5.781 | 5.715 | 5.809 | 5.663 | 6552.17 | 600519 |
26 | 2001-10-09 | 5.718 | 5.827 | 5.859 | 5.718 | 9558.52 | 600519 |
27 | 2001-10-10 | 5.827 | 5.640 | 5.848 | 5.629 | 17548.69 | 600519 |
28 | 2001-10-11 | 5.626 | 5.585 | 5.702 | 5.570 | 12306.84 | 600519 |
29 | 2001-10-12 | 5.609 | 5.624 | 5.726 | 5.320 | 20010.70 | 600519 |
... | ... | ... | ... | ... | ... | ... | ... |
4376 | 2019-12-30 | 1170.200 | 1185.800 | 1195.500 | 1170.200 | 40760.00 | 600519 |
4377 | 2019-12-31 | 1183.000 | 1183.000 | 1188.000 | 1176.510 | 22588.00 | 600519 |
4378 | 2020-01-02 | 1128.000 | 1130.000 | 1145.060 | 1116.000 | 148099.00 | 600519 |
4379 | 2020-01-03 | 1117.000 | 1078.560 | 1117.000 | 1076.900 | 130318.00 | 600519 |
4380 | 2020-01-06 | 1070.860 | 1077.990 | 1092.900 | 1067.300 | 63414.00 | 600519 |
4381 | 2020-01-07 | 1077.500 | 1094.530 | 1099.000 | 1076.400 | 47853.00 | 600519 |
4382 | 2020-01-08 | 1085.050 | 1088.140 | 1095.500 | 1082.580 | 25008.00 | 600519 |
4383 | 2020-01-09 | 1094.000 | 1102.700 | 1105.390 | 1090.000 | 37405.00 | 600519 |
4384 | 2020-01-10 | 1109.000 | 1112.500 | 1115.990 | 1102.500 | 35975.00 | 600519 |
4385 | 2020-01-13 | 1112.500 | 1124.270 | 1129.200 | 1112.000 | 38515.00 | 600519 |
4386 | 2020-01-14 | 1124.200 | 1107.400 | 1124.890 | 1103.000 | 35144.00 | 600519 |
4387 | 2020-01-15 | 1109.010 | 1112.130 | 1121.600 | 1105.000 | 26029.00 | 600519 |
4388 | 2020-01-16 | 1118.870 | 1107.000 | 1118.870 | 1102.580 | 23191.00 | 600519 |
4389 | 2020-01-17 | 1110.000 | 1107.500 | 1112.780 | 1101.010 | 23472.00 | 600519 |
4390 | 2020-01-20 | 1111.860 | 1091.000 | 1111.860 | 1082.000 | 35391.00 | 600519 |
4391 | 2020-01-21 | 1081.000 | 1075.300 | 1087.000 | 1072.300 | 32874.00 | 600519 |
4392 | 2020-01-22 | 1070.000 | 1075.510 | 1084.000 | 1055.380 | 36200.00 | 600519 |
4393 | 2020-01-23 | 1076.000 | 1052.800 | 1076.000 | 1037.000 | 53468.00 | 600519 |
4394 | 2020-02-03 | 985.000 | 1003.920 | 1010.680 | 980.000 | 123442.00 | 600519 |
4395 | 2020-02-04 | 1015.000 | 1038.010 | 1057.000 | 1011.010 | 62624.00 | 600519 |
4396 | 2020-02-05 | 1050.000 | 1049.990 | 1054.000 | 1033.030 | 47418.00 | 600519 |
4397 | 2020-02-06 | 1059.430 | 1071.000 | 1075.000 | 1052.020 | 47171.00 | 600519 |
4398 | 2020-02-07 | 1070.010 | 1076.000 | 1077.000 | 1061.020 | 31278.00 | 600519 |
4399 | 2020-02-10 | 1062.000 | 1066.490 | 1074.600 | 1057.200 | 30533.00 | 600519 |
4400 | 2020-02-11 | 1063.000 | 1098.000 | 1099.680 | 1062.800 | 47917.00 | 600519 |
4401 | 2020-02-12 | 1089.000 | 1097.270 | 1098.790 | 1085.880 | 28125.00 | 600519 |
4402 | 2020-02-13 | 1098.000 | 1091.000 | 1113.890 | 1088.010 | 30357.00 | 600519 |
4403 | 2020-02-14 | 1090.450 | 1088.000 | 1093.510 | 1083.110 | 23287.00 | 600519 |
4404 | 2020-02-17 | 1082.500 | 1093.820 | 1096.190 | 1082.400 | 27028.00 | 600519 |
4405 | 2020-02-18 | 1090.010 | 1084.000 | 1096.880 | 1083.000 | 26664.00 | 600519 |
4406 rows × 7 columns
#将互联网上获取的股票数据存储到本地
df.to_csv('./maotai.csv')#调用to_xxx方法将df中的数据写入到本地进行存储
#将本地存储的数据读入到df
df = pd.read_csv('./maotai.csv')
df.head()
Unnamed: 0 | date | open | close | high | low | volume | code | |
---|---|---|---|---|---|---|---|---|
0 | 0 | 2001-08-27 | 5.392 | 5.554 | 5.902 | 5.132 | 406318.00 | 600519 |
1 | 1 | 2001-08-28 | 5.467 | 5.759 | 5.781 | 5.407 | 129647.79 | 600519 |
2 | 2 | 2001-08-29 | 5.777 | 5.684 | 5.781 | 5.640 | 53252.75 | 600519 |
3 | 3 | 2001-08-30 | 5.668 | 5.796 | 5.860 | 5.624 | 48013.06 | 600519 |
4 | 4 | 2001-08-31 | 5.804 | 5.782 | 5.877 | 5.749 | 23231.48 | 600519 |
#需要对读取出来的数据进行相关的处理
#删除df中指定的一列
df.drop(labels='Unnamed: 0',axis=1,inplace=True)
#查看每一列的数据类型
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4406 entries, 0 to 4405
Data columns (total 7 columns):
date 4406 non-null object
open 4406 non-null float64
close 4406 non-null float64
high 4406 non-null float64
low 4406 non-null float64
volume 4406 non-null float64
code 4406 non-null int64
dtypes: float64(5), int64(1), object(1)
memory usage: 241.0+ KB
#将date列转为时间序列类型
df['date'] = pd.to_datetime(df['date'])
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4406 entries, 0 to 4405
Data columns (total 7 columns):
date 4406 non-null datetime64[ns]
open 4406 non-null float64
close 4406 non-null float64
high 4406 non-null float64
low 4406 non-null float64
volume 4406 non-null float64
code 4406 non-null int64
dtypes: datetime64[ns](1), float64(5), int64(1)
memory usage: 241.0 KB
#将date列作为源数据的行索引
df.set_index('date',inplace=True)
df.head()
open | close | high | low | volume | code | |
---|---|---|---|---|---|---|
date | ||||||
2001-08-27 | 5.392 | 5.554 | 5.902 | 5.132 | 406318.00 | 600519 |
2001-08-28 | 5.467 | 5.759 | 5.781 | 5.407 | 129647.79 | 600519 |
2001-08-29 | 5.777 | 5.684 | 5.781 | 5.640 | 53252.75 | 600519 |
2001-08-30 | 5.668 | 5.796 | 5.860 | 5.624 | 48013.06 | 600519 |
2001-08-31 | 5.804 | 5.782 | 5.877 | 5.749 | 23231.48 | 600519 |
#输出该股票所有收盘比开盘上涨3%以上的日期
#伪代码:(收盘-开盘)/开盘 > 0.03
(df['open'] - df['close']) / df['open'] > 0.03
#在分析的过程中如果产生了boolean值则下一步马上将布尔值作为源数据的行索引
#如果布尔值作为df的行索引,则可以取出true对应的行数据,忽略false对应的行数据
df.loc[(df['open'] - df['close']) / df['open'] > 0.03] #获取了True对应的行数据(满足需求的行数据)
df.loc[(df['open'] - df['close']) / df['open'] > 0.03].index #df的行数据
DatetimeIndex(['2001-10-10', '2001-11-07', '2001-11-16', '2001-12-20',
'2002-01-04', '2002-01-17', '2002-01-28', '2002-04-17',
'2002-11-08', '2003-01-02',
...
'2018-06-27', '2018-07-02', '2018-08-17', '2018-10-08',
'2018-10-10', '2018-10-23', '2019-07-03', '2019-09-11',
'2019-11-29', '2020-01-03'],
dtype='datetime64[ns]', name='date', length=165, freq=None)
#输出该股票所有开盘比前日收盘跌幅超过2%的日期
#伪代码:(开盘-前日收盘)/前日收盘 < -0.02
(df['open'] - df['close'].shift(1))/df['close'].shift(1) < -0.02
#将布尔值作为源数据的行索引取出True对应的行数据
df.loc[(df['open'] - df['close'].shift(1))/df['close'].shift(1) < -0.02]
df.loc[(df['open'] - df['close'].shift(1))/df['close'].shift(1) < -0.02].index
DatetimeIndex(['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'],
dtype='datetime64[ns]', name='date', freq=None)
- 需求:假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
- 分析:
- 时间节点:2010-2020
- 一手股票:100支股票
- 买:
- 一个完整的年需要买入1200支股票
- 卖:
- 一个完整的年需要卖出1200支股票
- 买卖股票的单价:
- 开盘价
new_df = df['2010-01':'2020-02']
new_df
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 |
2010-01-11 | 104.400 | 102.926 | 105.230 | 102.422 | 24461.03 | 600519 |
2010-01-12 | 103.028 | 105.708 | 106.040 | 102.492 | 31063.40 | 600519 |
2010-01-13 | 104.649 | 103.022 | 105.389 | 102.741 | 37924.44 | 600519 |
2010-01-14 | 103.379 | 107.552 | 107.974 | 103.379 | 46454.64 | 600519 |
2010-01-15 | 107.533 | 108.401 | 110.641 | 107.533 | 45938.50 | 600519 |
2010-01-18 | 108.484 | 109.110 | 109.926 | 108.420 | 21461.53 | 600519 |
2010-01-19 | 109.116 | 108.337 | 109.441 | 108.165 | 17818.91 | 600519 |
2010-01-20 | 108.427 | 105.881 | 108.580 | 105.804 | 20972.95 | 600519 |
2010-01-21 | 105.842 | 106.397 | 107.450 | 105.613 | 17257.48 | 600519 |
2010-01-22 | 106.314 | 104.738 | 106.755 | 103.309 | 25432.94 | 600519 |
2010-01-25 | 104.560 | 105.957 | 106.761 | 103.704 | 23239.15 | 600519 |
2010-01-26 | 107.380 | 106.378 | 108.593 | 105.038 | 32889.16 | 600519 |
2010-01-27 | 105.951 | 104.643 | 107.068 | 104.400 | 19316.57 | 600519 |
2010-01-28 | 104.566 | 107.974 | 108.708 | 104.336 | 30267.52 | 600519 |
2010-01-29 | 108.452 | 107.552 | 108.612 | 107.004 | 37172.82 | 600519 |
2010-02-01 | 107.769 | 107.776 | 108.216 | 106.576 | 29655.94 | 600519 |
2010-02-02 | 107.208 | 106.263 | 108.484 | 106.117 | 15493.53 | 600519 |
2010-02-03 | 106.066 | 105.887 | 107.272 | 104.783 | 23034.65 | 600519 |
2010-02-04 | 105.868 | 107.591 | 108.006 | 105.376 | 22475.33 | 600519 |
2010-02-05 | 106.959 | 109.282 | 109.684 | 106.570 | 26234.30 | 600519 |
2010-02-08 | 109.282 | 109.269 | 112.058 | 108.816 | 31496.10 | 600519 |
2010-02-09 | 109.760 | 109.193 | 110.609 | 108.822 | 14151.24 | 600519 |
2010-02-10 | 109.760 | 109.652 | 110.137 | 108.931 | 6398.14 | 600519 |
2010-02-11 | 109.633 | 110.641 | 111.318 | 109.505 | 14945.05 | 600519 |
2010-02-12 | 111.018 | 110.456 | 111.164 | 109.888 | 9346.40 | 600519 |
... | ... | ... | ... | ... | ... | ... |
2019-12-30 | 1170.200 | 1185.800 | 1195.500 | 1170.200 | 40760.00 | 600519 |
2019-12-31 | 1183.000 | 1183.000 | 1188.000 | 1176.510 | 22588.00 | 600519 |
2020-01-02 | 1128.000 | 1130.000 | 1145.060 | 1116.000 | 148099.00 | 600519 |
2020-01-03 | 1117.000 | 1078.560 | 1117.000 | 1076.900 | 130318.00 | 600519 |
2020-01-06 | 1070.860 | 1077.990 | 1092.900 | 1067.300 | 63414.00 | 600519 |
2020-01-07 | 1077.500 | 1094.530 | 1099.000 | 1076.400 | 47853.00 | 600519 |
2020-01-08 | 1085.050 | 1088.140 | 1095.500 | 1082.580 | 25008.00 | 600519 |
2020-01-09 | 1094.000 | 1102.700 | 1105.390 | 1090.000 | 37405.00 | 600519 |
2020-01-10 | 1109.000 | 1112.500 | 1115.990 | 1102.500 | 35975.00 | 600519 |
2020-01-13 | 1112.500 | 1124.270 | 1129.200 | 1112.000 | 38515.00 | 600519 |
2020-01-14 | 1124.200 | 1107.400 | 1124.890 | 1103.000 | 35144.00 | 600519 |
2020-01-15 | 1109.010 | 1112.130 | 1121.600 | 1105.000 | 26029.00 | 600519 |
2020-01-16 | 1118.870 | 1107.000 | 1118.870 | 1102.580 | 23191.00 | 600519 |
2020-01-17 | 1110.000 | 1107.500 | 1112.780 | 1101.010 | 23472.00 | 600519 |
2020-01-20 | 1111.860 | 1091.000 | 1111.860 | 1082.000 | 35391.00 | 600519 |
2020-01-21 | 1081.000 | 1075.300 | 1087.000 | 1072.300 | 32874.00 | 600519 |
2020-01-22 | 1070.000 | 1075.510 | 1084.000 | 1055.380 | 36200.00 | 600519 |
2020-01-23 | 1076.000 | 1052.800 | 1076.000 | 1037.000 | 53468.00 | 600519 |
2020-02-03 | 985.000 | 1003.920 | 1010.680 | 980.000 | 123442.00 | 600519 |
2020-02-04 | 1015.000 | 1038.010 | 1057.000 | 1011.010 | 62624.00 | 600519 |
2020-02-05 | 1050.000 | 1049.990 | 1054.000 | 1033.030 | 47418.00 | 600519 |
2020-02-06 | 1059.430 | 1071.000 | 1075.000 | 1052.020 | 47171.00 | 600519 |
2020-02-07 | 1070.010 | 1076.000 | 1077.000 | 1061.020 | 31278.00 | 600519 |
2020-02-10 | 1062.000 | 1066.490 | 1074.600 | 1057.200 | 30533.00 | 600519 |
2020-02-11 | 1063.000 | 1098.000 | 1099.680 | 1062.800 | 47917.00 | 600519 |
2020-02-12 | 1089.000 | 1097.270 | 1098.790 | 1085.880 | 28125.00 | 600519 |
2020-02-13 | 1098.000 | 1091.000 | 1113.890 | 1088.010 | 30357.00 | 600519 |
2020-02-14 | 1090.450 | 1088.000 | 1093.510 | 1083.110 | 23287.00 | 600519 |
2020-02-17 | 1082.500 | 1093.820 | 1096.190 | 1082.400 | 27028.00 | 600519 |
2020-02-18 | 1090.010 | 1084.000 | 1096.880 | 1083.000 | 26664.00 | 600519 |
2453 rows × 6 columns
new_df.head(2)
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 |
#买股票:找每个月的第一个交易日对应的行数据(捕获到开盘价)==》每月的第一行数据
#根据月份从原始数据中提取指定的数据
#每月第一个交易日对应的行数据
df_monthly = new_df.resample('M').first()#数据的重新取样
df_monthly
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 |
2010-06-30 | 84.075 | 84.637 | 85.166 | 83.278 | 23525.57 | 600519 |
2010-07-31 | 81.586 | 81.057 | 81.586 | 80.725 | 7449.69 | 600519 |
2010-08-31 | 89.296 | 92.465 | 93.567 | 89.296 | 42965.73 | 600519 |
2010-09-30 | 102.288 | 101.052 | 103.834 | 100.420 | 25589.00 | 600519 |
2010-10-31 | 108.858 | 111.776 | 113.045 | 108.858 | 31608.00 | 600519 |
2010-11-30 | 105.122 | 105.483 | 106.217 | 104.478 | 49658.00 | 600519 |
2010-12-31 | 130.759 | 130.372 | 133.980 | 128.839 | 38016.00 | 600519 |
2011-01-31 | 120.388 | 119.487 | 121.033 | 117.619 | 60462.00 | 600519 |
2011-02-28 | 114.656 | 116.124 | 117.136 | 114.334 | 17758.00 | 600519 |
2011-03-31 | 115.113 | 115.068 | 115.899 | 114.527 | 23059.00 | 600519 |
2011-04-30 | 115.931 | 115.596 | 116.813 | 115.010 | 18227.00 | 600519 |
2011-05-31 | 117.876 | 119.718 | 120.324 | 117.232 | 41270.00 | 600519 |
2011-06-30 | 131.467 | 135.197 | 135.255 | 130.772 | 39093.00 | 600519 |
2011-07-31 | 137.895 | 137.272 | 137.895 | 136.033 | 15810.00 | 600519 |
2011-08-31 | 148.519 | 145.689 | 148.519 | 145.274 | 18572.00 | 600519 |
2011-09-30 | 153.798 | 151.491 | 154.357 | 150.789 | 25689.00 | 600519 |
2011-10-31 | 136.735 | 134.672 | 137.394 | 134.407 | 14283.00 | 600519 |
2011-11-30 | 145.546 | 148.418 | 148.640 | 145.066 | 27147.00 | 600519 |
2011-12-31 | 152.724 | 152.466 | 154.149 | 150.625 | 33785.00 | 600519 |
2012-01-31 | 137.179 | 132.716 | 138.089 | 132.523 | 33878.00 | 600519 |
2012-02-29 | 133.382 | 133.347 | 135.044 | 132.437 | 24824.00 | 600519 |
2012-03-31 | 145.789 | 145.116 | 147.186 | 144.980 | 16920.00 | 600519 |
2012-04-30 | 140.474 | 147.566 | 147.852 | 140.474 | 40960.00 | 600519 |
2012-05-31 | 161.893 | 161.878 | 162.301 | 159.027 | 35339.00 | 600519 |
2012-06-30 | 170.489 | 172.187 | 174.071 | 169.565 | 38504.00 | 600519 |
... | ... | ... | ... | ... | ... | ... |
2017-09-30 | 483.337 | 488.248 | 489.332 | 483.170 | 31451.00 | 600519 |
2017-10-31 | 517.196 | 520.381 | 528.052 | 512.819 | 34891.00 | 600519 |
2017-11-30 | 612.188 | 614.288 | 622.698 | 610.551 | 43435.00 | 600519 |
2017-12-31 | 629.078 | 613.637 | 630.044 | 611.448 | 47192.00 | 600519 |
2018-01-31 | 690.200 | 693.996 | 700.218 | 680.232 | 49612.00 | 600519 |
2018-02-28 | 756.262 | 747.122 | 756.558 | 742.379 | 50582.00 | 600519 |
2018-03-31 | 717.808 | 731.582 | 736.394 | 713.637 | 44794.00 | 600519 |
2018-04-30 | 670.480 | 670.539 | 681.326 | 664.673 | 32039.00 | 600519 |
2018-05-31 | 650.760 | 658.480 | 659.624 | 636.029 | 70259.00 | 600519 |
2018-06-30 | 740.614 | 734.679 | 744.410 | 728.417 | 36177.00 | 600519 |
2018-07-31 | 734.520 | 711.550 | 739.330 | 703.000 | 37558.00 | 600519 |
2018-08-31 | 731.400 | 714.940 | 732.300 | 714.110 | 25237.00 | 600519 |
2018-09-30 | 652.000 | 666.210 | 667.670 | 650.800 | 30179.00 | 600519 |
2018-10-31 | 715.410 | 686.150 | 719.000 | 686.150 | 82745.00 | 600519 |
2018-11-30 | 555.000 | 563.000 | 585.500 | 551.250 | 98106.00 | 600519 |
2018-12-31 | 589.000 | 601.200 | 605.000 | 584.770 | 83414.00 | 600519 |
2019-01-31 | 609.980 | 598.980 | 612.000 | 595.010 | 62286.00 | 600519 |
2019-02-28 | 697.040 | 692.670 | 699.000 | 689.610 | 30520.00 | 600519 |
2019-03-31 | 761.500 | 789.300 | 790.000 | 761.000 | 63840.00 | 600519 |
2019-04-30 | 860.000 | 859.000 | 868.950 | 851.000 | 60934.00 | 600519 |
2019-05-31 | 925.500 | 906.000 | 935.000 | 893.000 | 135099.00 | 600519 |
2019-06-30 | 892.000 | 892.000 | 901.350 | 886.280 | 34479.00 | 600519 |
2019-07-31 | 1004.520 | 1031.860 | 1035.600 | 1000.220 | 52337.00 | 600519 |
2019-08-31 | 976.510 | 959.300 | 977.000 | 953.020 | 35089.00 | 600519 |
2019-09-30 | 1139.990 | 1138.760 | 1144.980 | 1129.000 | 28234.00 | 600519 |
2019-10-31 | 1153.000 | 1167.100 | 1180.000 | 1152.010 | 31045.00 | 600519 |
2019-11-30 | 1181.000 | 1185.000 | 1191.950 | 1172.500 | 22811.00 | 600519 |
2019-12-31 | 1118.200 | 1133.000 | 1140.020 | 1118.200 | 30784.00 | 600519 |
2020-01-31 | 1128.000 | 1130.000 | 1145.060 | 1116.000 | 148099.00 | 600519 |
2020-02-29 | 985.000 | 1003.920 | 1010.680 | 980.000 | 123442.00 | 600519 |
122 rows × 6 columns
#买入股票花费的总金额
cost = df_monthly['open'].sum()*100
cost
4010206.1
#卖出股票到手的钱
#特殊情况:2020年买入的股票卖不出去
new_df.resample('A').last()
#将2020年最后一行切出去
df_yearly = new_df.resample('A').last()[:-1]
df_yearly
open | close | high | low | volume | code | |
---|---|---|---|---|---|---|
date | ||||||
2010-12-31 | 117.103 | 118.469 | 118.701 | 116.620 | 46084.0 | 600519 |
2011-12-31 | 138.039 | 138.468 | 139.600 | 136.105 | 29460.0 | 600519 |
2012-12-31 | 155.208 | 152.087 | 156.292 | 150.144 | 51914.0 | 600519 |
2013-12-31 | 93.188 | 96.480 | 97.179 | 92.061 | 57546.0 | 600519 |
2014-12-31 | 157.642 | 161.056 | 161.379 | 157.132 | 46269.0 | 600519 |
2015-12-31 | 207.487 | 207.458 | 208.704 | 207.106 | 19673.0 | 600519 |
2016-12-31 | 317.239 | 324.563 | 325.670 | 317.239 | 34687.0 | 600519 |
2017-12-31 | 707.948 | 687.725 | 716.329 | 681.918 | 76038.0 | 600519 |
2018-12-31 | 563.300 | 590.010 | 596.400 | 560.000 | 63678.0 | 600519 |
2019-12-31 | 1183.000 | 1183.000 | 1188.000 | 1176.510 | 22588.0 | 600519 |
#卖出股票到手的钱
resv = df_yearly['open'].sum()*1200
resv
4368184.8
#最后手中剩余的股票需要估量其价值计算到总收益中
#使用昨天的收盘价作为剩余股票的单价
last_monry = 200*new_df['close'][-1]
#计算总收益
resv+last_monry-cost
574778.6999999997
需求:双均线策略制定
- 使用tushare包获取某股票的历史行情数据
df = pd.read_csv('./maotai.csv').drop(labels='Unnamed: 0',axis=1)
df
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 |
5 | 2001-09-03 | 5.812 | 5.779 | 5.870 | 5.757 | 22112.09 | 600519 |
6 | 2001-09-04 | 5.782 | 5.852 | 5.949 | 5.762 | 37006.77 | 600519 |
7 | 2001-09-05 | 5.876 | 5.849 | 5.924 | 5.813 | 26066.95 | 600519 |
8 | 2001-09-06 | 5.835 | 5.734 | 5.854 | 5.704 | 28997.03 | 600519 |
9 | 2001-09-07 | 5.702 | 5.574 | 5.773 | 5.570 | 31552.25 | 600519 |
10 | 2001-09-10 | 5.531 | 5.734 | 5.757 | 5.470 | 18878.89 | 600519 |
11 | 2001-09-11 | 5.749 | 5.670 | 5.773 | 5.656 | 11390.81 | 600519 |
12 | 2001-09-12 | 5.520 | 5.621 | 5.656 | 5.515 | 25045.19 | 600519 |
13 | 2001-09-13 | 5.626 | 5.607 | 5.671 | 5.577 | 10986.68 | 600519 |
14 | 2001-09-14 | 5.626 | 5.671 | 5.702 | 5.593 | 7672.83 | 600519 |
15 | 2001-09-17 | 5.637 | 5.599 | 5.670 | 5.546 | 8983.97 | 600519 |
16 | 2001-09-18 | 5.606 | 5.663 | 5.710 | 5.601 | 10773.26 | 600519 |
17 | 2001-09-19 | 5.671 | 5.768 | 5.768 | 5.634 | 8650.53 | 600519 |
18 | 2001-09-20 | 5.765 | 5.720 | 5.788 | 5.702 | 11173.35 | 600519 |
19 | 2001-09-21 | 5.668 | 5.634 | 5.718 | 5.624 | 7879.72 | 600519 |
20 | 2001-09-24 | 5.634 | 5.632 | 5.685 | 5.624 | 4068.60 | 600519 |
21 | 2001-09-25 | 5.668 | 5.660 | 5.709 | 5.632 | 3488.45 | 600519 |
22 | 2001-09-26 | 5.642 | 5.637 | 5.699 | 5.624 | 4956.26 | 600519 |
23 | 2001-09-27 | 5.637 | 5.734 | 5.777 | 5.624 | 8778.04 | 600519 |
24 | 2001-09-28 | 5.765 | 5.795 | 5.812 | 5.702 | 17088.47 | 600519 |
25 | 2001-10-08 | 5.781 | 5.715 | 5.809 | 5.663 | 6552.17 | 600519 |
26 | 2001-10-09 | 5.718 | 5.827 | 5.859 | 5.718 | 9558.52 | 600519 |
27 | 2001-10-10 | 5.827 | 5.640 | 5.848 | 5.629 | 17548.69 | 600519 |
28 | 2001-10-11 | 5.626 | 5.585 | 5.702 | 5.570 | 12306.84 | 600519 |
29 | 2001-10-12 | 5.609 | 5.624 | 5.726 | 5.320 | 20010.70 | 600519 |
... | ... | ... | ... | ... | ... | ... | ... |
4376 | 2019-12-30 | 1170.200 | 1185.800 | 1195.500 | 1170.200 | 40760.00 | 600519 |
4377 | 2019-12-31 | 1183.000 | 1183.000 | 1188.000 | 1176.510 | 22588.00 | 600519 |
4378 | 2020-01-02 | 1128.000 | 1130.000 | 1145.060 | 1116.000 | 148099.00 | 600519 |
4379 | 2020-01-03 | 1117.000 | 1078.560 | 1117.000 | 1076.900 | 130318.00 | 600519 |
4380 | 2020-01-06 | 1070.860 | 1077.990 | 1092.900 | 1067.300 | 63414.00 | 600519 |
4381 | 2020-01-07 | 1077.500 | 1094.530 | 1099.000 | 1076.400 | 47853.00 | 600519 |
4382 | 2020-01-08 | 1085.050 | 1088.140 | 1095.500 | 1082.580 | 25008.00 | 600519 |
4383 | 2020-01-09 | 1094.000 | 1102.700 | 1105.390 | 1090.000 | 37405.00 | 600519 |
4384 | 2020-01-10 | 1109.000 | 1112.500 | 1115.990 | 1102.500 | 35975.00 | 600519 |
4385 | 2020-01-13 | 1112.500 | 1124.270 | 1129.200 | 1112.000 | 38515.00 | 600519 |
4386 | 2020-01-14 | 1124.200 | 1107.400 | 1124.890 | 1103.000 | 35144.00 | 600519 |
4387 | 2020-01-15 | 1109.010 | 1112.130 | 1121.600 | 1105.000 | 26029.00 | 600519 |
4388 | 2020-01-16 | 1118.870 | 1107.000 | 1118.870 | 1102.580 | 23191.00 | 600519 |
4389 | 2020-01-17 | 1110.000 | 1107.500 | 1112.780 | 1101.010 | 23472.00 | 600519 |
4390 | 2020-01-20 | 1111.860 | 1091.000 | 1111.860 | 1082.000 | 35391.00 | 600519 |
4391 | 2020-01-21 | 1081.000 | 1075.300 | 1087.000 | 1072.300 | 32874.00 | 600519 |
4392 | 2020-01-22 | 1070.000 | 1075.510 | 1084.000 | 1055.380 | 36200.00 | 600519 |
4393 | 2020-01-23 | 1076.000 | 1052.800 | 1076.000 | 1037.000 | 53468.00 | 600519 |
4394 | 2020-02-03 | 985.000 | 1003.920 | 1010.680 | 980.000 | 123442.00 | 600519 |
4395 | 2020-02-04 | 1015.000 | 1038.010 | 1057.000 | 1011.010 | 62624.00 | 600519 |
4396 | 2020-02-05 | 1050.000 | 1049.990 | 1054.000 | 1033.030 | 47418.00 | 600519 |
4397 | 2020-02-06 | 1059.430 | 1071.000 | 1075.000 | 1052.020 | 47171.00 | 600519 |
4398 | 2020-02-07 | 1070.010 | 1076.000 | 1077.000 | 1061.020 | 31278.00 | 600519 |
4399 | 2020-02-10 | 1062.000 | 1066.490 | 1074.600 | 1057.200 | 30533.00 | 600519 |
4400 | 2020-02-11 | 1063.000 | 1098.000 | 1099.680 | 1062.800 | 47917.00 | 600519 |
4401 | 2020-02-12 | 1089.000 | 1097.270 | 1098.790 | 1085.880 | 28125.00 | 600519 |
4402 | 2020-02-13 | 1098.000 | 1091.000 | 1113.890 | 1088.010 | 30357.00 | 600519 |
4403 | 2020-02-14 | 1090.450 | 1088.000 | 1093.510 | 1083.110 | 23287.00 | 600519 |
4404 | 2020-02-17 | 1082.500 | 1093.820 | 1096.190 | 1082.400 | 27028.00 | 600519 |
4405 | 2020-02-18 | 1090.010 | 1084.000 | 1096.880 | 1083.000 | 26664.00 | 600519 |
4406 rows × 7 columns
#将date列转为时间序列且将其作为源数据的行索引
df['date'] = pd.to_datetime(df['date'])
df.set_index('date',inplace=True)
df.head()
open | close | high | low | volume | code | |
---|---|---|---|---|---|---|
date | ||||||
2001-08-27 | 5.392 | 5.554 | 5.902 | 5.132 | 406318.00 | 600519 |
2001-08-28 | 5.467 | 5.759 | 5.781 | 5.407 | 129647.79 | 600519 |
2001-08-29 | 5.777 | 5.684 | 5.781 | 5.640 | 53252.75 | 600519 |
2001-08-30 | 5.668 | 5.796 | 5.860 | 5.624 | 48013.06 | 600519 |
2001-08-31 | 5.804 | 5.782 | 5.877 | 5.749 | 23231.48 | 600519 |
- 计算该股票历史数据的5日均线和30日均线
- 什么是均线?
- 对于每一个交易日,都可以计算出前N天的移动平均值,然后把这些移动平均值连起来,成为一条线,就叫做N日移动平均线。移动平均线常用线有5天、10天、30天、60天、120天和240天的指标。
- 5天和10天的是短线操作的参照指标,称做日均线指标;
- 30天和60天的是中期均线指标,称做季均线指标;
- 120天和240天的是长期均线指标,称做年均线指标。
- 均线计算方法:MA=(C1+C2+C3+...+Cn)/N C:某日收盘价 N:移动平均周期(天数)
- 什么是均线?
ma5 = df['close'].rolling(5).mean()
ma30 = df['close'].rolling(30).mean()
ma5
date
2001-08-27 NaN
2001-08-28 NaN
2001-08-29 NaN
2001-08-30 NaN
2001-08-31 5.7150
2001-09-03 5.7600
2001-09-04 5.7786
2001-09-05 5.8116
2001-09-06 5.7992
2001-09-07 5.7576
2001-09-10 5.7486
2001-09-11 5.7122
2001-09-12 5.6666
2001-09-13 5.6412
2001-09-14 5.6606
2001-09-17 5.6336
2001-09-18 5.6322
2001-09-19 5.6616
2001-09-20 5.6842
2001-09-21 5.6768
2001-09-24 5.6834
2001-09-25 5.6828
2001-09-26 5.6566
2001-09-27 5.6594
2001-09-28 5.6916
2001-10-08 5.7082
2001-10-09 5.7416
2001-10-10 5.7422
2001-10-11 5.7124
2001-10-12 5.6782
...
2019-12-30 1153.1200
2019-12-31 1160.1200
2020-01-02 1159.3800
2020-01-03 1148.0720
2020-01-06 1131.0700
2020-01-07 1112.8160
2020-01-08 1093.8440
2020-01-09 1088.3840
2020-01-10 1095.1720
2020-01-13 1104.4280
2020-01-14 1107.0020
2020-01-15 1111.8000
2020-01-16 1112.6600
2020-01-17 1111.6600
2020-01-20 1105.0060
2020-01-21 1098.5860
2020-01-22 1091.2620
2020-01-23 1080.4220
2020-02-03 1059.7060
2020-02-04 1049.1080
2020-02-05 1044.0460
2020-02-06 1043.1440
2020-02-07 1047.7840
2020-02-10 1060.2980
2020-02-11 1072.2960
2020-02-12 1081.7520
2020-02-13 1085.7520
2020-02-14 1088.1520
2020-02-17 1093.6180
2020-02-18 1090.8180
Name: close, Length: 4406, dtype: float64
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(ma5[50:180])
plt.plot(ma30[50:180])
[<matplotlib.lines.Line2D at 0x125d96588>]
- 分析输出所有金叉日期和死叉日期
- 股票分析技术中的金叉和死叉,可以简单解释为:
- 分析指标中的两根线,一根为短时间内的指标线,另一根为较长时间的指标线。
- 如果短时间的指标线方向拐头向上,并且穿过了较长时间的指标线,这种状态叫“金叉”;
- 如果短时间的指标线方向拐头向下,并且穿过了较长时间的指标线,这种状态叫“死叉”;
- 一般情况下,出现金叉后,操作趋向买入;死叉则趋向卖出。当然,金叉和死叉只是分析指标之一,要和其他很多指标配合使用,才能增加操作的准确性。
- 股票分析技术中的金叉和死叉,可以简单解释为:
ma5 = ma5[30:]
ma30 = ma30[30:]
s1 = ma5 < ma30
s2 = ma5 > ma30
df = df[30:]
death_ex = s1 & s2.shift(1) #判定死叉的条件
df.loc[death_ex] #死叉对应的行数据
death_date = df.loc[death_ex].index
golden_ex = ~(s1 | s2.shift(1))#判定金叉的条件
golden_date = df.loc[golden_ex].index #金叉的时间
- 如果我从假如我从2010年1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益率如何?
- 分析:
- 买卖股票的单价使用开盘价
- 买卖股票的时机
- 最终手里会有剩余的股票没有卖出去
- 会有。如果最后一天为金叉,则买入股票。估量剩余股票的价值计算到总收益。
- 剩余股票的单价就是用最后一天的收盘价。
- 会有。如果最后一天为金叉,则买入股票。估量剩余股票的价值计算到总收益。
s1 = Series(data=1,index=golden_date) #1作为金叉的标识
s2 = Series(data=0,index=death_date) #0作为死叉的标识
s = s1.append(s2)
s = s.sort_index() #存储的是金叉和死叉对应的时间
s = s['2010':'2020']##存储的是金叉和死叉对应的时间
first_monry = 100000 #本金,不变
money = first_monry #可变的,买股票话的钱和卖股票收入的钱都从该变量中进行操作
hold = 0 #持有股票的数量(股数:100股=1手)
for i in range(0,len(s)): #i表示的s这个Series中的隐式索引
#i = 0(死叉:卖) = 1(金叉:买)
if s[i] == 1:#金叉的时间
#基于100000的本金尽可能多的去买入股票
#获取股票的单价(金叉时间对应的行数据中的开盘价)
time = s.index[i] #金叉的时间
p = df.loc[time]['open'] #股票的单价
hand_count = money // (p*100) #使用100000最多买入多少手股票
hold = hand_count * 100
money -= (hold * p) #将买股票话的钱从money中减去
else:
#将买入的股票卖出去
#找出卖出股票的单价
death_time = s.index[i]
p_death = df.loc[death_time]['open'] #卖股票的单价
money += (p_death * hold) #卖出的股票收入加入到money
hold = 0
#如何判定最后一天为金叉还是死叉
last_monry = hold * df['close'][-1] #剩余股票的价值
#总收益
money + last_monry - first_monry
1501254.9999999995
标签:实战,分析,01,02,600519,2020,09,Pandas,2001 来源: https://www.cnblogs.com/huahuawang/p/14888953.html