def create(self): df = get_industries(name='%s_l%s' % (self.project.name[:2], self.project.name[-1])) #生成列表 self.to_track(df) # 调用父类方法保存: TPool.save(self, df)
def run(self): # get stock blocks from sina for category, name_ch in self.category_map.items(): df = get_industries(name=category, date=None) df['code'] = df.index df['exchange'] = 'cn' df['list_date'] = df['start_date'] df['timestamp'] = df['list_date'] df['entity_type'] = 'block' df['block_type'] = category.replace("_", "") df['category'] = "industry" df['id'] = df['entity_id'] = df.apply(lambda x: "block_" + x.exchange + "_" + x.code, axis=1) df_to_db(data_schema=self.data_schema, df=df, provider=self.provider, force_update=True) self.logger.info(f"完成聚宽数据行业数据保存:{name_ch}") concept_df = get_concepts() concept_df['code'] = concept_df.index concept_df['exchange'] = 'cn' concept_df['list_date'] = concept_df['start_date'] concept_df['timestamp'] = concept_df['list_date'] concept_df['entity_type'] = 'block' concept_df['category'] = "concept" concept_df['id'] = concept_df['entity_id'] = concept_df.apply(lambda x: "block_" + x.exchange + "_" + x.code, axis=1) df_to_db(data_schema=self.data_schema, df=concept_df, provider=self.provider, force_update=True) self.logger.info(f"完成聚宽数据概念数据保存")
def get_sw1_valuation(start_date=None, end_date=None): #2014-02-22之后申万一级行业28个 code = jd.get_industries(name='sw_l1',date='2014-02-22').index.tolist() days = jd.get_trade_days(start_date,end_date) index = jd.finance.run_query(jd.query(jd.finance.SW1_DAILY_VALUATION).filter( jd.finance.SW1_DAILY_VALUATION.date=='2014-02-22' ).limit(1)).columns.tolist() data = pd.DataFrame(columns = index) for day in days: df=jd.finance.run_query(jd.query(jd.finance.SW1_DAILY_VALUATION).filter( jd.finance.SW1_DAILY_VALUATION.code.in_(code), jd.finance.SW1_DAILY_VALUATION.date==day )) name1 = set(list(map(lambda x:x[:-1],jd.get_industries(name='sw_l1',date='2014-02-22').name.tolist()))) name2 = set(df.name.tolist()) if not name1-name2: data = pd.concat([data, df], axis = 0, sort=False) return data
def init_industry(): jq.login() industry_list = [] # sw_l1 sw_l1_data = sdk.get_industries(name='sw_l1', date='2020-10-30') for index in sw_l1_data.index: name = sw_l1_data.loc[index]['name'] industry = (index, name, 'sw_l1') industry_list.append(industry) # sw_l2 sw_l1_data = sdk.get_industries(name='sw_l1', date='2020-10-30') for index in sw_l1_data.index: name = sw_l1_data.loc[index]['name'] industry = (index, name, 'sw_l2') industry_list.append(industry) # jq_l1 sw_l1_data = sdk.get_industries(name='jq_l1', date='2020-10-30') for index in sw_l1_data.index: name = sw_l1_data.loc[index]['name'] industry = (index, name, 'jq_l1') industry_list.append(industry) # jq_l2 sw_l1_data = sdk.get_industries(name='jq_l2', date='2020-10-30') for index in sw_l1_data.index: name = sw_l1_data.loc[index]['name'] industry = (index, name, 'jq_l2') industry_list.append(industry) # zjw sw_l1_data = sdk.get_industries(name='zjw', date='2020-10-30') for index in sw_l1_data.index: name = sw_l1_data.loc[index]['name'] industry = (index, name, 'zjw') industry_list.append(industry) print(industry_list) insert_sql = "insert into industry(code, name, type) values (%s, %s, %s)" my.insert_many(insert_sql, industry_list)
def get_industries_store(self, name='jq_l2', date=None): """ 获取行业信息并存入数据 :param name: :param date: :return: """ res = jq.get_industries(name=name, date=None) '''增加类别列,行业分类者,聚宽,申万,国证''' res['type'] = name '''DataFrame入库''' pymysql.install_as_MySQLdb() mysqlconnect = create_engine(self.mysql_conf.MYSQL_CON_STR) res.to_sql(name='industries', con=mysqlconnect, if_exists='append', index=True, index_label='index', chunksize=1000) flog.FinanceLoger.logger.info('所有行业信息已经保存成功') return
def get_industry_stocks(): df = jq.get_industries(name='zjw') s2i = {} row_num = len(df.index) for i in range(row_num): industry_code = df.index[i] industry_name = df.iloc[i]['name'] #print industry_code, industry_name stocks = jq.get_industry_stocks(industry_code) for stock_code in stocks: if stock_code in s2i: s2i[stock_code].append(industry_name) else: s2i[stock_code] = [industry_name] return s2i
def save_data(start_date, end_date): jqdatasdk.auth(username=user_passwd.user_name, password=user_passwd.passwd) names_to_id = red.get('names_to_id') names_to_id = json.loads(names_to_id) indices = pd.read_csv( r'C:\Users\never\Desktop\website-practice\indices.csv') names = [] display_names = [] index_name_to_id = {} for i in range(len(indices)): if str(indices.start_date[i]) <= start_date and end_date <= str( indices.end_date[i]): names.append(indices.id[i]) display_names.append(indices.display_name[i]) index_name_to_id[indices.display_name[i]] = indices.id[i] indices = display_names names_to_id['index'] = index_name_to_id industries_df = jqdatasdk.get_industries('sw_l1') industry_end_date = { '建筑建材I': '2014-02-21', '机械设备I': '2014-02-21', '交运设备I': '2014-02-21', '信息设备I': '2014-02-21', '金融服务I': '2014-02-21', '信息服务I': '2014-02-21', } industries = [] industry_name_to_id = {} for industry_name, industry_id, start_time in zip( industries_df.name.values, industries_df.index.values, industries_df.start_date.values): st = str(start_time) if industry_name in industry_end_date.keys( ) and st <= start_date and end_date < industry_end_date[ industry_name]: industries.append(industry_name) industry_name_to_id.update({industry_name: industry_id}) elif industry_name not in industry_end_date.keys( ) and st <= start_date: industries.append(industry_name) industry_name_to_id.update({industry_name: industry_id}) names_to_id['industry_index'] = industry_name_to_id stock_pool = {} stock_ids, stock_names = [], [] stock_name_to_id = {} for i in range(len(display_names)): display, pool_id = display_names[i], names[i] p = jqdatasdk.get_all_securities(types=['stock'], date=start_date) p = p[p.end_date >= end_date] q = jqdatasdk.get_index_stocks(pool_id) id_to_name = dict(zip(p.index.values, p.display_name.values)) stock_name_to_id.update( dict(zip(p.display_name.values, p.index.values))) stocks = [] for item in q: try: stocks.append(id_to_name[item]) stock_ids.append(item) stock_names.append(id_to_name[item]) except Exception as e: pass stocks = sorted(stocks, key=lambda x: ' '.join(lazy_pinyin(x))) stock_pool[display] = stocks names_to_id['stock'] = stock_name_to_id get_funds = jqdatasdk.get_all_securities('fund', date=start_date) display_names = ['场内ETF', '场内LOF', '分级基金A', '分级基金B', '场内货币基金'] names = ['etf', 'lof', 'fja', 'fjb', 'money_market_fund'] exchanged_fund_pool = {} exchanged_name_to_id = dict(zip(display_names, names)) for i in range(len(display_names)): display, pool_id = display_names[i], names[i] p = get_funds[get_funds['type'] == pool_id] y1 = (p.start_date <= start_date).values y2 = (p.end_date >= end_date).values p = p[np.logical_and(y1, y2)] q = p.index.values id_to_name = dict(zip(p.index.values, p.display_name.values)) exchanged_name_to_id.update( dict(zip(p.display_name.values, p.index.values))) funds = [] for item in q: try: funds.append(id_to_name[item]) except Exception as e: pass funds = sorted(funds, key=lambda x: ' '.join(lazy_pinyin(x))) exchanged_fund_pool[display] = funds names_to_id['exchanged_fund'] = exchanged_name_to_id get_funds = jqdatasdk.get_all_securities('open_fund', date=start_date) get_funds = get_funds[get_funds.end_date >= end_date] display_names = ['股票型', '混合型', '债券型', '货币型', 'ETF联接', '黄金', 'QDII'] names = [ 'stock_fund', 'mixture_fund', 'bond_fund', 'money_market_fund', 'fund_fund', 'noble_metal_fund', 'QDII_fund' ] open_fund_pool = {} open_fund_name_to_id = dict(zip(display_names, names)) for i in range(len(display_names)): display, pool_id = display_names[i], names[i] if pool_id != 'QDII_fund': p = get_funds[get_funds['type'] == pool_id] else: p = jqdatasdk.get_all_securities(pool_id) q = p.index.values id_to_name = dict(zip(p.index.values, p.display_name.values)) open_fund_name_to_id.update( dict(zip(p.display_name.values, p.index.values))) funds = [] for item in q: try: funds.append(id_to_name[item]) except Exception as e: pass funds = sorted(funds, key=lambda x: ' '.join(lazy_pinyin(x))) open_fund_pool[display] = funds names_to_id['open_fund'] = open_fund_name_to_id data = { 'stock_pool': stock_pool, 'exchanged_fund_pool': exchanged_fund_pool, 'open_fund_pool': open_fund_pool, 'industry_pool': industries, 'index_pool': indices } key_name = 'data-' + start_date + '-' + end_date + '.pkl' red[key_name] = json.dumps(data) red['names_to_id'] = json.dumps(names_to_id)
# valuation是内置市值数据对象(这里表示查询valuation下所有的属性) q = jq.query(jq.valuation).filter(jq.valuation.code=='000001.XSHE') df = jq.get_fundamentals(q, '2021-01-01') df # ## 获取金融板块的股票 # In[17]: import pandas as pd pd.set_option('max_rows', None) # 汽车制造 C36,零售业 F52,其他金融业:J69, 货币金融服务:J66 # 计算机、通信和其他电子设备制造业 C39 temp = jq.get_industries() # 筛选出和金融相关的产业 temp[temp['name'].str.contains('计算机')] # In[18]: """ 智能投研 1.筛选2020年市盈率小于行业平均水平 2.市值大于行业平均水平的金融行业股票 3.然后观察2021年的市盈率表现 """ #设置benchmark_date benchmark_date = '2020-12-31'
def get_industries(*args, **kwargs): return jqdatasdk.get_industries(*args, **kwargs)
def get_industries(self, industry_code: cfg.IndustryCode): return jq.get_industries(name=industry_code.name)