使用tushare包获取某股票的历史行情数据
- pip install tushare
import tushare as ts
import pandas as pd
# 茅台的数据
maotai = ts.get_k_data(code='600519',start='1900-01-01')
maotai.head()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
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 |
# 存储本地
maotai.to_csv('./maotai.csv')
# 从本地读取数据
# 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表示,是否修改原数据
df.head()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
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['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
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)
# 输出该股票所有开盘比前日收盘跌幅超过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
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)
#假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
df_2010 = df['2010':'2019']
- 基于开盘价进行股票的买卖
- 买股票的时机:
- 每月的第一个交易日买入一手(100股)股票
- 一个完整的年会买入12次股票共计1200股
- 卖股票的时机:
- 每年的最后一个交易日卖出所有(1200股)的股票
- 一共可以卖9次股票
- 注意:19年只可以买入不可以卖出,最后剩余的不能卖出的1000股股票是需要计算到总收益中
- 数据的重新取样resample()
# 买入股票花费的钱数
# df_2010.resample('M').first() 取出每月的第一天为索引的行
df_monthly = df_2010.resample('M').first()
cost_money = df_monthly['open'].sum() * 100
cost_money
3568986.0999999996
# 卖出股票收到多少钱
# df_2010.resample('A').last()取出每年的最后一天为索引的行
# 并且排除2019年
df_yearly = df_2010.resample('A').last()[:-1]
df_yearly
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
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 |
last_price = df_2010['close'][-1] # 昨天的收盘价
recv_monry = df_yearly['open'].sum() * 1200 + last_price * 1000
recv_monry - cost_money
590598.6999999997
原文地址:https://www.cnblogs.com/zyyhxbs/p/11693210.html
时间: 2024-11-02 01:42:03