股票分析

引言

使用tushare包获取某股票的历史行情数据

  • pip install tushare
1
2
import tushare as ts
import pandas as pd
1
2
# 茅台的数据
maotai = ts.get_k_data(code='600519',start='1900-01-01')
1
maotai.head()

打印:

dateopenclosehighlowvolumecode
02001-08-275.3925.5545.9025.132406318.00600519
12001-08-285.4675.7595.7815.407129647.79600519
22001-08-295.7775.6845.7815.64053252.75600519
32001-08-305.6685.7965.8605.62448013.06600519
42001-08-315.8045.7825.8775.74923231.48600519
1
2
# 存储本地
maotai.to_csv('./maotai.csv')
1
2
3
4
5
6
# 从本地读取数据
# index_col将哪一列['date']作为原数据的行索引
# parse_dates: 将哪些列的数据转换为date类型
# 将date的类型转成时间类型然后将其作为原数据的行索引
df = pd.read_csv('./maotai.csv', index_col='date', parse_dates=['date'])
df.drop(labels='Unnamed: 0',axis=1,inplace=True) # 删除多余的一列 inplace表示,是否修改原数据
1
df.head()

打印:

openclosehighlowvolumecode
date
2001-08-275.3925.5545.9025.132406318.00600519
2001-08-285.4675.7595.7815.407129647.79600519
2001-08-295.7775.6845.7815.64053252.75600519
2001-08-305.6685.7965.8605.62448013.06600519
2001-08-315.8045.7825.8775.74923231.48600519
1
2
3
4
5
6
7
# 输出该股票所有收盘比开盘上涨3%以上的日期。
# (收盘-开盘)/开盘 > 0.03
(df['close'] - df['open']) / df['open'] > 0.03
# 将True对应的行数据取出
df.loc[(df['close'] - df['open']) / df['open'] > 0.03]
# 取行索引(时间)
df.loc[(df['close'] - df['open']) / df['open'] > 0.03].index

打印:

1
2
3
4
5
6
7
8
DatetimeIndex(['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',
...
'2019-03-01', '2019-03-18', '2019-04-10', '2019-04-16',
'2019-05-10', '2019-05-15', '2019-06-11', '2019-06-20',
'2019-09-12', '2019-09-18'],
dtype='datetime64[ns]', name='date', length=303, freq=None)
1
2
3
4
5
6
# 输出该股票所有开盘比前日收盘跌幅超过2%的日期。
# (开盘-前日收盘)/前日收盘 < -0.02
# shift(1): 向下移动一行
(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]
df.loc[(df['open'] - df['close'].shift(1)) / df['close'].shift(1) < -0.02].index

打印:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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'],
dtype='datetime64[ns]', name='date', freq=None)
1
2
#假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
df_2010 = df['2010':'2019']
  • 基于开盘价进行股票的买卖
  • 买股票的时机:
    • 每月的第一个交易日买入一手(100股)股票
    • 一个完整的年会买入12次股票共计1200股
  • 卖股票的时机:
    • 每年的最后一个交易日卖出所有(1200股)的股票
    • 一共可以卖9次股票
  • 注意:19年只可以买入不可以卖出,最后剩余的不能卖出的1000股股票是需要计算到总收益中
  • 数据的重新取样resample()
1
2
3
4
5
# 买入股票花费的钱数
# df_2010.resample('M').first() 取出每月的第一天为索引的行
df_monthly = df_2010.resample('M').first()
cost_money = df_monthly['open'].sum() * 100
cost_money

打印:

1
3568986.0999999996
1
2
3
4
5
# 卖出股票收到多少钱
# df_2010.resample('A').last()取出每年的最后一天为索引的行
# 并且排除2019年
df_yearly = df_2010.resample('A').last()[:-1]
df_yearly

打印:

openclosehighlowvolumecode
date
2010-12-31117.103118.469118.701116.62046084.0600519
2011-12-31138.039138.468139.600136.10529460.0600519
2012-12-31155.208152.087156.292150.14451914.0600519
2013-12-3193.18896.48097.17992.06157546.0600519
2014-12-31157.642161.056161.379157.13246269.0600519
2015-12-31207.487207.458208.704207.10619673.0600519
2016-12-31317.239324.563325.670317.23934687.0600519
2017-12-31707.948687.725716.329681.91876038.0600519
2018-12-31563.300590.010596.400560.00063678.0600519
1
last_price = df_2010['close'][-1]  # 昨天的收盘价
1
2
recv_monry = df_yearly['open'].sum() * 1200 + last_price * 1000
recv_monry - cost_money

打印:

1
590598.6999999997
-------------The End-------------

本文标题:股票分析

文章作者:Naqin

发布时间:2019年10月20日 - 09:10

最后更新:2019年10月29日 - 00:10

原始链接:https://chennq.top/数据分析/20191020-data_analysis_2.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Naqin wechat
欢迎看官加我微信!
坚持原创技术分享,您的支持将鼓励我继续创作!
0%