def load_index(index, query_func, data_path): data_file = os.path.join(data_path, index, 'stocks.csv') if os.path.exists(data_file): return load_stocks_from_file(data_file) lg = bs.login() if lg.error_code != '0': raise ValueError('baostock login failed:{}, {}'.format(lg.error_code, lg.error_msg)) try: rs = query_func() if rs.error_code != '0': raise ValueError('baostock query {} failed:{}, {}'.format(index, rs.error_code, rs.error_msg)) index_stocks = [] while (rs.error_code == '0') & rs.next(): index_stocks.append(rs.get_row_data()) if rs.error_code != '0': raise ValueError('baostock query {} failed:{}, {}'.format(index, rs.error_code, rs.error_msg)) result = pd.DataFrame(index_stocks, columns=rs.fields) pathlib.Path(os.path.join(data_path, index)).mkdir(parents=True, exist_ok=True) result.to_csv(data_file, encoding="utf-8", index=False) return load_stocks_from_file(data_file) finally: bs.logout()
def refresh_all_stock_day_k_noadjust(start_date="2020-03-27", current_date="2020-03-30"): bs.login() stock_rs = bs.query_all_stock(day=current_date) stock_df = stock_rs.get_data() data_list = [] for code in stock_df["code"]: k_rs = bs.query_history_k_data_plus( code, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST", start_date=start_date, end_date='', frequency="d", adjustflag="3") while (k_rs.error_code == '0') & k_rs.next(): data_list.append(k_rs.get_row_data()) print('query_history_k_data_plus no adjust code:' + code) bs.logout() db_conn = create_engine(common.db_path_sqlalchemy) db_conn.execute( r''' INSERT OR REPLACE INTO stock_day_k_noadjust VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ''', data_list)
def query_trade_dates(start_date, end_date): """ 交易日查询:query_trade_dates() 方法说明:查询股票交易日信息信息,可以通过参数设置获取起止年份数据,提供2014-2018年数据。 返回类型:pandas的DataFrame类型。 """ #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 #print('login respond error_code:'+lg.error_code) #print('login respond error_msg:'+lg.error_msg) #### 获取交易日信息 #### rs = bs.query_trade_dates(start_date=start_date, end_date=end_date) #print('query_trade_dates respond error_code:'+rs.error_code) #print('query_trade_dates respond error_msg:'+rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) #### 结果集输出到csv文件 #### result.to_csv(path + "trade_dates.csv", encoding="gbk", index=False) #### 登出系统 #### bs.logout() return
def get_stock_data(t1, t2, stock_name): lg = bs.login() print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) #### 获取沪深A股历史K线数据 #### # 详细指标参数,参见baostock rs = bs.query_history_k_data( stock_name, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST", start_date=t1, end_date=t2, frequency="d", adjustflag="3") print('query_history_k_data respond error_code:' + rs.error_code) print('query_history_k_data respond error_msg:' + rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) print(result) #### 结果集输出到csv文件 #### result.to_csv("history_A_stock_k_data.csv", index=False) print(result) #### 登出系统 #### bs.logout() result['date'] = pd.to_datetime(result['date']) result.set_index("date", inplace=True) return result
def download(stockCode, beginDate, endDate, fileName, isCsv): #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) #### 获取沪深A股历史K线数据 #### # 详细指标参数,参见“历史行情指标参数”章节;“分钟线”参数与“日线”参数不同。 # 分钟线指标:date,time,code,open,high,low,close,volume,amount,adjustflag # code:股票代码,sh或sz.+6位数字代码,或者指数代码,如:sh.601398。sh:上海;sz:深圳。此参数不可为空; # fields:指示简称,支持多指标输入,以半角逗号分隔,填写内容作为返回类型的列。详细指标列表见历史行情指标参数章节,日线与分钟线参数不同。此参数不可为空; # start:开始日期(包含),格式“YYYY-MM-DD”,为空时取2015-01-01; # end:结束日期(包含),格式“YYYY-MM-DD”,为空时取最近一个交易日; # frequency:数据类型,默认为d,日k线;d=日k线、w=周、m=月、5=5分钟、15=15分钟、30=30分钟、60=60分钟k线数据,不区分大小写;指数没有分钟线数据;周线每周最后一个交易日才可以获取,月线每月最后一个交易日才可以获取。 # adjustflag:复权类型,默认不复权:3;1:后复权;2:前复权。已支持分钟线、日线、周线、月线前后复权。 BaoStock提供的是涨跌幅复权算法复权因子,具体介绍见: rs = bs.query_history_k_data_plus( stockCode, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST", start_date="2006-01-01", end_date=endDate, frequency="d", adjustflag="2") print('query_history_k_data_plus respond error_code:' + rs.error_code) print('query_history_k_data_plus respond error_msg:' + rs.error_msg) if isCsv: saveToCsv(rs, fileName) else: saveToMysql(rs) #### 登出系统 #### bs.logout()
def start_tk_crawl(): global dm dm = DBManager("history_k_data") # 初始化baostock bs.login() # 获取股票代码列表 code_list = dm.get_code_list() for item in code_list: max_try = 8 # 失败重连次数 ticker = item["ticker"] print("开始加载股票数据:" + str(ticker)) for tries in range(max_try): rs = bs.query_history_k_data_plus( ticker, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST", start_date='2008-01-01', end_date='2019-05-31', frequency="d", adjustflag="3") if rs.error_code == '0': parse_pager(rs, ticker) # 解析数据 break elif tries < (max_try - 1): sleep(2) continue else: print("加载股票数据失败:" + str(ticker)) print("加载股票数据完成") bs.logout()
def Kget(stockcode, checkdate1, checkdate2): import pandas as pd import baostock as bs #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 # print('login respond error_code:'+lg.error_code) # print('login respond error_msg:'+lg.error_msg) #### 获取沪深A股历史K线数据 #### # 详细指标参数,参见“历史行情指标参数”章节;“分钟线”参数与“日线”参数不同。 # 分钟线指标:date,time,code,open,high,low,close,volume,amount,adjustflag rs = bs.query_history_k_data_plus( stockcode, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM", start_date=checkdate1, end_date=checkdate2, frequency="d", adjustflag="3") # print('query_history_k_data_plus respond error_code:'+rs.error_code) # print('query_history_k_data_plus respond error_msg:'+rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) #### 结果集输出到csv文件 #### result.to_csv("history_A_stock_k_data.csv", encoding="utf8", index=False) # print(result) #### 登=出系统 #### bs.logout()
def getBasicInfoByName(name): # 登陆系统 lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) # 获取证券基本资料 rs = bs.query_stock_basic(code_name=name) # 支持模糊查询 print('query_stock_basic respond error_code:' + rs.error_code) print('query_stock_basic respond error_msg:' + rs.error_msg) obj = {} if (rs.error_code != '0'): obj['error_code'] = rs.error_code obj['error_msg'] = rs.error_msg return obj #print(type(rs))#<class 'baostock.data.resultset.ResultData'> #打印结果集 data_list = [] while (rs.error_code == '0' and rs.next()): #获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) obj['error_code'] = '0' obj['data_list'] = data_list #登出系统 bs.logout() return obj
def start_pe_crawl(): global dm, log log = Logger() dm = DBManager("em_pe_database") log.logger.info("开始更新指数数据") bs.login() code_list = dm.get_code_list() for item in code_list: ticker = item["ticker"] max_try = 8 for tries in range(max_try): rs = bs.query_history_k_data( ticker, "date,code,close,peTTM,pbMRQ,psTTM,pcfNcfTTM", frequency="d", adjustflag="2") if rs.error_code == "0": parse_pager(rs, ticker) break elif tries < (max_try - 1): sleep(2) continue else: log.logger.error("更新股票指数数据:" + str(ticker)) log.logger.info("结束更新指数数据") bs.logout()
def update_stock_daily(history=False): try: et = utils.get_recently_trade_date() st = et if history: st = settings.START_DATE print(st, et) # 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code + ', error_msg:' + lg.error_msg) stock = redisUtils.get("Ashare") stockJson = json.loads(stock) for code in stockJson: name = stockJson[code][1] if "ST" in name: continue print("Downloading :" + code + " , name :" + name) k_rs = bs.query_history_k_data_plus(code, settings.STOCK_FIELDS, start_date=st, end_date=et) # print(k_rs.get_data()) stock_records = k_rs.get_data().to_dict('records') for record in stock_records: rJsonStr = json.dumps(record, indent=4, ensure_ascii=False).encode('utf-8') r = redisUtils.sadd(code, rJsonStr) print("{} redis add finish".format(code)) bs.logout() except IOError: print("Update Data Error ")
def update_trades(): try: et = stock_utils.get_recently_trade_date() st = ConfigUtils.get_stock("START_DATE") # 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code + ', error_msg:' + lg.error_msg) pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME")) data = list(pd_names.iterrows()) # multi processing number_kernel = 1 size = int((len(data) + number_kernel - 1) / number_kernel) p = Pool(number_kernel) for i in range(number_kernel): start = size * i _end = size * (i + 1) end = len(data) if _end > len(data) else _end p.apply_async(loading_stock, args=(data[start:end], st, et)) p.close() p.join() print('all subprocesses done.') bs.logout() except IOError as e: print("Update Data Error ", e)
def load_adjfactor(symbol, data_path): adj_file = os.path.join(data_path, symbol, 'adj.csv') if os.path.exists(adj_file): return load_adjfactor_from_file(adj_file) lg = bs.login('anonymous', '123456') if lg.error_code != '0': raise ValueError('baostock login failed:{}, {}'.format(lg.error_code, lg.error_msg)) rs_list = [] rs_factor = bs.query_adjust_factor(convert_symbol(symbol), start_date="1990-01-01", end_date="2199-12-31") while (rs_factor.error_code == '0') & rs_factor.next(): rs_list.append(rs_factor.get_row_data()) if rs_factor.error_code != '0': bs.logout() raise ValueError('baostock read data failed:{}'.format(rs_factor.error_msg)) bs.logout() result_factor = pd.DataFrame(rs_list, columns=rs_factor.fields) result_factor.to_csv(adj_file, encoding="utf-8", index=False) return load_adjfactor_from_file(adj_file)
def init_trade_date(): settings.init() # 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) # 获取交易日信息 #### rs = bs.query_trade_dates(start_date=settings.START_DATE, end_date=settings.END_DATE) print('query_trade_dates respond error_code:' + rs.error_code) print('query_trade_dates respond error_msg:' + rs.error_msg) # 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) # 结果集输出到csv文件 #### result.to_csv(settings.STOCKS_DATE, encoding="utf-8", index=False, header=True) print(result) # 登出系统 #### bs.logout()
def start_tk_crawl(): global dm, log log = Logger() dm = DBManager("em_tk_database") log.logger.info("开始更新股票数据") bs.login() code_list = dm.get_code_list() for item in code_list: ticker = item["ticker"] max_try = 8 for tries in range(max_try): rs = bs.query_history_k_data( ticker, "date,code,open,high,low,close,volume,amount,adjustflag,turn," "pctChg", frequency="w", adjustflag="3") if rs.error_code == '0': parse_pager(rs, ticker) break elif tries < (max_try - 1): sleep(2) continue else: log.logger.error("更新股票数据失败:" + str(ticker)) log.logger.info("结束更新股票数据") bs.logout()
def get(self, request, *args, **kwargs): queryset = SharesCategory.objects.all() if len(queryset) <= 0: lg = bs.login() rs = bs.query_stock_industry() # print(rs) data_list = [] while (rs.error_code == '0') & rs.next(): data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) result1 = pd.DataFrame(result['industry']) result1['count'] = pd.Series(np.random.randint(1, 2, 4061)) result1['industryClassification'] = result[ 'industryClassification'] result1 = result1.groupby('industry').sum() shares_cate_list = [] for index, row in result1.iterrows(): cate_obj = SharesCategory(industry=index, industryClassification='申万一级行业', count=row['count']) cate_obj.save() shares_cate_list.append({ 'industry': index, 'industryClassification': '申万一级行业', 'count': float(row['count']) }) bs.logout() return JsonResponse(shares_cate_list, safe=False) else: return JsonResponse([], safe=False)
def all_stock_list(): #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) #### 获取证券信息 #### date = datetime.datetime.now() print(date) start_date = (date + datetime.timedelta(days=-5)).strftime('%Y-%m-%d') rs = bs.query_all_stock(day='2020-07-16') print('query_all_stock respond error_code:' + rs.error_code) print('query_all_stock respond error_msg:' + rs.error_msg) #### 登出系统 #### bs.logout() #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data = rs.get_row_data() if data[1] != '0': #data[0] = data[0].replace('.', '') data_list.append(data) result = pd.DataFrame(data_list, columns=rs.fields) return result
def query_stock_k_data_plus(date): #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) date = date.strftime("%Y-%m-%d") print(date) stock_rs = bs.query_all_stock(date) stock_df = stock_rs.get_data() print(stock_df.empty) if not (stock_df is None or stock_df.empty): data_df = pd.DataFrame() for code in stock_df["code"]: k_rs = bs.query_history_k_data_plus( code, "date,code,open,high,low,close,preclose,volume," "amount,adjustflag,turn,tradestatus,pctChg,peTTM," "pbMRQ,psTTM,pcfNcfTTM,isST", date, date) data_df = data_df.append(k_rs.get_data()) data = data_df.drop_duplicates(subset={"date", "code"}, keep="last") common.insert_db(data, "bs_stock_k_data", False, "`date`,`code`") print(date + " done!") else: print(date + " no data .") bs.logout()
def get_history_companies(self) -> pd.DataFrame: """ Returns ------- pd.DataFrame: symbol date type SH600000 2019-11-11 add SH600000 2020-11-10 remove dtypes: symbol: str date: pd.Timestamp type: str, value from ["add", "remove"] """ bs.login() today = pd.datetime.now() date_range = pd.DataFrame( pd.date_range(start="2007-01-15", end=today, freq="7D"))[0].dt.date ret_list = [] col = ["date", "symbol", "code_name"] for date in tqdm(date_range, desc="Download CSI500"): rs = bs.query_zz500_stocks(date=str(date)) zz500_stocks = [] while (rs.error_code == "0") & rs.next(): zz500_stocks.append(rs.get_row_data()) result = pd.DataFrame(zz500_stocks, columns=col) result["symbol"] = result["symbol"].apply( lambda x: x.replace(".", "").upper()) result = self.get_data_from_baostock(date) ret_list.append(result[["date", "symbol"]]) bs.logout() return pd.concat(ret_list, sort=False)
def growget(stackcode): import baostock as bs import pandas as pd # 登陆系统 lg = bs.login() # 显示登陆返回信息 # print('login respond error_code:'+lg.error_code) # print('login respond error_msg:'+lg.error_msg) # 查询季频估值指标盈利能力 growth_list = [] for y in range(2007, 2021): qu = 4 if y == 2021: qu = 1 for i in range(qu): rs_profit = bs.query_growth_data(code=stackcode, year=y, quarter=i + 1) while (rs_profit.error_code == '0') & rs_profit.next(): growth_list.append(rs_profit.get_row_data()) result_growth = pd.DataFrame(growth_list, columns=rs_profit.fields) # 结果集输出到csv文件 result.to_csv("grow_data.csv", encoding="utf8", index=False) # 登出系统 bs.logout()
def get_new_companies(self) -> pd.DataFrame: """ Returns ------- pd.DataFrame: symbol start_date end_date SH600000 2000-01-01 2099-12-31 dtypes: symbol: str start_date: pd.Timestamp end_date: pd.Timestamp """ logger.info("get new companies......") today = datetime.date.today() bs.login() result = self.get_data_from_baostock(today) bs.logout() df = result[["date", "symbol"]] df.columns = [self.END_DATE_FIELD, self.SYMBOL_FIELD_NAME] df[self.END_DATE_FIELD] = pd.to_datetime( df[self.END_DATE_FIELD].astype(str)) df[self.START_DATE_FIELD] = self.bench_start_date logger.info("end of get new companies.") return df
def get_his_trade_date(start_date="2018-01-27", end_date="2019-05-01"): #### 登陆系统 #### lg = bs.login(user_id="anonymous", password="******") # 显示登陆返回信息 print('login respond error_code:'+lg.error_code) print('login respond error_msg:'+lg.error_msg) #### 获取交易日信息 #### rs = bs.query_trade_dates(start_date=start_date, end_date=end_date) print('query_trade_dates respond error_code:'+rs.error_code) print('query_trade_dates respond error_msg:'+rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) #### 登出系统 #### bs.logout() #### 结果集输出到csv文件 #### #result.to_csv("/data/trade_datas.csv", encoding="gbk", index=False) return result
def get_history_k_data(code,start_date,end_date,frequency,adjustflag): bs.login() rs = bs.query_history_k_data_plus(code, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST", start_date=start_date, end_date=end_date,frequency=frequency,adjustflag=adjustflag) data_list = [] while (rs.error_code == '0') & rs.next() : data={} # 获取一条记录,将记录合并在一起 rowdata = rs.get_row_data() data['date']=rowdata[0] data['code']=rowdata[1] data['open']=rowdata[2] data['high']=rowdata[3] data['low']=rowdata[4] data['close']=rowdata[5] data['preclose']=rowdata[6] data['volume']=rowdata[7] data['amount']=rowdata[8] data['adjustflag']=rowdata[10] data['tradestatus']=rowdata[11] data['pctChg']=rowdata[12] data['isST']=rowdata[13] data_list.append(data) return data_list bs.logout()
def Zijinmian(): today = datetime.datetime.today() year = today.year month = today.month dic = {'m2': []} # 登陆系统 lg = bs.login() # 获取货币供应量 if month < 10: enddate = str(year) + '-' + '0' + str(month) else: enddate = str(year) + '-' + str(month) rs5 = bs.query_money_supply_data_month(start_date="2020-01", end_date=enddate) # 打印结果集 data_list = [] while (rs5.error_code == '0') & rs5.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs5.get_row_data()) result = pd.DataFrame(data_list, columns=rs5.fields) print(np.array(result['m2Month'])) # 登出系统 bs.logout() dic['m2'] = np.array(result['m2Month']).tolist() dic['m2'] = list(map(eval, dic['m2'])) return json.dumps(dic, ensure_ascii=False)
def create_stock_industry(): """ BS-创建行业分类 """ # 登陆系统 lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) # 获取行业分类数据 rs = bs.query_stock_industry() print('query_stock_industry error_code:' + rs.error_code) print('query_stock_industry respond error_msg:' + rs.error_msg) # 打印结果集 industry_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 industry_list.append(rs.get_row_data()) result = pd.DataFrame(industry_list, columns=rs.fields) dtype = { 'updateDate': String(10), 'code': String(9), 'code_name': String(10), 'industry': String(4), 'industryClassification': String(6) } result.to_sql('odl_bs_stock_industry', engine, schema=CQ_Config.DB_SCHEMA, if_exists='replace', index=False, dtype=dtype) # 登出系统 bs.logout()
def refresh_all_stock_adjust(start_date="2020-03-27", current_date="2020-03-30"): bs.login() db_conn = create_engine(common.db_path_sqlalchemy) stock_rs = bs.query_all_stock(day=current_date) stock_df = stock_rs.get_data() rs_list = [] for code in stock_df["code"]: if code.startswith("sh.6") | code.startswith( "sz.00") | code.startswith("sz.300"): print('query_stock_factor code:' + code) rs_factor = bs.query_adjust_factor(code=code, start_date=start_date, end_date=current_date) while (rs_factor.error_code == '0') & rs_factor.next(): rs_list.append(rs_factor.get_row_data()) factor_list = [] for d in rs_list: code = d[0] print("refresh all factor for " + code) rs_factor = bs.query_adjust_factor(code=code, start_date='2006-01-01', end_date=current_date) while (rs_factor.error_code == '0') & rs_factor.next(): factor_list.append(rs_factor.get_row_data()) if len(factor_list) > 0: db_conn.execute( r''' INSERT OR REPLACE INTO stock_adjustfactor VALUES (?, ?, ?, ?, ?) ''', factor_list) bs.logout()
def Fetch000001WithBaostock(startDate): #### 登陆系统 #### lg = bs.login() # 详细指标参数,参见“历史行情指标参数”章节 rs = bs.query_history_k_data("sh.000001", "date,open,high,low,close,volume", start_date=Utils.DateForm1(startDate), end_date='', frequency="d", adjustflag="3") #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): data_list.append(rs.get_row_data()) df = pd.DataFrame(data_list, columns=rs.fields) for index, row in df.iterrows(): row['date'] = row['date'].replace('-', '') df.rename(columns={'date': 'trade_date', 'volume': 'vol'}, inplace=True) dfInRange = df[(df['trade_date'] > str(startDate))] bs.logout() return dfInRange
def get_growth_data(stock_number,stock_name,year,quarter): print('==========================================================') print("开始进行: "+stock_name+"("+stock_number+")"+"的数据处理") print("尝试登陆baostock") #####login##### lg=bs.login(user_id="anonymous",password="******") if(lg.error_code == '0'): print("登陆成功") else: print("登录失败") #####get stock data##### rs=bs.query_growth_data(code=stock_number,year=year,quarter=quarter) print('请求历史数据返回代码:'+rs.error_code) print('请求历史数据返回信息:'+rs.error_msg) data_list=[] while(rs.error_code=='0')&rs.next(): data_list.append(rs.get_row_data()) result=pd.DataFrame(data_list,columns=rs.fields) bs.logout() print(stock_name+"("+stock_number+")"+"的数据处理完成") print('==========================================================') return result
def get_sz50_stocks(): """ 获取上证50成分股数据 """ #删除数据 BS_SZ50_Stocks.del_all_data() # 登陆系统 lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) # 获取上证50成分股 rs = bs.query_sz50_stocks() print('query_sz50 error_code:' + rs.error_code) print('query_sz50 error_msg:' + rs.error_msg) # 打印结果集 sz50_stocks = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 sz50_stocks.append(rs.get_row_data()) result = pd.DataFrame(sz50_stocks, columns=rs.fields) result.to_sql('odl_bs_sz50_stocks', engine, schema=CQ_Config.DB_SCHEMA, if_exists='append', index=False) # 登出系统 bs.logout()
def logout(self): ''' 退出 ''' hide() bs.logout() show()
def data_download(self, start_date='2017-07-01', end_date='2020-12-31'): #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:' + lg.error_code) print('login respond error_msg:' + lg.error_msg) #### 获取沪深A股历史K线数据 #### rs = bs.query_history_k_data_plus( self.code, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST", start_date=start_date, end_date=end_date, frequency="d", adjustflag="3") print('query_history_k_data_plus respond error_code:' + rs.error_code) print('query_history_k_data_plus respond error_msg:' + rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) self.mydata = pd.DataFrame(data_list, columns=rs.fields) #### 登出系统 #### bs.logout()