python股票数据分析
介绍
项目地址 https://github.com/zouxlin3/StockDataAnalysis
使用python分析csv格式的股票数据
使用方法如下
from StockData import StockData # 从源代码下载StockData.py和666666.SZ.csv虚拟数据
1 读取csv
虚拟数据的股票代码为000001
每个股票代码应对应一个csv文件
read(symbols: List[str])
stockdata = StockData('data') # 虚拟数据文件所在目录
symbols = ['000001'] # symbols为股票代码列表
stockdata.read(symbols)
stockdata.dataframes['000001'] # 查看内容
Unnamed: 0 | OBJECT_ID | S_INFO_WINDCODE | TRADE_DT | CRNCY_CODE | S_DQ_PRECLOSE | S_DQ_OPEN | S_DQ_HIGH | S_DQ_LOW | S_DQ_CLOSE | ... | S_DQ_ADJPRECLOSE | S_DQ_ADJOPEN | S_DQ_ADJHIGH | S_DQ_ADJLOW | S_DQ_ADJCLOSE | S_DQ_ADJFACTOR | S_DQ_AVGPRICE | S_DQ_TRADESTATUS | OPDATE | OPMODE | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 336601 | -488126 | 000001.SZ | 1991-04-03 | CNY | 61.49 | 49.00 | 49.00 | 49.00 | 49.00 | ... | 61.49 | 49.00 | 49.00 | 49.00 | 49.00 | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 |
1 | 336602 | -488127 | 000001.SZ | 1991-04-04 | CNY | 49.00 | 48.76 | 48.76 | 48.76 | 48.76 | ... | 49.00 | 48.76 | 48.76 | 48.76 | 48.76 | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 |
2 | 336603 | -488128 | 000001.SZ | 1991-04-05 | CNY | 48.76 | 48.52 | 48.52 | 48.52 | 48.52 | ... | 48.76 | 48.52 | 48.52 | 48.52 | 48.52 | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 |
3 | 336605 | -488130 | 000001.SZ | 1991-04-08 | CNY | 48.52 | 48.04 | 48.04 | 48.04 | 48.04 | ... | 48.52 | 48.04 | 48.04 | 48.04 | 48.04 | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 |
4 | 336606 | -488131 | 000001.SZ | 1991-04-09 | CNY | 48.04 | 47.80 | 47.80 | 47.80 | 47.80 | ... | 48.04 | 47.80 | 47.80 | 47.80 | 47.80 | 1.000000 | 47.5000 | ?? | 2016-12-27 10:24:33 | 0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
6298 | 6864894 | {D1A958FC-E2CC-11E6-9220-6C0B84A6895D} | 000001.SZ | 2017-01-25 | CNY | 9.27 | 9.27 | 9.28 | 9.25 | 9.26 | ... | 971.11 | 971.11 | 972.16 | 969.01 | 970.06 | 104.758253 | 9.2633 | ?? | 2017-01-25 15:10:52 | 0 |
6299 | 1717817 | {1930026C-E396-11E6-9CA9-4437E6DAC6D1} | 000001.SZ | 2017-01-26 | CNY | 9.26 | 9.27 | 9.34 | 9.26 | 9.33 | ... | 970.06 | 971.11 | 978.44 | 970.06 | 977.39 | 104.758253 | 9.3138 | ?? | 2017-01-26 15:26:14 | 0 |
6300 | 3288514 | {518B8FC9-E9DF-11E6-84F4-6C0B84A6895D} | 000001.SZ | 2017-02-03 | CNY | 9.33 | 9.34 | 9.36 | 9.23 | 9.26 | ... | 977.39 | 978.44 | 980.54 | 966.92 | 970.06 | 104.758253 | 9.2756 | ?? | 2017-02-03 15:09:52 | 0 |
6301 | 6878988 | {D21CCA60-EC3A-11E6-85F7-6C0B84A6895D} | 000001.SZ | 2017-02-06 | CNY | 9.26 | 9.26 | 9.32 | 9.26 | 9.31 | ... | 970.06 | 970.06 | 976.35 | 970.06 | 975.30 | 104.758253 | 9.2967 | ?? | 2017-02-06 15:20:12 | 0 |
6302 | 1204979 | {0700EF99-ED04-11E6-A728-6C0B84A6895D} | 000001.SZ | 2017-02-07 | CNY | 9.31 | 9.31 | 9.32 | 9.27 | 9.30 | ... | 975.30 | 975.30 | 976.35 | 971.11 | 974.25 | 104.758253 | 9.2912 | ?? | 2017-02-07 15:12:19 | 0 |
6303 rows × 24 columns
2 根据条件查看数据
2.1 根据时间段查看
查看一个股票在时间段的open high low close四个标签的数据
get_data_by_symbol(symbol: str, start_date: str, end_date: str)
stockdata.get_data_by_symbol('000001', '19910409', '19910419')
date | open | high | low | close | |
---|---|---|---|---|---|
4 | 1991-04-09 | 47.80 | 47.80 | 47.80 | 47.80 |
5 | 1991-04-10 | 47.56 | 47.56 | 47.56 | 47.56 |
6 | 1991-04-11 | 47.56 | 47.56 | 47.56 | 47.56 |
7 | 1991-04-12 | 47.08 | 47.08 | 47.08 | 47.08 |
8 | 1991-04-16 | 46.38 | 46.38 | 46.38 | 46.38 |
9 | 1991-04-17 | 46.15 | 46.15 | 46.15 | 46.15 |
10 | 1991-04-18 | 45.92 | 45.92 | 45.92 | 45.92 |
11 | 1991-04-19 | 45.69 | 45.69 | 45.69 | 45.69 |
2.2 根据时间段查看
查看指定日期一些股票的open high low close四个标签的数据
get_data_by_date( adate: str, symbols: List[str])
stockdata.get_data_by_date('20170207', symbols)
symbols | open | high | low | close | |
---|---|---|---|---|---|
0 | 000001 | 9.31 | 9.32 | 9.27 | 9.3 |
2.3 根据时间段查看
查看一些股票指定标签的数据
get_data_by_field(field: str, symbols: List[str])
stockdata.get_data_by_field('open', symbols)
date | 000001 | |
---|---|---|
0 | 1991-04-03 | 49.00 |
1 | 1991-04-04 | 48.76 |
2 | 1991-04-05 | 48.52 |
3 | 1991-04-08 | 48.04 |
4 | 1991-04-09 | 47.80 |
... | ... | ... |
6298 | 2017-01-25 | 9.27 |
6299 | 2017-01-26 | 9.27 |
6300 | 2017-02-03 | 9.34 |
6301 | 2017-02-06 | 9.26 |
6302 | 2017-02-07 | 9.31 |
6303 rows × 2 columns
3 绘制走势图
需要指定一个标签,volume和turnover标签时绘制柱状图,其余为折线图
plot(symbol: str, field: str)
stockdata.plot('000001', 'open')
4 数据处理
4.1 价格复权计算
对open high low close四类价格从后往前复权
adjust_data(symbol: str)
stockdata.adjust_data('000001')
stockdata.dataframes['000001']
Unnamed: 0 | OBJECT_ID | S_INFO_WINDCODE | TRADE_DT | CRNCY_CODE | S_DQ_PRECLOSE | S_DQ_OPEN | S_DQ_HIGH | S_DQ_LOW | S_DQ_CLOSE | ... | S_DQ_ADJFACTOR | S_DQ_AVGPRICE | S_DQ_TRADESTATUS | OPDATE | OPMODE | forward_af | forward_adjust_open | forward_adjust_high | forward_adjust_low | forward_adjust_close | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 336601 | -488126 | 000001.SZ | 1991-04-03 | CNY | 61.49 | 49 | 49.00 | 49.00 | 49.00 | ... | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 | 0.01051 | 49.0 | 49.00 | 49.00 | 49.00 |
1 | 336602 | -488127 | 000001.SZ | 1991-04-04 | CNY | 49.00 | 48 | 48.76 | 48.76 | 48.76 | ... | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 | 0.01051 | 48.0 | 48.76 | 48.76 | 48.76 |
2 | 336603 | -488128 | 000001.SZ | 1991-04-05 | CNY | 48.76 | 48 | 48.52 | 48.52 | 48.52 | ... | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 | 0.01051 | 48.0 | 48.52 | 48.52 | 48.52 |
3 | 336605 | -488130 | 000001.SZ | 1991-04-08 | CNY | 48.52 | 48 | 48.04 | 48.04 | 48.04 | ... | 1.000000 | 50.0000 | ?? | 2016-12-27 10:24:33 | 0 | 0.01051 | 48.0 | 48.04 | 48.04 | 48.04 |
4 | 336606 | -488131 | 000001.SZ | 1991-04-09 | CNY | 48.04 | 47 | 47.80 | 47.80 | 47.80 | ... | 1.000000 | 47.5000 | ?? | 2016-12-27 10:24:33 | 0 | 0.01051 | 47.0 | 47.80 | 47.80 | 47.80 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
6298 | 6864894 | {D1A958FC-E2CC-11E6-9220-6C0B84A6895D} | 000001.SZ | 2017-01-25 | CNY | 9.27 | 9 | 9.28 | 9.25 | 9.26 | ... | 104.758253 | 9.2633 | ?? | 2017-01-25 15:10:52 | 0 | 1.00000 | 9.0 | 9.28 | 9.25 | 9.26 |
6299 | 1717817 | {1930026C-E396-11E6-9CA9-4437E6DAC6D1} | 000001.SZ | 2017-01-26 | CNY | 9.26 | 9 | 9.34 | 9.26 | 9.33 | ... | 104.758253 | 9.3138 | ?? | 2017-01-26 15:26:14 | 0 | 1.00000 | 9.0 | 9.34 | 9.26 | 9.33 |
6300 | 3288514 | {518B8FC9-E9DF-11E6-84F4-6C0B84A6895D} | 000001.SZ | 2017-02-03 | CNY | 9.33 | 9 | 9.36 | 9.23 | 9.26 | ... | 104.758253 | 9.2756 | ?? | 2017-02-03 15:09:52 | 0 | 1.00000 | 9.0 | 9.36 | 9.23 | 9.26 |
6301 | 6878988 | {D21CCA60-EC3A-11E6-85F7-6C0B84A6895D} | 000001.SZ | 2017-02-06 | CNY | 9.26 | 9 | 9.32 | 9.26 | 9.31 | ... | 104.758253 | 9.2967 | ?? | 2017-02-06 15:20:12 | 0 | 1.00000 | 9.0 | 9.32 | 9.26 | 9.31 |
6302 | 1204979 | {0700EF99-ED04-11E6-A728-6C0B84A6895D} | 000001.SZ | 2017-02-07 | CNY | 9.31 | 9 | 9.32 | 9.27 | 9.30 | ... | 104.758253 | 9.2912 | ?? | 2017-02-07 15:12:19 | 0 | 1.00000 | 9.0 | 9.32 | 9.27 | 9.30 |
6303 rows × 29 columns
4.2 日频数据重采样
按照时间窗口进行重采样,采样的时间取区间左端
resample(symbol: str, freq: int)
stockdata.resample('000001', 5)
date | open | close | high | low | volume | turnover | vwap | |
---|---|---|---|---|---|---|---|---|
0 | 1991-04-09 | 49 | 47.80 | 49.00 | 47.80 | 12.0 | 59.000 | 4.9167 |
1 | 1991-04-17 | 47 | 46.15 | 47.56 | 46.15 | 26.0 | 123.000 | 4.7308 |
2 | 1991-04-24 | 45 | 44.78 | 45.92 | 44.78 | 50.0 | 225.000 | 4.5000 |
3 | 1991-05-02 | 44 | 43.46 | 44.56 | 43.46 | 31.0 | 137.000 | 4.4194 |
4 | 1991-05-09 | 43 | 42.17 | 43.24 | 42.17 | 129.0 | 546.000 | 4.2326 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
1255 | 2016-12-28 | 9 | 9.06 | 9.16 | 9.02 | 1631209.0 | 1483308.481 | 0.9093 |
1256 | 2017-01-05 | 9 | 9.17 | 9.18 | 9.05 | 1894909.0 | 1729934.514 | 0.9129 |
1257 | 2017-01-12 | 9 | 9.15 | 9.17 | 9.11 | 1691727.0 | 1547168.777 | 0.9145 |
1258 | 2017-01-19 | 9 | 9.18 | 9.24 | 9.07 | 2675002.0 | 2447679.663 | 0.9150 |
1259 | 2017-01-26 | 9 | 9.33 | 9.34 | 9.17 | 2008986.0 | 1858492.536 | 0.9251 |
1260 rows × 8 columns
4.3 计算移动平均
计算滑动窗口内数据的平均值
moving_average(symbol: str, field: str, window: int)
stockdata.moving_average('000001', 'open', 5)
TRADE_DT
1991-04-03 NaN
1991-04-04 NaN
1991-04-05 NaN
1991-04-08 NaN
1991-04-09 48.0
...
2017-01-25 9.0
2017-01-26 9.0
2017-02-03 9.0
2017-02-06 9.0
2017-02-07 9.0
Name: forward_adjust_open, Length: 6303, dtype: float64
5 相关指标计算
四类指标
ema(symbol: str, periods: int)
atr(symbol: str, periods: int)
rsi(symbol: str, periods: int)
macd(symbol: str, long: int, short: int, dea_periods: int)
stockdata.ema('000001', 5)
TRADE_DT
1991-04-03 NaN
1991-04-04 NaN
1991-04-05 NaN
1991-04-08 NaN
1991-04-09 49.500000
...
2017-01-25 9.233294
2017-01-26 9.265529
2017-02-03 9.263686
2017-02-06 9.279124
2017-02-07 9.286083
Name: ema_5, Length: 6303, dtype: float64
stockdata.atr('000001', 5)
TRADE_DT
1991-04-03 NaN
1991-04-04 NaN
1991-04-05 NaN
1991-04-08 NaN
1991-04-09 2.738
...
2017-01-25 0.064
2017-01-26 0.062
2017-02-03 0.076
2017-02-06 0.076
2017-02-07 0.070
Name: atr_5, Length: 6303, dtype: float64
stockdata.rsi('000001', 5)
TRADE_DT
1991-04-03 NaN
1991-04-04 NaN
1991-04-05 NaN
1991-04-08 NaN
1991-04-09 NaN
...
2017-01-25 75.156853
2017-01-26 81.025235
2017-02-03 57.391103
2017-02-06 58.812990
2017-02-07 65.561024
Name: rsi_5, Length: 6303, dtype: float64
stockdata.macd('000001', 25, 9, 5)
TRADE_DT
1991-04-03 NaN
1991-04-04 NaN
1991-04-05 NaN
1991-04-08 NaN
1991-04-09 NaN
...
2017-01-25 0.017994
2017-01-26 0.021877
2017-02-03 0.014808
2017-02-06 0.013298
2017-02-07 0.010009
Name: macd, Length: 6303, dtype: float64
6 回报计算
freq可选m、q、h、y,分别代表月、季度、半年、年的时间跨频率
6.1 回报率
calc_return(symbol: str, freq: str)
stockdata.calc_return('000001', 'q')
date | return_q | return | |
---|---|---|---|
0 | 1991-06-28 | NaT | -30.6327 |
1 | 1991-09-30 | NaT | -57.0462 |
2 | 1991-12-31 | NaT | 101.0274 |
3 | 1992-03-31 | NaT | -11.2436 |
4 | 1992-06-30 | NaT | 61.2284 |
... | ... | ... | ... |
99 | 2016-03-31 | NaT | -11.2594 |
100 | 2016-06-30 | NaT | -18.2331 |
101 | 2016-09-30 | NaT | 4.2529 |
102 | 2016-12-30 | NaT | 0.3308 |
103 | 2017-02-07 | NaT | 2.1978 |
104 rows × 3 columns
6.2 夏普比率
calc_sharpe_ratio(symbol: str, freq: str)
stockdata.calc_sharpe_ratio('000001', 'q')
0.04988574991791399
6.3 最大回撤率
calc_max_drawdown_ratio(symbol: str)
stockdata.calc_max_drawdown_ratio('000001')
91.63934426229508