龙空技术网

Python 获取股票(A股)日线行情数据

Pgabc 99

前言:

如今小伙伴们对“python判断交易日”大致比较关切,朋友们都需要了解一些“python判断交易日”的相关知识。那么小编也在网上收集了一些有关“python判断交易日””的相关文章,希望各位老铁们能喜欢,兄弟们一起来学习一下吧!

"""Python 获取股票(A股)日线行情数据"""import timeimport datetimeimport tushare as tsts.set_token('your token')                    #自行注册获取pro = ts.pro_api()def get_data_stock_daily(ts_code='', start_date='', end_date='', retry_count=3, pause=2):    #试3次下载数据,如不成功,每次暂停2钞    #交易日每天约16点之后取完整数据    for g in range(retry_count):        try:            df = pro.daily(ts_code=ts_code, start_date=start_date, end_date=end_date,                           fields='ts_code,trade_date,open,high,low,close,pre_close,change,pct_chg,vol,amount')        except Exception as err:            print(err)            time.sleep(pause)        else:            return dfdef get_data_stock_daily_a(trade_date='', retry_count=3, pause=2):    #试3次下载数据,如不成功,每次暂停2钞    #交易日每天约16点之后取完整数据    for g in range(retry_count):        try:            df = pro.daily(trade_date=trade_date, fields='ts_code,trade_date,open,high,low,close,pre_close,change,\                                                         pct_chg,vol,amount')        except Exception as err:            print(err)            time.sleep(pause)        else:            return dfdef end_dt(days=0):    try:        time_temp = datetime.datetime.now() - datetime.timedelta(days=days)        dt = time_temp.strftime('%Y%m%d')        return dt    except Exception as err:        print('end_dt is err', err)if __name__ == '__main__':    start_dt = end_dt(days=20)                #行情开始日期    end_dt = end_dt(days=0)                   #行情终止日期    print(start_dt, end_dt)    last_trade_dt = '20221111'                #指定行情日期    code_wm = '002594.SZ'                     #指定股票代码 以比亚迪为例    #获取002594.SZ股票期间日线行情    df_wm = get_data_stock_daily(ts_code=code_wm, start_date=start_dt, end_date=end_dt, retry_count=3, pause=2)    print(df_wm)    #获取2022年11月11日所有股票日线行情    df_nv = get_data_stock_daily_a(trade_date=last_trade_dt, retry_count=3, pause=2)    print(df_nv)    """运行结果    20221023 20221112          ts_code trade_date    open  ...  pct_chg        vol       amount    0   002594.SZ   20221111  270.50  ...   3.2813  166099.59  4467701.122    1   002594.SZ   20221110  264.63  ...  -3.4751  155496.77  4039239.830    2   002594.SZ   20221109  278.15  ...  -2.4529  111946.36  3038705.770    3   002594.SZ   20221108  279.45  ...  -0.8857  105634.49  2925766.812    4   002594.SZ   20221107  275.00  ...  -0.4711  135842.06  3781158.420    5   002594.SZ   20221104  268.98  ...   4.5600  224886.89  6270505.264    6   002594.SZ   20221103  265.02  ...   0.1570  102383.97  2747240.766    7   002594.SZ   20221102  261.00  ...   2.1923  187319.29  4980989.815    8   002594.SZ   20221101  247.11  ...   6.1418  219282.71  5620272.304    9   002594.SZ   20221031  237.50  ...   2.8263  187942.33  4625511.532    10  002594.SZ   20221028  253.00  ...  -5.9255  263163.93  6372054.067    11  002594.SZ   20221027  258.99  ...  -1.1130   97765.82  2516238.985    12  002594.SZ   20221026  260.00  ...  -0.0155  126415.27  3279130.696    13  002594.SZ   20221025  253.00  ...   0.9156  113242.67  2897739.554    14  002594.SZ   20221024  264.90  ...  -4.0293  167412.56  4324097.663        [15 rows x 11 columns]            ts_code trade_date   open  ...  pct_chg         vol       amount    0     000001.SZ   20221111  11.13  ...   5.1518  2545070.55  2878557.392    1     000002.SZ   20221111  14.73  ...   9.9791  2911654.04  4449223.899    2     000004.SZ   20221111   9.77  ...  -2.3933    22947.25    22190.216    3     000005.SZ   20221111   1.81  ...  -1.1050   121048.82    21737.140    4     000006.SZ   20221111   3.98  ...   4.1026   246891.22    99659.542    ...         ...        ...    ...  ...      ...         ...          ...    4977  873122.BJ   20221111  11.95  ...  -1.3445     5547.54     6557.802    4978  873169.BJ   20221111   6.62  ...   0.1517     6569.34     4333.168    4979  873223.BJ   20221111   3.84  ...   1.3123    21598.31     8352.987    4980  873527.BJ   20221111   9.48  ...   0.1079     2668.09     2483.030    4981  689009.SH   20221111  33.50  ...   1.8416    48064.52   158772.548        [4982 rows x 11 columns]        Process finished with exit code 0    """    """    Pgabc 2022000029    author : Pgabc        更多函数,安装工具包    pip install pgabc    """

标签: #python判断交易日