def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) result = get_holders(code=entity.code, end_date=the_date) if result: holders = [] new_actors = [] for item in result: # 机构 if item['IS_HOLDORG'] == '1': domains: List[ActorMeta] = ActorMeta.query_data(filters=[ActorMeta.code == item['HOLDER_CODE']], return_type='domain') if not domains: actor_type = ActorType.corporation.value actor = ActorMeta(entity_id=f'{actor_type}_cn_{item["HOLDER_CODE"]}', id=f'{actor_type}_cn_{item["HOLDER_CODE"]}', entity_type=actor_type, exchange='cn', code=item["HOLDER_CODE"], name=item["HOLDER_NAME"]) else: actor = domains[0] else: actor_type = ActorType.individual.value actor = ActorMeta(entity_id=f'{actor_type}_cn_{item["HOLDER_NAME"]}', id=f'{actor_type}_cn_{item["HOLDER_NAME"]}', entity_type=actor_type, exchange='cn', code=item["HOLDER_NAME"], name=item["HOLDER_NAME"]) new_actors.append(actor.__dict__) holder = {'id': f'{entity.entity_id}_{the_date}_{actor.entity_id}', 'entity_id': entity.entity_id, 'timestamp': timestamp, 'code': entity.code, 'name': entity.name, 'actor_id': actor.entity_id, 'actor_type': actor.entity_type, 'actor_code': actor.code, 'actor_name': actor.name, 'report_date': timestamp, 'report_period': to_report_period_type(timestamp), 'holding_numbers': item['HOLD_NUM'], 'holding_ratio': value_to_pct(item['HOLD_NUM_RATIO'], default=0)} holders.append(holder) if holders: df = pd.DataFrame.from_records(holders) df_to_db(data_schema=self.data_schema, df=df, provider=self.provider, force_update=True) if new_actors: df = pd.DataFrame.from_records(new_actors) df_to_db(data_schema=ActorMeta, df=df, provider=self.provider, force_update=False)
def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) self.logger.info(f"to {entity.code} {the_date}") for actor_type in ActorType: if actor_type == ActorType.private_equity or actor_type == ActorType.individual: continue result = get_ii_holder( code=entity.code, report_date=the_date, org_type=actor_type_to_org_type(actor_type) ) if result: holders = [ { "id": f'{entity.entity_id}_{the_date}_{actor_type.value}_cn_{item["HOLDER_CODE"]}', "entity_id": entity.entity_id, "timestamp": timestamp, "code": entity.code, "name": entity.name, "actor_id": f'{actor_type.value}_cn_{item["HOLDER_CODE"]}', "actor_type": actor_type.value, "actor_code": item["HOLDER_CODE"], "actor_name": f'{item["HOLDER_NAME"]}', "report_date": timestamp, "report_period": to_report_period_type(timestamp), "holding_numbers": item["TOTAL_SHARES"], "holding_ratio": value_to_pct(item["FREESHARES_RATIO"], 0), "holding_values": item["HOLD_VALUE"], } for item in result ] df = pd.DataFrame.from_records(holders) df_to_db( data_schema=self.data_schema, df=df, provider=self.provider, force_update=True, drop_duplicates=True, ) # save the actors actors = [ { "id": f'{actor_type.value}_cn_{item["HOLDER_CODE"]}', "entity_id": f'{actor_type.value}_cn_{item["HOLDER_CODE"]}', "entity_type": actor_type.value, "exchange": "cn", "code": item["HOLDER_CODE"], "name": f'{item["HOLDER_NAME"]}', } for item in result ] df1 = pd.DataFrame.from_records(actors) df_to_db( data_schema=ActorMeta, df=df1, provider=self.provider, force_update=False, drop_duplicates=True )
def to_jq_report_period(timestamp): the_date = to_pd_timestamp(timestamp) report_period = to_report_period_type(timestamp) if report_period == ReportPeriod.year.value: return "{}".format(the_date.year) if report_period == ReportPeriod.season1.value: return "{}q1".format(the_date.year) if report_period == ReportPeriod.half_year.value: return "{}q2".format(the_date.year) if report_period == ReportPeriod.season3.value: return "{}q3".format(the_date.year) assert False
def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) self.logger.info(f'to {entity.code} {the_date}') for actor_type in ActorType: if actor_type == ActorType.private_equity or actor_type == ActorType.individual: continue result = get_ii_summary( code=entity.code, report_date=the_date, org_type=actor_type_to_org_type(actor_type)) if result: summary_list = [{ 'id': f'{entity.entity_id}_{the_date}_{actor_type.value}', 'entity_id': entity.entity_id, 'timestamp': timestamp, 'code': entity.code, 'name': entity.name, 'actor_type': actor_type.value, 'actor_count': item['TOTAL_ORG_NUM'], 'report_date': timestamp, 'report_period': to_report_period_type(timestamp), 'change_ratio': value_to_pct(item['CHANGE_RATIO'], default=1), 'is_complete': item['IS_COMPLETE'], 'holding_numbers': item['TOTAL_FREE_SHARES'], 'holding_ratio': value_to_pct(item['TOTAL_SHARES_RATIO'], default=0), 'holding_values': item['TOTAL_MARKET_CAP'] } for item in result] df = pd.DataFrame.from_records(summary_list) df_to_db(data_schema=self.data_schema, df=df, provider=self.provider, force_update=True, drop_duplicates=True)
def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) self.logger.info(f"to {entity.code} {the_date}") for actor_type in ActorType: if actor_type == ActorType.private_equity or actor_type == ActorType.individual: continue result = get_ii_summary( code=entity.code, report_date=the_date, org_type=actor_type_to_org_type(actor_type) ) if result: summary_list = [ { "id": f"{entity.entity_id}_{the_date}_{actor_type.value}", "entity_id": entity.entity_id, "timestamp": timestamp, "code": entity.code, "name": entity.name, "actor_type": actor_type.value, "actor_count": item["TOTAL_ORG_NUM"], "report_date": timestamp, "report_period": to_report_period_type(timestamp), "change_ratio": value_to_pct(item["CHANGE_RATIO"], default=1), "is_complete": item["IS_COMPLETE"], "holding_numbers": item["TOTAL_FREE_SHARES"], "holding_ratio": value_to_pct(item["TOTAL_SHARES_RATIO"], default=0), "holding_values": item["TOTAL_MARKET_CAP"], } for item in result ] df = pd.DataFrame.from_records(summary_list) df_to_db( data_schema=self.data_schema, df=df, provider=self.provider, force_update=True, drop_duplicates=True, )
def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) self.logger.info(f'to {entity.code} {the_date}') for actor_type in ActorType: if actor_type == ActorType.private_equity or actor_type == ActorType.individual: continue result = get_ii_holder( code=entity.code, report_date=the_date, org_type=actor_type_to_org_type(actor_type)) if result: holders = [{ 'id': f'{entity.entity_id}_{the_date}_{actor_type.value}_cn_{item["HOLDER_CODE"]}', 'entity_id': entity.entity_id, 'timestamp': timestamp, 'code': entity.code, 'name': entity.name, 'actor_id': f'{actor_type.value}_cn_{item["HOLDER_CODE"]}', 'actor_type': actor_type.value, 'actor_code': item["HOLDER_CODE"], 'actor_name': f'{item["HOLDER_NAME"]}', 'report_date': timestamp, 'report_period': to_report_period_type(timestamp), 'holding_numbers': item['TOTAL_SHARES'], 'holding_ratio': value_to_pct(item['FREESHARES_RATIO'], 0), 'holding_values': item['HOLD_VALUE'] } for item in result] df = pd.DataFrame.from_records(holders) df_to_db(data_schema=self.data_schema, df=df, provider=self.provider, force_update=True, drop_duplicates=True) # save the actors actors = [{ 'id': f'{actor_type.value}_cn_{item["HOLDER_CODE"]}', 'entity_id': f'{actor_type.value}_cn_{item["HOLDER_CODE"]}', 'entity_type': actor_type.value, 'exchange': 'cn', 'code': item["HOLDER_CODE"], 'name': f'{item["HOLDER_NAME"]}' } for item in result] df1 = pd.DataFrame.from_records(actors) df_to_db(data_schema=ActorMeta, df=df1, provider=self.provider, force_update=False, drop_duplicates=True)
def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) result = get_holders(code=entity.code, end_date=the_date) if result: holders = [] new_actors = [] for item in result: # 机构 if item["IS_HOLDORG"] == "1": domains: List[ActorMeta] = ActorMeta.query_data( filters=[ActorMeta.code == item["HOLDER_CODE"]], return_type="domain") if not domains: actor_type = ActorType.corporation.value actor = ActorMeta( entity_id= f'{actor_type}_cn_{item["HOLDER_CODE"]}', id=f'{actor_type}_cn_{item["HOLDER_CODE"]}', entity_type=actor_type, exchange="cn", code=item["HOLDER_CODE"], name=item["HOLDER_NAME"], ) else: actor = domains[0] else: actor_type = ActorType.individual.value actor = ActorMeta( entity_id=f'{actor_type}_cn_{item["HOLDER_NAME"]}', id=f'{actor_type}_cn_{item["HOLDER_NAME"]}', entity_type=actor_type, exchange="cn", code=item["HOLDER_NAME"], name=item["HOLDER_NAME"], ) new_actors.append(actor.__dict__) holder = { "id": f"{entity.entity_id}_{the_date}_{actor.entity_id}", "entity_id": entity.entity_id, "timestamp": timestamp, "code": entity.code, "name": entity.name, "actor_id": actor.entity_id, "actor_type": actor.entity_type, "actor_code": actor.code, "actor_name": actor.name, "report_date": timestamp, "report_period": to_report_period_type(timestamp), "holding_numbers": item["HOLD_NUM"], "holding_ratio": value_to_pct(item["HOLD_NUM_RATIO"], default=0), } holders.append(holder) if holders: df = pd.DataFrame.from_records(holders) df_to_db(data_schema=self.data_schema, df=df, provider=self.provider, force_update=True) if new_actors: df = pd.DataFrame.from_records(new_actors) df_to_db(data_schema=ActorMeta, df=df, provider=self.provider, force_update=False)
def record(self, entity, start, end, size, timestamps): for timestamp in timestamps: the_date = to_time_str(timestamp) result = get_free_holders(code=entity.code, end_date=the_date) if result: holders = [] new_actors = [] for item in result: # {'END_DATE': '2021-03-31 00:00:00', # 'FREE_HOLDNUM_RATIO': 0.631949916991, # 'FREE_RATIO_QOQ': '-5.33046217', # 'HOLDER_CODE': '161606', # 'HOLDER_CODE_OLD': '161606', # 'HOLDER_NAME': '交通银行-融通行业景气证券投资基金', # 'HOLDER_RANK': 10, # 'HOLD_NUM': 39100990, # 'IS_HOLDORG': '1', # 'SECUCODE': '000338.SZ'} # 机构 if item["IS_HOLDORG"] == "1": domains: List[ActorMeta] = ActorMeta.query_data( filters=[ActorMeta.code == item["HOLDER_CODE"]], return_type="domain") if not domains: actor_type = ActorType.corporation.value actor = ActorMeta( entity_id= f'{actor_type}_cn_{item["HOLDER_CODE"]}', id=f'{actor_type}_cn_{item["HOLDER_CODE"]}', entity_type=actor_type, exchange="cn", code=item["HOLDER_CODE"], name=item["HOLDER_NAME"], ) else: actor = domains[0] else: actor_type = ActorType.individual.value actor = ActorMeta( entity_id=f'{actor_type}_cn_{item["HOLDER_NAME"]}', id=f'{actor_type}_cn_{item["HOLDER_NAME"]}', entity_type=actor_type, exchange="cn", code=item["HOLDER_NAME"], name=item["HOLDER_NAME"], ) new_actors.append(actor.__dict__) holder = { "id": f"{entity.entity_id}_{the_date}_{actor.entity_id}", "entity_id": entity.entity_id, "timestamp": timestamp, "code": entity.code, "name": entity.name, "actor_id": actor.entity_id, "actor_type": actor.entity_type, "actor_code": actor.code, "actor_name": actor.name, "report_date": timestamp, "report_period": to_report_period_type(timestamp), "holding_numbers": item["HOLD_NUM"], "holding_ratio": value_to_pct(item["FREE_HOLDNUM_RATIO"], 0), } holders.append(holder) if holders: df = pd.DataFrame.from_records(holders) df_to_db(data_schema=self.data_schema, df=df, provider=self.provider, force_update=True) if new_actors: df = pd.DataFrame.from_records(new_actors) df_to_db(data_schema=ActorMeta, df=df, provider=self.provider, force_update=False)