import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import tushare as ts
pingan = ts.get_k_data('601318',start='2011-01-01')
pingan.head()
|
date |
open |
close |
high |
low |
volume |
code |
0 |
2011-01-04 |
24.984 |
25.011 |
25.314 |
24.830 |
245626.0 |
601318 |
1 |
2011-01-05 |
24.870 |
24.110 |
24.962 |
24.083 |
427554.0 |
601318 |
2 |
2011-01-06 |
24.145 |
23.112 |
24.145 |
22.611 |
947078.0 |
601318 |
3 |
2011-01-07 |
23.279 |
23.297 |
23.842 |
22.765 |
659013.0 |
601318 |
4 |
2011-01-10 |
23.248 |
22.866 |
23.688 |
22.809 |
282919.0 |
601318 |
pingan.tail()
|
date |
open |
close |
high |
low |
volume |
code |
2018 |
2019-05-06 |
82.80 |
81.20 |
83.60 |
79.66 |
1808609.0 |
601318 |
2019 |
2019-05-07 |
81.78 |
81.17 |
82.28 |
79.89 |
974951.0 |
601318 |
2020 |
2019-05-08 |
78.73 |
78.78 |
80.55 |
78.38 |
868375.0 |
601318 |
2021 |
2019-05-09 |
78.26 |
76.66 |
78.26 |
76.11 |
1125721.0 |
601318 |
2022 |
2019-05-10 |
77.95 |
81.38 |
81.87 |
77.30 |
1392098.0 |
601318 |
pingan.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2023 entries, 0 to 2022
Data columns (total 7 columns):
date 2023 non-null object
open 2023 non-null float64
close 2023 non-null float64
high 2023 non-null float64
low 2023 non-null float64
volume 2023 non-null float64
code 2023 non-null object
dtypes: float64(5), object(2)
memory usage: 126.4+ KB
pingan['date'] = pd.to_datetime(pingan.date)
pingan.set_index('date',inplace = True)
pingan.index
DatetimeIndex(['2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07',
'2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13',
'2011-01-14', '2011-01-17',
...
'2019-04-24', '2019-04-25', '2019-04-26', '2019-04-29',
'2019-04-30', '2019-05-06', '2019-05-07', '2019-05-08',
'2019-05-09', '2019-05-10'],
dtype='datetime64[ns]', name='date', length=2023, freq=None)
pingan.head()
|
open |
close |
high |
low |
volume |
code |
date |
|
|
|
|
|
|
2011-01-04 |
24.984 |
25.011 |
25.314 |
24.830 |
245626.0 |
601318 |
2011-01-05 |
24.870 |
24.110 |
24.962 |
24.083 |
427554.0 |
601318 |
2011-01-06 |
24.145 |
23.112 |
24.145 |
22.611 |
947078.0 |
601318 |
2011-01-07 |
23.279 |
23.297 |
23.842 |
22.765 |
659013.0 |
601318 |
2011-01-10 |
23.248 |
22.866 |
23.688 |
22.809 |
282919.0 |
601318 |
pingan['close'].plot(figsize=(12,8))
<matplotlib.axes._subplots.AxesSubplot at 0x1a3adcc3c50>
g_yue = pingan['close'].resample('M').ohlc()
g_yue.head()
|
open |
high |
low |
close |
date |
|
|
|
|
2011-01-31 |
25.011 |
25.011 |
21.249 |
21.842 |
2011-02-28 |
21.952 |
22.980 |
21.477 |
21.868 |
2011-03-31 |
22.084 |
23.512 |
21.433 |
21.736 |
2011-04-30 |
22.514 |
24.123 |
22.514 |
22.954 |
2011-05-31 |
23.103 |
23.103 |
20.972 |
21.407 |
pingan['close'].loc['2017'].plot(figsize=(12,8))
<matplotlib.axes._subplots.AxesSubplot at 0x1a3ae7719b0>
shouyi = (pingan['close'][-1] - pingan['close'][0])/pingan['close'][0]
shouyi
2.253768341929551
y_num = pingan.index[-1].year - pingan.index[0].year
y_num
8
shouyi**(1/y_num)
1.1069134365512923
pingan['close'].to_period('A').groupby(level = 0).first().plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1a3ae771748>
pingan['42d'] = pingan['close'].rolling(window=42).mean()
pingan['250d'] = pingan['close'].rolling(window=250).mean()
pingan[['close','42d','250d']].plot(figsize=(12,8),color = ['y','b','r'])
<matplotlib.axes._subplots.AxesSubplot at 0x1a3ae784c88>
pingan['duishu'] = np.log(pingan['close']/pingan['close'].shift(1))
pingan.head()
|
open |
close |
high |
low |
volume |
code |
42d |
250d |
duishu |
date |
|
|
|
|
|
|
|
|
|
2011-01-04 |
24.984 |
25.011 |
25.314 |
24.830 |
245626.0 |
601318 |
NaN |
NaN |
NaN |
2011-01-05 |
24.870 |
24.110 |
24.962 |
24.083 |
427554.0 |
601318 |
NaN |
NaN |
-0.036689 |
2011-01-06 |
24.145 |
23.112 |
24.145 |
22.611 |
947078.0 |
601318 |
NaN |
NaN |
-0.042275 |
2011-01-07 |
23.279 |
23.297 |
23.842 |
22.765 |
659013.0 |
601318 |
NaN |
NaN |
0.007973 |
2011-01-10 |
23.248 |
22.866 |
23.688 |
22.809 |
282919.0 |
601318 |
NaN |
NaN |
-0.018674 |
pingan['duishu'].plot(subplots = True,figsize=(12,12))
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000001A3AEDBE4E0>],
dtype=object)
波动挺大的