def query_dividend_data(self, year): data_list = [] fields = ['code', 'dividPreNoticeDate', 'dividAgmPumDate', 'dividPlanAnnounceDate', 'dividPlanDate', 'dividRegistDate', 'dividOperateDate', 'dividPayDate', 'dividStockMarketDate', 'dividCashPsBeforeTax', 'dividCashPsAfterTax', 'dividStocksPs', 'dividCashStock', 'dividReserveToStockPs'] rs = bs.query_dividend_data(self.code, year, yearType=self.year_type) while (rs.error_code == '0') & rs.next(): data_list.append(rs.get_row_data()) data = jsonWrapper(data_list, fields) return rs.error_code, rs.error_msg, list(map(lambda x: { attr: x.get(attr, '') for attr in self.attr_fields }, data)) if self.attr_fields else data
def generate_divident_single(code, first_year): if (code is None) or (first_year is None): return 0 to_return = 0 exist_operations = Operation.objects.filter(code=code, operationType='DV') date_array = list(map(lambda x: str(x.date), exist_operations)) year_now = datetime.date.today().year new_code = code[0:2] + '.' + code[2:] for year in range(int(first_year), int(year_now) + 1, 1): rs = bs.query_dividend_data(code=new_code, year=str(year), yearType="operate") while (rs.error_code == '0') & rs.next(): data = rs.get_row_data() date = data[6] cash = 0 if data[9] == '' else float(data[9]) reserve = 0 if data[11] == '' else float(data[11]) stock = 0 if data[13] == '' else float(data[13]) find = False for exist_date in date_array: if exist_date == date: find = True if find == False: Operation.objects.create(date=date, code=code, operationType='DV', cash=cash, reserve=reserve, stock=stock) to_return += 1 operations = Operation.objects.filter(code=code).order_by('date') current_hold = 0 for operation in operations: if operation.operationType == 'BUY': current_hold += operation.count elif operation.operationType == 'SELL': current_hold -= operation.count elif operation.operationType == 'DV': if current_hold == 0: operation.delete() to_return -= 1 else: operation.count = current_hold operation.save() current_hold += current_hold * (operation.reserve + operation.stock) return to_return
def query_dividend_data(self, code, year, yearType = 'report'): ''' code:股票代码,sh或sz.+6位数字代码,或者指数代码,如:sh.601398。sh:上海;sz:深圳。此参数不可为空; year:年份,如:2017。此参数不可为空; yearType:年份类别,默认为"report":预案公告年份,可选项"operate":除权除息年份。此参数不可为空。 ''' data_list = [] rs = bs.query_dividend_data(code=code, year=year, yearType=yearType) self.log('query_dividend_data respond error_msg:', rs) while (rs.error_code == '0') & rs.next(): data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields)
def query_dividend_data(code, year, yearType): """ 查询除权除息信息 :param code:股票代码,sh或sz.+6位数字代码,或者指数代码,如:sh.601398。sh:上海;sz:深圳。此参数不可为空; :param year:年份,如:2017。此参数不可为空; :param yearType:年份类别。"report":预案公告年份,"operate":除权除息年份。此参数不可为空。 """ lg = bs.login() if lg.error_code != '1': logger.error('login respond error_msg:' + lg.error_msg) rs_list = [] rs_dividend = bs.query_dividend_data(code=code, year=year, yearType=yearType) while (rs_dividend.error_code == '0') & rs_dividend.next(): rs_list.append(rs_dividend.get_row_data()) result_dividend = pd.DataFrame(rs_list, columns=rs_dividend.fields) bs.logout() return result_dividend
def get_dividend_data(stock_number, stock_name, start_year, end_year, yearType): print('==========================================================') print("开始进行: " + stock_name + "(" + stock_number + ")" + "的数据处理") print("尝试登陆baostock") #####login##### lg = bs.login(user_id="anonymous", password="******") if (lg.error_code == '0'): print("登陆成功") else: print("登录失败") rs_list = [] for iter in range(start_year, end_year + 1): rs_dividend = bs.query_dividend_data(code=stock_number, year=iter, yearType=yearType) while (rs_dividend.error_code == '0') & rs_dividend.next(): rs_list.append(rs_dividend.get_row_data()) result = pd.DataFrame(rs_list, columns=rs_dividend.fields) bs.logout() return result
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) #### 查询除权除息信息#### # 查询2015年除权除息信息 rs_list = [] rs_dividend_2017 = bs.query_dividend_data(code="sz.000001", year="2017", yearType="report") while (rs_dividend_2017.error_code == '0') & rs_dividend_2017.next(): rs_list.append(rs_dividend_2017.get_row_data()) # 查询2016年除权除息信息 rs_dividend_2018 = bs.query_dividend_data(code="sz.000001", year="2018", yearType="report") while (rs_dividend_2018.error_code == '0') & rs_dividend_2018.next(): rs_list.append(rs_dividend_2018.get_row_data()) # 查询2017年除权除息信息 rs_dividend_2019 = bs.query_dividend_data(code="sz.000001", year="2019", yearType="report") while (rs_dividend_2019.error_code == '0') & rs_dividend_2019.next(): rs_list.append(rs_dividend_2019.get_row_data())
code = [] data = pd.read_csv('/home/toby/data/sz_code.csv', encoding='utf-8', header=None) code.extend(data[0].values.tolist()) data = pd.read_csv('/home/toby/data/sh_code.csv', encoding='utf-8', header=None) code.extend(data[0].values.tolist()) #### 查询除权除息信息#### # 查询某年除权除息信息 rs_list = [] for sym in code: rs_dividend = bs.query_dividend_data(code=sym, year=year, yearType="report") while (rs_dividend.error_code == '0') & rs_dividend.next(): rs_list.append(rs_dividend.get_row_data()) result_dividend = pd.DataFrame(rs_list, columns=rs_dividend.fields) #### 结果集输出到csv文件 #### result_dividend.to_csv("/home/toby/data/datasource/baostock/dividend_" + year + ".csv", encoding="gbk", index=False, columns=['code', 'dividOperateDate']) #### 登出系统 #### bs.logout()
def dividend_data(self, code, year, yearType='report'): self.getStockInstance() rs = bs.query_dividend_data(code, year, yearType) self._test_rs(rs) return rs
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) #### 查询除权除息信息#### # 查询2015年除权除息信息 rs_list = [] rs_dividend_2015 = bs.query_dividend_data(code="sh.600000", year="2015", yearType="report") while (rs_dividend_2015.error_code == '0') & rs_dividend_2015.next(): rs_list.append(rs_dividend_2015.get_row_data()) # 查询2016年除权除息信息 rs_dividend_2016 = bs.query_dividend_data(code="sh.600000", year="2016", yearType="report") while (rs_dividend_2016.error_code == '0') & rs_dividend_2016.next(): rs_list.append(rs_dividend_2016.get_row_data()) # 查询2017年除权除息信息 rs_dividend_2017 = bs.query_dividend_data(code="sh.600000", year="2017", yearType="report") while (rs_dividend_2017.error_code == '0') & rs_dividend_2017.next(): rs_list.append(rs_dividend_2017.get_row_data())
def __generate_divident_single(self, code): if code is None: return "" to_return = 0 single_operation_list = self.operation_list[code] single_real_time = self.realtime_list[code] name = single_real_time[0] exist_dv_operations = list( filter(lambda x: x.operationType == "DV", single_operation_list)) date_array = list(map(lambda x: str(x.date), exist_dv_operations)) first_year = single_operation_list[0].date.year year_now = datetime.date.today().year new_code = code[0:2] + "." + code[2:] for year in range(int(first_year), int(year_now) + 1, 1): rs = bs.query_dividend_data(code=new_code, year=str(year), yearType="operate") while (rs.error_code == "0") & rs.next(): data = rs.get_row_data() date = data[6] cash = 0 if data[9] == "" else float(data[9]) reserve = 0 if data[11] == "" else float(data[11]) stock = 0 if data[13] == "" else float(data[13]) find = False for exist_date in date_array: if exist_date == date: find = True if find == False: Operation.objects.create( date=date, code=code, operationType="DV", cash=cash, reserve=reserve, stock=stock, ) to_return += 1 operations = Operation.objects.filter(code=code).order_by("date") current_hold = 0 for operation in operations: if operation.operationType == "BUY": current_hold += operation.count elif operation.operationType == "SELL": current_hold -= operation.count elif operation.operationType == "DV": if current_hold == 0: operation.delete() to_return -= 1 else: operation.count = current_hold operation.save() current_hold += current_hold * (operation.reserve + operation.stock) if to_return > 0: return name else: return ""