baostock_add.py日常维护增加数据

#!/usr/bin/env python
import baostock as bs
import pandas as pd
import time
import os
def download_factor(start_date,end_date,stock_df):
    rs_list = []
    file_w = pathsave + "\\" + "list.csv"
    stock_df.to_csv(file_w, sep=",", index=False, header=True)
    result_factor = pd.DataFrame()
    for code in stock_df["code"]:
        print("Downloading factor:" + code)
        rs_factor = bs.query_adjust_factor(code=code, start_date=start_date, end_date=end_date)
        while (rs_factor.error_code == '0') & rs_factor.next():
            rs_list.append(rs_factor.get_row_data())
        result_factor = pd.DataFrame(rs_list, columns=rs_factor.fields)
    print(result_factor)
    return result_factor


def download_data(start_date,end_date,code):
    # 获取指定日期的指数、股票数据
    data_df = pd.DataFrame()
    #print("Downloading :" + code)
    k_rs = bs.query_history_k_data_plus(code, "date,code,open,high,low,close,volume,amount,turn,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM",
                                        start_date=start_date, end_date=end_date,adjustflag= "2",frequency="d")
    data_df = data_df.append(k_rs.get_data())
    return data_df

def conpare_list():
    stock_rs = bs.query_all_stock(end_date)
    stock_df = stock_rs.get_data()
    file_name = pathsave + "\\" + "all.csv"
    stock_read = pd.read_csv(file_name)
    #print(stock_read.columns)
    #print(stock_read[220:240])
    for code in stock_df["code"]:
        #print(code)
        flag_t = stock_read.loc[stock_read["code"] == code,"flag"]
        flag_t = flag_t.reset_index(drop=True)
        flag_t = pd.DataFrame(flag_t)
        t = ''
        if flag_t.empty:
            t = "new"
        else:
            t = flag_t.loc[0,"flag"]
        stock_df.loc[stock_df["code"] == code,"flag"] = t
    return stock_df

def add_data(end_date,stock_df):
    stock_df = stock_df.drop_duplicates(subset=["code"], keep="last", inplace=False)
    stock_df["code2"] = stock_df["code"].str.replace("sh.", "SH")
    stock_df["code2"] = stock_df["code2"].str.replace("sz.", "SZ")
    stock_df = stock_df.set_index("code")
    #print(stock_df)
    for code in stock_df.index:
        file = pathsave + "\\"  + stock_df.loc[code,"flag"]  +"\\"+ stock_df.loc[code,"code2"]+".csv"
        print(file)
        df_old = pd.DataFrame()
        if  os.path.isfile(file):
            df_old = pd.read_csv(file)
        df_all = download_data(stock_df.loc[code,"start_date"],end_date,code)
        df_all["code"] = df_all["code"].str.replace("sh.", "SH")
        df_all["code"] = df_all["code"].str.replace("sz.", "SZ")
        df_all["date"] = df_all["date"].str.replace("-", "")
        df_old = df_old.append(df_all)
        #df_new = df_old.reset_index(drop=True)
        df_old["date"] = df_old["date"].astype(str)
        df_old = df_old.drop_duplicates(subset=["date"], keep="last", inplace=False)
        df_old.to_csv(file,sep=",",encoding="gbk", index=False)
if __name__ == '__main__':
    # 获取指定日期全部股票的日K线数据
    print("hello")
    lg = bs.login()
    print('login respond error_code:'+lg.error_code)
    print('login respond  error_msg:'+lg.error_msg)
    pathsave = 'G:\\datas of status\\python codes\\baostock'  # 设定临时文件存放位置

    ori_date = "2018-01-01"#设定最初日期数据
    start_date = "2020-05-16"     #常设,设定这次要下载的数据开始日期
    end_date = "2020-06-01"       #常设,设定这次要下载的数据结束日期
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    stock_df = conpare_list()    #分清指数,上证,深证
    df_factor = download_factor(start_date,end_date,stock_df)  #分清有无复权,若有则设定开初下载数据时间有最初日期,然后再重新下载数据
    df_factor = df_factor.drop_duplicates(subset=["code"], keep="last", inplace=False)
    stock_df["start_date"] = start_date
    for code in df_factor["code"]:
        stock_df.loc[stock_df["code"] == code,"start_date"] = ori_date
    #print(stock_df[220:240])
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    add_data(end_date,stock_df)
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    #print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    bs.logout()

  

posted @ 2020-06-01 21:58  rongye  阅读(410)  评论(0编辑  收藏  举报