def test_add_tushare_provider(): from zvt.contract.register import register_schema from zvt.domain.meta.stock_meta import StockMetaBase register_schema(providers=['tushare'], db_name='stock_meta', schema_base=StockMetaBase) from zvt.domain import Stock Stock.query_data(provider='tushare') try: Stock.record_data(provider='tushare') assert False except Exception as e: print(e)
def tag(self, timestamp): stock_df = Stock.query_data(filters=[Stock.list_date <= timestamp], index="entity_id") block_df = Block.query_data(provider="eastmoney", filters=[Block.category == "industry"], index="entity_id") block_ids = block_df.index.tolist() block_stock_df = BlockStock.query_data( provider="eastmoney", entity_ids=block_ids, filters=[BlockStock.stock_id.in_(stock_df.index.tolist())], index="stock_id", ) block_stock_df["cycle_tag"] = block_stock_df["name"].apply( lambda name: get_cycle_tag(name)) strong_cycle_stocks = block_stock_df[block_stock_df.cycle_tag == "strong_cycle"]["stock_id"] weak_cycle_stocks = block_stock_df[block_stock_df.cycle_tag == "weak_cycle"]["stock_id"] non_cycle_stocks = block_stock_df[block_stock_df.cycle_tag == "non_cycle"]["stock_id"] strong_cycle_domains = self.get_tag_domains( entity_ids=strong_cycle_stocks, timestamp=timestamp, cycle_tag=CycleTag.strong_cycle.value) weak_cycle_domains = self.get_tag_domains( entity_ids=weak_cycle_stocks, timestamp=timestamp, cycle_tag=CycleTag.weak_cycle.value) non_cycle_domains = self.get_tag_domains( entity_ids=non_cycle_stocks, timestamp=timestamp, cycle_tag=CycleTag.non_cycle.value) self.session.add_all(strong_cycle_domains) self.session.add_all(weak_cycle_domains) self.session.add_all(non_cycle_domains) self.session.commit()
def tag(self, timestamp): stock_df = Stock.query_data(filters=[Stock.list_date <= timestamp], index='entity_id') block_df = Block.query_data(provider='eastmoney', filters=[Block.category == 'industry'], index='entity_id') block_ids = block_df.index.tolist() block_stock_df = BlockStock.query_data( provider='eastmoney', entity_ids=block_ids, filters=[BlockStock.stock_id.in_(stock_df.index.tolist())], index='stock_id') block_stock_df['cycle_tag'] = block_stock_df['name'].apply( lambda name: get_cycle_tag(name)) strong_cycle_stocks = block_stock_df[block_stock_df.cycle_tag == 'strong_cycle']['stock_id'] weak_cycle_stocks = block_stock_df[block_stock_df.cycle_tag == 'weak_cycle']['stock_id'] non_cycle_stocks = block_stock_df[block_stock_df.cycle_tag == 'non_cycle']['stock_id'] strong_cycle_domains = self.get_tag_domains( entity_ids=strong_cycle_stocks, timestamp=timestamp, cycle_tag=CycleTag.strong_cycle.value) weak_cycle_domains = self.get_tag_domains( entity_ids=weak_cycle_stocks, timestamp=timestamp, cycle_tag=CycleTag.weak_cycle.value) non_cycle_domains = self.get_tag_domains( entity_ids=non_cycle_stocks, timestamp=timestamp, cycle_tag=CycleTag.non_cycle.value) self.session.add_all(strong_cycle_domains) self.session.add_all(weak_cycle_domains) self.session.add_all(non_cycle_domains) self.session.commit()
def make_decision(self, timestamp, trading_level: IntervalLevel): longs, shorts = super().make_decision(timestamp, trading_level) if longs or shorts: # 成交超过1亿的前300个股 df = Stock1dKdata.query_data( start_timestamp=timestamp, end_timestamp=timestamp, columns=[Stock1dKdata.entity_id], filters=[Stock1dKdata.turnover > 100000000], limit=300, order=Stock1dKdata.volume.desc()) longs1 = set(df['entity_id'].to_list()) long_targets = set(longs) & longs1 if shorts: all = Stock.query_data(columns=[Stock.entity_id]) short_targets = set(shorts) | ( set(all['entity_id'].to_list()) - longs1) else: short_targets = shorts return long_targets, short_targets return longs, shorts