tushare获取股票日线行情数据及日期转换

import tushare as ts
import pandas as pd
import numpy as np
#import mpl_finance as mpf 已过时
import mplfinance as mpf


def str_to_date(str):
    if len(str)==8:
        return str[0:4]+'-'+str[4:6]+'-'+str[6:8]
    else:
        return str


pro = ts.pro_api()
df = pro.daily(ts_code='000625.SZ', start_date='20180701', end_date='20200708')
#print(df)


period_type = 'D'
print(df)
#df = df[['ts_code','trade_date', 'open', 'close', 'high', 'low']]
df['trade_date']=df['trade_date'].apply(str_to_date)

df['trade_date'] = pd.to_datetime(df['trade_date'])
df=df.rename(columns={'vol': 'volume'})
df.to_excel('000625.xlsx')

print(df)

结果:

ts_code trade_date open high low close pre_close change
0 000625.SZ 20200708 12.88 13.18 12.79 12.94 12.95 -0.01
1 000625.SZ 20200707 12.53 13.10 12.31 12.95 12.55 0.40
2 000625.SZ 20200706 12.19 12.66 11.83 12.55 12.25 0.30
3 000625.SZ 20200703 11.30 12.31 11.26 12.25 11.20 1.05
4 000625.SZ 20200702 11.03 11.34 10.94 11.20 11.06 0.14
… … … … … … … … …
486 000625.SZ 20180706 8.53 8.54 8.07 8.28 8.49 -0.21
487 000625.SZ 20180705 9.05 9.13 8.91 8.94 9.03 -0.09
488 000625.SZ 20180704 9.01 9.07 8.96 9.03 9.01 0.02
489 000625.SZ 20180703 8.92 9.01 8.83 9.01 8.92 0.09
490 000625.SZ 20180702 9.01 9.11 8.80 8.92 9.00 -0.08

 pct_chg         vol       amount  

0 -0.0772 536769.12 694559.317
1 3.1873 783503.08 1007899.063
2 2.4490 996682.88 1218626.455
3 9.3750 1024575.01 1231345.889
4 1.2658 530836.04 591704.645
… … … …
486 -2.4700 175904.46 146602.257
487 -1.0000 129476.77 116576.508
488 0.2200 107497.52 96925.355
489 1.0100 85118.73 76084.956
490 -0.8900 141131.01 126972.936

[491 rows x 11 columns]
ts_code trade_date open high low close pre_close change
0 000625.SZ 2020-07-08 12.88 13.18 12.79 12.94 12.95 -0.01
1 000625.SZ 2020-07-07 12.53 13.10 12.31 12.95 12.55 0.40
2 000625.SZ 2020-07-06 12.19 12.66 11.83 12.55 12.25 0.30
3 000625.SZ 2020-07-03 11.30 12.31 11.26 12.25 11.20 1.05
4 000625.SZ 2020-07-02 11.03 11.34 10.94 11.20 11.06 0.14
… … … … … … … … …
486 000625.SZ 2018-07-06 8.53 8.54 8.07 8.28 8.49 -0.21
487 000625.SZ 2018-07-05 9.05 9.13 8.91 8.94 9.03 -0.09
488 000625.SZ 2018-07-04 9.01 9.07 8.96 9.03 9.01 0.02
489 000625.SZ 2018-07-03 8.92 9.01 8.83 9.01 8.92 0.09
490 000625.SZ 2018-07-02 9.01 9.11 8.80 8.92 9.00 -0.08

 pct_chg      volume       amount  

0 -0.0772 536769.12 694559.317
1 3.1873 783503.08 1007899.063
2 2.4490 996682.88 1218626.455
3 9.3750 1024575.01 1231345.889
4 1.2658 530836.04 591704.645
… … … …
486 -2.4700 175904.46 146602.257
487 -1.0000 129476.77 116576.508
488 0.2200 107497.52 96925.355
489 1.0100 85118.73 76084.956
490 -0.8900 141131.01 126972.936

[491 rows x 11 columns]

你可能感兴趣的:(tushare)